11da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* 21da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * sr.c Copyright (C) 1992 David Giller 31da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Copyright (C) 1993, 1994, 1995, 1999 Eric Youngdale 41da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * 51da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * adapted from: 61da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * sd.c Copyright (C) 1992 Drew Eckhardt 71da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Linux scsi disk driver by 81da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Drew Eckhardt <drew@colorado.edu> 91da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * 101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Modified by Eric Youngdale ericy@andante.org to 111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * add scatter-gather, multiple outstanding request, and other 121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * enhancements. 131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * 141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Modified by Eric Youngdale eric@andante.org to support loadable 151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * low-level scsi drivers. 161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * 171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Modified by Thomas Quinot thomas@melchior.cuivre.fdn.fr to 181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * provide auto-eject. 191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * 201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Modified by Gerd Knorr <kraxel@cs.tu-berlin.de> to support the 211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * generic cdrom interface 221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * 231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Modified by Jens Axboe <axboe@suse.de> - Uniform sr_packet() 241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * interface, capabilities probe additions, ioctl cleanups, etc. 251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * 261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Modified by Richard Gooch <rgooch@atnf.csiro.au> to support devfs 271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * 281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Modified by Jens Axboe <axboe@suse.de> - support DVD-RAM 291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * transparently and lose the GHOST hack 301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * 311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Modified by Arnaldo Carvalho de Melo <acme@conectiva.com.br> 321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * check resource allocation in sr_init and some cleanups 331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */ 341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/module.h> 361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/fs.h> 371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/kernel.h> 381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/mm.h> 391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/bio.h> 401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/string.h> 411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/errno.h> 421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/cdrom.h> 431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/interrupt.h> 441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/init.h> 451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/blkdev.h> 460b9506723826c68b50fa33e345700ddcac1bed36Arjan van de Ven#include <linux/mutex.h> 475a0e3ad6af8660be21ca98a971cd00f331318c05Tejun Heo#include <linux/slab.h> 486c7f1e2f12f6f37ddda01180c9e5c407eccce616Aaron Lu#include <linux/pm_runtime.h> 491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <asm/uaccess.h> 501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <scsi/scsi.h> 521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <scsi/scsi_dbg.h> 531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <scsi/scsi_device.h> 541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <scsi/scsi_driver.h> 55820732b501a5bbdd3bde1263f391891e21b5ed8cJames Bottomley#include <scsi/scsi_cmnd.h> 561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <scsi/scsi_eh.h> 571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <scsi/scsi_host.h> 581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <scsi/scsi_ioctl.h> /* For the door lock/unlock commands */ 591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include "scsi_logging.h" 611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include "sr.h" 621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 64f018fa552c52642a6b9db2bda90477762e42163fRene HermanMODULE_DESCRIPTION("SCSI cdrom (sr) driver"); 65f018fa552c52642a6b9db2bda90477762e42163fRene HermanMODULE_LICENSE("GPL"); 66f018fa552c52642a6b9db2bda90477762e42163fRene HermanMODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_CDROM_MAJOR); 67d7b8bcb0a0819315a51cae620ff7ae0c1704c069Michael TokarevMODULE_ALIAS_SCSI_DEVICE(TYPE_ROM); 68d7b8bcb0a0819315a51cae620ff7ae0c1704c069Michael TokarevMODULE_ALIAS_SCSI_DEVICE(TYPE_WORM); 69f018fa552c52642a6b9db2bda90477762e42163fRene Herman 701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define SR_DISKS 256 711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define SR_CAPABILITIES \ 731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds (CDC_CLOSE_TRAY|CDC_OPEN_TRAY|CDC_LOCK|CDC_SELECT_SPEED| \ 741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds CDC_SELECT_DISC|CDC_MULTI_SESSION|CDC_MCN|CDC_MEDIA_CHANGED| \ 756a2900b67652421b51fe25e4b86ecfec742b1f30Christoph Hellwig CDC_PLAY_AUDIO|CDC_RESET|CDC_DRIVE_STATUS| \ 761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds CDC_CD_R|CDC_CD_RW|CDC_DVD|CDC_DVD_R|CDC_DVD_RAM|CDC_GENERIC_PACKET| \ 771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds CDC_MRW|CDC_MRW_W|CDC_RAM) 781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 792a48fc0ab24241755dc93bfd4f01d68efab47f5aArnd Bergmannstatic DEFINE_MUTEX(sr_mutex); 801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int sr_probe(struct device *); 811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int sr_remove(struct device *); 82a1b73fc194e73ed33c8b77bf09374cb05b58151bChristoph Hellwigstatic int sr_init_command(struct scsi_cmnd *SCpnt); 837b3d9545f9ac8b31528dd2d6d8ec8d19922917b8Linus Torvaldsstatic int sr_done(struct scsi_cmnd *); 846c7f1e2f12f6f37ddda01180c9e5c407eccce616Aaron Lustatic int sr_runtime_suspend(struct device *dev); 856c7f1e2f12f6f37ddda01180c9e5c407eccce616Aaron Lu 866c7f1e2f12f6f37ddda01180c9e5c407eccce616Aaron Lustatic struct dev_pm_ops sr_pm_ops = { 876c7f1e2f12f6f37ddda01180c9e5c407eccce616Aaron Lu .runtime_suspend = sr_runtime_suspend, 886c7f1e2f12f6f37ddda01180c9e5c407eccce616Aaron Lu}; 891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct scsi_driver sr_template = { 911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds .owner = THIS_MODULE, 921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds .gendrv = { 931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds .name = "sr", 941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds .probe = sr_probe, 951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds .remove = sr_remove, 966c7f1e2f12f6f37ddda01180c9e5c407eccce616Aaron Lu .pm = &sr_pm_ops, 971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds }, 98a1b73fc194e73ed33c8b77bf09374cb05b58151bChristoph Hellwig .init_command = sr_init_command, 997b3d9545f9ac8b31528dd2d6d8ec8d19922917b8Linus Torvalds .done = sr_done, 1001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}; 1011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 1021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic unsigned long sr_index_bits[SR_DISKS / BITS_PER_LONG]; 1031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic DEFINE_SPINLOCK(sr_index_lock); 1041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 1051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* This semaphore is used to mediate the 0->1 reference get in the 1061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * face of object destruction (i.e. we can't allow a get on an 1071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * object after last put) */ 1080b9506723826c68b50fa33e345700ddcac1bed36Arjan van de Venstatic DEFINE_MUTEX(sr_ref_mutex); 1091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 1101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int sr_open(struct cdrom_device_info *, int); 1111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void sr_release(struct cdrom_device_info *); 1121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 1131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void get_sectorsize(struct scsi_cd *); 1141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void get_capabilities(struct scsi_cd *); 1151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 11693aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heostatic unsigned int sr_check_events(struct cdrom_device_info *cdi, 11793aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo unsigned int clearing, int slot); 1181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int sr_packet(struct cdrom_device_info *, struct packet_command *); 1191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 1201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct cdrom_device_ops sr_dops = { 1211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds .open = sr_open, 1221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds .release = sr_release, 1231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds .drive_status = sr_drive_status, 12493aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo .check_events = sr_check_events, 1251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds .tray_move = sr_tray_move, 1261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds .lock_door = sr_lock_door, 1271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds .select_speed = sr_select_speed, 1281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds .get_last_session = sr_get_last_session, 1291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds .get_mcn = sr_get_mcn, 1301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds .reset = sr_reset, 1311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds .audio_ioctl = sr_audio_ioctl, 1321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds .capability = SR_CAPABILITIES, 1331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds .generic_packet = sr_packet, 1341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}; 1351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 1361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void sr_kref_release(struct kref *kref); 1371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 1381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic inline struct scsi_cd *scsi_cd(struct gendisk *disk) 1391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{ 1401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return container_of(disk->private_data, struct scsi_cd, driver); 1411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} 1421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 1436c7f1e2f12f6f37ddda01180c9e5c407eccce616Aaron Lustatic int sr_runtime_suspend(struct device *dev) 1446c7f1e2f12f6f37ddda01180c9e5c407eccce616Aaron Lu{ 1456c7f1e2f12f6f37ddda01180c9e5c407eccce616Aaron Lu struct scsi_cd *cd = dev_get_drvdata(dev); 1466c7f1e2f12f6f37ddda01180c9e5c407eccce616Aaron Lu 1476c7f1e2f12f6f37ddda01180c9e5c407eccce616Aaron Lu if (cd->media_present) 1486c7f1e2f12f6f37ddda01180c9e5c407eccce616Aaron Lu return -EBUSY; 1496c7f1e2f12f6f37ddda01180c9e5c407eccce616Aaron Lu else 1506c7f1e2f12f6f37ddda01180c9e5c407eccce616Aaron Lu return 0; 1516c7f1e2f12f6f37ddda01180c9e5c407eccce616Aaron Lu} 1526c7f1e2f12f6f37ddda01180c9e5c407eccce616Aaron Lu 1531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* 1541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * The get and put routines for the struct scsi_cd. Note this entity 1551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * has a scsi_device pointer and owns a reference to this. 1561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */ 1571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic inline struct scsi_cd *scsi_cd_get(struct gendisk *disk) 1581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{ 1591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds struct scsi_cd *cd = NULL; 1601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 1610b9506723826c68b50fa33e345700ddcac1bed36Arjan van de Ven mutex_lock(&sr_ref_mutex); 1621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (disk->private_data == NULL) 1631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds goto out; 1641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds cd = scsi_cd(disk); 1651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds kref_get(&cd->kref); 1666627b38fd902cd31c6ad52f9be12a17a38f0faefAaron Lu if (scsi_device_get(cd->device)) { 1676627b38fd902cd31c6ad52f9be12a17a38f0faefAaron Lu kref_put(&cd->kref, sr_kref_release); 1686627b38fd902cd31c6ad52f9be12a17a38f0faefAaron Lu cd = NULL; 1696627b38fd902cd31c6ad52f9be12a17a38f0faefAaron Lu } 1701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds out: 1710b9506723826c68b50fa33e345700ddcac1bed36Arjan van de Ven mutex_unlock(&sr_ref_mutex); 1721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return cd; 1731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} 1741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 175858119e159384308a5dde67776691a2ebf70df0fArjan van de Venstatic void scsi_cd_put(struct scsi_cd *cd) 1761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{ 1771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds struct scsi_device *sdev = cd->device; 1781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 1790b9506723826c68b50fa33e345700ddcac1bed36Arjan van de Ven mutex_lock(&sr_ref_mutex); 1801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds kref_put(&cd->kref, sr_kref_release); 1811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds scsi_device_put(sdev); 1820b9506723826c68b50fa33e345700ddcac1bed36Arjan van de Ven mutex_unlock(&sr_ref_mutex); 1831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} 1841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 18593aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heostatic unsigned int sr_get_events(struct scsi_device *sdev) 18693aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo{ 18793aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo u8 buf[8]; 18893aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo u8 cmd[] = { GET_EVENT_STATUS_NOTIFICATION, 18993aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo 1, /* polled */ 19093aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo 0, 0, /* reserved */ 19193aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo 1 << 4, /* notification class: media */ 19293aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo 0, 0, /* reserved */ 19393aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo 0, sizeof(buf), /* allocation length */ 19493aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo 0, /* control */ 19593aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo }; 19693aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo struct event_header *eh = (void *)buf; 19793aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo struct media_event_desc *med = (void *)(buf + 4); 19893aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo struct scsi_sense_hdr sshdr; 19993aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo int result; 20093aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo 20193aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo result = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buf, sizeof(buf), 20293aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo &sshdr, SR_TIMEOUT, MAX_RETRIES, NULL); 20393aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo if (scsi_sense_valid(&sshdr) && sshdr.sense_key == UNIT_ATTENTION) 20493aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo return DISK_EVENT_MEDIA_CHANGE; 20593aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo 20693aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo if (result || be16_to_cpu(eh->data_len) < sizeof(*med)) 20793aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo return 0; 20893aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo 20993aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo if (eh->nea || eh->notification_class != 0x4) 21093aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo return 0; 21193aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo 21293aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo if (med->media_event_code == 1) 21393aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo return DISK_EVENT_EJECT_REQUEST; 21493aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo else if (med->media_event_code == 2) 21593aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo return DISK_EVENT_MEDIA_CHANGE; 21693aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo return 0; 21793aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo} 21893aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo 2191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* 22093aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo * This function checks to see if the media has been changed or eject 22193aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo * button has been pressed. It is possible that we have already 22293aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo * sensed a change, or the drive may have sensed one and not yet 22393aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo * reported it. The past events are accumulated in sdev->changed and 22493aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo * returned together with the current state. 2251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */ 22693aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heostatic unsigned int sr_check_events(struct cdrom_device_info *cdi, 22793aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo unsigned int clearing, int slot) 2281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{ 2291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds struct scsi_cd *cd = cdi->handle; 23093aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo bool last_present; 23193aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo struct scsi_sense_hdr sshdr; 23293aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo unsigned int events; 23393aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo int ret; 2341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 23593aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo /* no changer support */ 23693aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo if (CDSL_CURRENT != slot) 23793aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo return 0; 23893aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo 23993aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo events = sr_get_events(cd->device); 24079b9677d885d1a792bc103f2febb06f91f92de43Kay Sievers cd->get_event_changed |= events & DISK_EVENT_MEDIA_CHANGE; 24179b9677d885d1a792bc103f2febb06f91f92de43Kay Sievers 24279b9677d885d1a792bc103f2febb06f91f92de43Kay Sievers /* 24379b9677d885d1a792bc103f2febb06f91f92de43Kay Sievers * If earlier GET_EVENT_STATUS_NOTIFICATION and TUR did not agree 24479b9677d885d1a792bc103f2febb06f91f92de43Kay Sievers * for several times in a row. We rely on TUR only for this likely 24579b9677d885d1a792bc103f2febb06f91f92de43Kay Sievers * broken device, to prevent generating incorrect media changed 24679b9677d885d1a792bc103f2febb06f91f92de43Kay Sievers * events for every open(). 24779b9677d885d1a792bc103f2febb06f91f92de43Kay Sievers */ 24879b9677d885d1a792bc103f2febb06f91f92de43Kay Sievers if (cd->ignore_get_event) { 24979b9677d885d1a792bc103f2febb06f91f92de43Kay Sievers events &= ~DISK_EVENT_MEDIA_CHANGE; 25079b9677d885d1a792bc103f2febb06f91f92de43Kay Sievers goto do_tur; 25179b9677d885d1a792bc103f2febb06f91f92de43Kay Sievers } 25279b9677d885d1a792bc103f2febb06f91f92de43Kay Sievers 25393aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo /* 25493aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo * GET_EVENT_STATUS_NOTIFICATION is enough unless MEDIA_CHANGE 25593aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo * is being cleared. Note that there are devices which hang 25693aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo * if asked to execute TUR repeatedly. 25793aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo */ 25879b9677d885d1a792bc103f2febb06f91f92de43Kay Sievers if (cd->device->changed) { 25979b9677d885d1a792bc103f2febb06f91f92de43Kay Sievers events |= DISK_EVENT_MEDIA_CHANGE; 26079b9677d885d1a792bc103f2febb06f91f92de43Kay Sievers cd->device->changed = 0; 26179b9677d885d1a792bc103f2febb06f91f92de43Kay Sievers cd->tur_changed = true; 26279b9677d885d1a792bc103f2febb06f91f92de43Kay Sievers } 26393aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo 26479b9677d885d1a792bc103f2febb06f91f92de43Kay Sievers if (!(clearing & DISK_EVENT_MEDIA_CHANGE)) 26579b9677d885d1a792bc103f2febb06f91f92de43Kay Sievers return events; 26679b9677d885d1a792bc103f2febb06f91f92de43Kay Sieversdo_tur: 26793aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo /* let's see whether the media is there with TUR */ 26893aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo last_present = cd->media_present; 26993aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo ret = scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES, &sshdr); 2701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 271638428ece619495edc9579b1e21493eb00f9687cTejun Heo /* 272638428ece619495edc9579b1e21493eb00f9687cTejun Heo * Media is considered to be present if TUR succeeds or fails with 273638428ece619495edc9579b1e21493eb00f9687cTejun Heo * sense data indicating something other than media-not-present 274638428ece619495edc9579b1e21493eb00f9687cTejun Heo * (ASC 0x3a). 275638428ece619495edc9579b1e21493eb00f9687cTejun Heo */ 27693aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo cd->media_present = scsi_status_is_good(ret) || 27793aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo (scsi_sense_valid(&sshdr) && sshdr.asc != 0x3a); 2781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 27993aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo if (last_present != cd->media_present) 28079b9677d885d1a792bc103f2febb06f91f92de43Kay Sievers cd->device->changed = 1; 28179b9677d885d1a792bc103f2febb06f91f92de43Kay Sievers 28293aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo if (cd->device->changed) { 28393aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo events |= DISK_EVENT_MEDIA_CHANGE; 28493aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo cd->device->changed = 0; 28579b9677d885d1a792bc103f2febb06f91f92de43Kay Sievers cd->tur_changed = true; 28679b9677d885d1a792bc103f2febb06f91f92de43Kay Sievers } 28779b9677d885d1a792bc103f2febb06f91f92de43Kay Sievers 28879b9677d885d1a792bc103f2febb06f91f92de43Kay Sievers if (cd->ignore_get_event) 28979b9677d885d1a792bc103f2febb06f91f92de43Kay Sievers return events; 29079b9677d885d1a792bc103f2febb06f91f92de43Kay Sievers 29179b9677d885d1a792bc103f2febb06f91f92de43Kay Sievers /* check whether GET_EVENT is reporting spurious MEDIA_CHANGE */ 29279b9677d885d1a792bc103f2febb06f91f92de43Kay Sievers if (!cd->tur_changed) { 29379b9677d885d1a792bc103f2febb06f91f92de43Kay Sievers if (cd->get_event_changed) { 29479b9677d885d1a792bc103f2febb06f91f92de43Kay Sievers if (cd->tur_mismatch++ > 8) { 29596eefad2d9e5a0d988cdfee85193b6154c0ae1d2Hannes Reinecke sr_printk(KERN_WARNING, cd, 29696eefad2d9e5a0d988cdfee85193b6154c0ae1d2Hannes Reinecke "GET_EVENT and TUR disagree continuously, suppress GET_EVENT events\n"); 29779b9677d885d1a792bc103f2febb06f91f92de43Kay Sievers cd->ignore_get_event = true; 29879b9677d885d1a792bc103f2febb06f91f92de43Kay Sievers } 29979b9677d885d1a792bc103f2febb06f91f92de43Kay Sievers } else { 30079b9677d885d1a792bc103f2febb06f91f92de43Kay Sievers cd->tur_mismatch = 0; 30179b9677d885d1a792bc103f2febb06f91f92de43Kay Sievers } 3021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } 30379b9677d885d1a792bc103f2febb06f91f92de43Kay Sievers cd->tur_changed = false; 30479b9677d885d1a792bc103f2febb06f91f92de43Kay Sievers cd->get_event_changed = false; 305285e9670d91cdeb6b6693729950339cb45410fdcKay Sievers 30693aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo return events; 3071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} 30893aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo 3091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* 3107b3d9545f9ac8b31528dd2d6d8ec8d19922917b8Linus Torvalds * sr_done is the interrupt routine for the device driver. 3111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * 3127b3d9545f9ac8b31528dd2d6d8ec8d19922917b8Linus Torvalds * It will be notified on the end of a SCSI read / write, and will take one 3131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * of several actions based on success or failure. 3141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */ 3157b3d9545f9ac8b31528dd2d6d8ec8d19922917b8Linus Torvaldsstatic int sr_done(struct scsi_cmnd *SCpnt) 3161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{ 3171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds int result = SCpnt->result; 31830b0c37b27485a9cb897bfe3824f6f517b8c80d6Boaz Harrosh int this_count = scsi_bufflen(SCpnt); 3191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds int good_bytes = (result == 0 ? this_count : 0); 3201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds int block_sectors = 0; 3211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds long error_sector; 3221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds struct scsi_cd *cd = scsi_cd(SCpnt->request->rq_disk); 3231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 3241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#ifdef DEBUG 32596eefad2d9e5a0d988cdfee85193b6154c0ae1d2Hannes Reinecke scmd_printk(KERN_INFO, SCpnt, "done: %x\n", result); 3261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#endif 3271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 3281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* 3291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Handle MEDIUM ERRORs or VOLUME OVERFLOWs that indicate partial 3301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * success. Since this is a relatively rare error condition, no 3311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * care is taken to avoid unnecessary additional work such as 3321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * memcpy's that could be avoided. 3331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */ 3341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (driver_byte(result) != 0 && /* An error occurred */ 3351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds (SCpnt->sense_buffer[0] & 0x7f) == 0x70) { /* Sense current */ 3361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds switch (SCpnt->sense_buffer[2]) { 3371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds case MEDIUM_ERROR: 3381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds case VOLUME_OVERFLOW: 3391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds case ILLEGAL_REQUEST: 3401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (!(SCpnt->sense_buffer[0] & 0x90)) 3411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds break; 3421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds error_sector = (SCpnt->sense_buffer[3] << 24) | 3431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds (SCpnt->sense_buffer[4] << 16) | 3441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds (SCpnt->sense_buffer[5] << 8) | 3451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds SCpnt->sense_buffer[6]; 3461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (SCpnt->request->bio != NULL) 3471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds block_sectors = 3481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds bio_sectors(SCpnt->request->bio); 3491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (block_sectors < 4) 3501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds block_sectors = 4; 3511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (cd->device->sector_size == 2048) 3521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds error_sector <<= 2; 3531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds error_sector &= ~(block_sectors - 1); 35483096ebf1263b2c1ee5e653ba37d993d02e3eb7bTejun Heo good_bytes = (error_sector - 35583096ebf1263b2c1ee5e653ba37d993d02e3eb7bTejun Heo blk_rq_pos(SCpnt->request)) << 9; 3561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (good_bytes < 0 || good_bytes >= this_count) 3571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds good_bytes = 0; 3581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* 3591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * The SCSI specification allows for the value 3601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * returned by READ CAPACITY to be up to 75 2K 3611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * sectors past the last readable block. 3621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Therefore, if we hit a medium error within the 3631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * last 75 2K sectors, we decrease the saved size 3641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * value. 3651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */ 3661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (error_sector < get_capacity(cd->disk) && 3671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds cd->capacity - error_sector < 4 * 75) 3681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds set_capacity(cd->disk, error_sector); 3691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds break; 3701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 3711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds case RECOVERED_ERROR: 3721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds good_bytes = this_count; 3731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds break; 3741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 3751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds default: 3761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds break; 3771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } 3781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } 3791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 3807b3d9545f9ac8b31528dd2d6d8ec8d19922917b8Linus Torvalds return good_bytes; 3811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} 3821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 383a1b73fc194e73ed33c8b77bf09374cb05b58151bChristoph Hellwigstatic int sr_init_command(struct scsi_cmnd *SCpnt) 3841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{ 385242f9dcb8ba6f68fcd217a119a7648a4f69290e9Jens Axboe int block = 0, this_count, s_size; 3867f9a6bc4e9d59e7fcf03ed23f60cd81ca5d80b65James Bottomley struct scsi_cd *cd; 387a1b73fc194e73ed33c8b77bf09374cb05b58151bChristoph Hellwig struct request *rq = SCpnt->request; 3887f9a6bc4e9d59e7fcf03ed23f60cd81ca5d80b65James Bottomley int ret; 3897f9a6bc4e9d59e7fcf03ed23f60cd81ca5d80b65James Bottomley 3903868cf8ea70a57fc3f927872d8296f287ce4b96aChristoph Hellwig ret = scsi_init_io(SCpnt, GFP_ATOMIC); 3917f9a6bc4e9d59e7fcf03ed23f60cd81ca5d80b65James Bottomley if (ret != BLKPREP_OK) 3927f9a6bc4e9d59e7fcf03ed23f60cd81ca5d80b65James Bottomley goto out; 3937f9a6bc4e9d59e7fcf03ed23f60cd81ca5d80b65James Bottomley SCpnt = rq->special; 3947f9a6bc4e9d59e7fcf03ed23f60cd81ca5d80b65James Bottomley cd = scsi_cd(rq->rq_disk); 3957f9a6bc4e9d59e7fcf03ed23f60cd81ca5d80b65James Bottomley 3967f9a6bc4e9d59e7fcf03ed23f60cd81ca5d80b65James Bottomley /* from here on until we're complete, any goto out 3977f9a6bc4e9d59e7fcf03ed23f60cd81ca5d80b65James Bottomley * is used for a killable error condition */ 3987f9a6bc4e9d59e7fcf03ed23f60cd81ca5d80b65James Bottomley ret = BLKPREP_KILL; 3991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 40096eefad2d9e5a0d988cdfee85193b6154c0ae1d2Hannes Reinecke SCSI_LOG_HLQUEUE(1, scmd_printk(KERN_INFO, SCpnt, 40196eefad2d9e5a0d988cdfee85193b6154c0ae1d2Hannes Reinecke "Doing sr request, block = %d\n", block)); 4021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 4031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (!cd->device || !scsi_device_online(cd->device)) { 40496eefad2d9e5a0d988cdfee85193b6154c0ae1d2Hannes Reinecke SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt, 40596eefad2d9e5a0d988cdfee85193b6154c0ae1d2Hannes Reinecke "Finishing %u sectors\n", blk_rq_sectors(rq))); 40696eefad2d9e5a0d988cdfee85193b6154c0ae1d2Hannes Reinecke SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt, 40796eefad2d9e5a0d988cdfee85193b6154c0ae1d2Hannes Reinecke "Retry with 0x%p\n", SCpnt)); 4087f9a6bc4e9d59e7fcf03ed23f60cd81ca5d80b65James Bottomley goto out; 4091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } 4101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 4111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (cd->device->changed) { 4121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* 4131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * quietly refuse to do anything to a changed disc until the 4141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * changed bit has been reset 4151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */ 4167f9a6bc4e9d59e7fcf03ed23f60cd81ca5d80b65James Bottomley goto out; 4171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } 4181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 4191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* 4201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * we do lazy blocksize switching (when reading XA sectors, 4211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * see CDROMREADMODE2 ioctl) 4221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */ 4231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds s_size = cd->device->sector_size; 4241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (s_size > 2048) { 4251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (!in_interrupt()) 4261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds sr_set_blocklength(cd, 2048); 4271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds else 42896eefad2d9e5a0d988cdfee85193b6154c0ae1d2Hannes Reinecke scmd_printk(KERN_INFO, SCpnt, 42996eefad2d9e5a0d988cdfee85193b6154c0ae1d2Hannes Reinecke "can't switch blocksize: in interrupt\n"); 4301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } 4311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 4321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (s_size != 512 && s_size != 1024 && s_size != 2048) { 4333bf743e7c891d8be8295650b7a6a9b5af083b096Jeff Garzik scmd_printk(KERN_ERR, SCpnt, "bad sector size %d\n", s_size); 4347f9a6bc4e9d59e7fcf03ed23f60cd81ca5d80b65James Bottomley goto out; 4351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } 4361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 4377f9a6bc4e9d59e7fcf03ed23f60cd81ca5d80b65James Bottomley if (rq_data_dir(rq) == WRITE) { 438fd2eb9034e48cdca358dc06a833a736e7c6f68ddChristoph Hellwig if (!cd->writeable) 4397f9a6bc4e9d59e7fcf03ed23f60cd81ca5d80b65James Bottomley goto out; 4401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds SCpnt->cmnd[0] = WRITE_10; 44196eefad2d9e5a0d988cdfee85193b6154c0ae1d2Hannes Reinecke cd->cdi.media_written = 1; 4427f9a6bc4e9d59e7fcf03ed23f60cd81ca5d80b65James Bottomley } else if (rq_data_dir(rq) == READ) { 4431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds SCpnt->cmnd[0] = READ_10; 4441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } else { 4457f9a6bc4e9d59e7fcf03ed23f60cd81ca5d80b65James Bottomley blk_dump_rq_flags(rq, "Unknown sr command"); 4467f9a6bc4e9d59e7fcf03ed23f60cd81ca5d80b65James Bottomley goto out; 4471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } 4481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 4491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds { 45030b0c37b27485a9cb897bfe3824f6f517b8c80d6Boaz Harrosh struct scatterlist *sg; 45130b0c37b27485a9cb897bfe3824f6f517b8c80d6Boaz Harrosh int i, size = 0, sg_count = scsi_sg_count(SCpnt); 4521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 45330b0c37b27485a9cb897bfe3824f6f517b8c80d6Boaz Harrosh scsi_for_each_sg(SCpnt, sg, sg_count, i) 45430b0c37b27485a9cb897bfe3824f6f517b8c80d6Boaz Harrosh size += sg->length; 45530b0c37b27485a9cb897bfe3824f6f517b8c80d6Boaz Harrosh 45630b0c37b27485a9cb897bfe3824f6f517b8c80d6Boaz Harrosh if (size != scsi_bufflen(SCpnt)) { 4573bf743e7c891d8be8295650b7a6a9b5af083b096Jeff Garzik scmd_printk(KERN_ERR, SCpnt, 4583bf743e7c891d8be8295650b7a6a9b5af083b096Jeff Garzik "mismatch count %d, bytes %d\n", 45930b0c37b27485a9cb897bfe3824f6f517b8c80d6Boaz Harrosh size, scsi_bufflen(SCpnt)); 46030b0c37b27485a9cb897bfe3824f6f517b8c80d6Boaz Harrosh if (scsi_bufflen(SCpnt) > size) 46130b0c37b27485a9cb897bfe3824f6f517b8c80d6Boaz Harrosh SCpnt->sdb.length = size; 4621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } 4631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } 4641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 4651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* 4661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * request doesn't start on hw block boundary, add scatter pads 4671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */ 46883096ebf1263b2c1ee5e653ba37d993d02e3eb7bTejun Heo if (((unsigned int)blk_rq_pos(rq) % (s_size >> 9)) || 46930b0c37b27485a9cb897bfe3824f6f517b8c80d6Boaz Harrosh (scsi_bufflen(SCpnt) % s_size)) { 4703bf743e7c891d8be8295650b7a6a9b5af083b096Jeff Garzik scmd_printk(KERN_NOTICE, SCpnt, "unaligned transfer\n"); 4717f9a6bc4e9d59e7fcf03ed23f60cd81ca5d80b65James Bottomley goto out; 4721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } 4731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 47430b0c37b27485a9cb897bfe3824f6f517b8c80d6Boaz Harrosh this_count = (scsi_bufflen(SCpnt) >> 9) / (s_size >> 9); 4751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 4761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 47796eefad2d9e5a0d988cdfee85193b6154c0ae1d2Hannes Reinecke SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt, 47896eefad2d9e5a0d988cdfee85193b6154c0ae1d2Hannes Reinecke "%s %d/%u 512 byte blocks.\n", 47996eefad2d9e5a0d988cdfee85193b6154c0ae1d2Hannes Reinecke (rq_data_dir(rq) == WRITE) ? 4801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds "writing" : "reading", 48196eefad2d9e5a0d988cdfee85193b6154c0ae1d2Hannes Reinecke this_count, blk_rq_sectors(rq))); 4821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 4831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds SCpnt->cmnd[1] = 0; 48483096ebf1263b2c1ee5e653ba37d993d02e3eb7bTejun Heo block = (unsigned int)blk_rq_pos(rq) / (s_size >> 9); 4851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 4861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (this_count > 0xffff) { 4871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds this_count = 0xffff; 48830b0c37b27485a9cb897bfe3824f6f517b8c80d6Boaz Harrosh SCpnt->sdb.length = this_count * s_size; 4891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } 4901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 4911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff; 4921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff; 4931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff; 4941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds SCpnt->cmnd[5] = (unsigned char) block & 0xff; 4951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0; 4961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff; 4971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds SCpnt->cmnd[8] = (unsigned char) this_count & 0xff; 4981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 4991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* 5001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * We shouldn't disconnect in the middle of a sector, so with a dumb 5011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * host adapter, it's safe to assume that we can at least transfer 5021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * this many bytes between each connect / disconnect. 5031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */ 5041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds SCpnt->transfersize = cd->device->sector_size; 5051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds SCpnt->underflow = this_count << 9; 5061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds SCpnt->allowed = MAX_RETRIES; 5071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 5081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* 5091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * This indicates that the command is ready from our end to be 5101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * queued. 5111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */ 5127f9a6bc4e9d59e7fcf03ed23f60cd81ca5d80b65James Bottomley ret = BLKPREP_OK; 5137f9a6bc4e9d59e7fcf03ed23f60cd81ca5d80b65James Bottomley out: 514a1b73fc194e73ed33c8b77bf09374cb05b58151bChristoph Hellwig return ret; 5151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} 5161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 51740cc51be699e37b78102ce7b2196ec9c32fe1908Al Virostatic int sr_block_open(struct block_device *bdev, fmode_t mode) 5181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{ 5196e9624b8caec290d28b4c6d9ec75749df6372b87Arnd Bergmann struct scsi_cd *cd; 52040cc51be699e37b78102ce7b2196ec9c32fe1908Al Viro int ret = -ENXIO; 5211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 5222a48fc0ab24241755dc93bfd4f01d68efab47f5aArnd Bergmann mutex_lock(&sr_mutex); 5236e9624b8caec290d28b4c6d9ec75749df6372b87Arnd Bergmann cd = scsi_cd_get(bdev->bd_disk); 52440cc51be699e37b78102ce7b2196ec9c32fe1908Al Viro if (cd) { 52540cc51be699e37b78102ce7b2196ec9c32fe1908Al Viro ret = cdrom_open(&cd->cdi, bdev, mode); 52640cc51be699e37b78102ce7b2196ec9c32fe1908Al Viro if (ret) 52740cc51be699e37b78102ce7b2196ec9c32fe1908Al Viro scsi_cd_put(cd); 52840cc51be699e37b78102ce7b2196ec9c32fe1908Al Viro } 5292a48fc0ab24241755dc93bfd4f01d68efab47f5aArnd Bergmann mutex_unlock(&sr_mutex); 5301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return ret; 5311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} 5321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 533db2a144bedd58b3dcf19950c2f476c58c9f39d18Al Virostatic void sr_block_release(struct gendisk *disk, fmode_t mode) 5341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{ 53540cc51be699e37b78102ce7b2196ec9c32fe1908Al Viro struct scsi_cd *cd = scsi_cd(disk); 5362a48fc0ab24241755dc93bfd4f01d68efab47f5aArnd Bergmann mutex_lock(&sr_mutex); 53740cc51be699e37b78102ce7b2196ec9c32fe1908Al Viro cdrom_release(&cd->cdi, mode); 5381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds scsi_cd_put(cd); 5392a48fc0ab24241755dc93bfd4f01d68efab47f5aArnd Bergmann mutex_unlock(&sr_mutex); 5401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} 5411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 54240cc51be699e37b78102ce7b2196ec9c32fe1908Al Virostatic int sr_block_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd, 5431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds unsigned long arg) 5441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{ 54540cc51be699e37b78102ce7b2196ec9c32fe1908Al Viro struct scsi_cd *cd = scsi_cd(bdev->bd_disk); 5461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds struct scsi_device *sdev = cd->device; 5476a2900b67652421b51fe25e4b86ecfec742b1f30Christoph Hellwig void __user *argp = (void __user *)arg; 5486a2900b67652421b51fe25e4b86ecfec742b1f30Christoph Hellwig int ret; 5491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 5502a48fc0ab24241755dc93bfd4f01d68efab47f5aArnd Bergmann mutex_lock(&sr_mutex); 5518a6cfeb6deca3a8fefd639d898b0d163c0b5d368Arnd Bergmann 5526a2900b67652421b51fe25e4b86ecfec742b1f30Christoph Hellwig /* 5536a2900b67652421b51fe25e4b86ecfec742b1f30Christoph Hellwig * Send SCSI addressing ioctls directly to mid level, send other 5546a2900b67652421b51fe25e4b86ecfec742b1f30Christoph Hellwig * ioctls to cdrom/block level. 5556a2900b67652421b51fe25e4b86ecfec742b1f30Christoph Hellwig */ 5566a2900b67652421b51fe25e4b86ecfec742b1f30Christoph Hellwig switch (cmd) { 5576a2900b67652421b51fe25e4b86ecfec742b1f30Christoph Hellwig case SCSI_IOCTL_GET_IDLUN: 5586a2900b67652421b51fe25e4b86ecfec742b1f30Christoph Hellwig case SCSI_IOCTL_GET_BUS_NUMBER: 5598a6cfeb6deca3a8fefd639d898b0d163c0b5d368Arnd Bergmann ret = scsi_ioctl(sdev, cmd, argp); 5608a6cfeb6deca3a8fefd639d898b0d163c0b5d368Arnd Bergmann goto out; 5611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } 5626a2900b67652421b51fe25e4b86ecfec742b1f30Christoph Hellwig 56340cc51be699e37b78102ce7b2196ec9c32fe1908Al Viro ret = cdrom_ioctl(&cd->cdi, bdev, mode, cmd, arg); 5646397256b0af52082db3b7f804aa3b612a17dcbc9Tejun Heo if (ret != -ENOSYS) 5658a6cfeb6deca3a8fefd639d898b0d163c0b5d368Arnd Bergmann goto out; 5666a2900b67652421b51fe25e4b86ecfec742b1f30Christoph Hellwig 5676a2900b67652421b51fe25e4b86ecfec742b1f30Christoph Hellwig /* 5686a2900b67652421b51fe25e4b86ecfec742b1f30Christoph Hellwig * ENODEV means that we didn't recognise the ioctl, or that we 5696a2900b67652421b51fe25e4b86ecfec742b1f30Christoph Hellwig * cannot execute it in the current device state. In either 5706a2900b67652421b51fe25e4b86ecfec742b1f30Christoph Hellwig * case fall through to scsi_ioctl, which will return ENDOEV again 5716a2900b67652421b51fe25e4b86ecfec742b1f30Christoph Hellwig * if it doesn't recognise the ioctl 5726a2900b67652421b51fe25e4b86ecfec742b1f30Christoph Hellwig */ 57383ff6fe8580a7c834dba4389d742332fff9b9929Al Viro ret = scsi_nonblockable_ioctl(sdev, cmd, argp, 574fd4ce1acd0f8558033b1a6968001552bd7671e6dChristoph Hellwig (mode & FMODE_NDELAY) != 0); 5756a2900b67652421b51fe25e4b86ecfec742b1f30Christoph Hellwig if (ret != -ENODEV) 5768a6cfeb6deca3a8fefd639d898b0d163c0b5d368Arnd Bergmann goto out; 5778a6cfeb6deca3a8fefd639d898b0d163c0b5d368Arnd Bergmann ret = scsi_ioctl(sdev, cmd, argp); 5788a6cfeb6deca3a8fefd639d898b0d163c0b5d368Arnd Bergmann 5798a6cfeb6deca3a8fefd639d898b0d163c0b5d368Arnd Bergmannout: 5802a48fc0ab24241755dc93bfd4f01d68efab47f5aArnd Bergmann mutex_unlock(&sr_mutex); 5818a6cfeb6deca3a8fefd639d898b0d163c0b5d368Arnd Bergmann return ret; 5821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} 5831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 58493aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heostatic unsigned int sr_block_check_events(struct gendisk *disk, 58593aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo unsigned int clearing) 5861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{ 5871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds struct scsi_cd *cd = scsi_cd(disk); 5886c7f1e2f12f6f37ddda01180c9e5c407eccce616Aaron Lu 5896627b38fd902cd31c6ad52f9be12a17a38f0faefAaron Lu if (atomic_read(&cd->device->disk_events_disable_depth)) 5906627b38fd902cd31c6ad52f9be12a17a38f0faefAaron Lu return 0; 5916c7f1e2f12f6f37ddda01180c9e5c407eccce616Aaron Lu 5926627b38fd902cd31c6ad52f9be12a17a38f0faefAaron Lu return cdrom_check_events(&cd->cdi, clearing); 59393aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo} 59493aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo 59593aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heostatic int sr_block_revalidate_disk(struct gendisk *disk) 59693aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo{ 59793aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo struct scsi_cd *cd = scsi_cd(disk); 59893aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo struct scsi_sense_hdr sshdr; 59993aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo 60093aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo /* if the unit is not ready, nothing more to do */ 60193aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo if (scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES, &sshdr)) 6026c7f1e2f12f6f37ddda01180c9e5c407eccce616Aaron Lu goto out; 60393aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo 60493aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo sr_cd_check(&cd->cdi); 60593aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo get_sectorsize(cd); 6066c7f1e2f12f6f37ddda01180c9e5c407eccce616Aaron Luout: 60793aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo return 0; 6081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} 6091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 61083d5cde47dedf01b6a4a4331882cbc0a7eea3c2eAlexey Dobriyanstatic const struct block_device_operations sr_bdops = 6111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{ 6121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds .owner = THIS_MODULE, 61340cc51be699e37b78102ce7b2196ec9c32fe1908Al Viro .open = sr_block_open, 61440cc51be699e37b78102ce7b2196ec9c32fe1908Al Viro .release = sr_block_release, 6158a6cfeb6deca3a8fefd639d898b0d163c0b5d368Arnd Bergmann .ioctl = sr_block_ioctl, 61693aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo .check_events = sr_block_check_events, 61793aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo .revalidate_disk = sr_block_revalidate_disk, 6181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* 6191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * No compat_ioctl for now because sr_block_ioctl never 62025985edcedea6396277003854657b5f3cb31a628Lucas De Marchi * seems to pass arbitrary ioctls down to host drivers. 6211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */ 6221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}; 6231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 6241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int sr_open(struct cdrom_device_info *cdi, int purpose) 6251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{ 6261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds struct scsi_cd *cd = cdi->handle; 6271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds struct scsi_device *sdev = cd->device; 6281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds int retval; 6291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 6301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* 6311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * If the device is in error recovery, wait until it is done. 6321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * If the device is offline, then disallow any access to it. 6331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */ 6341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds retval = -ENXIO; 6351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (!scsi_block_when_processing_errors(sdev)) 6361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds goto error_out; 6371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 6381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return 0; 6391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 6401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldserror_out: 6411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return retval; 6421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} 6431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 6441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void sr_release(struct cdrom_device_info *cdi) 6451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{ 6461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds struct scsi_cd *cd = cdi->handle; 6471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 6481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (cd->device->sector_size > 2048) 6491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds sr_set_blocklength(cd, 2048); 6501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 6511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} 6521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 6531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int sr_probe(struct device *dev) 6541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{ 6551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds struct scsi_device *sdev = to_scsi_device(dev); 6561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds struct gendisk *disk; 6571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds struct scsi_cd *cd; 6581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds int minor, error; 6591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 6606fe8c1dbefd63ef3988edb745d9eb81fc6d0513cSubhash Jadavani scsi_autopm_get_device(sdev); 6611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds error = -ENODEV; 6621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (sdev->type != TYPE_ROM && sdev->type != TYPE_WORM) 6631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds goto fail; 6641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 6651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds error = -ENOMEM; 66624669f75a3231fa37444977c92d1f4838bec1233Jes Sorensen cd = kzalloc(sizeof(*cd), GFP_KERNEL); 6671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (!cd) 6681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds goto fail; 6691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 6701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds kref_init(&cd->kref); 6711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 6721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds disk = alloc_disk(1); 6731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (!disk) 6741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds goto fail_free; 6751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 6761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds spin_lock(&sr_index_lock); 6771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds minor = find_first_zero_bit(sr_index_bits, SR_DISKS); 6781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (minor == SR_DISKS) { 6791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds spin_unlock(&sr_index_lock); 6801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds error = -EBUSY; 6811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds goto fail_put; 6821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } 6831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds __set_bit(minor, sr_index_bits); 6841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds spin_unlock(&sr_index_lock); 6851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 6861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds disk->major = SCSI_CDROM_MAJOR; 6871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds disk->first_minor = minor; 6881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds sprintf(disk->disk_name, "sr%d", minor); 6891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds disk->fops = &sr_bdops; 690d4dc210f69bcb0b4bef5a83b1c323817be89bad1Tejun Heo disk->flags = GENHD_FL_CD | GENHD_FL_BLOCK_EVENTS_ON_EXCL_WRITE; 69193aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo disk->events = DISK_EVENT_MEDIA_CHANGE | DISK_EVENT_EJECT_REQUEST; 6921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 693242f9dcb8ba6f68fcd217a119a7648a4f69290e9Jens Axboe blk_queue_rq_timeout(sdev->request_queue, SR_TIMEOUT); 694242f9dcb8ba6f68fcd217a119a7648a4f69290e9Jens Axboe 6951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds cd->device = sdev; 6961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds cd->disk = disk; 6971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds cd->driver = &sr_template; 6981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds cd->disk = disk; 6991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds cd->capacity = 0x1fffff; 7001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds cd->device->changed = 1; /* force recheck CD type */ 70193aae17af1172c40c6f74b7294e93a90c3cfaa5dTejun Heo cd->media_present = 1; 7021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds cd->use = 1; 7031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds cd->readcd_known = 0; 7041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds cd->readcd_cdda = 0; 7051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 7061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds cd->cdi.ops = &sr_dops; 7071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds cd->cdi.handle = cd; 7081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds cd->cdi.mask = 0; 7091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds cd->cdi.capacity = 1; 7101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds sprintf(cd->cdi.name, "sr%d", minor); 7111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 7121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds sdev->sector_size = 2048; /* A guess, just in case */ 7131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 7141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* FIXME: need to handle a get_capabilities failure properly ?? */ 7151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds get_capabilities(cd); 7161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds sr_vendor_init(cd); 7171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 7181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds disk->driverfs_dev = &sdev->sdev_gendev; 7191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds set_capacity(disk, cd->capacity); 7201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds disk->private_data = &cd->driver; 7211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds disk->queue = sdev->request_queue; 7221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds cd->cdi.disk = disk; 7231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 7241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (register_cdrom(&cd->cdi)) 7251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds goto fail_put; 7261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 7276627b38fd902cd31c6ad52f9be12a17a38f0faefAaron Lu /* 7286627b38fd902cd31c6ad52f9be12a17a38f0faefAaron Lu * Initialize block layer runtime PM stuffs before the 7296627b38fd902cd31c6ad52f9be12a17a38f0faefAaron Lu * periodic event checking request gets started in add_disk. 7306627b38fd902cd31c6ad52f9be12a17a38f0faefAaron Lu */ 7316627b38fd902cd31c6ad52f9be12a17a38f0faefAaron Lu blk_pm_runtime_init(sdev->request_queue, dev); 7326627b38fd902cd31c6ad52f9be12a17a38f0faefAaron Lu 7331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds dev_set_drvdata(dev, cd); 7341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds disk->flags |= GENHD_FL_REMOVABLE; 7351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds add_disk(disk); 7361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 7379ccfc756a70d454dfa82f48897e2883560c01a0eJames Bottomley sdev_printk(KERN_DEBUG, sdev, 7389ccfc756a70d454dfa82f48897e2883560c01a0eJames Bottomley "Attached scsi CD-ROM %s\n", cd->cdi.name); 7396c7f1e2f12f6f37ddda01180c9e5c407eccce616Aaron Lu scsi_autopm_put_device(cd->device); 7406c7f1e2f12f6f37ddda01180c9e5c407eccce616Aaron Lu 7411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return 0; 7421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 7431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfail_put: 7441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds put_disk(disk); 7451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfail_free: 7461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds kfree(cd); 7471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfail: 7486fe8c1dbefd63ef3988edb745d9eb81fc6d0513cSubhash Jadavani scsi_autopm_put_device(sdev); 7491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return error; 7501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} 7511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 7521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 7531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void get_sectorsize(struct scsi_cd *cd) 7541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{ 7551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds unsigned char cmd[10]; 75662858dacc8dea55c5bdb474ccd8acb0657e23dd0FUJITA Tomonori unsigned char buffer[8]; 7571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds int the_result, retries = 3; 7581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds int sector_size; 759165125e1e480f9510a5ffcfbfee4e3ee38c05f23Jens Axboe struct request_queue *queue; 7601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 7611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds do { 7621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds cmd[0] = READ_CAPACITY; 7631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds memset((void *) &cmd[1], 0, 9); 76462858dacc8dea55c5bdb474ccd8acb0657e23dd0FUJITA Tomonori memset(buffer, 0, sizeof(buffer)); 7651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 7661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* Do the command and wait.. */ 767820732b501a5bbdd3bde1263f391891e21b5ed8cJames Bottomley the_result = scsi_execute_req(cd->device, cmd, DMA_FROM_DEVICE, 76862858dacc8dea55c5bdb474ccd8acb0657e23dd0FUJITA Tomonori buffer, sizeof(buffer), NULL, 769f4f4e47e4af6b02dd1c425b931c65d0165356e33FUJITA Tomonori SR_TIMEOUT, MAX_RETRIES, NULL); 7701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 7711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds retries--; 7721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 7731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } while (the_result && retries); 7741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 7751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 7761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (the_result) { 7771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds cd->capacity = 0x1fffff; 7781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds sector_size = 2048; /* A guess, just in case */ 7791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } else { 7805915136d4d3954867cced8a2297bddd16caf36a1Tejun Heo long last_written; 7815915136d4d3954867cced8a2297bddd16caf36a1Tejun Heo 7825915136d4d3954867cced8a2297bddd16caf36a1Tejun Heo cd->capacity = 1 + ((buffer[0] << 24) | (buffer[1] << 16) | 7835915136d4d3954867cced8a2297bddd16caf36a1Tejun Heo (buffer[2] << 8) | buffer[3]); 7845915136d4d3954867cced8a2297bddd16caf36a1Tejun Heo /* 7855915136d4d3954867cced8a2297bddd16caf36a1Tejun Heo * READ_CAPACITY doesn't return the correct size on 7865915136d4d3954867cced8a2297bddd16caf36a1Tejun Heo * certain UDF media. If last_written is larger, use 7875915136d4d3954867cced8a2297bddd16caf36a1Tejun Heo * it instead. 7885915136d4d3954867cced8a2297bddd16caf36a1Tejun Heo * 7895915136d4d3954867cced8a2297bddd16caf36a1Tejun Heo * http://bugzilla.kernel.org/show_bug.cgi?id=9668 7905915136d4d3954867cced8a2297bddd16caf36a1Tejun Heo */ 7915915136d4d3954867cced8a2297bddd16caf36a1Tejun Heo if (!cdrom_get_last_written(&cd->cdi, &last_written)) 7925915136d4d3954867cced8a2297bddd16caf36a1Tejun Heo cd->capacity = max_t(long, cd->capacity, last_written); 7935915136d4d3954867cced8a2297bddd16caf36a1Tejun Heo 7941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds sector_size = (buffer[4] << 24) | 7951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds (buffer[5] << 16) | (buffer[6] << 8) | buffer[7]; 7961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds switch (sector_size) { 7971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* 7981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * HP 4020i CD-Recorder reports 2340 byte sectors 7991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Philips CD-Writers report 2352 byte sectors 8001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * 8011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Use 2k sectors for them.. 8021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */ 8031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds case 0: 8041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds case 2340: 8051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds case 2352: 8061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds sector_size = 2048; 8071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* fall through */ 8081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds case 2048: 8091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds cd->capacity *= 4; 8101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* fall through */ 8111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds case 512: 8121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds break; 8131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds default: 81496eefad2d9e5a0d988cdfee85193b6154c0ae1d2Hannes Reinecke sr_printk(KERN_INFO, cd, 81596eefad2d9e5a0d988cdfee85193b6154c0ae1d2Hannes Reinecke "unsupported sector size %d.", sector_size); 8161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds cd->capacity = 0; 8171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } 8181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 8191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds cd->device->sector_size = sector_size; 8201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 8211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* 8221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Add this so that we have the ability to correctly gauge 8231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * what the device is capable of. 8241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */ 8251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds set_capacity(cd->disk, cd->capacity); 8261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } 8271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 8281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds queue = cd->device->request_queue; 829e1defc4ff0cf57aca6c5e3ff99fa503f5943c1f1Martin K. Petersen blk_queue_logical_block_size(queue, sector_size); 8301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 83162858dacc8dea55c5bdb474ccd8acb0657e23dd0FUJITA Tomonori return; 8321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} 8331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 8341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void get_capabilities(struct scsi_cd *cd) 8351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{ 8361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds unsigned char *buffer; 8371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds struct scsi_mode_data data; 838820732b501a5bbdd3bde1263f391891e21b5ed8cJames Bottomley struct scsi_sense_hdr sshdr; 83938582a62ecd337de4212004c7d4844899dc57890James Bottomley int rc, n; 8401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 8410ad78200baf1f85a21e6b26c225717ad80980d8fArjan van de Ven static const char *loadmech[] = 8421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds { 8431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds "caddy", 8441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds "tray", 8451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds "pop-up", 8461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds "", 8471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds "changer", 8481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds "cartridge changer", 8491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds "", 8501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds "" 8511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds }; 8521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 8531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 8541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* allocate transfer buffer */ 8551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds buffer = kmalloc(512, GFP_KERNEL | GFP_DMA); 8561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (!buffer) { 85796eefad2d9e5a0d988cdfee85193b6154c0ae1d2Hannes Reinecke sr_printk(KERN_ERR, cd, "out of memory.\n"); 8581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return; 8591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } 8601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 86138582a62ecd337de4212004c7d4844899dc57890James Bottomley /* eat unit attentions */ 8629f8a2c23c6c1140f515f601265c4dff7522110b7Tejun Heo scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES, &sshdr); 8631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 8641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* ask for mode page 0x2a */ 8651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds rc = scsi_mode_sense(cd->device, 0, 0x2a, buffer, 128, 8661cf72699c1530c3e4ac3d58344f6a6a40a2f46d3James Bottomley SR_TIMEOUT, 3, &data, NULL); 8671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 8681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (!scsi_status_is_good(rc)) { 8691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* failed, drive doesn't have capabilities mode page */ 8701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds cd->cdi.speed = 1; 8711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds cd->cdi.mask |= (CDC_CD_R | CDC_CD_RW | CDC_DVD_R | 872bcc1e382bce8de4f26182c6447916f9c82b0fbe5Chuck Ebbert CDC_DVD | CDC_DVD_RAM | 873bcc1e382bce8de4f26182c6447916f9c82b0fbe5Chuck Ebbert CDC_SELECT_DISC | CDC_SELECT_SPEED | 874bcc1e382bce8de4f26182c6447916f9c82b0fbe5Chuck Ebbert CDC_MRW | CDC_MRW_W | CDC_RAM); 8751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds kfree(buffer); 87696eefad2d9e5a0d988cdfee85193b6154c0ae1d2Hannes Reinecke sr_printk(KERN_INFO, cd, "scsi-1 drive"); 8771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return; 8781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } 8791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 8801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds n = data.header_length + data.block_descriptor_length; 8811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds cd->cdi.speed = ((buffer[n + 8] << 8) + buffer[n + 9]) / 176; 8821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds cd->readcd_known = 1; 8831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds cd->readcd_cdda = buffer[n + 5] & 0x01; 8841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* print some capability bits */ 88596eefad2d9e5a0d988cdfee85193b6154c0ae1d2Hannes Reinecke sr_printk(KERN_INFO, cd, 88696eefad2d9e5a0d988cdfee85193b6154c0ae1d2Hannes Reinecke "scsi3-mmc drive: %dx/%dx %s%s%s%s%s%s\n", 88796eefad2d9e5a0d988cdfee85193b6154c0ae1d2Hannes Reinecke ((buffer[n + 14] << 8) + buffer[n + 15]) / 176, 88896eefad2d9e5a0d988cdfee85193b6154c0ae1d2Hannes Reinecke cd->cdi.speed, 88996eefad2d9e5a0d988cdfee85193b6154c0ae1d2Hannes Reinecke buffer[n + 3] & 0x01 ? "writer " : "", /* CD Writer */ 89096eefad2d9e5a0d988cdfee85193b6154c0ae1d2Hannes Reinecke buffer[n + 3] & 0x20 ? "dvd-ram " : "", 89196eefad2d9e5a0d988cdfee85193b6154c0ae1d2Hannes Reinecke buffer[n + 2] & 0x02 ? "cd/rw " : "", /* can read rewriteable */ 89296eefad2d9e5a0d988cdfee85193b6154c0ae1d2Hannes Reinecke buffer[n + 4] & 0x20 ? "xa/form2 " : "", /* can read xa/from2 */ 89396eefad2d9e5a0d988cdfee85193b6154c0ae1d2Hannes Reinecke buffer[n + 5] & 0x01 ? "cdda " : "", /* can read audio data */ 89496eefad2d9e5a0d988cdfee85193b6154c0ae1d2Hannes Reinecke loadmech[buffer[n + 6] >> 5]); 8951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if ((buffer[n + 6] >> 5) == 0) 8961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* caddy drives can't close tray... */ 8971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds cd->cdi.mask |= CDC_CLOSE_TRAY; 8981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if ((buffer[n + 2] & 0x8) == 0) 8991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* not a DVD drive */ 9001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds cd->cdi.mask |= CDC_DVD; 90196eefad2d9e5a0d988cdfee85193b6154c0ae1d2Hannes Reinecke if ((buffer[n + 3] & 0x20) == 0) 9021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* can't write DVD-RAM media */ 9031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds cd->cdi.mask |= CDC_DVD_RAM; 9041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if ((buffer[n + 3] & 0x10) == 0) 9051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* can't write DVD-R media */ 9061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds cd->cdi.mask |= CDC_DVD_R; 9071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if ((buffer[n + 3] & 0x2) == 0) 9081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* can't write CD-RW media */ 9091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds cd->cdi.mask |= CDC_CD_RW; 9101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if ((buffer[n + 3] & 0x1) == 0) 9111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* can't write CD-R media */ 9121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds cd->cdi.mask |= CDC_CD_R; 9131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if ((buffer[n + 6] & 0x8) == 0) 9141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* can't eject */ 9151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds cd->cdi.mask |= CDC_OPEN_TRAY; 9161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 9171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if ((buffer[n + 6] >> 5) == mechtype_individual_changer || 9181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds (buffer[n + 6] >> 5) == mechtype_cartridge_changer) 9191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds cd->cdi.capacity = 9201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds cdrom_number_of_slots(&cd->cdi); 9211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (cd->cdi.capacity <= 1) 9221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* not a changer */ 9231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds cd->cdi.mask |= CDC_SELECT_DISC; 9241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /*else I don't think it can close its tray 9251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds cd->cdi.mask |= CDC_CLOSE_TRAY; */ 9261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 9271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds /* 9281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * if DVD-RAM, MRW-W or CD-RW, we are randomly writable 9291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */ 9301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if ((cd->cdi.mask & (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) != 9311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) { 932fd2eb9034e48cdca358dc06a833a736e7c6f68ddChristoph Hellwig cd->writeable = 1; 9331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds } 9341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 9351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds kfree(buffer); 9361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} 9371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 9381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* 9391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * sr_packet() is the entry point for the generic commands generated 94096eefad2d9e5a0d988cdfee85193b6154c0ae1d2Hannes Reinecke * by the Uniform CD-ROM layer. 9411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */ 9421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int sr_packet(struct cdrom_device_info *cdi, 9431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds struct packet_command *cgc) 9441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{ 9458e04d8056c1ea0e0aab730994b74756f0526cda8Hans de Goede struct scsi_cd *cd = cdi->handle; 9468e04d8056c1ea0e0aab730994b74756f0526cda8Hans de Goede struct scsi_device *sdev = cd->device; 9478e04d8056c1ea0e0aab730994b74756f0526cda8Hans de Goede 9488e04d8056c1ea0e0aab730994b74756f0526cda8Hans de Goede if (cgc->cmd[0] == GPCMD_READ_DISC_INFO && sdev->no_read_disc_info) 9498e04d8056c1ea0e0aab730994b74756f0526cda8Hans de Goede return -EDRIVE_CANT_DO_THIS; 9508e04d8056c1ea0e0aab730994b74756f0526cda8Hans de Goede 9511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (cgc->timeout <= 0) 9521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds cgc->timeout = IOCTL_TIMEOUT; 9531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 9548e04d8056c1ea0e0aab730994b74756f0526cda8Hans de Goede sr_do_ioctl(cd, cgc); 9551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 9561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return cgc->stat; 9571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} 9581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 9591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/** 9601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * sr_kref_release - Called to free the scsi_cd structure 9611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * @kref: pointer to embedded kref 9621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * 9630b9506723826c68b50fa33e345700ddcac1bed36Arjan van de Ven * sr_ref_mutex must be held entering this routine. Because it is 9641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * called on last put, you should always use the scsi_cd_get() 9651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * scsi_cd_put() helpers which manipulate the semaphore directly 9661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * and never do a direct kref_put(). 9671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds **/ 9681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void sr_kref_release(struct kref *kref) 9691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{ 9701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds struct scsi_cd *cd = container_of(kref, struct scsi_cd, kref); 9711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds struct gendisk *disk = cd->disk; 9721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 9731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds spin_lock(&sr_index_lock); 974f331c0296f2a9fee0d396a70598b954062603015Tejun Heo clear_bit(MINOR(disk_devt(disk)), sr_index_bits); 9751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds spin_unlock(&sr_index_lock); 9761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 9771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds unregister_cdrom(&cd->cdi); 9781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 9791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds disk->private_data = NULL; 9801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 9811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds put_disk(disk); 9821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 9831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds kfree(cd); 9841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} 9851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 9861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int sr_remove(struct device *dev) 9871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{ 9881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds struct scsi_cd *cd = dev_get_drvdata(dev); 9891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 9906c7f1e2f12f6f37ddda01180c9e5c407eccce616Aaron Lu scsi_autopm_get_device(cd->device); 9916c7f1e2f12f6f37ddda01180c9e5c407eccce616Aaron Lu 9921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds del_gendisk(cd->disk); 9931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 9940b9506723826c68b50fa33e345700ddcac1bed36Arjan van de Ven mutex_lock(&sr_ref_mutex); 9951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds kref_put(&cd->kref, sr_kref_release); 9960b9506723826c68b50fa33e345700ddcac1bed36Arjan van de Ven mutex_unlock(&sr_ref_mutex); 9971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 9981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return 0; 9991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} 10001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 10011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int __init init_sr(void) 10021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{ 10031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds int rc; 10041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 10051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds rc = register_blkdev(SCSI_CDROM_MAJOR, "sr"); 10061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds if (rc) 10071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds return rc; 1008da3962fe63eae4f490356cb54e4700eac752541bAkinobu Mita rc = scsi_register_driver(&sr_template.gendrv); 1009da3962fe63eae4f490356cb54e4700eac752541bAkinobu Mita if (rc) 1010da3962fe63eae4f490356cb54e4700eac752541bAkinobu Mita unregister_blkdev(SCSI_CDROM_MAJOR, "sr"); 1011da3962fe63eae4f490356cb54e4700eac752541bAkinobu Mita 1012da3962fe63eae4f490356cb54e4700eac752541bAkinobu Mita return rc; 10131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} 10141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 10151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void __exit exit_sr(void) 10161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{ 10171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds scsi_unregister_driver(&sr_template.gendrv); 10181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds unregister_blkdev(SCSI_CDROM_MAJOR, "sr"); 10191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} 10201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 10211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsmodule_init(init_sr); 10221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsmodule_exit(exit_sr); 10231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsMODULE_LICENSE("GPL"); 1024