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