blob: fd4b582110b29f3bb9ad0a6a556813e025495c36 [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 Hellwig7007e9d2020-10-05 10:41:28 +0200395 ret = scsi_alloc_sgtables(SCpnt);
Christoph Hellwig159b2cb2018-11-09 14:42:39 +0100396 if (ret != BLK_STS_OK)
Christoph Hellwig7007e9d2020-10-05 10:41:28 +0200397 return ret;
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500398 cd = scsi_cd(rq->rq_disk);
399
Hannes Reinecke96eefad22014-06-25 16:39:54 +0200400 SCSI_LOG_HLQUEUE(1, scmd_printk(KERN_INFO, SCpnt,
401 "Doing sr request, block = %d\n", block));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 if (!cd->device || !scsi_device_online(cd->device)) {
Hannes Reinecke96eefad22014-06-25 16:39:54 +0200404 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
405 "Finishing %u sectors\n", blk_rq_sectors(rq)));
406 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
407 "Retry with 0x%p\n", SCpnt));
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500408 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 }
410
411 if (cd->device->changed) {
412 /*
413 * quietly refuse to do anything to a changed disc until the
414 * changed bit has been reset
415 */
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500416 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 }
418
419 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 * we do lazy blocksize switching (when reading XA sectors,
421 * see CDROMREADMODE2 ioctl)
422 */
423 s_size = cd->device->sector_size;
424 if (s_size > 2048) {
425 if (!in_interrupt())
426 sr_set_blocklength(cd, 2048);
427 else
Hannes Reinecke96eefad22014-06-25 16:39:54 +0200428 scmd_printk(KERN_INFO, SCpnt,
429 "can't switch blocksize: in interrupt\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 }
431
432 if (s_size != 512 && s_size != 1024 && s_size != 2048) {
Jeff Garzik3bf743e2005-10-24 18:04:06 -0400433 scmd_printk(KERN_ERR, SCpnt, "bad sector size %d\n", s_size);
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500434 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 }
436
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100437 switch (req_op(rq)) {
438 case REQ_OP_WRITE:
Christoph Hellwigfd2eb902014-07-18 16:59:19 +0200439 if (!cd->writeable)
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500440 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 SCpnt->cmnd[0] = WRITE_10;
Hannes Reinecke96eefad22014-06-25 16:39:54 +0200442 cd->cdi.media_written = 1;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100443 break;
444 case REQ_OP_READ:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 SCpnt->cmnd[0] = READ_10;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100446 break;
447 default:
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500448 blk_dump_rq_flags(rq, "Unknown sr command");
449 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 }
451
452 {
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200453 struct scatterlist *sg;
454 int i, size = 0, sg_count = scsi_sg_count(SCpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200456 scsi_for_each_sg(SCpnt, sg, sg_count, i)
457 size += sg->length;
458
459 if (size != scsi_bufflen(SCpnt)) {
Jeff Garzik3bf743e2005-10-24 18:04:06 -0400460 scmd_printk(KERN_ERR, SCpnt,
461 "mismatch count %d, bytes %d\n",
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200462 size, scsi_bufflen(SCpnt));
463 if (scsi_bufflen(SCpnt) > size)
464 SCpnt->sdb.length = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 }
466 }
467
468 /*
469 * request doesn't start on hw block boundary, add scatter pads
470 */
Tejun Heo83096eb2009-05-07 22:24:39 +0900471 if (((unsigned int)blk_rq_pos(rq) % (s_size >> 9)) ||
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200472 (scsi_bufflen(SCpnt) % s_size)) {
Jeff Garzik3bf743e2005-10-24 18:04:06 -0400473 scmd_printk(KERN_NOTICE, SCpnt, "unaligned transfer\n");
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500474 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 }
476
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200477 this_count = (scsi_bufflen(SCpnt) >> 9) / (s_size >> 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
479
Hannes Reinecke96eefad22014-06-25 16:39:54 +0200480 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
481 "%s %d/%u 512 byte blocks.\n",
482 (rq_data_dir(rq) == WRITE) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 "writing" : "reading",
Hannes Reinecke96eefad22014-06-25 16:39:54 +0200484 this_count, blk_rq_sectors(rq)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 SCpnt->cmnd[1] = 0;
Tejun Heo83096eb2009-05-07 22:24:39 +0900487 block = (unsigned int)blk_rq_pos(rq) / (s_size >> 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 if (this_count > 0xffff) {
490 this_count = 0xffff;
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200491 SCpnt->sdb.length = this_count * s_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 }
493
Bart Van Assche655da8e52020-04-26 18:48:44 -0700494 put_unaligned_be32(block, &SCpnt->cmnd[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
Bart Van Assche655da8e52020-04-26 18:48:44 -0700496 put_unaligned_be16(this_count, &SCpnt->cmnd[7]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 /*
499 * We shouldn't disconnect in the middle of a sector, so with a dumb
500 * host adapter, it's safe to assume that we can at least transfer
501 * this many bytes between each connect / disconnect.
502 */
503 SCpnt->transfersize = cd->device->sector_size;
504 SCpnt->underflow = this_count << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 SCpnt->allowed = MAX_RETRIES;
Christoph Hellwig9120ac52020-10-08 22:06:10 +0200506 SCpnt->cmd_len = 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
508 /*
Christoph Hellwig7007e9d2020-10-05 10:41:28 +0200509 * This indicates that the command is ready from our end to be queued.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 */
Christoph Hellwig7007e9d2020-10-05 10:41:28 +0200511 return BLK_STS_OK;
James Bottomley7f9a6bc2007-08-04 10:06:25 -0500512 out:
Christoph Hellwig7007e9d2020-10-05 10:41:28 +0200513 scsi_free_sgtables(SCpnt);
514 return BLK_STS_IOERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515}
516
Christoph Hellwig38a2b552020-09-08 16:53:46 +0200517static void sr_revalidate_disk(struct scsi_cd *cd)
518{
519 struct scsi_sense_hdr sshdr;
520
521 /* if the unit is not ready, nothing more to do */
522 if (scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES, &sshdr))
523 return;
524 sr_cd_check(&cd->cdi);
525 get_sectorsize(cd);
526}
527
Al Viro40cc51b2008-03-02 10:41:28 -0500528static int sr_block_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200530 struct scsi_cd *cd;
Bart Van Assche1214fd72018-08-02 10:44:42 -0700531 struct scsi_device *sdev;
Al Viro40cc51b2008-03-02 10:41:28 -0500532 int ret = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Bart Van Assche1214fd72018-08-02 10:44:42 -0700534 cd = scsi_cd_get(bdev->bd_disk);
535 if (!cd)
536 goto out;
537
538 sdev = cd->device;
539 scsi_autopm_get_device(sdev);
Christoph Hellwigafd35c42020-09-08 16:53:45 +0200540 if (bdev_check_media_change(bdev))
Christoph Hellwig38a2b552020-09-08 16:53:46 +0200541 sr_revalidate_disk(cd);
Maurizio Lombardi2bbea6e2018-03-09 13:59:06 +0100542
Merlijn Wajer51a85882020-02-18 15:39:17 +0100543 mutex_lock(&cd->lock);
Bart Van Assche1214fd72018-08-02 10:44:42 -0700544 ret = cdrom_open(&cd->cdi, bdev, mode);
Merlijn Wajer51a85882020-02-18 15:39:17 +0100545 mutex_unlock(&cd->lock);
Bart Van Assche1214fd72018-08-02 10:44:42 -0700546
547 scsi_autopm_put_device(sdev);
548 if (ret)
549 scsi_cd_put(cd);
550
551out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 return ret;
553}
554
Al Virodb2a1442013-05-05 21:52:57 -0400555static void sr_block_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
Al Viro40cc51b2008-03-02 10:41:28 -0500557 struct scsi_cd *cd = scsi_cd(disk);
Bart Van Assche72655c02020-03-29 19:51:51 -0700558
Merlijn Wajer51a85882020-02-18 15:39:17 +0100559 mutex_lock(&cd->lock);
Al Viro40cc51b2008-03-02 10:41:28 -0500560 cdrom_release(&cd->cdi, mode);
Merlijn Wajer51a85882020-02-18 15:39:17 +0100561 mutex_unlock(&cd->lock);
Bart Van Assche72655c02020-03-29 19:51:51 -0700562
563 scsi_cd_put(cd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564}
565
Al Viro40cc51b2008-03-02 10:41:28 -0500566static int sr_block_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 unsigned long arg)
568{
Al Viro40cc51b2008-03-02 10:41:28 -0500569 struct scsi_cd *cd = scsi_cd(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 struct scsi_device *sdev = cd->device;
Christoph Hellwig6a2900b2006-03-23 03:00:15 -0800571 void __user *argp = (void __user *)arg;
572 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Merlijn Wajer51a85882020-02-18 15:39:17 +0100574 mutex_lock(&cd->lock);
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200575
Christoph Hellwig906d15f2014-10-11 16:25:31 +0200576 ret = scsi_ioctl_block_when_processing_errors(sdev, cmd,
577 (mode & FMODE_NDELAY) != 0);
578 if (ret)
579 goto out;
580
Bart Van Assche1214fd72018-08-02 10:44:42 -0700581 scsi_autopm_get_device(sdev);
582
Christoph Hellwig6a2900b2006-03-23 03:00:15 -0800583 /*
584 * Send SCSI addressing ioctls directly to mid level, send other
585 * ioctls to cdrom/block level.
586 */
587 switch (cmd) {
588 case SCSI_IOCTL_GET_IDLUN:
589 case SCSI_IOCTL_GET_BUS_NUMBER:
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200590 ret = scsi_ioctl(sdev, cmd, argp);
Bart Van Assche1214fd72018-08-02 10:44:42 -0700591 goto put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 }
Christoph Hellwig6a2900b2006-03-23 03:00:15 -0800593
Al Viro40cc51b2008-03-02 10:41:28 -0500594 ret = cdrom_ioctl(&cd->cdi, bdev, mode, cmd, arg);
Tejun Heo63972562007-01-02 17:41:04 +0900595 if (ret != -ENOSYS)
Bart Van Assche1214fd72018-08-02 10:44:42 -0700596 goto put;
Christoph Hellwig6a2900b2006-03-23 03:00:15 -0800597
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200598 ret = scsi_ioctl(sdev, cmd, argp);
599
Bart Van Assche1214fd72018-08-02 10:44:42 -0700600put:
601 scsi_autopm_put_device(sdev);
602
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200603out:
Merlijn Wajer51a85882020-02-18 15:39:17 +0100604 mutex_unlock(&cd->lock);
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200605 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606}
607
Arnd Bergmannd320a952019-03-15 17:39:44 +0100608#ifdef CONFIG_COMPAT
609static int sr_block_compat_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,
610 unsigned long arg)
611{
612 struct scsi_cd *cd = scsi_cd(bdev->bd_disk);
613 struct scsi_device *sdev = cd->device;
614 void __user *argp = compat_ptr(arg);
615 int ret;
616
Merlijn Wajer51a85882020-02-18 15:39:17 +0100617 mutex_lock(&cd->lock);
Arnd Bergmannd320a952019-03-15 17:39:44 +0100618
619 ret = scsi_ioctl_block_when_processing_errors(sdev, cmd,
620 (mode & FMODE_NDELAY) != 0);
621 if (ret)
622 goto out;
623
624 scsi_autopm_get_device(sdev);
625
626 /*
627 * Send SCSI addressing ioctls directly to mid level, send other
628 * ioctls to cdrom/block level.
629 */
630 switch (cmd) {
631 case SCSI_IOCTL_GET_IDLUN:
632 case SCSI_IOCTL_GET_BUS_NUMBER:
633 ret = scsi_compat_ioctl(sdev, cmd, argp);
634 goto put;
635 }
636
Arnd Bergmann64cbfa92019-11-28 15:55:17 +0100637 ret = cdrom_ioctl(&cd->cdi, bdev, mode, cmd, (unsigned long)argp);
638 if (ret != -ENOSYS)
Arnd Bergmannd320a952019-03-15 17:39:44 +0100639 goto put;
640
641 ret = scsi_compat_ioctl(sdev, cmd, argp);
642
643put:
644 scsi_autopm_put_device(sdev);
645
646out:
Merlijn Wajer51a85882020-02-18 15:39:17 +0100647 mutex_unlock(&cd->lock);
Arnd Bergmannd320a952019-03-15 17:39:44 +0100648 return ret;
649
650}
651#endif
652
Tejun Heo93aae172010-12-16 17:52:17 +0100653static unsigned int sr_block_check_events(struct gendisk *disk,
654 unsigned int clearing)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655{
Jens Axboe2d097c52018-04-11 11:26:09 -0600656 unsigned int ret = 0;
657 struct scsi_cd *cd;
Aaron Lu6c7f1e22013-01-23 15:09:31 +0800658
Jens Axboe2d097c52018-04-11 11:26:09 -0600659 cd = scsi_cd_get(disk);
660 if (!cd)
Aaron Lu6627b382013-10-28 15:27:49 +0800661 return 0;
Aaron Lu6c7f1e22013-01-23 15:09:31 +0800662
Jens Axboe2d097c52018-04-11 11:26:09 -0600663 if (!atomic_read(&cd->device->disk_events_disable_depth))
664 ret = cdrom_check_events(&cd->cdi, clearing);
665
666 scsi_cd_put(cd);
667 return ret;
Tejun Heo93aae172010-12-16 17:52:17 +0100668}
669
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700670static const struct block_device_operations sr_bdops =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
672 .owner = THIS_MODULE,
Al Viro40cc51b2008-03-02 10:41:28 -0500673 .open = sr_block_open,
674 .release = sr_block_release,
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200675 .ioctl = sr_block_ioctl,
Arnd Bergmannd320a952019-03-15 17:39:44 +0100676#ifdef CONFIG_COMPAT
Adam Williamson03264dd2020-02-19 17:50:07 +0100677 .compat_ioctl = sr_block_compat_ioctl,
Arnd Bergmannd320a952019-03-15 17:39:44 +0100678#endif
Tejun Heo93aae172010-12-16 17:52:17 +0100679 .check_events = sr_block_check_events,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680};
681
682static int sr_open(struct cdrom_device_info *cdi, int purpose)
683{
684 struct scsi_cd *cd = cdi->handle;
685 struct scsi_device *sdev = cd->device;
686 int retval;
687
688 /*
689 * If the device is in error recovery, wait until it is done.
690 * If the device is offline, then disallow any access to it.
691 */
692 retval = -ENXIO;
693 if (!scsi_block_when_processing_errors(sdev))
694 goto error_out;
695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 return 0;
697
698error_out:
699 return retval;
700}
701
702static void sr_release(struct cdrom_device_info *cdi)
703{
704 struct scsi_cd *cd = cdi->handle;
705
706 if (cd->device->sector_size > 2048)
707 sr_set_blocklength(cd, 2048);
708
709}
710
711static int sr_probe(struct device *dev)
712{
713 struct scsi_device *sdev = to_scsi_device(dev);
714 struct gendisk *disk;
715 struct scsi_cd *cd;
716 int minor, error;
717
Subhash Jadavani6fe8c1d2014-09-10 14:54:09 +0300718 scsi_autopm_get_device(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 error = -ENODEV;
720 if (sdev->type != TYPE_ROM && sdev->type != TYPE_WORM)
721 goto fail;
722
723 error = -ENOMEM;
Jes Sorensen24669f752006-01-16 10:31:18 -0500724 cd = kzalloc(sizeof(*cd), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 if (!cd)
726 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
728 kref_init(&cd->kref);
729
730 disk = alloc_disk(1);
731 if (!disk)
732 goto fail_free;
Merlijn Wajer51a85882020-02-18 15:39:17 +0100733 mutex_init(&cd->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
735 spin_lock(&sr_index_lock);
736 minor = find_first_zero_bit(sr_index_bits, SR_DISKS);
737 if (minor == SR_DISKS) {
738 spin_unlock(&sr_index_lock);
739 error = -EBUSY;
740 goto fail_put;
741 }
742 __set_bit(minor, sr_index_bits);
743 spin_unlock(&sr_index_lock);
744
745 disk->major = SCSI_CDROM_MAJOR;
746 disk->first_minor = minor;
747 sprintf(disk->disk_name, "sr%d", minor);
748 disk->fops = &sr_bdops;
Tejun Heod4dc2102011-04-21 20:54:46 +0200749 disk->flags = GENHD_FL_CD | GENHD_FL_BLOCK_EVENTS_ON_EXCL_WRITE;
Tejun Heo93aae172010-12-16 17:52:17 +0100750 disk->events = DISK_EVENT_MEDIA_CHANGE | DISK_EVENT_EJECT_REQUEST;
Martin Wilckc92e2f02019-03-27 14:51:02 +0100751 disk->event_flags = DISK_EVENT_FLAG_POLL | DISK_EVENT_FLAG_UEVENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Jens Axboe242f9dc2008-09-14 05:55:09 -0700753 blk_queue_rq_timeout(sdev->request_queue, SR_TIMEOUT);
754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 cd->device = sdev;
756 cd->disk = disk;
757 cd->driver = &sr_template;
758 cd->disk = disk;
759 cd->capacity = 0x1fffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 cd->device->changed = 1; /* force recheck CD type */
Tejun Heo93aae172010-12-16 17:52:17 +0100761 cd->media_present = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 cd->use = 1;
763 cd->readcd_known = 0;
764 cd->readcd_cdda = 0;
765
766 cd->cdi.ops = &sr_dops;
767 cd->cdi.handle = cd;
768 cd->cdi.mask = 0;
769 cd->cdi.capacity = 1;
770 sprintf(cd->cdi.name, "sr%d", minor);
771
772 sdev->sector_size = 2048; /* A guess, just in case */
773
774 /* FIXME: need to handle a get_capabilities failure properly ?? */
775 get_capabilities(cd);
776 sr_vendor_init(cd);
777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 set_capacity(disk, cd->capacity);
779 disk->private_data = &cd->driver;
780 disk->queue = sdev->request_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Christoph Hellwiga711d912020-04-25 09:57:00 +0200782 if (register_cdrom(disk, &cd->cdi))
Simon Arlott65557812020-05-30 18:59:44 +0100783 goto fail_minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Aaron Lu6627b382013-10-28 15:27:49 +0800785 /*
786 * Initialize block layer runtime PM stuffs before the
787 * periodic event checking request gets started in add_disk.
788 */
789 blk_pm_runtime_init(sdev->request_queue, dev);
790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 dev_set_drvdata(dev, cd);
792 disk->flags |= GENHD_FL_REMOVABLE;
Christoph Hellwig38a2b552020-09-08 16:53:46 +0200793 sr_revalidate_disk(cd);
Hannes Reineckefef912b2018-09-28 08:17:19 +0200794 device_add_disk(&sdev->sdev_gendev, disk, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
James Bottomley9ccfc752005-10-02 11:45:08 -0500796 sdev_printk(KERN_DEBUG, sdev,
797 "Attached scsi CD-ROM %s\n", cd->cdi.name);
Aaron Lu6c7f1e22013-01-23 15:09:31 +0800798 scsi_autopm_put_device(cd->device);
799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 return 0;
801
Simon Arlott65557812020-05-30 18:59:44 +0100802fail_minor:
803 spin_lock(&sr_index_lock);
804 clear_bit(minor, sr_index_bits);
805 spin_unlock(&sr_index_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806fail_put:
807 put_disk(disk);
Simon Arlotta247e072020-05-30 18:58:25 +0100808 mutex_destroy(&cd->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809fail_free:
810 kfree(cd);
811fail:
Subhash Jadavani6fe8c1d2014-09-10 14:54:09 +0300812 scsi_autopm_put_device(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 return error;
814}
815
816
817static void get_sectorsize(struct scsi_cd *cd)
818{
819 unsigned char cmd[10];
FUJITA Tomonori62858da2008-07-04 09:31:50 +0200820 unsigned char buffer[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 int the_result, retries = 3;
822 int sector_size;
Jens Axboe165125e2007-07-24 09:28:11 +0200823 struct request_queue *queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 do {
826 cmd[0] = READ_CAPACITY;
827 memset((void *) &cmd[1], 0, 9);
FUJITA Tomonori62858da2008-07-04 09:31:50 +0200828 memset(buffer, 0, sizeof(buffer));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
830 /* Do the command and wait.. */
James Bottomley820732b2005-06-12 22:21:29 -0500831 the_result = scsi_execute_req(cd->device, cmd, DMA_FROM_DEVICE,
FUJITA Tomonori62858da2008-07-04 09:31:50 +0200832 buffer, sizeof(buffer), NULL,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +0900833 SR_TIMEOUT, MAX_RETRIES, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 retries--;
836
837 } while (the_result && retries);
838
839
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 if (the_result) {
841 cd->capacity = 0x1fffff;
842 sector_size = 2048; /* A guess, just in case */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 } else {
Tejun Heo59151362009-09-04 11:38:02 +0900844 long last_written;
845
Bart Van Assche655da8e52020-04-26 18:48:44 -0700846 cd->capacity = 1 + get_unaligned_be32(&buffer[0]);
Tejun Heo59151362009-09-04 11:38:02 +0900847 /*
848 * READ_CAPACITY doesn't return the correct size on
849 * certain UDF media. If last_written is larger, use
850 * it instead.
851 *
852 * http://bugzilla.kernel.org/show_bug.cgi?id=9668
853 */
854 if (!cdrom_get_last_written(&cd->cdi, &last_written))
855 cd->capacity = max_t(long, cd->capacity, last_written);
856
Bart Van Assche655da8e52020-04-26 18:48:44 -0700857 sector_size = get_unaligned_be32(&buffer[4]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 switch (sector_size) {
859 /*
860 * HP 4020i CD-Recorder reports 2340 byte sectors
861 * Philips CD-Writers report 2352 byte sectors
862 *
863 * Use 2k sectors for them..
864 */
865 case 0:
866 case 2340:
867 case 2352:
868 sector_size = 2048;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500869 fallthrough;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 case 2048:
871 cd->capacity *= 4;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500872 fallthrough;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 case 512:
874 break;
875 default:
Hannes Reinecke96eefad22014-06-25 16:39:54 +0200876 sr_printk(KERN_INFO, cd,
877 "unsupported sector size %d.", sector_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 cd->capacity = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 }
880
881 cd->device->sector_size = sector_size;
882
883 /*
884 * Add this so that we have the ability to correctly gauge
885 * what the device is capable of.
886 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 set_capacity(cd->disk, cd->capacity);
888 }
889
890 queue = cd->device->request_queue;
Martin K. Petersene1defc42009-05-22 17:17:49 -0400891 blk_queue_logical_block_size(queue, sector_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
FUJITA Tomonori62858da2008-07-04 09:31:50 +0200893 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894}
895
896static void get_capabilities(struct scsi_cd *cd)
897{
898 unsigned char *buffer;
899 struct scsi_mode_data data;
James Bottomley820732b2005-06-12 22:21:29 -0500900 struct scsi_sense_hdr sshdr;
Martin K. Petersena00a7862017-03-17 08:47:14 -0400901 unsigned int ms_len = 128;
James Bottomley38582a62008-02-06 13:01:58 -0600902 int rc, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Arjan van de Ven0ad78202005-11-28 16:22:25 +0100904 static const char *loadmech[] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 {
906 "caddy",
907 "tray",
908 "pop-up",
909 "",
910 "changer",
911 "cartridge changer",
912 "",
913 ""
914 };
915
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
917 /* allocate transfer buffer */
918 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
919 if (!buffer) {
Hannes Reinecke96eefad22014-06-25 16:39:54 +0200920 sr_printk(KERN_ERR, cd, "out of memory.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 return;
922 }
923
James Bottomley38582a62008-02-06 13:01:58 -0600924 /* eat unit attentions */
Tejun Heo9f8a2c22010-12-08 20:57:40 +0100925 scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES, &sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
927 /* ask for mode page 0x2a */
Martin K. Petersena00a7862017-03-17 08:47:14 -0400928 rc = scsi_mode_sense(cd->device, 0, 0x2a, buffer, ms_len,
James Bottomley1cf72692005-08-28 11:27:01 -0500929 SR_TIMEOUT, 3, &data, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Martin K. Petersena00a7862017-03-17 08:47:14 -0400931 if (!scsi_status_is_good(rc) || data.length > ms_len ||
932 data.header_length + data.block_descriptor_length > data.length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 /* failed, drive doesn't have capabilities mode page */
934 cd->cdi.speed = 1;
935 cd->cdi.mask |= (CDC_CD_R | CDC_CD_RW | CDC_DVD_R |
Chuck Ebbertbcc1e382006-01-11 23:58:40 -0500936 CDC_DVD | CDC_DVD_RAM |
937 CDC_SELECT_DISC | CDC_SELECT_SPEED |
938 CDC_MRW | CDC_MRW_W | CDC_RAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 kfree(buffer);
Hannes Reinecke96eefad22014-06-25 16:39:54 +0200940 sr_printk(KERN_INFO, cd, "scsi-1 drive");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 return;
942 }
943
944 n = data.header_length + data.block_descriptor_length;
Bart Van Assche655da8e52020-04-26 18:48:44 -0700945 cd->cdi.speed = get_unaligned_be16(&buffer[n + 8]) / 176;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 cd->readcd_known = 1;
947 cd->readcd_cdda = buffer[n + 5] & 0x01;
948 /* print some capability bits */
Hannes Reinecke96eefad22014-06-25 16:39:54 +0200949 sr_printk(KERN_INFO, cd,
950 "scsi3-mmc drive: %dx/%dx %s%s%s%s%s%s\n",
Bart Van Assche655da8e52020-04-26 18:48:44 -0700951 get_unaligned_be16(&buffer[n + 14]) / 176,
Hannes Reinecke96eefad22014-06-25 16:39:54 +0200952 cd->cdi.speed,
953 buffer[n + 3] & 0x01 ? "writer " : "", /* CD Writer */
954 buffer[n + 3] & 0x20 ? "dvd-ram " : "",
955 buffer[n + 2] & 0x02 ? "cd/rw " : "", /* can read rewriteable */
956 buffer[n + 4] & 0x20 ? "xa/form2 " : "", /* can read xa/from2 */
957 buffer[n + 5] & 0x01 ? "cdda " : "", /* can read audio data */
958 loadmech[buffer[n + 6] >> 5]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 if ((buffer[n + 6] >> 5) == 0)
960 /* caddy drives can't close tray... */
961 cd->cdi.mask |= CDC_CLOSE_TRAY;
962 if ((buffer[n + 2] & 0x8) == 0)
963 /* not a DVD drive */
964 cd->cdi.mask |= CDC_DVD;
Hannes Reinecke96eefad22014-06-25 16:39:54 +0200965 if ((buffer[n + 3] & 0x20) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 /* can't write DVD-RAM media */
967 cd->cdi.mask |= CDC_DVD_RAM;
968 if ((buffer[n + 3] & 0x10) == 0)
969 /* can't write DVD-R media */
970 cd->cdi.mask |= CDC_DVD_R;
971 if ((buffer[n + 3] & 0x2) == 0)
972 /* can't write CD-RW media */
973 cd->cdi.mask |= CDC_CD_RW;
974 if ((buffer[n + 3] & 0x1) == 0)
975 /* can't write CD-R media */
976 cd->cdi.mask |= CDC_CD_R;
977 if ((buffer[n + 6] & 0x8) == 0)
978 /* can't eject */
979 cd->cdi.mask |= CDC_OPEN_TRAY;
980
981 if ((buffer[n + 6] >> 5) == mechtype_individual_changer ||
982 (buffer[n + 6] >> 5) == mechtype_cartridge_changer)
983 cd->cdi.capacity =
984 cdrom_number_of_slots(&cd->cdi);
985 if (cd->cdi.capacity <= 1)
986 /* not a changer */
987 cd->cdi.mask |= CDC_SELECT_DISC;
988 /*else I don't think it can close its tray
989 cd->cdi.mask |= CDC_CLOSE_TRAY; */
990
991 /*
992 * if DVD-RAM, MRW-W or CD-RW, we are randomly writable
993 */
994 if ((cd->cdi.mask & (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) !=
995 (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) {
Christoph Hellwigfd2eb902014-07-18 16:59:19 +0200996 cd->writeable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 }
998
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 kfree(buffer);
1000}
1001
1002/*
1003 * sr_packet() is the entry point for the generic commands generated
Hannes Reinecke96eefad22014-06-25 16:39:54 +02001004 * by the Uniform CD-ROM layer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 */
1006static int sr_packet(struct cdrom_device_info *cdi,
1007 struct packet_command *cgc)
1008{
Hans de Goede8e04d802010-10-01 14:20:08 -07001009 struct scsi_cd *cd = cdi->handle;
1010 struct scsi_device *sdev = cd->device;
1011
1012 if (cgc->cmd[0] == GPCMD_READ_DISC_INFO && sdev->no_read_disc_info)
1013 return -EDRIVE_CANT_DO_THIS;
1014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 if (cgc->timeout <= 0)
1016 cgc->timeout = IOCTL_TIMEOUT;
1017
Hans de Goede8e04d802010-10-01 14:20:08 -07001018 sr_do_ioctl(cd, cgc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
1020 return cgc->stat;
1021}
1022
1023/**
1024 * sr_kref_release - Called to free the scsi_cd structure
1025 * @kref: pointer to embedded kref
1026 *
Arjan van de Ven0b950672006-01-11 13:16:10 +01001027 * sr_ref_mutex must be held entering this routine. Because it is
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 * called on last put, you should always use the scsi_cd_get()
1029 * scsi_cd_put() helpers which manipulate the semaphore directly
1030 * and never do a direct kref_put().
1031 **/
1032static void sr_kref_release(struct kref *kref)
1033{
1034 struct scsi_cd *cd = container_of(kref, struct scsi_cd, kref);
1035 struct gendisk *disk = cd->disk;
1036
1037 spin_lock(&sr_index_lock);
Tejun Heof331c022008-09-03 09:01:48 +02001038 clear_bit(MINOR(disk_devt(disk)), sr_index_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 spin_unlock(&sr_index_lock);
1040
1041 unregister_cdrom(&cd->cdi);
1042
1043 disk->private_data = NULL;
1044
1045 put_disk(disk);
1046
Merlijn Wajer51a85882020-02-18 15:39:17 +01001047 mutex_destroy(&cd->lock);
1048
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 kfree(cd);
1050}
1051
1052static int sr_remove(struct device *dev)
1053{
1054 struct scsi_cd *cd = dev_get_drvdata(dev);
1055
Aaron Lu6c7f1e22013-01-23 15:09:31 +08001056 scsi_autopm_get_device(cd->device);
1057
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 del_gendisk(cd->disk);
Alan Stern13b43892016-01-20 11:26:01 -05001059 dev_set_drvdata(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
Arjan van de Ven0b950672006-01-11 13:16:10 +01001061 mutex_lock(&sr_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 kref_put(&cd->kref, sr_kref_release);
Arjan van de Ven0b950672006-01-11 13:16:10 +01001063 mutex_unlock(&sr_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
1065 return 0;
1066}
1067
1068static int __init init_sr(void)
1069{
1070 int rc;
1071
1072 rc = register_blkdev(SCSI_CDROM_MAJOR, "sr");
1073 if (rc)
1074 return rc;
Akinobu Mitada3962f2007-06-26 00:39:33 +09001075 rc = scsi_register_driver(&sr_template.gendrv);
1076 if (rc)
1077 unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
1078
1079 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080}
1081
1082static void __exit exit_sr(void)
1083{
1084 scsi_unregister_driver(&sr_template.gendrv);
1085 unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
1086}
1087
1088module_init(init_sr);
1089module_exit(exit_sr);
1090MODULE_LICENSE("GPL");