blob: c3f443d5aea89ca8fa76900eb8b1eb913d076cab [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#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>
Bart Van Asschebca6b062018-09-26 14:01:03 -070046#include <linux/blk-pm.h>
Arjan van de Ven0b950672006-01-11 13:16:10 +010047#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090048#include <linux/slab.h>
Aaron Lu6c7f1e22013-01-23 15:09:31 +080049#include <linux/pm_runtime.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080050#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52#include <scsi/scsi.h>
53#include <scsi/scsi_dbg.h>
54#include <scsi/scsi_device.h>
55#include <scsi/scsi_driver.h>
James Bottomley820732b2005-06-12 22:21:29 -050056#include <scsi/scsi_cmnd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <scsi/scsi_eh.h>
58#include <scsi/scsi_host.h>
59#include <scsi/scsi_ioctl.h> /* For the door lock/unlock commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61#include "scsi_logging.h"
62#include "sr.h"
63
64
Rene Hermanf018fa52006-03-08 00:14:20 -080065MODULE_DESCRIPTION("SCSI cdrom (sr) driver");
66MODULE_LICENSE("GPL");
67MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_CDROM_MAJOR);
Michael Tokarevd7b8bcb02006-10-27 16:02:37 +040068MODULE_ALIAS_SCSI_DEVICE(TYPE_ROM);
69MODULE_ALIAS_SCSI_DEVICE(TYPE_WORM);
Rene Hermanf018fa52006-03-08 00:14:20 -080070
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define SR_DISKS 256
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#define SR_CAPABILITIES \
74 (CDC_CLOSE_TRAY|CDC_OPEN_TRAY|CDC_LOCK|CDC_SELECT_SPEED| \
75 CDC_SELECT_DISC|CDC_MULTI_SESSION|CDC_MCN|CDC_MEDIA_CHANGED| \
Christoph Hellwig6a2900b2006-03-23 03:00:15 -080076 CDC_PLAY_AUDIO|CDC_RESET|CDC_DRIVE_STATUS| \
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 CDC_CD_R|CDC_CD_RW|CDC_DVD|CDC_DVD_R|CDC_DVD_RAM|CDC_GENERIC_PACKET| \
78 CDC_MRW|CDC_MRW_W|CDC_RAM)
79
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020080static DEFINE_MUTEX(sr_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081static int sr_probe(struct device *);
82static int sr_remove(struct device *);
Christoph Hellwig159b2cb2018-11-09 14:42:39 +010083static blk_status_t sr_init_command(struct scsi_cmnd *SCpnt);
Linus Torvalds7b3d9542008-01-06 10:17:12 -080084static int sr_done(struct scsi_cmnd *);
Aaron Lu6c7f1e22013-01-23 15:09:31 +080085static int sr_runtime_suspend(struct device *dev);
86
Julia Lawalla816b4c2016-08-28 22:17:38 +020087static const struct dev_pm_ops sr_pm_ops = {
Aaron Lu6c7f1e22013-01-23 15:09:31 +080088 .runtime_suspend = sr_runtime_suspend,
89};
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91static struct scsi_driver sr_template = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 .gendrv = {
93 .name = "sr",
Christoph Hellwig3af6b352014-11-12 18:34:51 +010094 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 .probe = sr_probe,
96 .remove = sr_remove,
Aaron Lu6c7f1e22013-01-23 15:09:31 +080097 .pm = &sr_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 },
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +020099 .init_command = sr_init_command,
Linus Torvalds7b3d9542008-01-06 10:17:12 -0800100 .done = sr_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101};
102
103static unsigned long sr_index_bits[SR_DISKS / BITS_PER_LONG];
104static DEFINE_SPINLOCK(sr_index_lock);
105
106/* This semaphore is used to mediate the 0->1 reference get in the
107 * face of object destruction (i.e. we can't allow a get on an
108 * object after last put) */
Arjan van de Ven0b950672006-01-11 13:16:10 +0100109static DEFINE_MUTEX(sr_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111static int sr_open(struct cdrom_device_info *, int);
112static void sr_release(struct cdrom_device_info *);
113
114static void get_sectorsize(struct scsi_cd *);
115static void get_capabilities(struct scsi_cd *);
116
Tejun Heo93aae172010-12-16 17:52:17 +0100117static unsigned int sr_check_events(struct cdrom_device_info *cdi,
118 unsigned int clearing, int slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119static int sr_packet(struct cdrom_device_info *, struct packet_command *);
120
Kees Cook853fe1b2017-02-13 16:25:26 -0800121static const struct cdrom_device_ops sr_dops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 .open = sr_open,
123 .release = sr_release,
124 .drive_status = sr_drive_status,
Tejun Heo93aae172010-12-16 17:52:17 +0100125 .check_events = sr_check_events,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 .tray_move = sr_tray_move,
127 .lock_door = sr_lock_door,
128 .select_speed = sr_select_speed,
129 .get_last_session = sr_get_last_session,
130 .get_mcn = sr_get_mcn,
131 .reset = sr_reset,
132 .audio_ioctl = sr_audio_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 .capability = SR_CAPABILITIES,
134 .generic_packet = sr_packet,
135};
136
137static void sr_kref_release(struct kref *kref);
138
139static inline struct scsi_cd *scsi_cd(struct gendisk *disk)
140{
141 return container_of(disk->private_data, struct scsi_cd, driver);
142}
143
Aaron Lu6c7f1e22013-01-23 15:09:31 +0800144static int sr_runtime_suspend(struct device *dev)
145{
146 struct scsi_cd *cd = dev_get_drvdata(dev);
147
Alan Stern13b43892016-01-20 11:26:01 -0500148 if (!cd) /* E.g.: runtime suspend following sr_remove() */
149 return 0;
150
Aaron Lu6c7f1e22013-01-23 15:09:31 +0800151 if (cd->media_present)
152 return -EBUSY;
153 else
154 return 0;
155}
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157/*
158 * The get and put routines for the struct scsi_cd. Note this entity
159 * has a scsi_device pointer and owns a reference to this.
160 */
161static inline struct scsi_cd *scsi_cd_get(struct gendisk *disk)
162{
163 struct scsi_cd *cd = NULL;
164
Arjan van de Ven0b950672006-01-11 13:16:10 +0100165 mutex_lock(&sr_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 if (disk->private_data == NULL)
167 goto out;
168 cd = scsi_cd(disk);
169 kref_get(&cd->kref);
Aaron Lu6627b382013-10-28 15:27:49 +0800170 if (scsi_device_get(cd->device)) {
171 kref_put(&cd->kref, sr_kref_release);
172 cd = NULL;
173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 out:
Arjan van de Ven0b950672006-01-11 13:16:10 +0100175 mutex_unlock(&sr_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 return cd;
177}
178
Arjan van de Ven858119e2006-01-14 13:20:43 -0800179static void scsi_cd_put(struct scsi_cd *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
181 struct scsi_device *sdev = cd->device;
182
Arjan van de Ven0b950672006-01-11 13:16:10 +0100183 mutex_lock(&sr_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 kref_put(&cd->kref, sr_kref_release);
185 scsi_device_put(sdev);
Arjan van de Ven0b950672006-01-11 13:16:10 +0100186 mutex_unlock(&sr_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187}
188
Tejun Heo93aae172010-12-16 17:52:17 +0100189static unsigned int sr_get_events(struct scsi_device *sdev)
190{
191 u8 buf[8];
192 u8 cmd[] = { GET_EVENT_STATUS_NOTIFICATION,
193 1, /* polled */
194 0, 0, /* reserved */
195 1 << 4, /* notification class: media */
196 0, 0, /* reserved */
197 0, sizeof(buf), /* allocation length */
198 0, /* control */
199 };
200 struct event_header *eh = (void *)buf;
201 struct media_event_desc *med = (void *)(buf + 4);
202 struct scsi_sense_hdr sshdr;
203 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Tejun Heo93aae172010-12-16 17:52:17 +0100205 result = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buf, sizeof(buf),
206 &sshdr, SR_TIMEOUT, MAX_RETRIES, NULL);
207 if (scsi_sense_valid(&sshdr) && sshdr.sense_key == UNIT_ATTENTION)
208 return DISK_EVENT_MEDIA_CHANGE;
209
210 if (result || be16_to_cpu(eh->data_len) < sizeof(*med))
211 return 0;
212
213 if (eh->nea || eh->notification_class != 0x4)
214 return 0;
215
216 if (med->media_event_code == 1)
217 return DISK_EVENT_EJECT_REQUEST;
218 else if (med->media_event_code == 2)
219 return DISK_EVENT_MEDIA_CHANGE;
220 return 0;
221}
222
223/*
224 * This function checks to see if the media has been changed or eject
225 * button has been pressed. It is possible that we have already
226 * sensed a change, or the drive may have sensed one and not yet
227 * reported it. The past events are accumulated in sdev->changed and
228 * returned together with the current state.
229 */
230static unsigned int sr_check_events(struct cdrom_device_info *cdi,
231 unsigned int clearing, int slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
233 struct scsi_cd *cd = cdi->handle;
Tejun Heo93aae172010-12-16 17:52:17 +0100234 bool last_present;
235 struct scsi_sense_hdr sshdr;
236 unsigned int events;
237 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Tejun Heo93aae172010-12-16 17:52:17 +0100239 /* no changer support */
240 if (CDSL_CURRENT != slot)
241 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Tejun Heo93aae172010-12-16 17:52:17 +0100243 events = sr_get_events(cd->device);
Kay Sievers79b96772011-06-30 15:03:48 +0200244 cd->get_event_changed |= events & DISK_EVENT_MEDIA_CHANGE;
245
246 /*
247 * If earlier GET_EVENT_STATUS_NOTIFICATION and TUR did not agree
248 * for several times in a row. We rely on TUR only for this likely
249 * broken device, to prevent generating incorrect media changed
250 * events for every open().
251 */
252 if (cd->ignore_get_event) {
253 events &= ~DISK_EVENT_MEDIA_CHANGE;
254 goto do_tur;
255 }
256
Tejun Heo93aae172010-12-16 17:52:17 +0100257 /*
258 * GET_EVENT_STATUS_NOTIFICATION is enough unless MEDIA_CHANGE
259 * is being cleared. Note that there are devices which hang
260 * if asked to execute TUR repeatedly.
261 */
Kay Sievers79b96772011-06-30 15:03:48 +0200262 if (cd->device->changed) {
263 events |= DISK_EVENT_MEDIA_CHANGE;
264 cd->device->changed = 0;
265 cd->tur_changed = true;
266 }
Tejun Heo93aae172010-12-16 17:52:17 +0100267
Kay Sievers79b96772011-06-30 15:03:48 +0200268 if (!(clearing & DISK_EVENT_MEDIA_CHANGE))
269 return events;
270do_tur:
Tejun Heo93aae172010-12-16 17:52:17 +0100271 /* let's see whether the media is there with TUR */
272 last_present = cd->media_present;
273 ret = scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES, &sshdr);
274
Tejun Heo638428e2010-12-09 11:18:42 +0100275 /*
276 * Media is considered to be present if TUR succeeds or fails with
277 * sense data indicating something other than media-not-present
278 * (ASC 0x3a).
279 */
Tejun Heo93aae172010-12-16 17:52:17 +0100280 cd->media_present = scsi_status_is_good(ret) ||
281 (scsi_sense_valid(&sshdr) && sshdr.asc != 0x3a);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Tejun Heo93aae172010-12-16 17:52:17 +0100283 if (last_present != cd->media_present)
Kay Sievers79b96772011-06-30 15:03:48 +0200284 cd->device->changed = 1;
285
Tejun Heo93aae172010-12-16 17:52:17 +0100286 if (cd->device->changed) {
287 events |= DISK_EVENT_MEDIA_CHANGE;
288 cd->device->changed = 0;
Kay Sievers79b96772011-06-30 15:03:48 +0200289 cd->tur_changed = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 }
Kay Sievers285e9672007-08-14 14:10:39 +0200291
Kay Sievers79b96772011-06-30 15:03:48 +0200292 if (cd->ignore_get_event)
293 return events;
294
295 /* check whether GET_EVENT is reporting spurious MEDIA_CHANGE */
296 if (!cd->tur_changed) {
297 if (cd->get_event_changed) {
298 if (cd->tur_mismatch++ > 8) {
Hannes Reinecke96eefad22014-06-25 16:39:54 +0200299 sr_printk(KERN_WARNING, cd,
300 "GET_EVENT and TUR disagree continuously, suppress GET_EVENT events\n");
Kay Sievers79b96772011-06-30 15:03:48 +0200301 cd->ignore_get_event = true;
302 }
303 } else {
304 cd->tur_mismatch = 0;
305 }
306 }
307 cd->tur_changed = false;
308 cd->get_event_changed = false;
309
Tejun Heo93aae172010-12-16 17:52:17 +0100310 return events;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
Tejun Heo93aae172010-12-16 17:52:17 +0100312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313/*
Linus Torvalds7b3d9542008-01-06 10:17:12 -0800314 * sr_done is the interrupt routine for the device driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 *
Linus Torvalds7b3d9542008-01-06 10:17:12 -0800316 * It will be notified on the end of a SCSI read / write, and will take one
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 * of several actions based on success or failure.
318 */
Linus Torvalds7b3d9542008-01-06 10:17:12 -0800319static int sr_done(struct scsi_cmnd *SCpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
321 int result = SCpnt->result;
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200322 int this_count = scsi_bufflen(SCpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 int good_bytes = (result == 0 ? this_count : 0);
324 int block_sectors = 0;
325 long error_sector;
326 struct scsi_cd *cd = scsi_cd(SCpnt->request->rq_disk);
327
328#ifdef DEBUG
Hannes Reinecke96eefad22014-06-25 16:39:54 +0200329 scmd_printk(KERN_INFO, SCpnt, "done: %x\n", result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330#endif
331
332 /*
333 * Handle MEDIUM ERRORs or VOLUME OVERFLOWs that indicate partial
334 * success. Since this is a relatively rare error condition, no
335 * care is taken to avoid unnecessary additional work such as
336 * memcpy's that could be avoided.
337 */
338 if (driver_byte(result) != 0 && /* An error occurred */
339 (SCpnt->sense_buffer[0] & 0x7f) == 0x70) { /* Sense current */
340 switch (SCpnt->sense_buffer[2]) {
341 case MEDIUM_ERROR:
342 case VOLUME_OVERFLOW:
343 case ILLEGAL_REQUEST:
344 if (!(SCpnt->sense_buffer[0] & 0x90))
345 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 error_sector = (SCpnt->sense_buffer[3] << 24) |
347 (SCpnt->sense_buffer[4] << 16) |
348 (SCpnt->sense_buffer[5] << 8) |
349 SCpnt->sense_buffer[6];
350 if (SCpnt->request->bio != NULL)
351 block_sectors =
352 bio_sectors(SCpnt->request->bio);
353 if (block_sectors < 4)
354 block_sectors = 4;
355 if (cd->device->sector_size == 2048)
356 error_sector <<= 2;
357 error_sector &= ~(block_sectors - 1);
Tejun Heo83096eb2009-05-07 22:24:39 +0900358 good_bytes = (error_sector -
359 blk_rq_pos(SCpnt->request)) << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 if (good_bytes < 0 || good_bytes >= this_count)
361 good_bytes = 0;
362 /*
363 * The SCSI specification allows for the value
364 * returned by READ CAPACITY to be up to 75 2K
365 * sectors past the last readable block.
366 * Therefore, if we hit a medium error within the
367 * last 75 2K sectors, we decrease the saved size
368 * value.
369 */
370 if (error_sector < get_capacity(cd->disk) &&
371 cd->capacity - error_sector < 4 * 75)
372 set_capacity(cd->disk, error_sector);
373 break;
374
375 case RECOVERED_ERROR:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 good_bytes = this_count;
377 break;
378
379 default:
380 break;
381 }
382 }
383
Linus Torvalds7b3d9542008-01-06 10:17:12 -0800384 return good_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385}
386
Christoph Hellwig159b2cb2018-11-09 14:42:39 +0100387static blk_status_t sr_init_command(struct scsi_cmnd *SCpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
Jens Axboe242f9dc2008-09-14 05:55:09 -0700389 int block = 0, this_count, s_size;
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500390 struct scsi_cd *cd;
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +0200391 struct request *rq = SCpnt->request;
Christoph Hellwig159b2cb2018-11-09 14:42:39 +0100392 blk_status_t ret;
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500393
Christoph Hellwig3c356bd2014-09-05 18:20:23 -0700394 ret = scsi_init_io(SCpnt);
Christoph Hellwig159b2cb2018-11-09 14:42:39 +0100395 if (ret != BLK_STS_OK)
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500396 goto out;
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500397 cd = scsi_cd(rq->rq_disk);
398
399 /* from here on until we're complete, any goto out
400 * is used for a killable error condition */
Christoph Hellwig159b2cb2018-11-09 14:42:39 +0100401 ret = BLK_STS_IOERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Hannes Reinecke96eefad22014-06-25 16:39:54 +0200403 SCSI_LOG_HLQUEUE(1, scmd_printk(KERN_INFO, SCpnt,
404 "Doing sr request, block = %d\n", block));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406 if (!cd->device || !scsi_device_online(cd->device)) {
Hannes Reinecke96eefad22014-06-25 16:39:54 +0200407 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
408 "Finishing %u sectors\n", blk_rq_sectors(rq)));
409 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
410 "Retry with 0x%p\n", SCpnt));
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500411 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 }
413
414 if (cd->device->changed) {
415 /*
416 * quietly refuse to do anything to a changed disc until the
417 * changed bit has been reset
418 */
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500419 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 }
421
422 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 * we do lazy blocksize switching (when reading XA sectors,
424 * see CDROMREADMODE2 ioctl)
425 */
426 s_size = cd->device->sector_size;
427 if (s_size > 2048) {
428 if (!in_interrupt())
429 sr_set_blocklength(cd, 2048);
430 else
Hannes Reinecke96eefad22014-06-25 16:39:54 +0200431 scmd_printk(KERN_INFO, SCpnt,
432 "can't switch blocksize: in interrupt\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 }
434
435 if (s_size != 512 && s_size != 1024 && s_size != 2048) {
Jeff Garzik3bf743e2005-10-24 18:04:06 -0400436 scmd_printk(KERN_ERR, SCpnt, "bad sector size %d\n", s_size);
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500437 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 }
439
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100440 switch (req_op(rq)) {
441 case REQ_OP_WRITE:
Christoph Hellwigfd2eb902014-07-18 16:59:19 +0200442 if (!cd->writeable)
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500443 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 SCpnt->cmnd[0] = WRITE_10;
Hannes Reinecke96eefad22014-06-25 16:39:54 +0200445 cd->cdi.media_written = 1;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100446 break;
447 case REQ_OP_READ:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 SCpnt->cmnd[0] = READ_10;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100449 break;
450 default:
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500451 blk_dump_rq_flags(rq, "Unknown sr command");
452 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 }
454
455 {
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200456 struct scatterlist *sg;
457 int i, size = 0, sg_count = scsi_sg_count(SCpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200459 scsi_for_each_sg(SCpnt, sg, sg_count, i)
460 size += sg->length;
461
462 if (size != scsi_bufflen(SCpnt)) {
Jeff Garzik3bf743e2005-10-24 18:04:06 -0400463 scmd_printk(KERN_ERR, SCpnt,
464 "mismatch count %d, bytes %d\n",
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200465 size, scsi_bufflen(SCpnt));
466 if (scsi_bufflen(SCpnt) > size)
467 SCpnt->sdb.length = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 }
469 }
470
471 /*
472 * request doesn't start on hw block boundary, add scatter pads
473 */
Tejun Heo83096eb2009-05-07 22:24:39 +0900474 if (((unsigned int)blk_rq_pos(rq) % (s_size >> 9)) ||
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200475 (scsi_bufflen(SCpnt) % s_size)) {
Jeff Garzik3bf743e2005-10-24 18:04:06 -0400476 scmd_printk(KERN_NOTICE, SCpnt, "unaligned transfer\n");
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500477 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 }
479
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200480 this_count = (scsi_bufflen(SCpnt) >> 9) / (s_size >> 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482
Hannes Reinecke96eefad22014-06-25 16:39:54 +0200483 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
484 "%s %d/%u 512 byte blocks.\n",
485 (rq_data_dir(rq) == WRITE) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 "writing" : "reading",
Hannes Reinecke96eefad22014-06-25 16:39:54 +0200487 this_count, blk_rq_sectors(rq)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 SCpnt->cmnd[1] = 0;
Tejun Heo83096eb2009-05-07 22:24:39 +0900490 block = (unsigned int)blk_rq_pos(rq) / (s_size >> 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492 if (this_count > 0xffff) {
493 this_count = 0xffff;
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200494 SCpnt->sdb.length = this_count * s_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 }
496
497 SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
498 SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
499 SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
500 SCpnt->cmnd[5] = (unsigned char) block & 0xff;
501 SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
502 SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
503 SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
504
505 /*
506 * We shouldn't disconnect in the middle of a sector, so with a dumb
507 * host adapter, it's safe to assume that we can at least transfer
508 * this many bytes between each connect / disconnect.
509 */
510 SCpnt->transfersize = cd->device->sector_size;
511 SCpnt->underflow = this_count << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 SCpnt->allowed = MAX_RETRIES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 * This indicates that the command is ready from our end to be
516 * queued.
517 */
Christoph Hellwig159b2cb2018-11-09 14:42:39 +0100518 ret = BLK_STS_OK;
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500519 out:
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +0200520 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521}
522
Al Viro40cc51b2008-03-02 10:41:28 -0500523static int sr_block_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524{
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200525 struct scsi_cd *cd;
Bart Van Assche1214fd72018-08-02 10:44:42 -0700526 struct scsi_device *sdev;
Al Viro40cc51b2008-03-02 10:41:28 -0500527 int ret = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Bart Van Assche1214fd72018-08-02 10:44:42 -0700529 cd = scsi_cd_get(bdev->bd_disk);
530 if (!cd)
531 goto out;
532
533 sdev = cd->device;
534 scsi_autopm_get_device(sdev);
Maurizio Lombardi2bbea6e2018-03-09 13:59:06 +0100535 check_disk_change(bdev);
536
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200537 mutex_lock(&sr_mutex);
Bart Van Assche1214fd72018-08-02 10:44:42 -0700538 ret = cdrom_open(&cd->cdi, bdev, mode);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200539 mutex_unlock(&sr_mutex);
Bart Van Assche1214fd72018-08-02 10:44:42 -0700540
541 scsi_autopm_put_device(sdev);
542 if (ret)
543 scsi_cd_put(cd);
544
545out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 return ret;
547}
548
Al Virodb2a1442013-05-05 21:52:57 -0400549static void sr_block_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
Al Viro40cc51b2008-03-02 10:41:28 -0500551 struct scsi_cd *cd = scsi_cd(disk);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200552 mutex_lock(&sr_mutex);
Al Viro40cc51b2008-03-02 10:41:28 -0500553 cdrom_release(&cd->cdi, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 scsi_cd_put(cd);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200555 mutex_unlock(&sr_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556}
557
Al Viro40cc51b2008-03-02 10:41:28 -0500558static int sr_block_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 unsigned long arg)
560{
Al Viro40cc51b2008-03-02 10:41:28 -0500561 struct scsi_cd *cd = scsi_cd(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 struct scsi_device *sdev = cd->device;
Christoph Hellwig6a2900b2006-03-23 03:00:15 -0800563 void __user *argp = (void __user *)arg;
564 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200566 mutex_lock(&sr_mutex);
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200567
Christoph Hellwig906d15f2014-10-11 16:25:31 +0200568 ret = scsi_ioctl_block_when_processing_errors(sdev, cmd,
569 (mode & FMODE_NDELAY) != 0);
570 if (ret)
571 goto out;
572
Bart Van Assche1214fd72018-08-02 10:44:42 -0700573 scsi_autopm_get_device(sdev);
574
Christoph Hellwig6a2900b2006-03-23 03:00:15 -0800575 /*
576 * Send SCSI addressing ioctls directly to mid level, send other
577 * ioctls to cdrom/block level.
578 */
579 switch (cmd) {
580 case SCSI_IOCTL_GET_IDLUN:
581 case SCSI_IOCTL_GET_BUS_NUMBER:
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200582 ret = scsi_ioctl(sdev, cmd, argp);
Bart Van Assche1214fd72018-08-02 10:44:42 -0700583 goto put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 }
Christoph Hellwig6a2900b2006-03-23 03:00:15 -0800585
Al Viro40cc51b2008-03-02 10:41:28 -0500586 ret = cdrom_ioctl(&cd->cdi, bdev, mode, cmd, arg);
Tejun Heo63972562007-01-02 17:41:04 +0900587 if (ret != -ENOSYS)
Bart Van Assche1214fd72018-08-02 10:44:42 -0700588 goto put;
Christoph Hellwig6a2900b2006-03-23 03:00:15 -0800589
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200590 ret = scsi_ioctl(sdev, cmd, argp);
591
Bart Van Assche1214fd72018-08-02 10:44:42 -0700592put:
593 scsi_autopm_put_device(sdev);
594
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200595out:
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200596 mutex_unlock(&sr_mutex);
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200597 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598}
599
Tejun Heo93aae172010-12-16 17:52:17 +0100600static unsigned int sr_block_check_events(struct gendisk *disk,
601 unsigned int clearing)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
Jens Axboe2d097c52018-04-11 11:26:09 -0600603 unsigned int ret = 0;
604 struct scsi_cd *cd;
Aaron Lu6c7f1e22013-01-23 15:09:31 +0800605
Jens Axboe2d097c52018-04-11 11:26:09 -0600606 cd = scsi_cd_get(disk);
607 if (!cd)
Aaron Lu6627b382013-10-28 15:27:49 +0800608 return 0;
Aaron Lu6c7f1e22013-01-23 15:09:31 +0800609
Jens Axboe2d097c52018-04-11 11:26:09 -0600610 if (!atomic_read(&cd->device->disk_events_disable_depth))
611 ret = cdrom_check_events(&cd->cdi, clearing);
612
613 scsi_cd_put(cd);
614 return ret;
Tejun Heo93aae172010-12-16 17:52:17 +0100615}
616
617static int sr_block_revalidate_disk(struct gendisk *disk)
618{
Tejun Heo93aae172010-12-16 17:52:17 +0100619 struct scsi_sense_hdr sshdr;
Jens Axboe2d097c52018-04-11 11:26:09 -0600620 struct scsi_cd *cd;
621
622 cd = scsi_cd_get(disk);
623 if (!cd)
624 return -ENXIO;
Tejun Heo93aae172010-12-16 17:52:17 +0100625
626 /* if the unit is not ready, nothing more to do */
627 if (scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES, &sshdr))
Aaron Lu6c7f1e22013-01-23 15:09:31 +0800628 goto out;
Tejun Heo93aae172010-12-16 17:52:17 +0100629
630 sr_cd_check(&cd->cdi);
631 get_sectorsize(cd);
Aaron Lu6c7f1e22013-01-23 15:09:31 +0800632out:
Jens Axboe2d097c52018-04-11 11:26:09 -0600633 scsi_cd_put(cd);
Tejun Heo93aae172010-12-16 17:52:17 +0100634 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635}
636
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700637static const struct block_device_operations sr_bdops =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
639 .owner = THIS_MODULE,
Al Viro40cc51b2008-03-02 10:41:28 -0500640 .open = sr_block_open,
641 .release = sr_block_release,
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200642 .ioctl = sr_block_ioctl,
Tejun Heo93aae172010-12-16 17:52:17 +0100643 .check_events = sr_block_check_events,
644 .revalidate_disk = sr_block_revalidate_disk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 /*
646 * No compat_ioctl for now because sr_block_ioctl never
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300647 * seems to pass arbitrary ioctls down to host drivers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 */
649};
650
651static int sr_open(struct cdrom_device_info *cdi, int purpose)
652{
653 struct scsi_cd *cd = cdi->handle;
654 struct scsi_device *sdev = cd->device;
655 int retval;
656
657 /*
658 * If the device is in error recovery, wait until it is done.
659 * If the device is offline, then disallow any access to it.
660 */
661 retval = -ENXIO;
662 if (!scsi_block_when_processing_errors(sdev))
663 goto error_out;
664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 return 0;
666
667error_out:
668 return retval;
669}
670
671static void sr_release(struct cdrom_device_info *cdi)
672{
673 struct scsi_cd *cd = cdi->handle;
674
675 if (cd->device->sector_size > 2048)
676 sr_set_blocklength(cd, 2048);
677
678}
679
680static int sr_probe(struct device *dev)
681{
682 struct scsi_device *sdev = to_scsi_device(dev);
683 struct gendisk *disk;
684 struct scsi_cd *cd;
685 int minor, error;
686
Subhash Jadavani6fe8c1d2014-09-10 14:54:09 +0300687 scsi_autopm_get_device(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 error = -ENODEV;
689 if (sdev->type != TYPE_ROM && sdev->type != TYPE_WORM)
690 goto fail;
691
692 error = -ENOMEM;
Jes Sorensen24669f752006-01-16 10:31:18 -0500693 cd = kzalloc(sizeof(*cd), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 if (!cd)
695 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697 kref_init(&cd->kref);
698
699 disk = alloc_disk(1);
700 if (!disk)
701 goto fail_free;
702
703 spin_lock(&sr_index_lock);
704 minor = find_first_zero_bit(sr_index_bits, SR_DISKS);
705 if (minor == SR_DISKS) {
706 spin_unlock(&sr_index_lock);
707 error = -EBUSY;
708 goto fail_put;
709 }
710 __set_bit(minor, sr_index_bits);
711 spin_unlock(&sr_index_lock);
712
713 disk->major = SCSI_CDROM_MAJOR;
714 disk->first_minor = minor;
715 sprintf(disk->disk_name, "sr%d", minor);
716 disk->fops = &sr_bdops;
Tejun Heod4dc2102011-04-21 20:54:46 +0200717 disk->flags = GENHD_FL_CD | GENHD_FL_BLOCK_EVENTS_ON_EXCL_WRITE;
Tejun Heo93aae172010-12-16 17:52:17 +0100718 disk->events = DISK_EVENT_MEDIA_CHANGE | DISK_EVENT_EJECT_REQUEST;
Martin Wilckc92e2f02019-03-27 14:51:02 +0100719 disk->event_flags = DISK_EVENT_FLAG_POLL | DISK_EVENT_FLAG_UEVENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Jens Axboe242f9dc2008-09-14 05:55:09 -0700721 blk_queue_rq_timeout(sdev->request_queue, SR_TIMEOUT);
722
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 cd->device = sdev;
724 cd->disk = disk;
725 cd->driver = &sr_template;
726 cd->disk = disk;
727 cd->capacity = 0x1fffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 cd->device->changed = 1; /* force recheck CD type */
Tejun Heo93aae172010-12-16 17:52:17 +0100729 cd->media_present = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 cd->use = 1;
731 cd->readcd_known = 0;
732 cd->readcd_cdda = 0;
733
734 cd->cdi.ops = &sr_dops;
735 cd->cdi.handle = cd;
736 cd->cdi.mask = 0;
737 cd->cdi.capacity = 1;
738 sprintf(cd->cdi.name, "sr%d", minor);
739
740 sdev->sector_size = 2048; /* A guess, just in case */
741
742 /* FIXME: need to handle a get_capabilities failure properly ?? */
743 get_capabilities(cd);
744 sr_vendor_init(cd);
745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 set_capacity(disk, cd->capacity);
747 disk->private_data = &cd->driver;
748 disk->queue = sdev->request_queue;
749 cd->cdi.disk = disk;
750
751 if (register_cdrom(&cd->cdi))
752 goto fail_put;
753
Aaron Lu6627b382013-10-28 15:27:49 +0800754 /*
755 * Initialize block layer runtime PM stuffs before the
756 * periodic event checking request gets started in add_disk.
757 */
758 blk_pm_runtime_init(sdev->request_queue, dev);
759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 dev_set_drvdata(dev, cd);
761 disk->flags |= GENHD_FL_REMOVABLE;
Hannes Reineckefef912b2018-09-28 08:17:19 +0200762 device_add_disk(&sdev->sdev_gendev, disk, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
James Bottomley9ccfc752005-10-02 11:45:08 -0500764 sdev_printk(KERN_DEBUG, sdev,
765 "Attached scsi CD-ROM %s\n", cd->cdi.name);
Aaron Lu6c7f1e22013-01-23 15:09:31 +0800766 scsi_autopm_put_device(cd->device);
767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 return 0;
769
770fail_put:
771 put_disk(disk);
772fail_free:
773 kfree(cd);
774fail:
Subhash Jadavani6fe8c1d2014-09-10 14:54:09 +0300775 scsi_autopm_put_device(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 return error;
777}
778
779
780static void get_sectorsize(struct scsi_cd *cd)
781{
782 unsigned char cmd[10];
FUJITA Tomonori62858da2008-07-04 09:31:50 +0200783 unsigned char buffer[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 int the_result, retries = 3;
785 int sector_size;
Jens Axboe165125e2007-07-24 09:28:11 +0200786 struct request_queue *queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 do {
789 cmd[0] = READ_CAPACITY;
790 memset((void *) &cmd[1], 0, 9);
FUJITA Tomonori62858da2008-07-04 09:31:50 +0200791 memset(buffer, 0, sizeof(buffer));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
793 /* Do the command and wait.. */
James Bottomley820732b2005-06-12 22:21:29 -0500794 the_result = scsi_execute_req(cd->device, cmd, DMA_FROM_DEVICE,
FUJITA Tomonori62858da2008-07-04 09:31:50 +0200795 buffer, sizeof(buffer), NULL,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +0900796 SR_TIMEOUT, MAX_RETRIES, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 retries--;
799
800 } while (the_result && retries);
801
802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 if (the_result) {
804 cd->capacity = 0x1fffff;
805 sector_size = 2048; /* A guess, just in case */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 } else {
Tejun Heo59151362009-09-04 11:38:02 +0900807 long last_written;
808
809 cd->capacity = 1 + ((buffer[0] << 24) | (buffer[1] << 16) |
810 (buffer[2] << 8) | buffer[3]);
811 /*
812 * READ_CAPACITY doesn't return the correct size on
813 * certain UDF media. If last_written is larger, use
814 * it instead.
815 *
816 * http://bugzilla.kernel.org/show_bug.cgi?id=9668
817 */
818 if (!cdrom_get_last_written(&cd->cdi, &last_written))
819 cd->capacity = max_t(long, cd->capacity, last_written);
820
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 sector_size = (buffer[4] << 24) |
822 (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
823 switch (sector_size) {
824 /*
825 * HP 4020i CD-Recorder reports 2340 byte sectors
826 * Philips CD-Writers report 2352 byte sectors
827 *
828 * Use 2k sectors for them..
829 */
830 case 0:
831 case 2340:
832 case 2352:
833 sector_size = 2048;
834 /* fall through */
835 case 2048:
836 cd->capacity *= 4;
837 /* fall through */
838 case 512:
839 break;
840 default:
Hannes Reinecke96eefad22014-06-25 16:39:54 +0200841 sr_printk(KERN_INFO, cd,
842 "unsupported sector size %d.", sector_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 cd->capacity = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 }
845
846 cd->device->sector_size = sector_size;
847
848 /*
849 * Add this so that we have the ability to correctly gauge
850 * what the device is capable of.
851 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 set_capacity(cd->disk, cd->capacity);
853 }
854
855 queue = cd->device->request_queue;
Martin K. Petersene1defc42009-05-22 17:17:49 -0400856 blk_queue_logical_block_size(queue, sector_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
FUJITA Tomonori62858da2008-07-04 09:31:50 +0200858 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859}
860
861static void get_capabilities(struct scsi_cd *cd)
862{
863 unsigned char *buffer;
864 struct scsi_mode_data data;
James Bottomley820732b2005-06-12 22:21:29 -0500865 struct scsi_sense_hdr sshdr;
Martin K. Petersena00a7862017-03-17 08:47:14 -0400866 unsigned int ms_len = 128;
James Bottomley38582a62008-02-06 13:01:58 -0600867 int rc, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Arjan van de Ven0ad78202005-11-28 16:22:25 +0100869 static const char *loadmech[] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 {
871 "caddy",
872 "tray",
873 "pop-up",
874 "",
875 "changer",
876 "cartridge changer",
877 "",
878 ""
879 };
880
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
882 /* allocate transfer buffer */
883 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
884 if (!buffer) {
Hannes Reinecke96eefad22014-06-25 16:39:54 +0200885 sr_printk(KERN_ERR, cd, "out of memory.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 return;
887 }
888
James Bottomley38582a62008-02-06 13:01:58 -0600889 /* eat unit attentions */
Tejun Heo9f8a2c22010-12-08 20:57:40 +0100890 scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES, &sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
892 /* ask for mode page 0x2a */
Martin K. Petersena00a7862017-03-17 08:47:14 -0400893 rc = scsi_mode_sense(cd->device, 0, 0x2a, buffer, ms_len,
James Bottomley1cf72692005-08-28 11:27:01 -0500894 SR_TIMEOUT, 3, &data, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Martin K. Petersena00a7862017-03-17 08:47:14 -0400896 if (!scsi_status_is_good(rc) || data.length > ms_len ||
897 data.header_length + data.block_descriptor_length > data.length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 /* failed, drive doesn't have capabilities mode page */
899 cd->cdi.speed = 1;
900 cd->cdi.mask |= (CDC_CD_R | CDC_CD_RW | CDC_DVD_R |
Chuck Ebbertbcc1e382006-01-11 23:58:40 -0500901 CDC_DVD | CDC_DVD_RAM |
902 CDC_SELECT_DISC | CDC_SELECT_SPEED |
903 CDC_MRW | CDC_MRW_W | CDC_RAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 kfree(buffer);
Hannes Reinecke96eefad22014-06-25 16:39:54 +0200905 sr_printk(KERN_INFO, cd, "scsi-1 drive");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 return;
907 }
908
909 n = data.header_length + data.block_descriptor_length;
910 cd->cdi.speed = ((buffer[n + 8] << 8) + buffer[n + 9]) / 176;
911 cd->readcd_known = 1;
912 cd->readcd_cdda = buffer[n + 5] & 0x01;
913 /* print some capability bits */
Hannes Reinecke96eefad22014-06-25 16:39:54 +0200914 sr_printk(KERN_INFO, cd,
915 "scsi3-mmc drive: %dx/%dx %s%s%s%s%s%s\n",
916 ((buffer[n + 14] << 8) + buffer[n + 15]) / 176,
917 cd->cdi.speed,
918 buffer[n + 3] & 0x01 ? "writer " : "", /* CD Writer */
919 buffer[n + 3] & 0x20 ? "dvd-ram " : "",
920 buffer[n + 2] & 0x02 ? "cd/rw " : "", /* can read rewriteable */
921 buffer[n + 4] & 0x20 ? "xa/form2 " : "", /* can read xa/from2 */
922 buffer[n + 5] & 0x01 ? "cdda " : "", /* can read audio data */
923 loadmech[buffer[n + 6] >> 5]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 if ((buffer[n + 6] >> 5) == 0)
925 /* caddy drives can't close tray... */
926 cd->cdi.mask |= CDC_CLOSE_TRAY;
927 if ((buffer[n + 2] & 0x8) == 0)
928 /* not a DVD drive */
929 cd->cdi.mask |= CDC_DVD;
Hannes Reinecke96eefad22014-06-25 16:39:54 +0200930 if ((buffer[n + 3] & 0x20) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 /* can't write DVD-RAM media */
932 cd->cdi.mask |= CDC_DVD_RAM;
933 if ((buffer[n + 3] & 0x10) == 0)
934 /* can't write DVD-R media */
935 cd->cdi.mask |= CDC_DVD_R;
936 if ((buffer[n + 3] & 0x2) == 0)
937 /* can't write CD-RW media */
938 cd->cdi.mask |= CDC_CD_RW;
939 if ((buffer[n + 3] & 0x1) == 0)
940 /* can't write CD-R media */
941 cd->cdi.mask |= CDC_CD_R;
942 if ((buffer[n + 6] & 0x8) == 0)
943 /* can't eject */
944 cd->cdi.mask |= CDC_OPEN_TRAY;
945
946 if ((buffer[n + 6] >> 5) == mechtype_individual_changer ||
947 (buffer[n + 6] >> 5) == mechtype_cartridge_changer)
948 cd->cdi.capacity =
949 cdrom_number_of_slots(&cd->cdi);
950 if (cd->cdi.capacity <= 1)
951 /* not a changer */
952 cd->cdi.mask |= CDC_SELECT_DISC;
953 /*else I don't think it can close its tray
954 cd->cdi.mask |= CDC_CLOSE_TRAY; */
955
956 /*
957 * if DVD-RAM, MRW-W or CD-RW, we are randomly writable
958 */
959 if ((cd->cdi.mask & (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) !=
960 (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) {
Christoph Hellwigfd2eb902014-07-18 16:59:19 +0200961 cd->writeable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 }
963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 kfree(buffer);
965}
966
967/*
968 * sr_packet() is the entry point for the generic commands generated
Hannes Reinecke96eefad22014-06-25 16:39:54 +0200969 * by the Uniform CD-ROM layer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 */
971static int sr_packet(struct cdrom_device_info *cdi,
972 struct packet_command *cgc)
973{
Hans de Goede8e04d802010-10-01 14:20:08 -0700974 struct scsi_cd *cd = cdi->handle;
975 struct scsi_device *sdev = cd->device;
976
977 if (cgc->cmd[0] == GPCMD_READ_DISC_INFO && sdev->no_read_disc_info)
978 return -EDRIVE_CANT_DO_THIS;
979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 if (cgc->timeout <= 0)
981 cgc->timeout = IOCTL_TIMEOUT;
982
Hans de Goede8e04d802010-10-01 14:20:08 -0700983 sr_do_ioctl(cd, cgc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
985 return cgc->stat;
986}
987
988/**
989 * sr_kref_release - Called to free the scsi_cd structure
990 * @kref: pointer to embedded kref
991 *
Arjan van de Ven0b950672006-01-11 13:16:10 +0100992 * sr_ref_mutex must be held entering this routine. Because it is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 * called on last put, you should always use the scsi_cd_get()
994 * scsi_cd_put() helpers which manipulate the semaphore directly
995 * and never do a direct kref_put().
996 **/
997static void sr_kref_release(struct kref *kref)
998{
999 struct scsi_cd *cd = container_of(kref, struct scsi_cd, kref);
1000 struct gendisk *disk = cd->disk;
1001
1002 spin_lock(&sr_index_lock);
Tejun Heof331c022008-09-03 09:01:48 +02001003 clear_bit(MINOR(disk_devt(disk)), sr_index_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 spin_unlock(&sr_index_lock);
1005
1006 unregister_cdrom(&cd->cdi);
1007
1008 disk->private_data = NULL;
1009
1010 put_disk(disk);
1011
1012 kfree(cd);
1013}
1014
1015static int sr_remove(struct device *dev)
1016{
1017 struct scsi_cd *cd = dev_get_drvdata(dev);
1018
Aaron Lu6c7f1e22013-01-23 15:09:31 +08001019 scsi_autopm_get_device(cd->device);
1020
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 del_gendisk(cd->disk);
Alan Stern13b43892016-01-20 11:26:01 -05001022 dev_set_drvdata(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Arjan van de Ven0b950672006-01-11 13:16:10 +01001024 mutex_lock(&sr_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 kref_put(&cd->kref, sr_kref_release);
Arjan van de Ven0b950672006-01-11 13:16:10 +01001026 mutex_unlock(&sr_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
1028 return 0;
1029}
1030
1031static int __init init_sr(void)
1032{
1033 int rc;
1034
1035 rc = register_blkdev(SCSI_CDROM_MAJOR, "sr");
1036 if (rc)
1037 return rc;
Akinobu Mitada3962f2007-06-26 00:39:33 +09001038 rc = scsi_register_driver(&sr_template.gendrv);
1039 if (rc)
1040 unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
1041
1042 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043}
1044
1045static void __exit exit_sr(void)
1046{
1047 scsi_unregister_driver(&sr_template.gendrv);
1048 unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
1049}
1050
1051module_init(init_sr);
1052module_exit(exit_sr);
1053MODULE_LICENSE("GPL");