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 
 319 static 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("sr_photocd: CDROM and/or the driver does 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 for me.
 337          * That's why mpcd_sector will be initialized with zero, because I'm not
 338          * able to get the right value. Necessary only if access_count is 1, else
 339          * no disk change happened since the last call of this function and we can
 340          * 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("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             printk("sr_photocd: ioctl error (NEC): 0x%x\n",rc);
 370             break;
 371         }
 372         if (rec[14] != 0 && rec[14] != 0xb0) {
 373             printk("sr_photocd: Hmm, seems the CDROM doesn't support multisession CD's\n");
 374             no_multi = 1;
 375             break;
 376         }
 377         min   = (unsigned long) rec[15]/16*10 + (unsigned long) rec[15]%16;
 378         sec   = (unsigned long) rec[16]/16*10 + (unsigned long) rec[16]%16;
 379         frame = (unsigned long) rec[17]/16*10 + (unsigned long) rec[17]%16;
 380         sector = min*CD_SECS*CD_FRAMES + sec*CD_FRAMES + frame;
 381         is_xa  = (rec[14] == 0xb0);
 382 #ifdef DEBUG
 383         if (sector) {
 384             printk("sr_photocd: multisession CD detected. start: %lu\n",sector);
 385         }
 386 #endif
 387         break;
 388         
 389     case SCSI_MAN_TOSHIBA:
 390 #ifdef DEBUG
 391         printk("sr_photocd: use TOSHIBA code\n");
 392 #endif
 393         
 394         /* we request some disc information (is it a XA-CD ?,
 395          * where starts the last session ?) */
 396         memset(buf,0,40);
 397         *((unsigned long*)buf)   = 0;
 398         *((unsigned long*)buf+1) = 4;  /* we receive 4 bytes from the drive */
 399         cmd[0] = 0xc7;
 400         cmd[1] = 3;
 401         rc = kernel_scsi_ioctl(scsi_CDs[MINOR(inode->i_rdev)].device,
 402                                SCSI_IOCTL_SEND_COMMAND, buf);
 403         if (rc != 0) {
 404             if (rc == 0x28000002) {
 405                 /* Got a "not ready" - error. No chance to find out if this is
 406                  * because there is no CD in the drive or because the drive
 407                  * don't knows multisession CD's. So I need to do an extra check... */
 408                 if (kernel_scsi_ioctl(scsi_CDs[MINOR(inode->i_rdev)].device,
 409                                       SCSI_IOCTL_TEST_UNIT_READY, NULL)) {
 410                     printk("sr_photocd: drive not ready\n");
 411                 } else {
 412                     printk("sr_photocd: Hmm, seems the CDROM doesn't support multisession CD's\n");
 413                     no_multi = 1;
 414                 }
 415             } else
 416                 printk("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("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("sr_photocd: ioctl error (TOSHIBA #2): 0x%x\n",rc);
 442             break;
 443         }
 444 #ifdef DEBUG
 445         printk("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("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-CD's, 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("sr_photocd: ioctl error (TOSHIBA #3): 0x%x\n",rc);
 467             }
 468             /* The set_density command may have changed the sector size or capacity. */
 469             scsi_CDs[MINOR(inode->i_rdev)].needs_sector_size = 1;
 470         }
 471         break;
 472 
 473     case SCSI_MAN_SONY: /* Thomas QUINOT <thomas@melchior.frmug.fr.net> */
 474 #ifdef DEBUG
 475         printk("sr_photocd: use SONY code\n");
 476 #endif
 477         memset(buf,0,40);
 478         *((unsigned long*)buf)   = 0x0;   /* we send nothing...     */
 479         *((unsigned long*)buf+1) = 0x0c;  /* and receive 0x0c bytes */
 480         cmd[0] = 0x43; /* Read TOC */
 481         cmd[8] = 0x0c;
 482         cmd[9] = 0x40;
 483         rc = kernel_scsi_ioctl(scsi_CDs[MINOR(inode->i_rdev)].device,
 484                                SCSI_IOCTL_SEND_COMMAND, buf);
 485         
 486         if ((rc != 0) || ((rec[0] << 8) + rec[1] != 0x0a)) {
 487             printk("sr_photocd: ioctl error (SONY): 0x%x\n",rc);
 488             break;
 489         }
 490         sector = rec[11] + (rec[10] << 8) + (rec[9] << 16) + (rec[8] << 24);
 491         is_xa = !!sector;
 492 #ifdef DEBUG
 493         if (sector)
 494             printk ("sr_photocd: multisession CD detected. start: %lu\n",sector);
 495 #endif
 496         break;
 497                 
 498     case SCSI_MAN_NEC_OLDCDR:
 499     case SCSI_MAN_UNKNOWN:
 500     default:
 501         sector = 0;
 502         no_multi = 1;
 503         break; }
 504     
 505     scsi_CDs[MINOR(inode->i_rdev)].mpcd_sector = sector;
 506     if (is_xa)
 507         scsi_CDs[MINOR(inode->i_rdev)].xa_flags |= 0x01;
 508     else
 509         scsi_CDs[MINOR(inode->i_rdev)].xa_flags &= ~0x01;
 510     if (no_multi)
 511         scsi_CDs[MINOR(inode->i_rdev)].xa_flags |= 0x02;
 512     return;
 513 }
 514 
 515 static int sr_open(struct inode * inode, struct file * filp)
     /* [previous][next][first][last][top][bottom][index][help] */
 516 {
 517         if(MINOR(inode->i_rdev) >= sr_template.nr_dev || 
 518            !scsi_CDs[MINOR(inode->i_rdev)].device) return -ENXIO;   /* No such device */
 519     
 520         if (filp->f_mode & 2)  
 521             return -EROFS;
 522     
 523     check_disk_change(inode->i_rdev);
 524     
 525         if(!scsi_CDs[MINOR(inode->i_rdev)].device->access_count++)
 526         sr_ioctl(inode, NULL, SCSI_IOCTL_DOORLOCK, 0);
 527         if (scsi_CDs[MINOR(inode->i_rdev)].device->host->hostt->usage_count)
 528         (*scsi_CDs[MINOR(inode->i_rdev)].device->host->hostt->usage_count)++;
 529         if(sr_template.usage_count) (*sr_template.usage_count)++;
 530     
 531         sr_photocd(inode);
 532     
 533         /* If this device did not have media in the drive at boot time, then
 534          * we would have been unable to get the sector size.  Check to see if
 535          * this is the case, and try again.
 536          */
 537     
 538         if(scsi_CDs[MINOR(inode->i_rdev)].needs_sector_size)
 539         get_sectorsize(MINOR(inode->i_rdev));
 540     
 541         return 0;
 542 }
 543 
 544 
 545 /*
 546  * do_sr_request() is the request handler function for the sr driver.
 547  * Its function in life is to take block device requests, and
 548  * translate them to SCSI commands.  
 549  */
 550 
 551 static void do_sr_request (void)
     /* [previous][next][first][last][top][bottom][index][help] */
 552 {
 553     Scsi_Cmnd * SCpnt = NULL;
 554     struct request * req = NULL;
 555     Scsi_Device * SDev;
 556     unsigned long flags;
 557     int flag = 0;
 558     
 559     while (1==1){
 560         save_flags(flags);
 561         cli();
 562         if (CURRENT != NULL && CURRENT->rq_status == RQ_INACTIVE) {
 563             restore_flags(flags);
 564             return;
 565         };
 566         
 567         INIT_SCSI_REQUEST;
 568  
 569         SDev = scsi_CDs[DEVICE_NR(CURRENT->rq_dev)].device;
 570         
 571         /*
 572          * I am not sure where the best place to do this is.  We need
 573          * to hook in a place where we are likely to come if in user
 574          * space.
 575          */
 576         if( SDev->was_reset )
 577         {
 578             /*
 579              * We need to relock the door, but we might
 580              * be in an interrupt handler.  Only do this
 581              * from user space, since we do not want to
 582              * sleep from an interrupt.
 583              */
 584             if( SDev->removable && !intr_count )
 585             {
 586                 scsi_ioctl(SDev, SCSI_IOCTL_DOORLOCK, 0);
 587             }
 588             SDev->was_reset = 0;
 589         }
 590         
 591         if (flag++ == 0)
 592             SCpnt = allocate_device(&CURRENT,
 593                                     scsi_CDs[DEVICE_NR(CURRENT->rq_dev)].device, 0); 
 594         else SCpnt = NULL;
 595         restore_flags(flags);
 596         
 597         /* This is a performance enhancement.  We dig down into the request list and
 598          * try and find a queueable request (i.e. device not busy, and host able to
 599          * accept another command.  If we find one, then we queue it. This can
 600          * make a big difference on systems with more than one disk drive.  We want
 601          * to have the interrupts off when monkeying with the request list, because
 602          * otherwise the kernel might try and slip in a request in between somewhere. */
 603         
 604         if (!SCpnt && sr_template.nr_dev > 1){
 605             struct request *req1;
 606             req1 = NULL;
 607             save_flags(flags);
 608             cli();
 609             req = CURRENT;
 610             while(req){
 611                 SCpnt = request_queueable(req,
 612                                           scsi_CDs[DEVICE_NR(req->rq_dev)].device);
 613                 if(SCpnt) break;
 614                 req1 = req;
 615                 req = req->next;
 616             };
 617             if (SCpnt && req->rq_status == RQ_INACTIVE) {
 618                 if (req == CURRENT) 
 619                     CURRENT = CURRENT->next;
 620                 else
 621                     req1->next = req->next;
 622             };
 623             restore_flags(flags);
 624         };
 625         
 626         if (!SCpnt)
 627             return; /* Could not find anything to do */
 628         
 629         wake_up(&wait_for_request);
 630 
 631         /* Queue command */
 632         requeue_sr_request(SCpnt);
 633     };  /* While */
 634 }    
 635 
 636 void requeue_sr_request (Scsi_Cmnd * SCpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
 637 {
 638         unsigned int dev, block, realcount;
 639         unsigned char cmd[10], *buffer, tries;
 640         int this_count, start, end_rec;
 641     
 642         tries = 2;
 643     
 644  repeat:
 645         if(!SCpnt || SCpnt->request.rq_status == RQ_INACTIVE) {
 646                 do_sr_request();
 647                 return;
 648         }
 649     
 650         dev =  MINOR(SCpnt->request.rq_dev);
 651         block = SCpnt->request.sector;  
 652         buffer = NULL;
 653         this_count = 0;
 654     
 655         if (dev >= sr_template.nr_dev) {
 656                 /* printk("CD-ROM request error: invalid device.\n");                   */
 657                 SCpnt = end_scsi_request(SCpnt, 0, SCpnt->request.nr_sectors);
 658                 tries = 2;
 659                 goto repeat;
 660         }
 661     
 662         if (!scsi_CDs[dev].use) {
 663                 /* printk("CD-ROM request error: device marked not in use.\n");         */
 664                 SCpnt = end_scsi_request(SCpnt, 0, SCpnt->request.nr_sectors);
 665                 tries = 2;
 666                 goto repeat;
 667         }
 668     
 669         if (scsi_CDs[dev].device->changed) {
 670         /* 
 671          * quietly refuse to do anything to a changed disc 
 672          * until the changed bit has been reset
 673          */
 674                 /* printk("CD-ROM has been changed.  Prohibiting further I/O.\n");      */
 675                 SCpnt = end_scsi_request(SCpnt, 0, SCpnt->request.nr_sectors);
 676                 tries = 2;
 677                 goto repeat;
 678         }
 679         
 680         switch (SCpnt->request.cmd)
 681     {
 682     case WRITE:                 
 683         SCpnt = end_scsi_request(SCpnt, 0, SCpnt->request.nr_sectors);
 684         goto repeat;
 685         break;
 686     case READ : 
 687         cmd[0] = READ_6;
 688         break;
 689     default : 
 690         panic ("Unknown sr command %d\n", SCpnt->request.cmd);
 691     }
 692         
 693         cmd[1] = (SCpnt->lun << 5) & 0xe0;
 694     
 695     /*
 696      * Now do the grungy work of figuring out which sectors we need, and
 697      * where in memory we are going to put them.
 698      * 
 699      * The variables we need are:
 700      * 
 701      * this_count= number of 512 byte sectors being read 
 702      * block     = starting cdrom sector to read.
 703      * realcount = # of cdrom sectors to read
 704      * 
 705      * The major difference between a scsi disk and a scsi cdrom
 706      * is that we will always use scatter-gather if we can, because we can
 707      * work around the fact that the buffer cache has a block size of 1024,
 708      * and we have 2048 byte sectors.  This code should work for buffers that
 709      * are any multiple of 512 bytes long.
 710      */
 711     
 712         SCpnt->use_sg = 0;
 713     
 714         if (SCpnt->host->sg_tablesize > 0 &&
 715             (!need_isa_buffer ||
 716          dma_free_sectors >= 10)) {
 717         struct buffer_head * bh;
 718         struct scatterlist * sgpnt;
 719         int count, this_count_max;
 720         bh = SCpnt->request.bh;
 721         this_count = 0;
 722         count = 0;
 723         this_count_max = (scsi_CDs[dev].ten ? 0xffff : 0xff) << 4;
 724         /* Calculate how many links we can use.  First see if we need
 725          * a padding record at the start */
 726         this_count = SCpnt->request.sector % 4;
 727         if(this_count) count++;
 728         while(bh && count < SCpnt->host->sg_tablesize) {
 729             if ((this_count + (bh->b_size >> 9)) > this_count_max) break;
 730             this_count += (bh->b_size >> 9);
 731             count++;
 732             bh = bh->b_reqnext;
 733         };
 734         /* Fix up in case of an odd record at the end */
 735         end_rec = 0;
 736         if(this_count % 4) {
 737             if (count < SCpnt->host->sg_tablesize) {
 738                 count++;
 739                 end_rec = (4 - (this_count % 4)) << 9;
 740                 this_count += 4 - (this_count % 4);
 741             } else {
 742                 count--;
 743                 this_count -= (this_count % 4);
 744             };
 745         };
 746         SCpnt->use_sg = count;  /* Number of chains */
 747         count = 512;/* scsi_malloc can only allocate in chunks of 512 bytes*/
 748         while( count < (SCpnt->use_sg * sizeof(struct scatterlist))) 
 749             count = count << 1;
 750         SCpnt->sglist_len = count;
 751         sgpnt = (struct scatterlist * ) scsi_malloc(count);
 752         if (!sgpnt) {
 753             printk("Warning - running *really* short on DMA buffers\n");
 754             SCpnt->use_sg = 0;  /* No memory left - bail out */
 755         } else {
 756             buffer = (unsigned char *) sgpnt;
 757             count = 0;
 758             bh = SCpnt->request.bh;
 759             if(SCpnt->request.sector % 4) {
 760                 sgpnt[count].length = (SCpnt->request.sector % 4) << 9;
 761                 sgpnt[count].address = (char *) scsi_malloc(sgpnt[count].length);
 762                 if(!sgpnt[count].address) panic("SCSI DMA pool exhausted.");
 763                 sgpnt[count].alt_address = sgpnt[count].address; /* Flag to delete
 764                                                                     if needed */
 765                 count++;
 766             };
 767             for(bh = SCpnt->request.bh; count < SCpnt->use_sg; 
 768                 count++, bh = bh->b_reqnext) {
 769                 if (bh) { /* Need a placeholder at the end of the record? */
 770                     sgpnt[count].address = bh->b_data;
 771                     sgpnt[count].length = bh->b_size;
 772                     sgpnt[count].alt_address = NULL;
 773                 } else {
 774                     sgpnt[count].address = (char *) scsi_malloc(end_rec);
 775                     if(!sgpnt[count].address) panic("SCSI DMA pool exhausted.");
 776                     sgpnt[count].length = end_rec;
 777                     sgpnt[count].alt_address = sgpnt[count].address;
 778                     if (count+1 != SCpnt->use_sg) panic("Bad sr request list");
 779                     break;
 780                 };
 781                 if (((long) sgpnt[count].address) + sgpnt[count].length > ISA_DMA_THRESHOLD &&
 782                   SCpnt->host->unchecked_isa_dma) {
 783                     sgpnt[count].alt_address = sgpnt[count].address;
 784                     /* We try and avoid exhausting the DMA pool, since it is easier
 785                      * to control usage here.  In other places we might have a more
 786                      * pressing need, and we would be screwed if we ran out */
 787                     if(dma_free_sectors < (sgpnt[count].length >> 9) + 5) {
 788                         sgpnt[count].address = NULL;
 789                     } else {
 790                         sgpnt[count].address = (char *) scsi_malloc(sgpnt[count].length);
 791                     };
 792                     /* If we start running low on DMA buffers, we abort the scatter-gather
 793                      * operation, and free all of the memory we have allocated.  We want to
 794                      * ensure that all scsi operations are able to do at least a non-scatter/gather
 795                      * operation */
 796                     if(sgpnt[count].address == NULL){ /* Out of dma memory */
 797                         printk("Warning: Running low on SCSI DMA buffers");
 798                         /* Try switching back to a non scatter-gather operation. */
 799                         while(--count >= 0){
 800                             if(sgpnt[count].alt_address) 
 801                                 scsi_free(sgpnt[count].address, sgpnt[count].length);
 802                         };
 803                         SCpnt->use_sg = 0;
 804                         scsi_free(buffer, SCpnt->sglist_len);
 805                         break;
 806                     }; /* if address == NULL */
 807                 };  /* if need DMA fixup */
 808             };  /* for loop to fill list */
 809 #ifdef DEBUG
 810             printk("SR: %d %d %d %d %d *** ",SCpnt->use_sg, SCpnt->request.sector,
 811                    this_count, 
 812                    SCpnt->request.current_nr_sectors,
 813                    SCpnt->request.nr_sectors);
 814             for(count=0; count<SCpnt->use_sg; count++)
 815                 printk("SGlist: %d %x %x %x\n", count,
 816                        sgpnt[count].address, 
 817                        sgpnt[count].alt_address, 
 818                        sgpnt[count].length);
 819 #endif
 820         };  /* Able to allocate scatter-gather list */
 821         };
 822         
 823         if (SCpnt->use_sg == 0){
 824         /* We cannot use scatter-gather.  Do this the old fashion way */
 825         if (!SCpnt->request.bh)         
 826             this_count = SCpnt->request.nr_sectors;
 827         else
 828             this_count = (SCpnt->request.bh->b_size >> 9);
 829         
 830         start = block % 4;
 831         if (start)
 832             {                             
 833             this_count = ((this_count > 4 - start) ? 
 834                           (4 - start) : (this_count));
 835             buffer = (unsigned char *) scsi_malloc(2048);
 836             } 
 837         else if (this_count < 4)
 838             {
 839             buffer = (unsigned char *) scsi_malloc(2048);
 840             }
 841         else
 842             {
 843             this_count -= this_count % 4;
 844             buffer = (unsigned char *) SCpnt->request.buffer;
 845             if (((long) buffer) + (this_count << 9) > ISA_DMA_THRESHOLD &&
 846                 SCpnt->host->unchecked_isa_dma)
 847                 buffer = (unsigned char *) scsi_malloc(this_count << 9);
 848             }
 849         };
 850     
 851         if (scsi_CDs[dev].sector_size == 2048)
 852         block = block >> 2; /* These are the sectors that the cdrom uses */
 853         else
 854         block = block & 0xfffffffc;
 855     
 856         realcount = (this_count + 3) / 4;
 857     
 858         if (scsi_CDs[dev].sector_size == 512) realcount = realcount << 2;
 859     
 860         if (((realcount > 0xff) || (block > 0x1fffff)) && scsi_CDs[dev].ten) 
 861     {
 862                 if (realcount > 0xffff)
 863         {
 864                         realcount = 0xffff;
 865                         this_count = realcount * (scsi_CDs[dev].sector_size >> 9);
 866         }
 867         
 868                 cmd[0] += READ_10 - READ_6 ;
 869                 cmd[2] = (unsigned char) (block >> 24) & 0xff;
 870                 cmd[3] = (unsigned char) (block >> 16) & 0xff;
 871                 cmd[4] = (unsigned char) (block >> 8) & 0xff;
 872                 cmd[5] = (unsigned char) block & 0xff;
 873                 cmd[6] = cmd[9] = 0;
 874                 cmd[7] = (unsigned char) (realcount >> 8) & 0xff;
 875                 cmd[8] = (unsigned char) realcount & 0xff;
 876     }
 877         else
 878     {
 879         if (realcount > 0xff)
 880         {
 881             realcount = 0xff;
 882             this_count = realcount * (scsi_CDs[dev].sector_size >> 9);
 883         }
 884         
 885         cmd[1] |= (unsigned char) ((block >> 16) & 0x1f);
 886         cmd[2] = (unsigned char) ((block >> 8) & 0xff);
 887         cmd[3] = (unsigned char) block & 0xff;
 888         cmd[4] = (unsigned char) realcount;
 889         cmd[5] = 0;
 890     }   
 891     
 892 #ifdef DEBUG
 893     { 
 894         int i;
 895         printk("ReadCD: %d %d %d %d\n",block, realcount, buffer, this_count);
 896         printk("Use sg: %d\n", SCpnt->use_sg);
 897         printk("Dumping command: ");
 898         for(i=0; i<12; i++) printk("%2.2x ", cmd[i]);
 899         printk("\n");
 900     };
 901 #endif
 902     
 903     /* Some dumb host adapters can speed transfers by knowing the
 904      * minimum transfersize in advance.
 905      *
 906      * We shouldn't disconnect in the middle of a sector, but the cdrom
 907      * sector size can be larger than the size of a buffer and the
 908      * transfer may be split to the size of a buffer.  So it's safe to
 909      * assume that we can at least transfer the minimum of the buffer
 910      * size (1024) and the sector size between each connect / disconnect.
 911      */
 912     
 913     SCpnt->transfersize = (scsi_CDs[dev].sector_size > 1024) ?
 914         1024 : scsi_CDs[dev].sector_size;
 915     
 916         SCpnt->this_count = this_count;
 917         scsi_do_cmd (SCpnt, (void *) cmd, buffer, 
 918                  realcount * scsi_CDs[dev].sector_size, 
 919                  rw_intr, SR_TIMEOUT, MAX_RETRIES);
 920 }
 921 
 922 static int sr_detect(Scsi_Device * SDp){
     /* [previous][next][first][last][top][bottom][index][help] */
 923     
 924     if(SDp->type != TYPE_ROM && SDp->type != TYPE_WORM) return 0;
 925     
 926     printk("Detected scsi CD-ROM sr%d at scsi%d, channel %d, id %d, lun %d\n", 
 927            sr_template.dev_noticed++,
 928            SDp->host->host_no, SDp->channel, SDp->id, SDp->lun); 
 929     
 930     return 1;
 931 }
 932 
 933 static int sr_attach(Scsi_Device * SDp){
     /* [previous][next][first][last][top][bottom][index][help] */
 934     Scsi_CD * cpnt;
 935     int i;
 936     
 937     if(SDp->type != TYPE_ROM && SDp->type != TYPE_WORM) return 1;
 938     
 939     if (sr_template.nr_dev >= sr_template.dev_max)
 940     {
 941         SDp->attached--;
 942         return 1;
 943     }
 944     
 945     for(cpnt = scsi_CDs, i=0; i<sr_template.dev_max; i++, cpnt++) 
 946         if(!cpnt->device) break;
 947     
 948     if(i >= sr_template.dev_max) panic ("scsi_devices corrupt (sr)");
 949     
 950     SDp->scsi_request_fn = do_sr_request;
 951     scsi_CDs[i].device = SDp;
 952     sr_template.nr_dev++;
 953     if(sr_template.nr_dev > sr_template.dev_max)
 954         panic ("scsi_devices corrupt (sr)");
 955     return 0;
 956 }
 957 
 958 
 959 static void sr_init_done (Scsi_Cmnd * SCpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
 960 {
 961     struct request * req;
 962     
 963     req = &SCpnt->request;
 964     req->rq_status = RQ_SCSI_DONE; /* Busy, but indicate request done */
 965     
 966     if (req->sem != NULL) {
 967         up(req->sem);
 968     }
 969 }
 970 
 971 static void get_sectorsize(int i){
     /* [previous][next][first][last][top][bottom][index][help] */
 972     unsigned char cmd[10];
 973     unsigned char *buffer;
 974     int the_result, retries;
 975     Scsi_Cmnd * SCpnt;
 976     
 977     buffer = (unsigned char *) scsi_malloc(512);
 978     SCpnt = allocate_device(NULL, scsi_CDs[i].device, 1);
 979     
 980     retries = 3;
 981     do {
 982         cmd[0] = READ_CAPACITY;
 983         cmd[1] = (scsi_CDs[i].device->lun << 5) & 0xe0;
 984         memset ((void *) &cmd[2], 0, 8);
 985         SCpnt->request.rq_status = RQ_SCSI_BUSY;  /* Mark as really busy */
 986         SCpnt->cmd_len = 0;
 987         
 988         memset(buffer, 0, 8);
 989         
 990         scsi_do_cmd (SCpnt,
 991                      (void *) cmd, (void *) buffer,
 992                      512, sr_init_done,  SR_TIMEOUT,
 993                      MAX_RETRIES);
 994         
 995         if (current->pid == 0)
 996             while(SCpnt->request.rq_status != RQ_SCSI_DONE)
 997                 barrier();
 998         else
 999             if (SCpnt->request.rq_status != RQ_SCSI_DONE){
1000                 struct semaphore sem = MUTEX_LOCKED;
1001                 SCpnt->request.sem = &sem;
1002                 down(&sem);
1003                 /* Hmm.. Have to ask about this */
1004                 while (SCpnt->request.rq_status != RQ_SCSI_DONE)
1005                     schedule();
1006             };
1007         
1008         the_result = SCpnt->result;
1009         retries--;
1010         
1011     } while(the_result && retries);
1012     
1013     SCpnt->request.rq_status = RQ_INACTIVE;  /* Mark as not busy */
1014     
1015     wake_up(&SCpnt->device->device_wait); 
1016     
1017     if (the_result) {
1018         scsi_CDs[i].capacity = 0x1fffff;
1019         scsi_CDs[i].sector_size = 2048;  /* A guess, just in case */
1020         scsi_CDs[i].needs_sector_size = 1;
1021     } else {
1022         scsi_CDs[i].capacity = (buffer[0] << 24) |
1023             (buffer[1] << 16) | (buffer[2] << 8) | buffer[3];
1024         scsi_CDs[i].sector_size = (buffer[4] << 24) |
1025             (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
1026         if(scsi_CDs[i].sector_size == 0) scsi_CDs[i].sector_size = 2048;
1027         if(scsi_CDs[i].sector_size != 2048 && 
1028            scsi_CDs[i].sector_size != 512) {
1029             printk ("scd%d : unsupported sector size %d.\n",
1030                     i, scsi_CDs[i].sector_size);
1031             scsi_CDs[i].capacity = 0;
1032             scsi_CDs[i].needs_sector_size = 1;
1033         };
1034         if(scsi_CDs[i].sector_size == 2048)
1035             scsi_CDs[i].capacity *= 4;
1036         scsi_CDs[i].needs_sector_size = 0;
1037         sr_sizes[i] = scsi_CDs[i].capacity;
1038     };
1039     scsi_free(buffer, 512);
1040 }
1041 
1042 static int sr_registered = 0;
1043 
1044 static int sr_init()
     /* [previous][next][first][last][top][bottom][index][help] */
1045 {
1046         int i;
1047     
1048         if(sr_template.dev_noticed == 0) return 0;
1049     
1050         if(!sr_registered) {
1051         if (register_blkdev(MAJOR_NR,"sr",&sr_fops)) {
1052             printk("Unable to get major %d for SCSI-CD\n",MAJOR_NR);
1053             return 1;
1054         }
1055         sr_registered++;
1056         }
1057     
1058         
1059         if (scsi_CDs) return 0;
1060         sr_template.dev_max = sr_template.dev_noticed + SR_EXTRA_DEVS;
1061         scsi_CDs = (Scsi_CD *) scsi_init_malloc(sr_template.dev_max * sizeof(Scsi_CD), GFP_ATOMIC);
1062         memset(scsi_CDs, 0, sr_template.dev_max * sizeof(Scsi_CD));
1063     
1064         sr_sizes = (int *) scsi_init_malloc(sr_template.dev_max * sizeof(int), GFP_ATOMIC);
1065         memset(sr_sizes, 0, sr_template.dev_max * sizeof(int));
1066     
1067         sr_blocksizes = (int *) scsi_init_malloc(sr_template.dev_max * 
1068                                              sizeof(int), GFP_ATOMIC);
1069         for(i=0;i<sr_template.dev_max;i++) sr_blocksizes[i] = 2048;
1070         blksize_size[MAJOR_NR] = sr_blocksizes;
1071         return 0;
1072 }
1073 
1074 void sr_finish()
     /* [previous][next][first][last][top][bottom][index][help] */
1075 {
1076     int i;
1077     
1078         blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
1079         blk_size[MAJOR_NR] = sr_sizes;  
1080     
1081         for (i = 0; i < sr_template.nr_dev; ++i)
1082     {
1083         /* If we have already seen this, then skip it.  Comes up
1084          * with loadable modules. */
1085         if (scsi_CDs[i].capacity) continue;
1086         scsi_CDs[i].capacity = 0x1fffff;
1087         scsi_CDs[i].sector_size = 2048;  /* A guess, just in case */
1088         scsi_CDs[i].needs_sector_size = 1;
1089 #if 0
1090         /* seems better to leave this for later */
1091         get_sectorsize(i);
1092         printk("Scd sectorsize = %d bytes.\n", scsi_CDs[i].sector_size);
1093 #endif
1094         scsi_CDs[i].use = 1;
1095         scsi_CDs[i].ten = 1;
1096         scsi_CDs[i].remap = 1;
1097         scsi_CDs[i].auto_eject = 0; /* Default is not to eject upon unmount. */
1098         sr_sizes[i] = scsi_CDs[i].capacity;
1099     }
1100     
1101     
1102         /* If our host adapter is capable of scatter-gather, then we increase
1103          * the read-ahead to 16 blocks (32 sectors).  If not, we use
1104          * a two block (4 sector) read ahead. */
1105         if(scsi_CDs[0].device && scsi_CDs[0].device->host->sg_tablesize)
1106         read_ahead[MAJOR_NR] = 32;  /* 32 sector read-ahead.  Always removable. */
1107         else
1108         read_ahead[MAJOR_NR] = 4;  /* 4 sector read-ahead */
1109     
1110         return;
1111 }       
1112 
1113 static void sr_detach(Scsi_Device * SDp)
     /* [previous][next][first][last][top][bottom][index][help] */
1114 {
1115     Scsi_CD * cpnt;
1116     int i;
1117     
1118     for(cpnt = scsi_CDs, i=0; i<sr_template.dev_max; i++, cpnt++) 
1119         if(cpnt->device == SDp) {
1120             kdev_t devi = MKDEV(MAJOR_NR, i);
1121 
1122             /*
1123              * Since the cdrom is read-only, no need to sync the device.
1124              * We should be kind to our buffer cache, however.
1125              */
1126             invalidate_inodes(devi);
1127             invalidate_buffers(devi);
1128             
1129             /*
1130              * Reset things back to a sane state so that one can re-load a new
1131              * driver (perhaps the same one).
1132              */
1133             cpnt->device = NULL;
1134             cpnt->capacity = 0;
1135             SDp->attached--;
1136             sr_template.nr_dev--;
1137             sr_template.dev_noticed--;
1138             sr_sizes[i] = 0;
1139             return;
1140         }
1141     return;
1142 }
1143 
1144 
1145 #ifdef MODULE
1146 #include <linux/module.h>
1147 #include <linux/version.h>
1148 
1149 char kernel_version[] = UTS_RELEASE;
1150 
1151 int init_module(void) {
     /* [previous][next][first][last][top][bottom][index][help] */
1152     sr_template.usage_count = &mod_use_count_;
1153     return scsi_register_module(MODULE_SCSI_DEV, &sr_template);
1154 }
1155 
1156 void cleanup_module( void) 
     /* [previous][next][first][last][top][bottom][index][help] */
1157 {
1158     if (MOD_IN_USE) {
1159         printk(KERN_INFO __FILE__ ": module is in use, remove rejected\n");
1160         return;
1161     }
1162     scsi_unregister_module(MODULE_SCSI_DEV, &sr_template);
1163     unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
1164     sr_registered--;
1165     if(scsi_CDs != NULL) {
1166         scsi_init_free((char *) scsi_CDs,
1167                        (sr_template.dev_noticed + SR_EXTRA_DEVS) 
1168                        * sizeof(Scsi_CD));
1169         
1170         scsi_init_free((char *) sr_sizes, sr_template.dev_max * sizeof(int));
1171         scsi_init_free((char *) sr_blocksizes, sr_template.dev_max * sizeof(int));
1172     }
1173     
1174     blksize_size[MAJOR_NR] = NULL;
1175     blk_dev[MAJOR_NR].request_fn = NULL;
1176     blk_size[MAJOR_NR] = NULL;  
1177     read_ahead[MAJOR_NR] = 0;
1178     
1179     sr_template.dev_max = 0;
1180 }
1181 #endif /* MODULE */
1182 
1183 /*
1184  * Overrides for Emacs so that we follow Linus's tabbing style.
1185  * Emacs will notice this stuff at the end of the file and automatically
1186  * adjust the settings for this buffer only.  This must remain at the end
1187  * of the file.
1188  * ---------------------------------------------------------------------------
1189  * Local variables:
1190  * c-indent-level: 4
1191  * c-brace-imaginary-offset: 0
1192  * c-brace-offset: -4
1193  * c-argdecl-indent: 4
1194  * c-label-offset: -4
1195  * c-continued-statement-offset: 4
1196  * c-continued-brace-offset: 0
1197  * indent-tabs-mode: nil
1198  * tab-width: 8
1199  * End:
1200  */

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