Annotation of Gnu-Mach/i386/i386at/gpl/linux/scsi/sr.c, revision 1.1.1.1

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

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.