blob: 0c4aa4665a2f9d2a6fa9db565a220f01322c5ec1 [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * sr.c Copyright (C) 1992 David Giller
4 * Copyright (C) 1993, 1994, 1995, 1999 Eric Youngdale
5 *
6 * adapted from:
7 * sd.c Copyright (C) 1992 Drew Eckhardt
8 * Linux scsi disk driver by
9 * Drew Eckhardt <drew@colorado.edu>
10 *
11 * Modified by Eric Youngdale ericy@andante.org to
12 * add scatter-gather, multiple outstanding request, and other
13 * enhancements.
14 *
15 * Modified by Eric Youngdale eric@andante.org to support loadable
16 * low-level scsi drivers.
17 *
18 * Modified by Thomas Quinot thomas@melchior.cuivre.fdn.fr to
19 * provide auto-eject.
20 *
21 * Modified by Gerd Knorr <kraxel@cs.tu-berlin.de> to support the
22 * generic cdrom interface
23 *
24 * Modified by Jens Axboe <axboe@suse.de> - Uniform sr_packet()
25 * interface, capabilities probe additions, ioctl cleanups, etc.
26 *
27 * Modified by Richard Gooch <rgooch@atnf.csiro.au> to support devfs
28 *
29 * Modified by Jens Axboe <axboe@suse.de> - support DVD-RAM
30 * transparently and lose the GHOST hack
31 *
32 * Modified by Arnaldo Carvalho de Melo <acme@conectiva.com.br>
33 * check resource allocation in sr_init and some cleanups
34 */
35
36#include <linux/module.h>
37#include <linux/fs.h>
38#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/mm.h>
40#include <linux/bio.h>
Arnd Bergmannd320a952019-03-15 17:39:44 +010041#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/string.h>
43#include <linux/errno.h>
44#include <linux/cdrom.h>
45#include <linux/interrupt.h>
46#include <linux/init.h>
47#include <linux/blkdev.h>
Bart Van Asschebca6b062018-09-26 14:01:03 -070048#include <linux/blk-pm.h>
Arjan van de Ven0b950672006-01-11 13:16:10 +010049#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090050#include <linux/slab.h>
Aaron Lu6c7f1e22013-01-23 15:09:31 +080051#include <linux/pm_runtime.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080052#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Bart Van Assche655da8e52020-04-26 18:48:44 -070054#include <asm/unaligned.h>
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <scsi/scsi.h>
57#include <scsi/scsi_dbg.h>
58#include <scsi/scsi_device.h>
59#include <scsi/scsi_driver.h>
James Bottomley820732b2005-06-12 22:21:29 -050060#include <scsi/scsi_cmnd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <scsi/scsi_eh.h>
62#include <scsi/scsi_host.h>
63#include <scsi/scsi_ioctl.h> /* For the door lock/unlock commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65#include "scsi_logging.h"
66#include "sr.h"
67
68
Rene Hermanf018fa52006-03-08 00:14:20 -080069MODULE_DESCRIPTION("SCSI cdrom (sr) driver");
70MODULE_LICENSE("GPL");
71MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_CDROM_MAJOR);
Michael Tokarevd7b8bcb02006-10-27 16:02:37 +040072MODULE_ALIAS_SCSI_DEVICE(TYPE_ROM);
73MODULE_ALIAS_SCSI_DEVICE(TYPE_WORM);
Rene Hermanf018fa52006-03-08 00:14:20 -080074
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#define SR_DISKS 256
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#define SR_CAPABILITIES \
78 (CDC_CLOSE_TRAY|CDC_OPEN_TRAY|CDC_LOCK|CDC_SELECT_SPEED| \
79 CDC_SELECT_DISC|CDC_MULTI_SESSION|CDC_MCN|CDC_MEDIA_CHANGED| \
Christoph Hellwig6a2900b2006-03-23 03:00:15 -080080 CDC_PLAY_AUDIO|CDC_RESET|CDC_DRIVE_STATUS| \
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 CDC_CD_R|CDC_CD_RW|CDC_DVD|CDC_DVD_R|CDC_DVD_RAM|CDC_GENERIC_PACKET| \
82 CDC_MRW|CDC_MRW_W|CDC_RAM)
83
84static int sr_probe(struct device *);
85static int sr_remove(struct device *);
Christoph Hellwig159b2cb2018-11-09 14:42:39 +010086static blk_status_t sr_init_command(struct scsi_cmnd *SCpnt);
Linus Torvalds7b3d9542008-01-06 10:17:12 -080087static int sr_done(struct scsi_cmnd *);
Aaron Lu6c7f1e22013-01-23 15:09:31 +080088static int sr_runtime_suspend(struct device *dev);
89
Julia Lawalla816b4c2016-08-28 22:17:38 +020090static const struct dev_pm_ops sr_pm_ops = {
Aaron Lu6c7f1e22013-01-23 15:09:31 +080091 .runtime_suspend = sr_runtime_suspend,
92};
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94static struct scsi_driver sr_template = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 .gendrv = {
96 .name = "sr",
Christoph Hellwig3af6b352014-11-12 18:34:51 +010097 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 .probe = sr_probe,
99 .remove = sr_remove,
Aaron Lu6c7f1e22013-01-23 15:09:31 +0800100 .pm = &sr_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 },
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +0200102 .init_command = sr_init_command,
Linus Torvalds7b3d9542008-01-06 10:17:12 -0800103 .done = sr_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104};
105
106static unsigned long sr_index_bits[SR_DISKS / BITS_PER_LONG];
107static DEFINE_SPINLOCK(sr_index_lock);
108
109/* This semaphore is used to mediate the 0->1 reference get in the
110 * face of object destruction (i.e. we can't allow a get on an
111 * object after last put) */
Arjan van de Ven0b950672006-01-11 13:16:10 +0100112static DEFINE_MUTEX(sr_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114static int sr_open(struct cdrom_device_info *, int);
115static void sr_release(struct cdrom_device_info *);
116
117static void get_sectorsize(struct scsi_cd *);
118static void get_capabilities(struct scsi_cd *);
119
Tejun Heo93aae172010-12-16 17:52:17 +0100120static unsigned int sr_check_events(struct cdrom_device_info *cdi,
121 unsigned int clearing, int slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122static int sr_packet(struct cdrom_device_info *, struct packet_command *);
123
Kees Cook853fe1b2017-02-13 16:25:26 -0800124static const struct cdrom_device_ops sr_dops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 .open = sr_open,
126 .release = sr_release,
127 .drive_status = sr_drive_status,
Tejun Heo93aae172010-12-16 17:52:17 +0100128 .check_events = sr_check_events,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 .tray_move = sr_tray_move,
130 .lock_door = sr_lock_door,
131 .select_speed = sr_select_speed,
132 .get_last_session = sr_get_last_session,
133 .get_mcn = sr_get_mcn,
134 .reset = sr_reset,
135 .audio_ioctl = sr_audio_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 .capability = SR_CAPABILITIES,
137 .generic_packet = sr_packet,
138};
139
140static void sr_kref_release(struct kref *kref);
141
142static inline struct scsi_cd *scsi_cd(struct gendisk *disk)
143{
144 return container_of(disk->private_data, struct scsi_cd, driver);
145}
146
Aaron Lu6c7f1e22013-01-23 15:09:31 +0800147static int sr_runtime_suspend(struct device *dev)
148{
149 struct scsi_cd *cd = dev_get_drvdata(dev);
150
Alan Stern13b43892016-01-20 11:26:01 -0500151 if (!cd) /* E.g.: runtime suspend following sr_remove() */
152 return 0;
153
Aaron Lu6c7f1e22013-01-23 15:09:31 +0800154 if (cd->media_present)
155 return -EBUSY;
156 else
157 return 0;
158}
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160/*
161 * The get and put routines for the struct scsi_cd. Note this entity
162 * has a scsi_device pointer and owns a reference to this.
163 */
164static inline struct scsi_cd *scsi_cd_get(struct gendisk *disk)
165{
166 struct scsi_cd *cd = NULL;
167
Arjan van de Ven0b950672006-01-11 13:16:10 +0100168 mutex_lock(&sr_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 if (disk->private_data == NULL)
170 goto out;
171 cd = scsi_cd(disk);
172 kref_get(&cd->kref);
Aaron Lu6627b382013-10-28 15:27:49 +0800173 if (scsi_device_get(cd->device)) {
174 kref_put(&cd->kref, sr_kref_release);
175 cd = NULL;
176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 out:
Arjan van de Ven0b950672006-01-11 13:16:10 +0100178 mutex_unlock(&sr_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 return cd;
180}
181
Arjan van de Ven858119e2006-01-14 13:20:43 -0800182static void scsi_cd_put(struct scsi_cd *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
184 struct scsi_device *sdev = cd->device;
185
Arjan van de Ven0b950672006-01-11 13:16:10 +0100186 mutex_lock(&sr_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 kref_put(&cd->kref, sr_kref_release);
188 scsi_device_put(sdev);
Arjan van de Ven0b950672006-01-11 13:16:10 +0100189 mutex_unlock(&sr_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
191
Tejun Heo93aae172010-12-16 17:52:17 +0100192static unsigned int sr_get_events(struct scsi_device *sdev)
193{
194 u8 buf[8];
195 u8 cmd[] = { GET_EVENT_STATUS_NOTIFICATION,
196 1, /* polled */
197 0, 0, /* reserved */
198 1 << 4, /* notification class: media */
199 0, 0, /* reserved */
200 0, sizeof(buf), /* allocation length */
201 0, /* control */
202 };
203 struct event_header *eh = (void *)buf;
204 struct media_event_desc *med = (void *)(buf + 4);
205 struct scsi_sense_hdr sshdr;
206 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Tejun Heo93aae172010-12-16 17:52:17 +0100208 result = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buf, sizeof(buf),
209 &sshdr, SR_TIMEOUT, MAX_RETRIES, NULL);
210 if (scsi_sense_valid(&sshdr) && sshdr.sense_key == UNIT_ATTENTION)
211 return DISK_EVENT_MEDIA_CHANGE;
212
213 if (result || be16_to_cpu(eh->data_len) < sizeof(*med))
214 return 0;
215
216 if (eh->nea || eh->notification_class != 0x4)
217 return 0;
218
219 if (med->media_event_code == 1)
220 return DISK_EVENT_EJECT_REQUEST;
221 else if (med->media_event_code == 2)
222 return DISK_EVENT_MEDIA_CHANGE;
223 return 0;
224}
225
226/*
227 * This function checks to see if the media has been changed or eject
228 * button has been pressed. It is possible that we have already
229 * sensed a change, or the drive may have sensed one and not yet
230 * reported it. The past events are accumulated in sdev->changed and
231 * returned together with the current state.
232 */
233static unsigned int sr_check_events(struct cdrom_device_info *cdi,
234 unsigned int clearing, int slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
236 struct scsi_cd *cd = cdi->handle;
Tejun Heo93aae172010-12-16 17:52:17 +0100237 bool last_present;
238 struct scsi_sense_hdr sshdr;
239 unsigned int events;
240 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Tejun Heo93aae172010-12-16 17:52:17 +0100242 /* no changer support */
243 if (CDSL_CURRENT != slot)
244 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Tejun Heo93aae172010-12-16 17:52:17 +0100246 events = sr_get_events(cd->device);
Kay Sievers79b96772011-06-30 15:03:48 +0200247 cd->get_event_changed |= events & DISK_EVENT_MEDIA_CHANGE;
248
249 /*
250 * If earlier GET_EVENT_STATUS_NOTIFICATION and TUR did not agree
251 * for several times in a row. We rely on TUR only for this likely
252 * broken device, to prevent generating incorrect media changed
253 * events for every open().
254 */
255 if (cd->ignore_get_event) {
256 events &= ~DISK_EVENT_MEDIA_CHANGE;
257 goto do_tur;
258 }
259
Tejun Heo93aae172010-12-16 17:52:17 +0100260 /*
261 * GET_EVENT_STATUS_NOTIFICATION is enough unless MEDIA_CHANGE
262 * is being cleared. Note that there are devices which hang
263 * if asked to execute TUR repeatedly.
264 */
Kay Sievers79b96772011-06-30 15:03:48 +0200265 if (cd->device->changed) {
266 events |= DISK_EVENT_MEDIA_CHANGE;
267 cd->device->changed = 0;
268 cd->tur_changed = true;
269 }
Tejun Heo93aae172010-12-16 17:52:17 +0100270
Kay Sievers79b96772011-06-30 15:03:48 +0200271 if (!(clearing & DISK_EVENT_MEDIA_CHANGE))
272 return events;
273do_tur:
Tejun Heo93aae172010-12-16 17:52:17 +0100274 /* let's see whether the media is there with TUR */
275 last_present = cd->media_present;
276 ret = scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES, &sshdr);
277
Tejun Heo638428e2010-12-09 11:18:42 +0100278 /*
279 * Media is considered to be present if TUR succeeds or fails with
280 * sense data indicating something other than media-not-present
281 * (ASC 0x3a).
282 */
Tejun Heo93aae172010-12-16 17:52:17 +0100283 cd->media_present = scsi_status_is_good(ret) ||
284 (scsi_sense_valid(&sshdr) && sshdr.asc != 0x3a);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Tejun Heo93aae172010-12-16 17:52:17 +0100286 if (last_present != cd->media_present)
Kay Sievers79b96772011-06-30 15:03:48 +0200287 cd->device->changed = 1;
288
Tejun Heo93aae172010-12-16 17:52:17 +0100289 if (cd->device->changed) {
290 events |= DISK_EVENT_MEDIA_CHANGE;
291 cd->device->changed = 0;
Kay Sievers79b96772011-06-30 15:03:48 +0200292 cd->tur_changed = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
Kay Sievers285e9672007-08-14 14:10:39 +0200294
Kay Sievers79b96772011-06-30 15:03:48 +0200295 if (cd->ignore_get_event)
296 return events;
297
298 /* check whether GET_EVENT is reporting spurious MEDIA_CHANGE */
299 if (!cd->tur_changed) {
300 if (cd->get_event_changed) {
301 if (cd->tur_mismatch++ > 8) {
Hannes Reinecke96eefad22014-06-25 16:39:54 +0200302 sr_printk(KERN_WARNING, cd,
303 "GET_EVENT and TUR disagree continuously, suppress GET_EVENT events\n");
Kay Sievers79b96772011-06-30 15:03:48 +0200304 cd->ignore_get_event = true;
305 }
306 } else {
307 cd->tur_mismatch = 0;
308 }
309 }
310 cd->tur_changed = false;
311 cd->get_event_changed = false;
312
Tejun Heo93aae172010-12-16 17:52:17 +0100313 return events;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314}
Tejun Heo93aae172010-12-16 17:52:17 +0100315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316/*
Linus Torvalds7b3d9542008-01-06 10:17:12 -0800317 * sr_done is the interrupt routine for the device driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 *
Linus Torvalds7b3d9542008-01-06 10:17:12 -0800319 * It will be notified on the end of a SCSI read / write, and will take one
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 * of several actions based on success or failure.
321 */
Linus Torvalds7b3d9542008-01-06 10:17:12 -0800322static int sr_done(struct scsi_cmnd *SCpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
324 int result = SCpnt->result;
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200325 int this_count = scsi_bufflen(SCpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 int good_bytes = (result == 0 ? this_count : 0);
327 int block_sectors = 0;
328 long error_sector;
329 struct scsi_cd *cd = scsi_cd(SCpnt->request->rq_disk);
330
331#ifdef DEBUG
Hannes Reinecke96eefad22014-06-25 16:39:54 +0200332 scmd_printk(KERN_INFO, SCpnt, "done: %x\n", result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333#endif
334
335 /*
336 * Handle MEDIUM ERRORs or VOLUME OVERFLOWs that indicate partial
337 * success. Since this is a relatively rare error condition, no
338 * care is taken to avoid unnecessary additional work such as
339 * memcpy's that could be avoided.
340 */
341 if (driver_byte(result) != 0 && /* An error occurred */
342 (SCpnt->sense_buffer[0] & 0x7f) == 0x70) { /* Sense current */
343 switch (SCpnt->sense_buffer[2]) {
344 case MEDIUM_ERROR:
345 case VOLUME_OVERFLOW:
346 case ILLEGAL_REQUEST:
347 if (!(SCpnt->sense_buffer[0] & 0x90))
348 break;
Bart Van Assche655da8e52020-04-26 18:48:44 -0700349 error_sector =
350 get_unaligned_be32(&SCpnt->sense_buffer[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 if (SCpnt->request->bio != NULL)
352 block_sectors =
353 bio_sectors(SCpnt->request->bio);
354 if (block_sectors < 4)
355 block_sectors = 4;
356 if (cd->device->sector_size == 2048)
357 error_sector <<= 2;
358 error_sector &= ~(block_sectors - 1);
Tejun Heo83096eb2009-05-07 22:24:39 +0900359 good_bytes = (error_sector -
360 blk_rq_pos(SCpnt->request)) << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 if (good_bytes < 0 || good_bytes >= this_count)
362 good_bytes = 0;
363 /*
364 * The SCSI specification allows for the value
365 * returned by READ CAPACITY to be up to 75 2K
366 * sectors past the last readable block.
367 * Therefore, if we hit a medium error within the
368 * last 75 2K sectors, we decrease the saved size
369 * value.
370 */
371 if (error_sector < get_capacity(cd->disk) &&
372 cd->capacity - error_sector < 4 * 75)
373 set_capacity(cd->disk, error_sector);
374 break;
375
376 case RECOVERED_ERROR:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 good_bytes = this_count;
378 break;
379
380 default:
381 break;
382 }
383 }
384
Linus Torvalds7b3d9542008-01-06 10:17:12 -0800385 return good_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
387
Christoph Hellwig159b2cb2018-11-09 14:42:39 +0100388static blk_status_t sr_init_command(struct scsi_cmnd *SCpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
Jens Axboe242f9dc2008-09-14 05:55:09 -0700390 int block = 0, this_count, s_size;
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500391 struct scsi_cd *cd;
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +0200392 struct request *rq = SCpnt->request;
Christoph Hellwig159b2cb2018-11-09 14:42:39 +0100393 blk_status_t ret;
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500394
Christoph Hellwig3c356bd2014-09-05 18:20:23 -0700395 ret = scsi_init_io(SCpnt);
Christoph Hellwig159b2cb2018-11-09 14:42:39 +0100396 if (ret != BLK_STS_OK)
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500397 goto out;
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500398 cd = scsi_cd(rq->rq_disk);
399
400 /* from here on until we're complete, any goto out
401 * is used for a killable error condition */
Christoph Hellwig159b2cb2018-11-09 14:42:39 +0100402 ret = BLK_STS_IOERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Hannes Reinecke96eefad22014-06-25 16:39:54 +0200404 SCSI_LOG_HLQUEUE(1, scmd_printk(KERN_INFO, SCpnt,
405 "Doing sr request, block = %d\n", block));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407 if (!cd->device || !scsi_device_online(cd->device)) {
Hannes Reinecke96eefad22014-06-25 16:39:54 +0200408 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
409 "Finishing %u sectors\n", blk_rq_sectors(rq)));
410 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
411 "Retry with 0x%p\n", SCpnt));
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500412 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
414
415 if (cd->device->changed) {
416 /*
417 * quietly refuse to do anything to a changed disc until the
418 * changed bit has been reset
419 */
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500420 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 }
422
423 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 * we do lazy blocksize switching (when reading XA sectors,
425 * see CDROMREADMODE2 ioctl)
426 */
427 s_size = cd->device->sector_size;
428 if (s_size > 2048) {
429 if (!in_interrupt())
430 sr_set_blocklength(cd, 2048);
431 else
Hannes Reinecke96eefad22014-06-25 16:39:54 +0200432 scmd_printk(KERN_INFO, SCpnt,
433 "can't switch blocksize: in interrupt\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 }
435
436 if (s_size != 512 && s_size != 1024 && s_size != 2048) {
Jeff Garzik3bf743e2005-10-24 18:04:06 -0400437 scmd_printk(KERN_ERR, SCpnt, "bad sector size %d\n", s_size);
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500438 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 }
440
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100441 switch (req_op(rq)) {
442 case REQ_OP_WRITE:
Christoph Hellwigfd2eb902014-07-18 16:59:19 +0200443 if (!cd->writeable)
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500444 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 SCpnt->cmnd[0] = WRITE_10;
Hannes Reinecke96eefad22014-06-25 16:39:54 +0200446 cd->cdi.media_written = 1;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100447 break;
448 case REQ_OP_READ:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 SCpnt->cmnd[0] = READ_10;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100450 break;
451 default:
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500452 blk_dump_rq_flags(rq, "Unknown sr command");
453 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 }
455
456 {
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200457 struct scatterlist *sg;
458 int i, size = 0, sg_count = scsi_sg_count(SCpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200460 scsi_for_each_sg(SCpnt, sg, sg_count, i)
461 size += sg->length;
462
463 if (size != scsi_bufflen(SCpnt)) {
Jeff Garzik3bf743e2005-10-24 18:04:06 -0400464 scmd_printk(KERN_ERR, SCpnt,
465 "mismatch count %d, bytes %d\n",
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200466 size, scsi_bufflen(SCpnt));
467 if (scsi_bufflen(SCpnt) > size)
468 SCpnt->sdb.length = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 }
470 }
471
472 /*
473 * request doesn't start on hw block boundary, add scatter pads
474 */
Tejun Heo83096eb2009-05-07 22:24:39 +0900475 if (((unsigned int)blk_rq_pos(rq) % (s_size >> 9)) ||
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200476 (scsi_bufflen(SCpnt) % s_size)) {
Jeff Garzik3bf743e2005-10-24 18:04:06 -0400477 scmd_printk(KERN_NOTICE, SCpnt, "unaligned transfer\n");
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500478 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 }
480
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200481 this_count = (scsi_bufflen(SCpnt) >> 9) / (s_size >> 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483
Hannes Reinecke96eefad22014-06-25 16:39:54 +0200484 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
485 "%s %d/%u 512 byte blocks.\n",
486 (rq_data_dir(rq) == WRITE) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 "writing" : "reading",
Hannes Reinecke96eefad22014-06-25 16:39:54 +0200488 this_count, blk_rq_sectors(rq)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490 SCpnt->cmnd[1] = 0;
Tejun Heo83096eb2009-05-07 22:24:39 +0900491 block = (unsigned int)blk_rq_pos(rq) / (s_size >> 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493 if (this_count > 0xffff) {
494 this_count = 0xffff;
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200495 SCpnt->sdb.length = this_count * s_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 }
497
Bart Van Assche655da8e52020-04-26 18:48:44 -0700498 put_unaligned_be32(block, &SCpnt->cmnd[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
Bart Van Assche655da8e52020-04-26 18:48:44 -0700500 put_unaligned_be16(this_count, &SCpnt->cmnd[7]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 /*
503 * We shouldn't disconnect in the middle of a sector, so with a dumb
504 * host adapter, it's safe to assume that we can at least transfer
505 * this many bytes between each connect / disconnect.
506 */
507 SCpnt->transfersize = cd->device->sector_size;
508 SCpnt->underflow = this_count << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 SCpnt->allowed = MAX_RETRIES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 * This indicates that the command is ready from our end to be
513 * queued.
514 */
Christoph Hellwig159b2cb2018-11-09 14:42:39 +0100515 ret = BLK_STS_OK;
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500516 out:
Christoph Hellwiga1b73fc2014-05-01 16:51:04 +0200517 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518}
519
Al Viro40cc51b2008-03-02 10:41:28 -0500520static int sr_block_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200522 struct scsi_cd *cd;
Bart Van Assche1214fd72018-08-02 10:44:42 -0700523 struct scsi_device *sdev;
Al Viro40cc51b2008-03-02 10:41:28 -0500524 int ret = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Bart Van Assche1214fd72018-08-02 10:44:42 -0700526 cd = scsi_cd_get(bdev->bd_disk);
527 if (!cd)
528 goto out;
529
530 sdev = cd->device;
531 scsi_autopm_get_device(sdev);
Maurizio Lombardi2bbea6e2018-03-09 13:59:06 +0100532 check_disk_change(bdev);
533
Merlijn Wajer51a85882020-02-18 15:39:17 +0100534 mutex_lock(&cd->lock);
Bart Van Assche1214fd72018-08-02 10:44:42 -0700535 ret = cdrom_open(&cd->cdi, bdev, mode);
Merlijn Wajer51a85882020-02-18 15:39:17 +0100536 mutex_unlock(&cd->lock);
Bart Van Assche1214fd72018-08-02 10:44:42 -0700537
538 scsi_autopm_put_device(sdev);
539 if (ret)
540 scsi_cd_put(cd);
541
542out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 return ret;
544}
545
Al Virodb2a1442013-05-05 21:52:57 -0400546static void sr_block_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
Al Viro40cc51b2008-03-02 10:41:28 -0500548 struct scsi_cd *cd = scsi_cd(disk);
Bart Van Assche72655c02020-03-29 19:51:51 -0700549
Merlijn Wajer51a85882020-02-18 15:39:17 +0100550 mutex_lock(&cd->lock);
Al Viro40cc51b2008-03-02 10:41:28 -0500551 cdrom_release(&cd->cdi, mode);
Merlijn Wajer51a85882020-02-18 15:39:17 +0100552 mutex_unlock(&cd->lock);
Bart Van Assche72655c02020-03-29 19:51:51 -0700553
554 scsi_cd_put(cd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555}
556
Al Viro40cc51b2008-03-02 10:41:28 -0500557static int sr_block_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 unsigned long arg)
559{
Al Viro40cc51b2008-03-02 10:41:28 -0500560 struct scsi_cd *cd = scsi_cd(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 struct scsi_device *sdev = cd->device;
Christoph Hellwig6a2900b2006-03-23 03:00:15 -0800562 void __user *argp = (void __user *)arg;
563 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Merlijn Wajer51a85882020-02-18 15:39:17 +0100565 mutex_lock(&cd->lock);
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200566
Christoph Hellwig906d15f2014-10-11 16:25:31 +0200567 ret = scsi_ioctl_block_when_processing_errors(sdev, cmd,
568 (mode & FMODE_NDELAY) != 0);
569 if (ret)
570 goto out;
571
Bart Van Assche1214fd72018-08-02 10:44:42 -0700572 scsi_autopm_get_device(sdev);
573
Christoph Hellwig6a2900b2006-03-23 03:00:15 -0800574 /*
575 * Send SCSI addressing ioctls directly to mid level, send other
576 * ioctls to cdrom/block level.
577 */
578 switch (cmd) {
579 case SCSI_IOCTL_GET_IDLUN:
580 case SCSI_IOCTL_GET_BUS_NUMBER:
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200581 ret = scsi_ioctl(sdev, cmd, argp);
Bart Van Assche1214fd72018-08-02 10:44:42 -0700582 goto put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 }
Christoph Hellwig6a2900b2006-03-23 03:00:15 -0800584
Al Viro40cc51b2008-03-02 10:41:28 -0500585 ret = cdrom_ioctl(&cd->cdi, bdev, mode, cmd, arg);
Tejun Heo63972562007-01-02 17:41:04 +0900586 if (ret != -ENOSYS)
Bart Van Assche1214fd72018-08-02 10:44:42 -0700587 goto put;
Christoph Hellwig6a2900b2006-03-23 03:00:15 -0800588
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200589 ret = scsi_ioctl(sdev, cmd, argp);
590
Bart Van Assche1214fd72018-08-02 10:44:42 -0700591put:
592 scsi_autopm_put_device(sdev);
593
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200594out:
Merlijn Wajer51a85882020-02-18 15:39:17 +0100595 mutex_unlock(&cd->lock);
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200596 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597}
598
Arnd Bergmannd320a952019-03-15 17:39:44 +0100599#ifdef CONFIG_COMPAT
600static int sr_block_compat_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,
601 unsigned long arg)
602{
603 struct scsi_cd *cd = scsi_cd(bdev->bd_disk);
604 struct scsi_device *sdev = cd->device;
605 void __user *argp = compat_ptr(arg);
606 int ret;
607
Merlijn Wajer51a85882020-02-18 15:39:17 +0100608 mutex_lock(&cd->lock);
Arnd Bergmannd320a952019-03-15 17:39:44 +0100609
610 ret = scsi_ioctl_block_when_processing_errors(sdev, cmd,
611 (mode & FMODE_NDELAY) != 0);
612 if (ret)
613 goto out;
614
615 scsi_autopm_get_device(sdev);
616
617 /*
618 * Send SCSI addressing ioctls directly to mid level, send other
619 * ioctls to cdrom/block level.
620 */
621 switch (cmd) {
622 case SCSI_IOCTL_GET_IDLUN:
623 case SCSI_IOCTL_GET_BUS_NUMBER:
624 ret = scsi_compat_ioctl(sdev, cmd, argp);
625 goto put;
626 }
627
Arnd Bergmann64cbfa92019-11-28 15:55:17 +0100628 ret = cdrom_ioctl(&cd->cdi, bdev, mode, cmd, (unsigned long)argp);
629 if (ret != -ENOSYS)
Arnd Bergmannd320a952019-03-15 17:39:44 +0100630 goto put;
631
632 ret = scsi_compat_ioctl(sdev, cmd, argp);
633
634put:
635 scsi_autopm_put_device(sdev);
636
637out:
Merlijn Wajer51a85882020-02-18 15:39:17 +0100638 mutex_unlock(&cd->lock);
Arnd Bergmannd320a952019-03-15 17:39:44 +0100639 return ret;
640
641}
642#endif
643
Tejun Heo93aae172010-12-16 17:52:17 +0100644static unsigned int sr_block_check_events(struct gendisk *disk,
645 unsigned int clearing)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
Jens Axboe2d097c52018-04-11 11:26:09 -0600647 unsigned int ret = 0;
648 struct scsi_cd *cd;
Aaron Lu6c7f1e22013-01-23 15:09:31 +0800649
Jens Axboe2d097c52018-04-11 11:26:09 -0600650 cd = scsi_cd_get(disk);
651 if (!cd)
Aaron Lu6627b382013-10-28 15:27:49 +0800652 return 0;
Aaron Lu6c7f1e22013-01-23 15:09:31 +0800653
Jens Axboe2d097c52018-04-11 11:26:09 -0600654 if (!atomic_read(&cd->device->disk_events_disable_depth))
655 ret = cdrom_check_events(&cd->cdi, clearing);
656
657 scsi_cd_put(cd);
658 return ret;
Tejun Heo93aae172010-12-16 17:52:17 +0100659}
660
661static int sr_block_revalidate_disk(struct gendisk *disk)
662{
Tejun Heo93aae172010-12-16 17:52:17 +0100663 struct scsi_sense_hdr sshdr;
Jens Axboe2d097c52018-04-11 11:26:09 -0600664 struct scsi_cd *cd;
665
666 cd = scsi_cd_get(disk);
667 if (!cd)
668 return -ENXIO;
Tejun Heo93aae172010-12-16 17:52:17 +0100669
670 /* if the unit is not ready, nothing more to do */
671 if (scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES, &sshdr))
Aaron Lu6c7f1e22013-01-23 15:09:31 +0800672 goto out;
Tejun Heo93aae172010-12-16 17:52:17 +0100673
674 sr_cd_check(&cd->cdi);
675 get_sectorsize(cd);
Aaron Lu6c7f1e22013-01-23 15:09:31 +0800676out:
Jens Axboe2d097c52018-04-11 11:26:09 -0600677 scsi_cd_put(cd);
Tejun Heo93aae172010-12-16 17:52:17 +0100678 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679}
680
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700681static const struct block_device_operations sr_bdops =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
683 .owner = THIS_MODULE,
Al Viro40cc51b2008-03-02 10:41:28 -0500684 .open = sr_block_open,
685 .release = sr_block_release,
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200686 .ioctl = sr_block_ioctl,
Arnd Bergmannd320a952019-03-15 17:39:44 +0100687#ifdef CONFIG_COMPAT
Adam Williamson03264dd2020-02-19 17:50:07 +0100688 .compat_ioctl = sr_block_compat_ioctl,
Arnd Bergmannd320a952019-03-15 17:39:44 +0100689#endif
Tejun Heo93aae172010-12-16 17:52:17 +0100690 .check_events = sr_block_check_events,
691 .revalidate_disk = sr_block_revalidate_disk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692};
693
694static int sr_open(struct cdrom_device_info *cdi, int purpose)
695{
696 struct scsi_cd *cd = cdi->handle;
697 struct scsi_device *sdev = cd->device;
698 int retval;
699
700 /*
701 * If the device is in error recovery, wait until it is done.
702 * If the device is offline, then disallow any access to it.
703 */
704 retval = -ENXIO;
705 if (!scsi_block_when_processing_errors(sdev))
706 goto error_out;
707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 return 0;
709
710error_out:
711 return retval;
712}
713
714static void sr_release(struct cdrom_device_info *cdi)
715{
716 struct scsi_cd *cd = cdi->handle;
717
718 if (cd->device->sector_size > 2048)
719 sr_set_blocklength(cd, 2048);
720
721}
722
723static int sr_probe(struct device *dev)
724{
725 struct scsi_device *sdev = to_scsi_device(dev);
726 struct gendisk *disk;
727 struct scsi_cd *cd;
728 int minor, error;
729
Subhash Jadavani6fe8c1d2014-09-10 14:54:09 +0300730 scsi_autopm_get_device(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 error = -ENODEV;
732 if (sdev->type != TYPE_ROM && sdev->type != TYPE_WORM)
733 goto fail;
734
735 error = -ENOMEM;
Jes Sorensen24669f752006-01-16 10:31:18 -0500736 cd = kzalloc(sizeof(*cd), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 if (!cd)
738 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
740 kref_init(&cd->kref);
741
742 disk = alloc_disk(1);
743 if (!disk)
744 goto fail_free;
Merlijn Wajer51a85882020-02-18 15:39:17 +0100745 mutex_init(&cd->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747 spin_lock(&sr_index_lock);
748 minor = find_first_zero_bit(sr_index_bits, SR_DISKS);
749 if (minor == SR_DISKS) {
750 spin_unlock(&sr_index_lock);
751 error = -EBUSY;
752 goto fail_put;
753 }
754 __set_bit(minor, sr_index_bits);
755 spin_unlock(&sr_index_lock);
756
757 disk->major = SCSI_CDROM_MAJOR;
758 disk->first_minor = minor;
759 sprintf(disk->disk_name, "sr%d", minor);
760 disk->fops = &sr_bdops;
Tejun Heod4dc2102011-04-21 20:54:46 +0200761 disk->flags = GENHD_FL_CD | GENHD_FL_BLOCK_EVENTS_ON_EXCL_WRITE;
Tejun Heo93aae172010-12-16 17:52:17 +0100762 disk->events = DISK_EVENT_MEDIA_CHANGE | DISK_EVENT_EJECT_REQUEST;
Martin Wilckc92e2f02019-03-27 14:51:02 +0100763 disk->event_flags = DISK_EVENT_FLAG_POLL | DISK_EVENT_FLAG_UEVENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Jens Axboe242f9dc2008-09-14 05:55:09 -0700765 blk_queue_rq_timeout(sdev->request_queue, SR_TIMEOUT);
766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 cd->device = sdev;
768 cd->disk = disk;
769 cd->driver = &sr_template;
770 cd->disk = disk;
771 cd->capacity = 0x1fffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 cd->device->changed = 1; /* force recheck CD type */
Tejun Heo93aae172010-12-16 17:52:17 +0100773 cd->media_present = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 cd->use = 1;
775 cd->readcd_known = 0;
776 cd->readcd_cdda = 0;
777
778 cd->cdi.ops = &sr_dops;
779 cd->cdi.handle = cd;
780 cd->cdi.mask = 0;
781 cd->cdi.capacity = 1;
782 sprintf(cd->cdi.name, "sr%d", minor);
783
784 sdev->sector_size = 2048; /* A guess, just in case */
785
786 /* FIXME: need to handle a get_capabilities failure properly ?? */
787 get_capabilities(cd);
788 sr_vendor_init(cd);
789
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 set_capacity(disk, cd->capacity);
791 disk->private_data = &cd->driver;
792 disk->queue = sdev->request_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
Christoph Hellwiga711d912020-04-25 09:57:00 +0200794 if (register_cdrom(disk, &cd->cdi))
Simon Arlott65557812020-05-30 18:59:44 +0100795 goto fail_minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Aaron Lu6627b382013-10-28 15:27:49 +0800797 /*
798 * Initialize block layer runtime PM stuffs before the
799 * periodic event checking request gets started in add_disk.
800 */
801 blk_pm_runtime_init(sdev->request_queue, dev);
802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 dev_set_drvdata(dev, cd);
804 disk->flags |= GENHD_FL_REMOVABLE;
Hannes Reineckefef912b2018-09-28 08:17:19 +0200805 device_add_disk(&sdev->sdev_gendev, disk, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
James Bottomley9ccfc752005-10-02 11:45:08 -0500807 sdev_printk(KERN_DEBUG, sdev,
808 "Attached scsi CD-ROM %s\n", cd->cdi.name);
Aaron Lu6c7f1e22013-01-23 15:09:31 +0800809 scsi_autopm_put_device(cd->device);
810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 return 0;
812
Simon Arlott65557812020-05-30 18:59:44 +0100813fail_minor:
814 spin_lock(&sr_index_lock);
815 clear_bit(minor, sr_index_bits);
816 spin_unlock(&sr_index_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817fail_put:
818 put_disk(disk);
Simon Arlotta247e072020-05-30 18:58:25 +0100819 mutex_destroy(&cd->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820fail_free:
821 kfree(cd);
822fail:
Subhash Jadavani6fe8c1d2014-09-10 14:54:09 +0300823 scsi_autopm_put_device(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 return error;
825}
826
827
828static void get_sectorsize(struct scsi_cd *cd)
829{
830 unsigned char cmd[10];
FUJITA Tomonori62858da2008-07-04 09:31:50 +0200831 unsigned char buffer[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 int the_result, retries = 3;
833 int sector_size;
Jens Axboe165125e2007-07-24 09:28:11 +0200834 struct request_queue *queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 do {
837 cmd[0] = READ_CAPACITY;
838 memset((void *) &cmd[1], 0, 9);
FUJITA Tomonori62858da2008-07-04 09:31:50 +0200839 memset(buffer, 0, sizeof(buffer));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
841 /* Do the command and wait.. */
James Bottomley820732b2005-06-12 22:21:29 -0500842 the_result = scsi_execute_req(cd->device, cmd, DMA_FROM_DEVICE,
FUJITA Tomonori62858da2008-07-04 09:31:50 +0200843 buffer, sizeof(buffer), NULL,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +0900844 SR_TIMEOUT, MAX_RETRIES, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 retries--;
847
848 } while (the_result && retries);
849
850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 if (the_result) {
852 cd->capacity = 0x1fffff;
853 sector_size = 2048; /* A guess, just in case */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 } else {
Tejun Heo59151362009-09-04 11:38:02 +0900855 long last_written;
856
Bart Van Assche655da8e52020-04-26 18:48:44 -0700857 cd->capacity = 1 + get_unaligned_be32(&buffer[0]);
Tejun Heo59151362009-09-04 11:38:02 +0900858 /*
859 * READ_CAPACITY doesn't return the correct size on
860 * certain UDF media. If last_written is larger, use
861 * it instead.
862 *
863 * http://bugzilla.kernel.org/show_bug.cgi?id=9668
864 */
865 if (!cdrom_get_last_written(&cd->cdi, &last_written))
866 cd->capacity = max_t(long, cd->capacity, last_written);
867
Bart Van Assche655da8e52020-04-26 18:48:44 -0700868 sector_size = get_unaligned_be32(&buffer[4]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 switch (sector_size) {
870 /*
871 * HP 4020i CD-Recorder reports 2340 byte sectors
872 * Philips CD-Writers report 2352 byte sectors
873 *
874 * Use 2k sectors for them..
875 */
876 case 0:
877 case 2340:
878 case 2352:
879 sector_size = 2048;
880 /* fall through */
881 case 2048:
882 cd->capacity *= 4;
883 /* fall through */
884 case 512:
885 break;
886 default:
Hannes Reinecke96eefad22014-06-25 16:39:54 +0200887 sr_printk(KERN_INFO, cd,
888 "unsupported sector size %d.", sector_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 cd->capacity = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 }
891
892 cd->device->sector_size = sector_size;
893
894 /*
895 * Add this so that we have the ability to correctly gauge
896 * what the device is capable of.
897 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 set_capacity(cd->disk, cd->capacity);
899 }
900
901 queue = cd->device->request_queue;
Martin K. Petersene1defc42009-05-22 17:17:49 -0400902 blk_queue_logical_block_size(queue, sector_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
FUJITA Tomonori62858da2008-07-04 09:31:50 +0200904 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905}
906
907static void get_capabilities(struct scsi_cd *cd)
908{
909 unsigned char *buffer;
910 struct scsi_mode_data data;
James Bottomley820732b2005-06-12 22:21:29 -0500911 struct scsi_sense_hdr sshdr;
Martin K. Petersena00a7862017-03-17 08:47:14 -0400912 unsigned int ms_len = 128;
James Bottomley38582a62008-02-06 13:01:58 -0600913 int rc, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Arjan van de Ven0ad78202005-11-28 16:22:25 +0100915 static const char *loadmech[] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 {
917 "caddy",
918 "tray",
919 "pop-up",
920 "",
921 "changer",
922 "cartridge changer",
923 "",
924 ""
925 };
926
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
928 /* allocate transfer buffer */
929 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
930 if (!buffer) {
Hannes Reinecke96eefad22014-06-25 16:39:54 +0200931 sr_printk(KERN_ERR, cd, "out of memory.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 return;
933 }
934
James Bottomley38582a62008-02-06 13:01:58 -0600935 /* eat unit attentions */
Tejun Heo9f8a2c22010-12-08 20:57:40 +0100936 scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES, &sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
938 /* ask for mode page 0x2a */
Martin K. Petersena00a7862017-03-17 08:47:14 -0400939 rc = scsi_mode_sense(cd->device, 0, 0x2a, buffer, ms_len,
James Bottomley1cf72692005-08-28 11:27:01 -0500940 SR_TIMEOUT, 3, &data, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Martin K. Petersena00a7862017-03-17 08:47:14 -0400942 if (!scsi_status_is_good(rc) || data.length > ms_len ||
943 data.header_length + data.block_descriptor_length > data.length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 /* failed, drive doesn't have capabilities mode page */
945 cd->cdi.speed = 1;
946 cd->cdi.mask |= (CDC_CD_R | CDC_CD_RW | CDC_DVD_R |
Chuck Ebbertbcc1e382006-01-11 23:58:40 -0500947 CDC_DVD | CDC_DVD_RAM |
948 CDC_SELECT_DISC | CDC_SELECT_SPEED |
949 CDC_MRW | CDC_MRW_W | CDC_RAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 kfree(buffer);
Hannes Reinecke96eefad22014-06-25 16:39:54 +0200951 sr_printk(KERN_INFO, cd, "scsi-1 drive");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 return;
953 }
954
955 n = data.header_length + data.block_descriptor_length;
Bart Van Assche655da8e52020-04-26 18:48:44 -0700956 cd->cdi.speed = get_unaligned_be16(&buffer[n + 8]) / 176;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 cd->readcd_known = 1;
958 cd->readcd_cdda = buffer[n + 5] & 0x01;
959 /* print some capability bits */
Hannes Reinecke96eefad22014-06-25 16:39:54 +0200960 sr_printk(KERN_INFO, cd,
961 "scsi3-mmc drive: %dx/%dx %s%s%s%s%s%s\n",
Bart Van Assche655da8e52020-04-26 18:48:44 -0700962 get_unaligned_be16(&buffer[n + 14]) / 176,
Hannes Reinecke96eefad22014-06-25 16:39:54 +0200963 cd->cdi.speed,
964 buffer[n + 3] & 0x01 ? "writer " : "", /* CD Writer */
965 buffer[n + 3] & 0x20 ? "dvd-ram " : "",
966 buffer[n + 2] & 0x02 ? "cd/rw " : "", /* can read rewriteable */
967 buffer[n + 4] & 0x20 ? "xa/form2 " : "", /* can read xa/from2 */
968 buffer[n + 5] & 0x01 ? "cdda " : "", /* can read audio data */
969 loadmech[buffer[n + 6] >> 5]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 if ((buffer[n + 6] >> 5) == 0)
971 /* caddy drives can't close tray... */
972 cd->cdi.mask |= CDC_CLOSE_TRAY;
973 if ((buffer[n + 2] & 0x8) == 0)
974 /* not a DVD drive */
975 cd->cdi.mask |= CDC_DVD;
Hannes Reinecke96eefad22014-06-25 16:39:54 +0200976 if ((buffer[n + 3] & 0x20) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 /* can't write DVD-RAM media */
978 cd->cdi.mask |= CDC_DVD_RAM;
979 if ((buffer[n + 3] & 0x10) == 0)
980 /* can't write DVD-R media */
981 cd->cdi.mask |= CDC_DVD_R;
982 if ((buffer[n + 3] & 0x2) == 0)
983 /* can't write CD-RW media */
984 cd->cdi.mask |= CDC_CD_RW;
985 if ((buffer[n + 3] & 0x1) == 0)
986 /* can't write CD-R media */
987 cd->cdi.mask |= CDC_CD_R;
988 if ((buffer[n + 6] & 0x8) == 0)
989 /* can't eject */
990 cd->cdi.mask |= CDC_OPEN_TRAY;
991
992 if ((buffer[n + 6] >> 5) == mechtype_individual_changer ||
993 (buffer[n + 6] >> 5) == mechtype_cartridge_changer)
994 cd->cdi.capacity =
995 cdrom_number_of_slots(&cd->cdi);
996 if (cd->cdi.capacity <= 1)
997 /* not a changer */
998 cd->cdi.mask |= CDC_SELECT_DISC;
999 /*else I don't think it can close its tray
1000 cd->cdi.mask |= CDC_CLOSE_TRAY; */
1001
1002 /*
1003 * if DVD-RAM, MRW-W or CD-RW, we are randomly writable
1004 */
1005 if ((cd->cdi.mask & (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) !=
1006 (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) {
Christoph Hellwigfd2eb902014-07-18 16:59:19 +02001007 cd->writeable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 }
1009
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 kfree(buffer);
1011}
1012
1013/*
1014 * sr_packet() is the entry point for the generic commands generated
Hannes Reinecke96eefad22014-06-25 16:39:54 +02001015 * by the Uniform CD-ROM layer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 */
1017static int sr_packet(struct cdrom_device_info *cdi,
1018 struct packet_command *cgc)
1019{
Hans de Goede8e04d802010-10-01 14:20:08 -07001020 struct scsi_cd *cd = cdi->handle;
1021 struct scsi_device *sdev = cd->device;
1022
1023 if (cgc->cmd[0] == GPCMD_READ_DISC_INFO && sdev->no_read_disc_info)
1024 return -EDRIVE_CANT_DO_THIS;
1025
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 if (cgc->timeout <= 0)
1027 cgc->timeout = IOCTL_TIMEOUT;
1028
Hans de Goede8e04d802010-10-01 14:20:08 -07001029 sr_do_ioctl(cd, cgc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
1031 return cgc->stat;
1032}
1033
1034/**
1035 * sr_kref_release - Called to free the scsi_cd structure
1036 * @kref: pointer to embedded kref
1037 *
Arjan van de Ven0b950672006-01-11 13:16:10 +01001038 * sr_ref_mutex must be held entering this routine. Because it is
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 * called on last put, you should always use the scsi_cd_get()
1040 * scsi_cd_put() helpers which manipulate the semaphore directly
1041 * and never do a direct kref_put().
1042 **/
1043static void sr_kref_release(struct kref *kref)
1044{
1045 struct scsi_cd *cd = container_of(kref, struct scsi_cd, kref);
1046 struct gendisk *disk = cd->disk;
1047
1048 spin_lock(&sr_index_lock);
Tejun Heof331c022008-09-03 09:01:48 +02001049 clear_bit(MINOR(disk_devt(disk)), sr_index_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 spin_unlock(&sr_index_lock);
1051
1052 unregister_cdrom(&cd->cdi);
1053
1054 disk->private_data = NULL;
1055
1056 put_disk(disk);
1057
Merlijn Wajer51a85882020-02-18 15:39:17 +01001058 mutex_destroy(&cd->lock);
1059
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 kfree(cd);
1061}
1062
1063static int sr_remove(struct device *dev)
1064{
1065 struct scsi_cd *cd = dev_get_drvdata(dev);
1066
Aaron Lu6c7f1e22013-01-23 15:09:31 +08001067 scsi_autopm_get_device(cd->device);
1068
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 del_gendisk(cd->disk);
Alan Stern13b43892016-01-20 11:26:01 -05001070 dev_set_drvdata(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
Arjan van de Ven0b950672006-01-11 13:16:10 +01001072 mutex_lock(&sr_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 kref_put(&cd->kref, sr_kref_release);
Arjan van de Ven0b950672006-01-11 13:16:10 +01001074 mutex_unlock(&sr_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
1076 return 0;
1077}
1078
1079static int __init init_sr(void)
1080{
1081 int rc;
1082
1083 rc = register_blkdev(SCSI_CDROM_MAJOR, "sr");
1084 if (rc)
1085 return rc;
Akinobu Mitada3962f2007-06-26 00:39:33 +09001086 rc = scsi_register_driver(&sr_template.gendrv);
1087 if (rc)
1088 unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
1089
1090 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091}
1092
1093static void __exit exit_sr(void)
1094{
1095 scsi_unregister_driver(&sr_template.gendrv);
1096 unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
1097}
1098
1099module_init(init_sr);
1100module_exit(exit_sr);
1101MODULE_LICENSE("GPL");