LCOV - code coverage report
Current view: top level - drivers/scsi - sr.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 337 435 77.5 %
Date: 2017-01-25 Functions: 19 21 90.5 %

          Line data    Source code
       1             : /*
       2             :  *  sr.c Copyright (C) 1992 David Giller
       3             :  *           Copyright (C) 1993, 1994, 1995, 1999 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@andante.org to
      11             :  *      add scatter-gather, multiple outstanding request, and other
      12             :  *      enhancements.
      13             :  *
      14             :  *      Modified by Eric Youngdale eric@andante.org to support loadable
      15             :  *      low-level scsi drivers.
      16             :  *
      17             :  *      Modified by Thomas Quinot thomas@melchior.cuivre.fdn.fr to
      18             :  *      provide auto-eject.
      19             :  *
      20             :  *      Modified by Gerd Knorr <kraxel@cs.tu-berlin.de> to support the
      21             :  *      generic cdrom interface
      22             :  *
      23             :  *      Modified by Jens Axboe <axboe@suse.de> - Uniform sr_packet()
      24             :  *      interface, capabilities probe additions, ioctl cleanups, etc.
      25             :  *
      26             :  *      Modified by Richard Gooch <rgooch@atnf.csiro.au> to support devfs
      27             :  *
      28             :  *      Modified by Jens Axboe <axboe@suse.de> - support DVD-RAM
      29             :  *      transparently and lose the GHOST hack
      30             :  *
      31             :  *      Modified by Arnaldo Carvalho de Melo <acme@conectiva.com.br>
      32             :  *      check resource allocation in sr_init and some cleanups
      33             :  */
      34             : 
      35             : #include <linux/module.h>
      36             : #include <linux/fs.h>
      37             : #include <linux/kernel.h>
      38             : #include <linux/mm.h>
      39             : #include <linux/bio.h>
      40             : #include <linux/string.h>
      41             : #include <linux/errno.h>
      42             : #include <linux/cdrom.h>
      43             : #include <linux/interrupt.h>
      44             : #include <linux/init.h>
      45             : #include <linux/blkdev.h>
      46             : #include <linux/mutex.h>
      47             : #include <asm/uaccess.h>
      48             : 
      49             : #include <scsi/scsi.h>
      50             : #include <scsi/scsi_dbg.h>
      51             : #include <scsi/scsi_device.h>
      52             : #include <scsi/scsi_driver.h>
      53             : #include <scsi/scsi_cmnd.h>
      54             : #include <scsi/scsi_eh.h>
      55             : #include <scsi/scsi_host.h>
      56             : #include <scsi/scsi_ioctl.h>      /* For the door lock/unlock commands */
      57             : 
      58             : #include "scsi_logging.h"
      59             : #include "sr.h"
      60             : 
      61             : 
      62             : MODULE_DESCRIPTION("SCSI cdrom (sr) driver");
      63             : MODULE_LICENSE("GPL");
      64             : MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_CDROM_MAJOR);
      65             : MODULE_ALIAS_SCSI_DEVICE(TYPE_ROM);
      66             : MODULE_ALIAS_SCSI_DEVICE(TYPE_WORM);
      67             : 
      68             : #define SR_DISKS        256
      69             : 
      70             : #define SR_CAPABILITIES \
      71             :         (CDC_CLOSE_TRAY|CDC_OPEN_TRAY|CDC_LOCK|CDC_SELECT_SPEED| \
      72             :          CDC_SELECT_DISC|CDC_MULTI_SESSION|CDC_MCN|CDC_MEDIA_CHANGED| \
      73             :          CDC_PLAY_AUDIO|CDC_RESET|CDC_DRIVE_STATUS| \
      74             :          CDC_CD_R|CDC_CD_RW|CDC_DVD|CDC_DVD_R|CDC_DVD_RAM|CDC_GENERIC_PACKET| \
      75             :          CDC_MRW|CDC_MRW_W|CDC_RAM)
      76             : 
      77             : static int sr_probe(struct device *);
      78             : static int sr_remove(struct device *);
      79             : static int sr_done(struct scsi_cmnd *);
      80             : 
      81           1 : static struct scsi_driver sr_template = {
      82             :         .owner                  = THIS_MODULE,
      83             :         .gendrv = {
      84             :                 .name           = "sr",
      85             :                 .probe          = sr_probe,
      86             :                 .remove         = sr_remove,
      87             :         },
      88             :         .done                   = sr_done,
      89             : };
      90             : 
      91           1 : static unsigned long sr_index_bits[SR_DISKS / BITS_PER_LONG];
      92           1 : static DEFINE_SPINLOCK(sr_index_lock);
      93             : 
      94             : /* This semaphore is used to mediate the 0->1 reference get in the
      95             :  * face of object destruction (i.e. we can't allow a get on an
      96             :  * object after last put) */
      97           1 : static DEFINE_MUTEX(sr_ref_mutex);
      98             : 
      99             : static int sr_open(struct cdrom_device_info *, int);
     100             : static void sr_release(struct cdrom_device_info *);
     101             : 
     102             : static void get_sectorsize(struct scsi_cd *);
     103             : static void get_capabilities(struct scsi_cd *);
     104             : 
     105             : static int sr_media_change(struct cdrom_device_info *, int);
     106             : static int sr_packet(struct cdrom_device_info *, struct packet_command *);
     107             : 
     108           1 : static struct cdrom_device_ops sr_dops = {
     109             :         .open                   = sr_open,
     110             :         .release                = sr_release,
     111             :         .drive_status           = sr_drive_status,
     112             :         .media_changed          = sr_media_change,
     113             :         .tray_move              = sr_tray_move,
     114             :         .lock_door              = sr_lock_door,
     115             :         .select_speed           = sr_select_speed,
     116             :         .get_last_session       = sr_get_last_session,
     117             :         .get_mcn                = sr_get_mcn,
     118             :         .reset                  = sr_reset,
     119             :         .audio_ioctl            = sr_audio_ioctl,
     120             :         .capability             = SR_CAPABILITIES,
     121             :         .generic_packet         = sr_packet,
     122             : };
     123             : 
     124             : static void sr_kref_release(struct kref *kref);
     125             : 
     126             : static inline struct scsi_cd *scsi_cd(struct gendisk *disk)
     127             : {
     128          20 :         return container_of(disk->private_data, struct scsi_cd, driver);
     129             : }
     130             : 
     131             : /*
     132             :  * The get and put routines for the struct scsi_cd.  Note this entity
     133             :  * has a scsi_device pointer and owns a reference to this.
     134             :  */
     135             : static inline struct scsi_cd *scsi_cd_get(struct gendisk *disk)
     136             : {
     137           2 :         struct scsi_cd *cd = NULL;
     138           1 : 
     139           1 :         mutex_lock(&sr_ref_mutex);
     140           3 :         if (disk->private_data == NULL)
     141           1 :                 goto out;
     142           2 :         cd = scsi_cd(disk);
     143           1 :         kref_get(&cd->kref);
     144           3 :         if (scsi_device_get(cd->device))
     145           1 :                 goto out_put;
     146           1 :         goto out;
     147           1 : 
     148             :  out_put:
     149           1 :         kref_put(&cd->kref, sr_kref_release);
     150           1 :         cd = NULL;
     151             :  out:
     152           3 :         mutex_unlock(&sr_ref_mutex);
     153           2 :         return cd;
     154             : }
     155             : 
     156             : static void scsi_cd_put(struct scsi_cd *cd)
     157             : {
     158           4 :         struct scsi_device *sdev = cd->device;
     159             : 
     160           2 :         mutex_lock(&sr_ref_mutex);
     161           2 :         kref_put(&cd->kref, sr_kref_release);
     162           2 :         scsi_device_put(sdev);
     163           2 :         mutex_unlock(&sr_ref_mutex);
     164           2 : }
     165             : 
     166             : /* identical to scsi_test_unit_ready except that it doesn't
     167             :  * eat the NOT_READY returns for removable media */
     168             : int sr_test_unit_ready(struct scsi_device *sdev, struct scsi_sense_hdr *sshdr)
     169             : {
     170           6 :         int retries = MAX_RETRIES;
     171           3 :         int the_result;
     172          21 :         u8 cmd[] = {TEST_UNIT_READY, 0, 0, 0, 0, 0 };
     173           6 : 
     174           3 :         /* issue TEST_UNIT_READY until the initial startup UNIT_ATTENTION
     175           3 :          * conditions are gone, or a timeout happens
     176           3 :          */
     177             :         do {
     178           9 :                 the_result = scsi_execute_req(sdev, cmd, DMA_NONE, NULL,
     179             :                                               0, sshdr, SR_TIMEOUT,
     180             :                                               retries--, NULL);
     181          21 :                 if (scsi_sense_valid(sshdr) &&
     182             :                     sshdr->sense_key == UNIT_ATTENTION)
     183           3 :                         sdev->changed = 1;
     184             : 
     185             :         } while (retries > 0 &&
     186             :                  (!scsi_status_is_good(the_result) ||
     187             :                   (scsi_sense_valid(sshdr) &&
     188          39 :                    sshdr->sense_key == UNIT_ATTENTION)));
     189           6 :         return the_result;
     190           6 : }
     191             : 
     192             : /*
     193           3 :  * This function checks to see if the media has been changed in the
     194             :  * CDROM drive.  It is possible that we have already sensed a change,
     195             :  * or the drive may have sensed one and not yet reported it.  We must
     196             :  * be ready for either case. This function always reports the current
     197             :  * value of the changed bit.  If flag is 0, then the changed bit is reset.
     198             :  * This function could be done as an ioctl, but we would need to have
     199           3 :  * an inode for that to work, and we do not always have one.
     200             :  */
     201             : 
     202             : static int sr_media_change(struct cdrom_device_info *cdi, int slot)
     203             : {
     204           3 :         struct scsi_cd *cd = cdi->handle;
     205           1 :         int retval;
     206           1 :         struct scsi_sense_hdr *sshdr;
     207           1 : 
     208           3 :         if (CDSL_CURRENT != slot) {
     209             :                 /* no changer support */
     210           1 :                 return -EINVAL;
     211             :         }
     212             : 
     213           3 :         sshdr =  kzalloc(sizeof(*sshdr), GFP_KERNEL);
     214           3 :         retval = sr_test_unit_ready(cd->device, sshdr);
     215           9 :         if (retval || (scsi_sense_valid(sshdr) &&
     216             :                        /* 0x3a is medium not present */
     217             :                        sshdr->asc == 0x3a)) {
     218             :                 /* Media not present or unable to test, unit probably not
     219             :                  * ready. This usually means there is no disc in the drive.
     220             :                  * Mark as changed, and we will figure it out later once
     221             :                  * the drive is available again.
     222             :                  */
     223           2 :                 cd->device->changed = 1;
     224             :                 /* This will force a flush, if called from check_disk_change */
     225           2 :                 retval = 1;
     226           2 :                 goto out;
     227             :         };
     228             : 
     229           2 :         retval = cd->device->changed;
     230           1 :         cd->device->changed = 0;
     231             :         /* If the disk changed, the capacity will now be different,
     232             :          * so we force a re-read of this information */
     233           2 :         if (retval) {
     234             :                 /* check multisession offset etc */
     235           6 :                 sr_cd_check(cdi);
     236           3 :                 get_sectorsize(cd);
     237             :         }
     238             : 
     239             : out:
     240             :         /* Notify userspace, that media has changed. */
     241          11 :         if (retval != cd->previous_state)
     242           3 :                 sdev_evt_send_simple(cd->device, SDEV_EVT_MEDIA_CHANGE,
     243             :                                      GFP_KERNEL);
     244           3 :         cd->previous_state = retval;
     245           3 :         kfree(sshdr);
     246             : 
     247           3 :         return retval;
     248             : }
     249             :  
     250             : /*
     251             :  * sr_done is the interrupt routine for the device driver.
     252             :  *
     253             :  * It will be notified on the end of a SCSI read / write, and will take one
     254             :  * of several actions based on success or failure.
     255             :  */
     256             : static int sr_done(struct scsi_cmnd *SCpnt)
     257             : {
     258           2 :         int result = SCpnt->result;
     259           4 :         int this_count = scsi_bufflen(SCpnt);
     260           7 :         int good_bytes = (result == 0 ? this_count : 0);
     261           2 :         int block_sectors = 0;
     262           1 :         long error_sector;
     263           4 :         struct scsi_cd *cd = scsi_cd(SCpnt->request->rq_disk);
     264           1 : 
     265           1 : #ifdef DEBUG
     266           1 :         printk("sr.c done: %x\n", result);
     267           1 : #endif
     268             : 
     269             :         /*
     270             :          * Handle MEDIUM ERRORs or VOLUME OVERFLOWs that indicate partial
     271             :          * success.  Since this is a relatively rare error condition, no
     272             :          * care is taken to avoid unnecessary additional work such as
     273             :          * memcpy's that could be avoided.
     274             :          */
     275           4 :         if (driver_byte(result) != 0 &&         /* An error occurred */
     276             :             (SCpnt->sense_buffer[0] & 0x7f) == 0x70) { /* Sense current */
     277             :                 switch (SCpnt->sense_buffer[2]) {
     278           3 :                 case MEDIUM_ERROR:
     279           3 :                 case VOLUME_OVERFLOW:
     280           3 :                 case ILLEGAL_REQUEST:
     281           2 :                         if (!(SCpnt->sense_buffer[0] & 0x90))
     282           1 :                                 break;
     283           1 :                         error_sector = (SCpnt->sense_buffer[3] << 24) |
     284             :                                 (SCpnt->sense_buffer[4] << 16) |
     285             :                                 (SCpnt->sense_buffer[5] << 8) |
     286             :                                 SCpnt->sense_buffer[6];
     287           3 :                         if (SCpnt->request->bio != NULL)
     288           1 :                                 block_sectors =
     289             :                                         bio_sectors(SCpnt->request->bio);
     290           2 :                         if (block_sectors < 4)
     291           1 :                                 block_sectors = 4;
     292           2 :                         if (cd->device->sector_size == 2048)
     293           1 :                                 error_sector <<= 2;
     294           1 :                         error_sector &= ~(block_sectors - 1);
     295           4 :                         good_bytes = (error_sector -
     296             :                                       blk_rq_pos(SCpnt->request)) << 9;
     297           4 :                         if (good_bytes < 0 || good_bytes >= this_count)
     298           1 :                                 good_bytes = 0;
     299             :                         /*
     300             :                          * The SCSI specification allows for the value
     301             :                          * returned by READ CAPACITY to be up to 75 2K
     302             :                          * sectors past the last readable block.
     303             :                          * Therefore, if we hit a medium error within the
     304             :                          * last 75 2K sectors, we decrease the saved size
     305             :                          * value.
     306             :                          */
     307           7 :                         if (error_sector < get_capacity(cd->disk) &&
     308             :                             cd->capacity - error_sector < 4 * 75)
     309           2 :                                 set_capacity(cd->disk, error_sector);
     310           2 :                         break;
     311           1 : 
     312           3 :                 case RECOVERED_ERROR:
     313           1 :                         good_bytes = this_count;
     314           1 :                         break;
     315           1 : 
     316           1 :                 default:
     317           2 :                         break;
     318           1 :                 }
     319           1 :         }
     320             : 
     321           3 :         return good_bytes;
     322             : }
     323             : 
     324             : static int sr_prep_fn(struct request_queue *q, struct request *rq)
     325             : {
     326           0 :         int block = 0, this_count, s_size;
     327           0 :         struct scsi_cd *cd;
     328           0 :         struct scsi_cmnd *SCpnt;
     329           0 :         struct scsi_device *sdp = q->queuedata;
     330           0 :         int ret;
     331           0 : 
     332           0 :         if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
     333           0 :                 ret = scsi_setup_blk_pc_cmnd(sdp, rq);
     334           0 :                 goto out;
     335           0 :         } else if (rq->cmd_type != REQ_TYPE_FS) {
     336           0 :                 ret = BLKPREP_KILL;
     337           0 :                 goto out;
     338           0 :         }
     339           0 :         ret = scsi_setup_fs_cmnd(sdp, rq);
     340           0 :         if (ret != BLKPREP_OK)
     341           0 :                 goto out;
     342           0 :         SCpnt = rq->special;
     343           0 :         cd = scsi_cd(rq->rq_disk);
     344           0 : 
     345           0 :         /* from here on until we're complete, any goto out
     346           0 :          * is used for a killable error condition */
     347           0 :         ret = BLKPREP_KILL;
     348           0 : 
     349           0 :         SCSI_LOG_HLQUEUE(1, printk("Doing sr request, dev = %s, block = %d\n",
     350           0 :                                 cd->disk->disk_name, block));
     351           0 : 
     352           0 :         if (!cd->device || !scsi_device_online(cd->device)) {
     353           0 :                 SCSI_LOG_HLQUEUE(2, printk("Finishing %u sectors\n",
     354           0 :                                            blk_rq_sectors(rq)));
     355           0 :                 SCSI_LOG_HLQUEUE(2, printk("Retry with 0x%p\n", SCpnt));
     356           0 :                 goto out;
     357           0 :         }
     358           0 : 
     359           0 :         if (cd->device->changed) {
     360           0 :                 /*
     361           0 :                  * quietly refuse to do anything to a changed disc until the
     362             :                  * changed bit has been reset
     363             :                  */
     364           0 :                 goto out;
     365             :         }
     366             : 
     367             :         /*
     368             :          * we do lazy blocksize switching (when reading XA sectors,
     369             :          * see CDROMREADMODE2 ioctl) 
     370             :          */
     371           0 :         s_size = cd->device->sector_size;
     372           0 :         if (s_size > 2048) {
     373           0 :                 if (!in_interrupt())
     374           0 :                         sr_set_blocklength(cd, 2048);
     375             :                 else
     376           0 :                         printk("sr: can't switch blocksize: in interrupt\n");
     377             :         }
     378             : 
     379           0 :         if (s_size != 512 && s_size != 1024 && s_size != 2048) {
     380           0 :                 scmd_printk(KERN_ERR, SCpnt, "bad sector size %d\n", s_size);
     381           0 :                 goto out;
     382             :         }
     383             : 
     384           0 :         if (rq_data_dir(rq) == WRITE) {
     385           0 :                 if (!cd->device->writeable)
     386           0 :                         goto out;
     387           0 :                 SCpnt->cmnd[0] = WRITE_10;
     388           0 :                 SCpnt->sc_data_direction = DMA_TO_DEVICE;
     389           0 :                 cd->cdi.media_written = 1;
     390           0 :         } else if (rq_data_dir(rq) == READ) {
     391           0 :                 SCpnt->cmnd[0] = READ_10;
     392           0 :                 SCpnt->sc_data_direction = DMA_FROM_DEVICE;
     393             :         } else {
     394           0 :                 blk_dump_rq_flags(rq, "Unknown sr command");
     395           0 :                 goto out;
     396             :         }
     397             : 
     398             :         {
     399             :                 struct scatterlist *sg;
     400           0 :                 int i, size = 0, sg_count = scsi_sg_count(SCpnt);
     401             : 
     402           0 :                 scsi_for_each_sg(SCpnt, sg, sg_count, i)
     403           0 :                         size += sg->length;
     404           0 : 
     405           0 :                 if (size != scsi_bufflen(SCpnt)) {
     406           0 :                         scmd_printk(KERN_ERR, SCpnt,
     407             :                                 "mismatch count %d, bytes %d\n",
     408             :                                 size, scsi_bufflen(SCpnt));
     409           0 :                         if (scsi_bufflen(SCpnt) > size)
     410           0 :                                 SCpnt->sdb.length = size;
     411             :                 }
     412             :         }
     413             : 
     414             :         /*
     415             :          * request doesn't start on hw block boundary, add scatter pads
     416             :          */
     417           0 :         if (((unsigned int)blk_rq_pos(rq) % (s_size >> 9)) ||
     418           0 :             (scsi_bufflen(SCpnt) % s_size)) {
     419           0 :                 scmd_printk(KERN_NOTICE, SCpnt, "unaligned transfer\n");
     420           0 :                 goto out;
     421             :         }
     422             : 
     423           0 :         this_count = (scsi_bufflen(SCpnt) >> 9) / (s_size >> 9);
     424             : 
     425             : 
     426             :         SCSI_LOG_HLQUEUE(2, printk("%s : %s %d/%u 512 byte blocks.\n",
     427             :                                 cd->cdi.name,
     428             :                                 (rq_data_dir(rq) == WRITE) ?
     429             :                                         "writing" : "reading",
     430             :                                 this_count, blk_rq_sectors(rq)));
     431             : 
     432           0 :         SCpnt->cmnd[1] = 0;
     433           0 :         block = (unsigned int)blk_rq_pos(rq) / (s_size >> 9);
     434             : 
     435           0 :         if (this_count > 0xffff) {
     436           0 :                 this_count = 0xffff;
     437           0 :                 SCpnt->sdb.length = this_count * s_size;
     438             :         }
     439             : 
     440           0 :         SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
     441           0 :         SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
     442           0 :         SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
     443           0 :         SCpnt->cmnd[5] = (unsigned char) block & 0xff;
     444           0 :         SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
     445           0 :         SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
     446           0 :         SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
     447             : 
     448             :         /*
     449             :          * We shouldn't disconnect in the middle of a sector, so with a dumb
     450             :          * host adapter, it's safe to assume that we can at least transfer
     451             :          * this many bytes between each connect / disconnect.
     452             :          */
     453           0 :         SCpnt->transfersize = cd->device->sector_size;
     454           0 :         SCpnt->underflow = this_count << 9;
     455           0 :         SCpnt->allowed = MAX_RETRIES;
     456             : 
     457             :         /*
     458             :          * This indicates that the command is ready from our end to be
     459             :          * queued.
     460             :          */
     461           0 :         ret = BLKPREP_OK;
     462           0 :  out:
     463           0 :         return scsi_prep_return(q, rq, ret);
     464             : }
     465             : 
     466             : static int sr_block_open(struct block_device *bdev, fmode_t mode)
     467             : {
     468           5 :         struct scsi_cd *cd = scsi_cd_get(bdev->bd_disk);
     469           2 :         int ret = -ENXIO;
     470           1 : 
     471           2 :         if (cd) {
     472           1 :                 ret = cdrom_open(&cd->cdi, bdev, mode);
     473           2 :                 if (ret)
     474           2 :                         scsi_cd_put(cd);
     475             :         }
     476           2 :         return ret;
     477             : }
     478             : 
     479             : static int sr_block_release(struct gendisk *disk, fmode_t mode)
     480             : {
     481           4 :         struct scsi_cd *cd = scsi_cd(disk);
     482           2 :         cdrom_release(&cd->cdi, mode);
     483           2 :         scsi_cd_put(cd);
     484           1 :         return 0;
     485             : }
     486             : 
     487             : static int sr_block_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,
     488             :                           unsigned long arg)
     489             : {
     490           4 :         struct scsi_cd *cd = scsi_cd(bdev->bd_disk);
     491           2 :         struct scsi_device *sdev = cd->device;
     492           2 :         void __user *argp = (void __user *)arg;
     493           1 :         int ret;
     494           1 : 
     495           1 :         /*
     496           1 :          * Send SCSI addressing ioctls directly to mid level, send other
     497             :          * ioctls to cdrom/block level.
     498             :          */
     499           1 :         switch (cmd) {
     500           4 :         case SCSI_IOCTL_GET_IDLUN:
     501           4 :         case SCSI_IOCTL_GET_BUS_NUMBER:
     502           2 :                 return scsi_ioctl(sdev, cmd, argp);
     503           1 :         }
     504             : 
     505           1 :         ret = cdrom_ioctl(&cd->cdi, bdev, mode, cmd, arg);
     506           2 :         if (ret != -ENOSYS)
     507           1 :                 return ret;
     508             : 
     509             :         /*
     510             :          * ENODEV means that we didn't recognise the ioctl, or that we
     511             :          * cannot execute it in the current device state.  In either
     512             :          * case fall through to scsi_ioctl, which will return ENDOEV again
     513             :          * if it doesn't recognise the ioctl
     514             :          */
     515           1 :         ret = scsi_nonblockable_ioctl(sdev, cmd, argp,
     516             :                                         (mode & FMODE_NDELAY) != 0);
     517           2 :         if (ret != -ENODEV)
     518           1 :                 return ret;
     519           2 :         return scsi_ioctl(sdev, cmd, argp);
     520             : }
     521             : 
     522             : static int sr_block_media_changed(struct gendisk *disk)
     523             : {
     524           4 :         struct scsi_cd *cd = scsi_cd(disk);
     525           3 :         return cdrom_media_changed(&cd->cdi);
     526           1 : }
     527             : 
     528           1 : static const struct block_device_operations sr_bdops =
     529             : {
     530             :         .owner          = THIS_MODULE,
     531             :         .open           = sr_block_open,
     532             :         .release        = sr_block_release,
     533             :         .locked_ioctl   = sr_block_ioctl,
     534             :         .media_changed  = sr_block_media_changed,
     535             :         /* 
     536             :          * No compat_ioctl for now because sr_block_ioctl never
     537             :          * seems to pass arbitary ioctls down to host drivers.
     538             :          */
     539             : };
     540             : 
     541             : static int sr_open(struct cdrom_device_info *cdi, int purpose)
     542             : {
     543           3 :         struct scsi_cd *cd = cdi->handle;
     544           2 :         struct scsi_device *sdev = cd->device;
     545           1 :         int retval;
     546           1 : 
     547             :         /*
     548             :          * If the device is in error recovery, wait until it is done.
     549             :          * If the device is offline, then disallow any access to it.
     550             :          */
     551           1 :         retval = -ENXIO;
     552           3 :         if (!scsi_block_when_processing_errors(sdev))
     553           1 :                 goto error_out;
     554             : 
     555           1 :         return 0;
     556           1 : 
     557             : error_out:
     558           1 :         return retval;  
     559             : }
     560             : 
     561             : static void sr_release(struct cdrom_device_info *cdi)
     562             : {
     563           3 :         struct scsi_cd *cd = cdi->handle;
     564             : 
     565           2 :         if (cd->device->sector_size > 2048)
     566           3 :                 sr_set_blocklength(cd, 2048);
     567           2 : 
     568             : }
     569             : 
     570             : static int sr_probe(struct device *dev)
     571             : {
     572           3 :         struct scsi_device *sdev = to_scsi_device(dev);
     573           1 :         struct gendisk *disk;
     574           1 :         struct scsi_cd *cd;
     575           1 :         int minor, error;
     576           1 : 
     577           2 :         error = -ENODEV;
     578           4 :         if (sdev->type != TYPE_ROM && sdev->type != TYPE_WORM)
     579           2 :                 goto fail;
     580           1 : 
     581           2 :         error = -ENOMEM;
     582           4 :         cd = kzalloc(sizeof(*cd), GFP_KERNEL);
     583           2 :         if (!cd)
     584           1 :                 goto fail;
     585             : 
     586           1 :         kref_init(&cd->kref);
     587             : 
     588           1 :         disk = alloc_disk(1);
     589           2 :         if (!disk)
     590           1 :                 goto fail_free;
     591             : 
     592           2 :         spin_lock(&sr_index_lock);
     593           2 :         minor = find_first_zero_bit(sr_index_bits, SR_DISKS);
     594           2 :         if (minor == SR_DISKS) {
     595           2 :                 spin_unlock(&sr_index_lock);
     596           1 :                 error = -EBUSY;
     597           1 :                 goto fail_put;
     598             :         }
     599           2 :         __set_bit(minor, sr_index_bits);
     600           2 :         spin_unlock(&sr_index_lock);
     601             : 
     602           1 :         disk->major = SCSI_CDROM_MAJOR;
     603           1 :         disk->first_minor = minor;
     604           1 :         sprintf(disk->disk_name, "sr%d", minor);
     605           1 :         disk->fops = &sr_bdops;
     606           1 :         disk->flags = GENHD_FL_CD;
     607             : 
     608           1 :         blk_queue_rq_timeout(sdev->request_queue, SR_TIMEOUT);
     609             : 
     610           1 :         cd->device = sdev;
     611           1 :         cd->disk = disk;
     612           1 :         cd->driver = &sr_template;
     613           1 :         cd->disk = disk;
     614           1 :         cd->capacity = 0x1fffff;
     615           1 :         cd->device->changed = 1;  /* force recheck CD type */
     616           1 :         cd->previous_state = 1;
     617           1 :         cd->use = 1;
     618           1 :         cd->readcd_known = 0;
     619           1 :         cd->readcd_cdda = 0;
     620             : 
     621           1 :         cd->cdi.ops = &sr_dops;
     622           1 :         cd->cdi.handle = cd;
     623           1 :         cd->cdi.mask = 0;
     624           1 :         cd->cdi.capacity = 1;
     625           1 :         sprintf(cd->cdi.name, "sr%d", minor);
     626             : 
     627           1 :         sdev->sector_size = 2048;    /* A guess, just in case */
     628             : 
     629             :         /* FIXME: need to handle a get_capabilities failure properly ?? */
     630           3 :         get_capabilities(cd);
     631           1 :         blk_queue_prep_rq(sdev->request_queue, sr_prep_fn);
     632           2 :         sr_vendor_init(cd);
     633             : 
     634           1 :         disk->driverfs_dev = &sdev->sdev_gendev;
     635           3 :         set_capacity(disk, cd->capacity);
     636           1 :         disk->private_data = &cd->driver;
     637           1 :         disk->queue = sdev->request_queue;
     638           1 :         cd->cdi.disk = disk;
     639             : 
     640           3 :         if (register_cdrom(&cd->cdi))
     641           1 :                 goto fail_put;
     642             : 
     643           1 :         dev_set_drvdata(dev, cd);
     644           1 :         disk->flags |= GENHD_FL_REMOVABLE;
     645           1 :         add_disk(disk);
     646             : 
     647           4 :         sdev_printk(KERN_DEBUG, sdev,
     648             :                     "Attached scsi CD-ROM %s\n", cd->cdi.name);
     649           1 :         return 0;
     650           2 : 
     651             : fail_put:
     652           2 :         put_disk(disk);
     653             : fail_free:
     654           5 :         kfree(cd);
     655             : fail:
     656           7 :         return error;
     657             : }
     658             : 
     659             : 
     660             : static void get_sectorsize(struct scsi_cd *cd)
     661             : {
     662           1 :         unsigned char cmd[10];
     663           1 :         unsigned char buffer[8];
     664           2 :         int the_result, retries = 3;
     665           2 :         int sector_size;
     666           1 :         struct request_queue *queue;
     667           1 : 
     668           1 :         do {
     669           2 :                 cmd[0] = READ_CAPACITY;
     670           2 :                 memset((void *) &cmd[1], 0, 9);
     671           2 :                 memset(buffer, 0, sizeof(buffer));
     672             : 
     673             :                 /* Do the command and wait.. */
     674           1 :                 the_result = scsi_execute_req(cd->device, cmd, DMA_FROM_DEVICE,
     675             :                                               buffer, sizeof(buffer), NULL,
     676             :                                               SR_TIMEOUT, MAX_RETRIES, NULL);
     677             : 
     678           1 :                 retries--;
     679             : 
     680           4 :         } while (the_result && retries);
     681             : 
     682           1 : 
     683           2 :         if (the_result) {
     684           1 :                 cd->capacity = 0x1fffff;
     685           1 :                 sector_size = 2048;     /* A guess, just in case */
     686             :         } else {
     687             :                 long last_written;
     688             : 
     689           1 :                 cd->capacity = 1 + ((buffer[0] << 24) | (buffer[1] << 16) |
     690             :                                     (buffer[2] << 8) | buffer[3]);
     691             :                 /*
     692             :                  * READ_CAPACITY doesn't return the correct size on
     693             :                  * certain UDF media.  If last_written is larger, use
     694             :                  * it instead.
     695             :                  *
     696             :                  * http://bugzilla.kernel.org/show_bug.cgi?id=9668
     697             :                  */
     698           3 :                 if (!cdrom_get_last_written(&cd->cdi, &last_written))
     699           9 :                         cd->capacity = max_t(long, cd->capacity, last_written);
     700             : 
     701           1 :                 sector_size = (buffer[4] << 24) |
     702             :                     (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
     703             :                 switch (sector_size) {
     704             :                         /*
     705             :                          * HP 4020i CD-Recorder reports 2340 byte sectors
     706             :                          * Philips CD-Writers report 2352 byte sectors
     707             :                          *
     708             :                          * Use 2k sectors for them..
     709             :                          */
     710           3 :                 case 0:
     711           3 :                 case 2340:
     712           3 :                 case 2352:
     713           1 :                         sector_size = 2048;
     714           1 :                         /* fall through */
     715           3 :                 case 2048:
     716           1 :                         cd->capacity *= 4;
     717           1 :                         /* fall through */
     718           3 :                 case 512:
     719           1 :                         break;
     720           2 :                 default:
     721           2 :                         printk("%s: unsupported sector size %d.\n",
     722           1 :                                cd->cdi.name, sector_size);
     723           2 :                         cd->capacity = 0;
     724             :                 }
     725           1 : 
     726           1 :                 cd->device->sector_size = sector_size;
     727           1 : 
     728             :                 /*
     729             :                  * Add this so that we have the ability to correctly gauge
     730             :                  * what the device is capable of.
     731             :                  */
     732           3 :                 set_capacity(cd->disk, cd->capacity);
     733             :         }
     734             : 
     735           2 :         queue = cd->device->request_queue;
     736           2 :         blk_queue_logical_block_size(queue, sector_size);
     737             : 
     738           2 :         return;
     739             : }
     740             : 
     741             : static void get_capabilities(struct scsi_cd *cd)
     742             : {
     743           1 :         unsigned char *buffer;
     744           1 :         struct scsi_mode_data data;
     745           1 :         struct scsi_sense_hdr sshdr;
     746           1 :         int rc, n;
     747           1 : 
     748           9 :         static const char *loadmech[] =
     749           1 :         {
     750           1 :                 "caddy",
     751             :                 "tray",
     752             :                 "pop-up",
     753             :                 "",
     754             :                 "changer",
     755             :                 "cartridge changer",
     756             :                 "",
     757             :                 ""
     758             :         };
     759             : 
     760             : 
     761             :         /* allocate transfer buffer */
     762           3 :         buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
     763           2 :         if (!buffer) {
     764           1 :                 printk(KERN_ERR "sr: out of memory.\n");
     765           1 :                 return;
     766             :         }
     767             : 
     768             :         /* eat unit attentions */
     769           3 :         sr_test_unit_ready(cd->device, &sshdr);
     770             : 
     771             :         /* ask for mode page 0x2a */
     772           1 :         rc = scsi_mode_sense(cd->device, 0, 0x2a, buffer, 128,
     773             :                              SR_TIMEOUT, 3, &data, NULL);
     774             : 
     775           4 :         if (!scsi_status_is_good(rc)) {
     776             :                 /* failed, drive doesn't have capabilities mode page */
     777           1 :                 cd->cdi.speed = 1;
     778           1 :                 cd->cdi.mask |= (CDC_CD_R | CDC_CD_RW | CDC_DVD_R |
     779             :                                  CDC_DVD | CDC_DVD_RAM |
     780             :                                  CDC_SELECT_DISC | CDC_SELECT_SPEED |
     781             :                                  CDC_MRW | CDC_MRW_W | CDC_RAM);
     782           1 :                 kfree(buffer);
     783           1 :                 printk("%s: scsi-1 drive\n", cd->cdi.name);
     784           1 :                 return;
     785             :         }
     786             : 
     787           1 :         n = data.header_length + data.block_descriptor_length;
     788           1 :         cd->cdi.speed = ((buffer[n + 8] << 8) + buffer[n + 9]) / 176;
     789           1 :         cd->readcd_known = 1;
     790           1 :         cd->readcd_cdda = buffer[n + 5] & 0x01;
     791             :         /* print some capability bits */
     792           6 :         printk("%s: scsi3-mmc drive: %dx/%dx %s%s%s%s%s%s\n", cd->cdi.name,
     793             :                ((buffer[n + 14] << 8) + buffer[n + 15]) / 176,
     794           4 :                cd->cdi.speed,
     795           4 :                buffer[n + 3] & 0x01 ? "writer " : "", /* CD Writer */
     796           4 :                buffer[n + 3] & 0x20 ? "dvd-ram " : "",
     797           4 :                buffer[n + 2] & 0x02 ? "cd/rw " : "", /* can read rewriteable */
     798           4 :                buffer[n + 4] & 0x20 ? "xa/form2 " : "", /* can read xa/from2 */
     799             :                buffer[n + 5] & 0x01 ? "cdda " : "", /* can read audio data */
     800             :                loadmech[buffer[n + 6] >> 5]);
     801           2 :         if ((buffer[n + 6] >> 5) == 0)
     802             :                 /* caddy drives can't close tray... */
     803           1 :                 cd->cdi.mask |= CDC_CLOSE_TRAY;
     804           2 :         if ((buffer[n + 2] & 0x8) == 0)
     805             :                 /* not a DVD drive */
     806           1 :                 cd->cdi.mask |= CDC_DVD;
     807           2 :         if ((buffer[n + 3] & 0x20) == 0) 
     808             :                 /* can't write DVD-RAM media */
     809           1 :                 cd->cdi.mask |= CDC_DVD_RAM;
     810           2 :         if ((buffer[n + 3] & 0x10) == 0)
     811             :                 /* can't write DVD-R media */
     812           1 :                 cd->cdi.mask |= CDC_DVD_R;
     813           2 :         if ((buffer[n + 3] & 0x2) == 0)
     814             :                 /* can't write CD-RW media */
     815           1 :                 cd->cdi.mask |= CDC_CD_RW;
     816           2 :         if ((buffer[n + 3] & 0x1) == 0)
     817             :                 /* can't write CD-R media */
     818           1 :                 cd->cdi.mask |= CDC_CD_R;
     819           2 :         if ((buffer[n + 6] & 0x8) == 0)
     820             :                 /* can't eject */
     821           1 :                 cd->cdi.mask |= CDC_OPEN_TRAY;
     822             : 
     823           2 :         if ((buffer[n + 6] >> 5) == mechtype_individual_changer ||
     824             :             (buffer[n + 6] >> 5) == mechtype_cartridge_changer)
     825           1 :                 cd->cdi.capacity =
     826             :                     cdrom_number_of_slots(&cd->cdi);
     827           2 :         if (cd->cdi.capacity <= 1)
     828             :                 /* not a changer */
     829           1 :                 cd->cdi.mask |= CDC_SELECT_DISC;
     830             :         /*else    I don't think it can close its tray
     831             :                 cd->cdi.mask |= CDC_CLOSE_TRAY; */
     832             : 
     833             :         /*
     834             :          * if DVD-RAM, MRW-W or CD-RW, we are randomly writable
     835             :          */
     836           2 :         if ((cd->cdi.mask & (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) !=
     837             :                         (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) {
     838           1 :                 cd->device->writeable = 1;
     839             :         }
     840             : 
     841           1 :         kfree(buffer);
     842           1 : }
     843             : 
     844             : /*
     845             :  * sr_packet() is the entry point for the generic commands generated
     846             :  * by the Uniform CD-ROM layer. 
     847             :  */
     848             : static int sr_packet(struct cdrom_device_info *cdi,
     849             :                 struct packet_command *cgc)
     850             : {
     851           2 :         if (cgc->timeout <= 0)
     852           1 :                 cgc->timeout = IOCTL_TIMEOUT;
     853             : 
     854           4 :         sr_do_ioctl(cdi->handle, cgc);
     855             : 
     856           1 :         return cgc->stat;
     857             : }
     858             : 
     859             : /**
     860             :  *      sr_kref_release - Called to free the scsi_cd structure
     861             :  *      @kref: pointer to embedded kref
     862             :  *
     863             :  *      sr_ref_mutex must be held entering this routine.  Because it is
     864             :  *      called on last put, you should always use the scsi_cd_get()
     865             :  *      scsi_cd_put() helpers which manipulate the semaphore directly
     866             :  *      and never do a direct kref_put().
     867             :  **/
     868             : static void sr_kref_release(struct kref *kref)
     869             : {
     870           0 :         struct scsi_cd *cd = container_of(kref, struct scsi_cd, kref);
     871           0 :         struct gendisk *disk = cd->disk;
     872           0 : 
     873           0 :         spin_lock(&sr_index_lock);
     874           0 :         clear_bit(MINOR(disk_devt(disk)), sr_index_bits);
     875           0 :         spin_unlock(&sr_index_lock);
     876             : 
     877           0 :         unregister_cdrom(&cd->cdi);
     878             : 
     879           0 :         disk->private_data = NULL;
     880             : 
     881           0 :         put_disk(disk);
     882             : 
     883           0 :         kfree(cd);
     884           0 : }
     885             : 
     886             : static int sr_remove(struct device *dev)
     887             : {
     888           4 :         struct scsi_cd *cd = dev_get_drvdata(dev);
     889           1 : 
     890           1 :         blk_queue_prep_rq(cd->device->request_queue, scsi_prep_fn);
     891           1 :         del_gendisk(cd->disk);
     892             : 
     893           1 :         mutex_lock(&sr_ref_mutex);
     894           1 :         kref_put(&cd->kref, sr_kref_release);
     895           1 :         mutex_unlock(&sr_ref_mutex);
     896             : 
     897           1 :         return 0;
     898             : }
     899             : 
     900             : static int __init init_sr(void)
     901             : {
     902           1 :         int rc;
     903             : 
     904           1 :         rc = register_blkdev(SCSI_CDROM_MAJOR, "sr");
     905           2 :         if (rc)
     906           1 :                 return rc;
     907           2 :         rc = scsi_register_driver(&sr_template.gendrv);
     908           2 :         if (rc)
     909           1 :                 unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
     910             : 
     911           1 :         return rc;
     912             : }
     913             : 
     914             : static void __exit exit_sr(void)
     915             : {
     916           4 :         scsi_unregister_driver(&sr_template.gendrv);
     917           2 :         unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
     918           2 : }
     919             : 
     920             : module_init(init_sr);
     921             : module_exit(exit_sr);
     922           1 : MODULE_LICENSE("GPL");

Generated by: LCOV version 1.10