blob: faca0f346fff2afe0afff8c593fffe71e9884156 [file] [log] [blame]
Uwe Zeisbergerf30c2262006-10-03 23:01:26 +02001/* linux/drivers/cdrom/cdrom.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 Copyright (c) 1996, 1997 David A. van Leeuwen.
3 Copyright (c) 1997, 1998 Erik Andersen <andersee@debian.org>
4 Copyright (c) 1998, 1999 Jens Axboe <axboe@image.dk>
5
6 May be copied or modified under the terms of the GNU General Public
7 License. See linux/COPYING for more information.
8
9 Uniform CD-ROM driver for Linux.
Mauro Carvalho Chehab8ea61882019-06-12 14:52:40 -030010 See Documentation/cdrom/cdrom-standard.rst for usage information.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
12 The routines in the file provide a uniform interface between the
13 software that uses CD-ROMs and the various low-level drivers that
14 actually talk to the hardware. Suggestions are welcome.
15 Patches that work are more welcome though. ;-)
16
17 To Do List:
18 ----------------------------------
19
20 -- Modify sysctl/proc interface. I plan on having one directory per
21 drive, with entries for outputing general drive information, and sysctl
22 based tunable parameters such as whether the tray should auto-close for
23 that drive. Suggestions (or patches) for this welcome!
24
25
26 Revision History
27 ----------------------------------
28 1.00 Date Unknown -- David van Leeuwen <david@tm.tno.nl>
29 -- Initial version by David A. van Leeuwen. I don't have a detailed
30 changelog for the 1.x series, David?
31
322.00 Dec 2, 1997 -- Erik Andersen <andersee@debian.org>
Lucas De Marchi25985ed2011-03-30 22:57:33 -030033 -- New maintainer! As David A. van Leeuwen has been too busy to actively
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 maintain and improve this driver, I am now carrying on the torch. If
35 you have a problem with this driver, please feel free to contact me.
36
37 -- Added (rudimentary) sysctl interface. I realize this is really weak
38 right now, and is _very_ badly implemented. It will be improved...
39
40 -- Modified CDROM_DISC_STATUS so that it is now incorporated into
41 the Uniform CD-ROM driver via the cdrom_count_tracks function.
42 The cdrom_count_tracks function helps resolve some of the false
43 assumptions of the CDROM_DISC_STATUS ioctl, and is also used to check
44 for the correct media type when mounting or playing audio from a CD.
45
46 -- Remove the calls to verify_area and only use the copy_from_user and
47 copy_to_user stuff, since these calls now provide their own memory
48 checking with the 2.1.x kernels.
49
50 -- Major update to return codes so that errors from low-level drivers
51 are passed on through (thanks to Gerd Knorr for pointing out this
52 problem).
53
54 -- Made it so if a function isn't implemented in a low-level driver,
55 ENOSYS is now returned instead of EINVAL.
56
57 -- Simplified some complex logic so that the source code is easier to read.
58
59 -- Other stuff I probably forgot to mention (lots of changes).
60
612.01 to 2.11 Dec 1997-Jan 1998
62 -- TO-DO! Write changelogs for 2.01 to 2.12.
63
642.12 Jan 24, 1998 -- Erik Andersen <andersee@debian.org>
65 -- Fixed a bug in the IOCTL_IN and IOCTL_OUT macros. It turns out that
66 copy_*_user does not return EFAULT on error, but instead returns the number
67 of bytes not copied. I was returning whatever non-zero stuff came back from
68 the copy_*_user functions directly, which would result in strange errors.
69
702.13 July 17, 1998 -- Erik Andersen <andersee@debian.org>
71 -- Fixed a bug in CDROM_SELECT_SPEED where you couldn't lower the speed
72 of the drive. Thanks to Tobias Ringstr|m <tori@prosolvia.se> for pointing
73 this out and providing a simple fix.
74 -- Fixed the procfs-unload-module bug with the fill_inode procfs callback.
75 thanks to Andrea Arcangeli
76 -- Fixed it so that the /proc entry now also shows up when cdrom is
77 compiled into the kernel. Before it only worked when loaded as a module.
78
79 2.14 August 17, 1998 -- Erik Andersen <andersee@debian.org>
80 -- Fixed a bug in cdrom_media_changed and handling of reporting that
81 the media had changed for devices that _don't_ implement media_changed.
82 Thanks to Grant R. Guenther <grant@torque.net> for spotting this bug.
83 -- Made a few things more pedanticly correct.
84
852.50 Oct 19, 1998 - Jens Axboe <axboe@image.dk>
86 -- New maintainers! Erik was too busy to continue the work on the driver,
87 so now Chris Zwilling <chris@cloudnet.com> and Jens Axboe <axboe@image.dk>
88 will do their best to follow in his footsteps
89
90 2.51 Dec 20, 1998 - Jens Axboe <axboe@image.dk>
91 -- Check if drive is capable of doing what we ask before blindly changing
92 cdi->options in various ioctl.
93 -- Added version to proc entry.
94
95 2.52 Jan 16, 1999 - Jens Axboe <axboe@image.dk>
96 -- Fixed an error in open_for_data where we would sometimes not return
97 the correct error value. Thanks Huba Gaspar <huba@softcell.hu>.
98 -- Fixed module usage count - usage was based on /proc/sys/dev
99 instead of /proc/sys/dev/cdrom. This could lead to an oops when other
100 modules had entries in dev. Feb 02 - real bug was in sysctl.c where
101 dev would be removed even though it was used. cdrom.c just illuminated
102 that bug.
103
104 2.53 Feb 22, 1999 - Jens Axboe <axboe@image.dk>
105 -- Fixup of several ioctl calls, in particular CDROM_SET_OPTIONS has
106 been "rewritten" because capabilities and options aren't in sync. They
107 should be...
108 -- Added CDROM_LOCKDOOR ioctl. Locks the door and keeps it that way.
109 -- Added CDROM_RESET ioctl.
110 -- Added CDROM_DEBUG ioctl. Enable debug messages on-the-fly.
111 -- Added CDROM_GET_CAPABILITY ioctl. This relieves userspace programs
112 from parsing /proc/sys/dev/cdrom/info.
113
114 2.54 Mar 15, 1999 - Jens Axboe <axboe@image.dk>
115 -- Check capability mask from low level driver when counting tracks as
116 per suggestion from Corey J. Scotts <cstotts@blue.weeg.uiowa.edu>.
117
118 2.55 Apr 25, 1999 - Jens Axboe <axboe@image.dk>
119 -- autoclose was mistakenly checked against CDC_OPEN_TRAY instead of
120 CDC_CLOSE_TRAY.
121 -- proc info didn't mask against capabilities mask.
122
123 3.00 Aug 5, 1999 - Jens Axboe <axboe@image.dk>
124 -- Unified audio ioctl handling across CD-ROM drivers. A lot of the
125 code was duplicated before. Drives that support the generic packet
126 interface are now being fed packets from here instead.
127 -- First attempt at adding support for MMC2 commands - for DVD and
128 CD-R(W) drives. Only the DVD parts are in now - the interface used is
129 the same as for the audio ioctls.
130 -- ioctl cleanups. if a drive couldn't play audio, it didn't get
131 a change to perform device specific ioctls as well.
132 -- Defined CDROM_CAN(CDC_XXX) for checking the capabilities.
133 -- Put in sysctl files for autoclose, autoeject, check_media, debug,
134 and lock.
135 -- /proc/sys/dev/cdrom/info has been updated to also contain info about
136 CD-Rx and DVD capabilities.
137 -- Now default to checking media type.
138 -- CDROM_SEND_PACKET ioctl added. The infrastructure was in place for
139 doing this anyway, with the generic_packet addition.
140
141 3.01 Aug 6, 1999 - Jens Axboe <axboe@image.dk>
142 -- Fix up the sysctl handling so that the option flags get set
143 correctly.
144 -- Fix up ioctl handling so the device specific ones actually get
145 called :).
146
147 3.02 Aug 8, 1999 - Jens Axboe <axboe@image.dk>
148 -- Fixed volume control on SCSI drives (or others with longer audio
149 page).
150 -- Fixed a couple of DVD minors. Thanks to Andrew T. Veliath
151 <andrewtv@usa.net> for telling me and for having defined the various
152 DVD structures and ioctls in the first place! He designed the original
153 DVD patches for ide-cd and while I rearranged and unified them, the
154 interface is still the same.
155
156 3.03 Sep 1, 1999 - Jens Axboe <axboe@image.dk>
157 -- Moved the rest of the audio ioctls from the CD-ROM drivers here. Only
158 CDROMREADTOCENTRY and CDROMREADTOCHDR are left.
159 -- Moved the CDROMREADxxx ioctls in here.
160 -- Defined the cdrom_get_last_written and cdrom_get_next_block as ioctls
161 and exported functions.
162 -- Erik Andersen <andersen@xmission.com> modified all SCMD_ commands
163 to now read GPCMD_ for the new generic packet interface. All low level
164 drivers are updated as well.
165 -- Various other cleanups.
166
167 3.04 Sep 12, 1999 - Jens Axboe <axboe@image.dk>
168 -- Fixed a couple of possible memory leaks (if an operation failed and
169 we didn't free the buffer before returning the error).
170 -- Integrated Uniform CD Changer handling from Richard Sharman
171 <rsharman@pobox.com>.
172 -- Defined CD_DVD and CD_CHANGER log levels.
173 -- Fixed the CDROMREADxxx ioctls.
174 -- CDROMPLAYTRKIND uses the GPCMD_PLAY_AUDIO_MSF command - too few
175 drives supported it. We lose the index part, however.
176 -- Small modifications to accommodate opens of /dev/hdc1, required
177 for ide-cd to handle multisession discs.
178 -- Export cdrom_mode_sense and cdrom_mode_select.
179 -- init_cdrom_command() for setting up a cgc command.
180
181 3.05 Oct 24, 1999 - Jens Axboe <axboe@image.dk>
182 -- Changed the interface for CDROM_SEND_PACKET. Before it was virtually
183 impossible to send the drive data in a sensible way.
184 -- Lowered stack usage in mmc_ioctl(), dvd_read_disckey(), and
185 dvd_read_manufact.
186 -- Added setup of write mode for packet writing.
187 -- Fixed CDDA ripping with cdda2wav - accept much larger requests of
188 number of frames and split the reads in blocks of 8.
189
190 3.06 Dec 13, 1999 - Jens Axboe <axboe@image.dk>
191 -- Added support for changing the region of DVD drives.
192 -- Added sense data to generic command.
193
194 3.07 Feb 2, 2000 - Jens Axboe <axboe@suse.de>
195 -- Do same "read header length" trick in cdrom_get_disc_info() as
196 we do in cdrom_get_track_info() -- some drive don't obey specs and
197 fail if they can't supply the full Mt Fuji size table.
198 -- Deleted stuff related to setting up write modes. It has a different
199 home now.
200 -- Clear header length in mode_select unconditionally.
201 -- Removed the register_disk() that was added, not needed here.
202
203 3.08 May 1, 2000 - Jens Axboe <axboe@suse.de>
204 -- Fix direction flag in setup_send_key and setup_report_key. This
205 gave some SCSI adapters problems.
206 -- Always return -EROFS for write opens
207 -- Convert to module_init/module_exit style init and remove some
208 of the #ifdef MODULE stuff
209 -- Fix several dvd errors - DVD_LU_SEND_ASF should pass agid,
210 DVD_HOST_SEND_RPC_STATE did not set buffer size in cdb, and
211 dvd_do_auth passed uninitialized data to drive because init_cdrom_command
212 did not clear a 0 sized buffer.
213
214 3.09 May 12, 2000 - Jens Axboe <axboe@suse.de>
215 -- Fix Video-CD on SCSI drives that don't support READ_CD command. In
216 that case switch block size and issue plain READ_10 again, then switch
217 back.
218
219 3.10 Jun 10, 2000 - Jens Axboe <axboe@suse.de>
220 -- Fix volume control on CD's - old SCSI-II drives now use their own
221 code, as doing MODE6 stuff in here is really not my intention.
222 -- Use READ_DISC_INFO for more reliable end-of-disc.
223
224 3.11 Jun 12, 2000 - Jens Axboe <axboe@suse.de>
225 -- Fix bug in getting rpc phase 2 region info.
226 -- Reinstate "correct" CDROMPLAYTRKIND
227
228 3.12 Oct 18, 2000 - Jens Axboe <axboe@suse.de>
229 -- Use quiet bit on packet commands not known to work
230
231 3.20 Dec 17, 2003 - Jens Axboe <axboe@suse.de>
232 -- Various fixes and lots of cleanups not listed :-)
233 -- Locking fixes
234 -- Mt Rainier support
235 -- DVD-RAM write open fixes
236
237 Nov 5 2001, Aug 8 2002. Modified by Andy Polyakov
238 <appro@fy.chalmers.se> to support MMC-3 compliant DVD+RW units.
239
240 Modified by Nigel Kukard <nkukard@lbsd.net> - support DVD+RW
241 2.4.x patch by Andy Polyakov <appro@fy.chalmers.se>
242
243-------------------------------------------------------------------------*/
244
Joe Perchese597cd02010-07-01 08:24:32 +0200245#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247#define REVISION "Revision: 3.20"
248#define VERSION "Id: cdrom.c 3.20 2003/12/17"
249
250/* I use an error-log mask to give fine grain control over the type of
251 messages dumped to the system logs. The available masks include: */
252#define CD_NOTHING 0x0
253#define CD_WARNING 0x1
254#define CD_REG_UNREG 0x2
255#define CD_DO_IOCTL 0x4
256#define CD_OPEN 0x8
257#define CD_CLOSE 0x10
258#define CD_COUNT_TRACKS 0x20
259#define CD_CHANGER 0x40
260#define CD_DVD 0x80
261
262/* Define this to remove _all_ the debugging messages */
263/* #define ERRLOGMASK CD_NOTHING */
264#define ERRLOGMASK CD_WARNING
265/* #define ERRLOGMASK (CD_WARNING|CD_OPEN|CD_COUNT_TRACKS|CD_CLOSE) */
266/* #define ERRLOGMASK (CD_WARNING|CD_REG_UNREG|CD_DO_IOCTL|CD_OPEN|CD_CLOSE|CD_COUNT_TRACKS) */
267
Guenter Roeckf25191b2019-02-06 21:13:49 -0800268#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269#include <linux/module.h>
270#include <linux/fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271#include <linux/major.h>
272#include <linux/types.h>
273#include <linux/errno.h>
274#include <linux/kernel.h>
275#include <linux/mm.h>
276#include <linux/slab.h>
277#include <linux/cdrom.h>
278#include <linux/sysctl.h>
279#include <linux/proc_fs.h>
280#include <linux/blkpg.h>
281#include <linux/init.h>
282#include <linux/fcntl.h>
283#include <linux/blkdev.h>
284#include <linux/times.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -0800285#include <linux/uaccess.h>
Kees Cook4e178c12018-07-31 12:51:52 -0700286#include <scsi/scsi_common.h>
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100287#include <scsi/scsi_request.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289/* used to tell the module to turn on full debugging messages */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030290static bool debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291/* default compatibility mode */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030292static bool autoclose=1;
293static bool autoeject;
294static bool lockdoor = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295/* will we ever get to use this... sigh. */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030296static bool check_media_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297/* automatically restart mrw format */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030298static bool mrw_format_restart = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299module_param(debug, bool, 0);
300module_param(autoclose, bool, 0);
301module_param(autoeject, bool, 0);
302module_param(lockdoor, bool, 0);
303module_param(check_media_type, bool, 0);
304module_param(mrw_format_restart, bool, 0);
305
Dave Young554988d2007-06-19 09:14:26 +0200306static DEFINE_MUTEX(cdrom_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308static const char *mrw_format_status[] = {
309 "not mrw",
310 "bgformat inactive",
311 "bgformat active",
312 "mrw complete",
313};
314
315static const char *mrw_address_space[] = { "DMA", "GAA" };
316
Joe Perches5944b2c2014-05-04 17:05:02 -0700317#if (ERRLOGMASK != CD_NOTHING)
318#define cd_dbg(type, fmt, ...) \
Joe Perchese597cd02010-07-01 08:24:32 +0200319do { \
320 if ((ERRLOGMASK & type) || debug == 1) \
Joe Perches5944b2c2014-05-04 17:05:02 -0700321 pr_debug(fmt, ##__VA_ARGS__); \
Joe Perchese597cd02010-07-01 08:24:32 +0200322} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323#else
Joe Perches5944b2c2014-05-04 17:05:02 -0700324#define cd_dbg(type, fmt, ...) \
Joe Perchese597cd02010-07-01 08:24:32 +0200325do { \
326 if (0 && (ERRLOGMASK & type) || debug == 1) \
Joe Perches5944b2c2014-05-04 17:05:02 -0700327 pr_debug(fmt, ##__VA_ARGS__); \
Joe Perchese597cd02010-07-01 08:24:32 +0200328} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329#endif
330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331/* The (cdo->capability & ~cdi->mask & CDC_XXX) construct was used in
332 a lot of places. This macro makes the code more clear. */
333#define CDROM_CAN(type) (cdi->ops->capability & ~cdi->mask & (type))
334
Jens Axboe2e11c202007-01-03 08:10:35 +0100335/*
336 * Another popular OS uses 7 seconds as the hard timeout for default
337 * commands, so it is a good choice for us as well.
338 */
339#define CDROM_DEF_TIMEOUT (7 * HZ)
340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341/* Not-exported routines. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343static void cdrom_sysctl_register(void);
Akinobu Mita17672cf2008-03-26 12:08:59 +0100344
Akinobu Mita7fd097d2008-03-26 12:09:02 +0100345static LIST_HEAD(cdrom_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Kees Cook853fe1b2017-02-13 16:25:26 -0800347int cdrom_dummy_generic_packet(struct cdrom_device_info *cdi,
348 struct packet_command *cgc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
Kees Cooke7d07482018-08-02 15:22:13 -0600350 if (cgc->sshdr) {
351 cgc->sshdr->sense_key = 0x05;
352 cgc->sshdr->asc = 0x20;
353 cgc->sshdr->ascq = 0x00;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 }
355
356 cgc->stat = -EIO;
357 return -EIO;
358}
Kees Cook853fe1b2017-02-13 16:25:26 -0800359EXPORT_SYMBOL(cdrom_dummy_generic_packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Joe Perches40569c62014-05-04 17:05:12 -0700361static int cdrom_flush_cache(struct cdrom_device_info *cdi)
362{
363 struct packet_command cgc;
364
365 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
366 cgc.cmd[0] = GPCMD_FLUSH_CACHE;
367
368 cgc.timeout = 5 * 60 * HZ;
369
370 return cdi->ops->generic_packet(cdi, &cgc);
371}
372
Joe Perchesbd6f0bb2014-05-04 17:05:13 -0700373/* requires CD R/RW */
374static int cdrom_get_disc_info(struct cdrom_device_info *cdi,
375 disc_information *di)
376{
Kees Cook853fe1b2017-02-13 16:25:26 -0800377 const struct cdrom_device_ops *cdo = cdi->ops;
Joe Perchesbd6f0bb2014-05-04 17:05:13 -0700378 struct packet_command cgc;
379 int ret, buflen;
380
381 /* set up command and get the disc info */
382 init_cdrom_command(&cgc, di, sizeof(*di), CGC_DATA_READ);
383 cgc.cmd[0] = GPCMD_READ_DISC_INFO;
384 cgc.cmd[8] = cgc.buflen = 2;
385 cgc.quiet = 1;
386
387 ret = cdo->generic_packet(cdi, &cgc);
388 if (ret)
389 return ret;
390
391 /* not all drives have the same disc_info length, so requeue
392 * packet with the length the drive tells us it can supply
393 */
394 buflen = be16_to_cpu(di->disc_information_length) +
395 sizeof(di->disc_information_length);
396
397 if (buflen > sizeof(disc_information))
398 buflen = sizeof(disc_information);
399
400 cgc.cmd[8] = cgc.buflen = buflen;
401 ret = cdo->generic_packet(cdi, &cgc);
402 if (ret)
403 return ret;
404
405 /* return actual fill size */
406 return buflen;
407}
408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409/* This macro makes sure we don't have to check on cdrom_device_ops
410 * existence in the run-time routines below. Change_capability is a
411 * hack to have the capability flags defined const, while we can still
412 * change it here without gcc complaining at every line.
413 */
Jens Axboe8f940042018-10-14 13:20:48 -0600414#define ENSURE(cdo, call, bits) \
415do { \
416 if (cdo->call == NULL) \
417 WARN_ON_ONCE((cdo)->capability & (bits)); \
Joe Perches40569c62014-05-04 17:05:12 -0700418} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420/*
421 * the first prototypes used 0x2c as the page code for the mrw mode page,
422 * subsequently this was changed to 0x03. probe the one used by this drive
423 */
424static int cdrom_mrw_probe_pc(struct cdrom_device_info *cdi)
425{
426 struct packet_command cgc;
FUJITA Tomonorifce53842008-07-04 09:33:01 +0200427 char buffer[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
FUJITA Tomonorifce53842008-07-04 09:33:01 +0200429 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 cgc.timeout = HZ;
432 cgc.quiet = 1;
433
434 if (!cdrom_mode_sense(cdi, &cgc, MRW_MODE_PC, 0)) {
435 cdi->mrw_mode_page = MRW_MODE_PC;
FUJITA Tomonorifce53842008-07-04 09:33:01 +0200436 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 } else if (!cdrom_mode_sense(cdi, &cgc, MRW_MODE_PC_PRE1, 0)) {
438 cdi->mrw_mode_page = MRW_MODE_PC_PRE1;
FUJITA Tomonorifce53842008-07-04 09:33:01 +0200439 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 }
FUJITA Tomonorifce53842008-07-04 09:33:01 +0200441
442 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443}
444
445static int cdrom_is_mrw(struct cdrom_device_info *cdi, int *write)
446{
447 struct packet_command cgc;
448 struct mrw_feature_desc *mfd;
FUJITA Tomonorifce53842008-07-04 09:33:01 +0200449 unsigned char buffer[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 int ret;
451
452 *write = 0;
453
FUJITA Tomonorifce53842008-07-04 09:33:01 +0200454 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 cgc.cmd[0] = GPCMD_GET_CONFIGURATION;
457 cgc.cmd[3] = CDF_MRW;
FUJITA Tomonorifce53842008-07-04 09:33:01 +0200458 cgc.cmd[8] = sizeof(buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 cgc.quiet = 1;
460
461 if ((ret = cdi->ops->generic_packet(cdi, &cgc)))
FUJITA Tomonorifce53842008-07-04 09:33:01 +0200462 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464 mfd = (struct mrw_feature_desc *)&buffer[sizeof(struct feature_header)];
FUJITA Tomonorifce53842008-07-04 09:33:01 +0200465 if (be16_to_cpu(mfd->feature_code) != CDF_MRW)
466 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 *write = mfd->write;
468
469 if ((ret = cdrom_mrw_probe_pc(cdi))) {
470 *write = 0;
FUJITA Tomonorifce53842008-07-04 09:33:01 +0200471 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 }
FUJITA Tomonorifce53842008-07-04 09:33:01 +0200473
474 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475}
476
477static int cdrom_mrw_bgformat(struct cdrom_device_info *cdi, int cont)
478{
479 struct packet_command cgc;
FUJITA Tomonorifce53842008-07-04 09:33:01 +0200480 unsigned char buffer[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 int ret;
482
Joe Perchese597cd02010-07-01 08:24:32 +0200483 pr_info("%sstarting format\n", cont ? "Re" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 /*
486 * FmtData bit set (bit 4), format type is 1
487 */
FUJITA Tomonorifce53842008-07-04 09:33:01 +0200488 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 cgc.cmd[0] = GPCMD_FORMAT_UNIT;
490 cgc.cmd[1] = (1 << 4) | 1;
491
492 cgc.timeout = 5 * 60 * HZ;
493
494 /*
495 * 4 byte format list header, 8 byte format list descriptor
496 */
497 buffer[1] = 1 << 1;
498 buffer[3] = 8;
499
500 /*
501 * nr_blocks field
502 */
503 buffer[4] = 0xff;
504 buffer[5] = 0xff;
505 buffer[6] = 0xff;
506 buffer[7] = 0xff;
507
508 buffer[8] = 0x24 << 2;
509 buffer[11] = cont;
510
511 ret = cdi->ops->generic_packet(cdi, &cgc);
512 if (ret)
Joe Perchese597cd02010-07-01 08:24:32 +0200513 pr_info("bgformat failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515 return ret;
516}
517
518static int cdrom_mrw_bgformat_susp(struct cdrom_device_info *cdi, int immed)
519{
520 struct packet_command cgc;
521
522 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
523 cgc.cmd[0] = GPCMD_CLOSE_TRACK;
524
525 /*
526 * Session = 1, Track = 0
527 */
528 cgc.cmd[1] = !!immed;
529 cgc.cmd[2] = 1 << 1;
530
531 cgc.timeout = 5 * 60 * HZ;
532
533 return cdi->ops->generic_packet(cdi, &cgc);
534}
535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536static int cdrom_mrw_exit(struct cdrom_device_info *cdi)
537{
538 disc_information di;
539 int ret;
540
541 ret = cdrom_get_disc_info(cdi, &di);
542 if (ret < 0 || ret < (int)offsetof(typeof(di),disc_type))
543 return 1;
544
545 ret = 0;
546 if (di.mrw_status == CDM_MRW_BGFORMAT_ACTIVE) {
Joe Perchese597cd02010-07-01 08:24:32 +0200547 pr_info("issuing MRW background format suspend\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 ret = cdrom_mrw_bgformat_susp(cdi, 0);
549 }
550
Jens Axboe7da21a02005-05-01 12:12:52 -0700551 if (!ret && cdi->media_written)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 ret = cdrom_flush_cache(cdi);
553
554 return ret;
555}
556
557static int cdrom_mrw_set_lba_space(struct cdrom_device_info *cdi, int space)
558{
559 struct packet_command cgc;
560 struct mode_page_header *mph;
FUJITA Tomonorifce53842008-07-04 09:33:01 +0200561 char buffer[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 int ret, offset, size;
563
FUJITA Tomonorifce53842008-07-04 09:33:01 +0200564 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
FUJITA Tomonorifce53842008-07-04 09:33:01 +0200566 cgc.buffer = buffer;
567 cgc.buflen = sizeof(buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Joe Perches40569c62014-05-04 17:05:12 -0700569 ret = cdrom_mode_sense(cdi, &cgc, cdi->mrw_mode_page, 0);
570 if (ret)
FUJITA Tomonorifce53842008-07-04 09:33:01 +0200571 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Joe Perches40569c62014-05-04 17:05:12 -0700573 mph = (struct mode_page_header *)buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 offset = be16_to_cpu(mph->desc_length);
575 size = be16_to_cpu(mph->mode_data_length) + 2;
576
577 buffer[offset + 3] = space;
578 cgc.buflen = size;
579
Joe Perches40569c62014-05-04 17:05:12 -0700580 ret = cdrom_mode_select(cdi, &cgc);
581 if (ret)
FUJITA Tomonorifce53842008-07-04 09:33:01 +0200582 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Joe Perchese597cd02010-07-01 08:24:32 +0200584 pr_info("%s: mrw address space %s selected\n",
585 cdi->name, mrw_address_space[space]);
FUJITA Tomonorifce53842008-07-04 09:33:01 +0200586 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587}
588
Joe Perches40569c62014-05-04 17:05:12 -0700589int register_cdrom(struct cdrom_device_info *cdi)
590{
591 static char banner_printed;
Kees Cook853fe1b2017-02-13 16:25:26 -0800592 const struct cdrom_device_ops *cdo = cdi->ops;
Joe Perches40569c62014-05-04 17:05:12 -0700593
594 cd_dbg(CD_OPEN, "entering register_cdrom\n");
595
596 if (cdo->open == NULL || cdo->release == NULL)
597 return -EINVAL;
598 if (!banner_printed) {
599 pr_info("Uniform CD-ROM driver " REVISION "\n");
600 banner_printed = 1;
601 cdrom_sysctl_register();
602 }
603
Jens Axboe8f940042018-10-14 13:20:48 -0600604 ENSURE(cdo, drive_status, CDC_DRIVE_STATUS);
Joe Perches40569c62014-05-04 17:05:12 -0700605 if (cdo->check_events == NULL && cdo->media_changed == NULL)
Jens Axboe8f940042018-10-14 13:20:48 -0600606 WARN_ON_ONCE(cdo->capability & (CDC_MEDIA_CHANGED | CDC_SELECT_DISC));
607 ENSURE(cdo, tray_move, CDC_CLOSE_TRAY | CDC_OPEN_TRAY);
608 ENSURE(cdo, lock_door, CDC_LOCK);
609 ENSURE(cdo, select_speed, CDC_SELECT_SPEED);
610 ENSURE(cdo, get_last_session, CDC_MULTI_SESSION);
611 ENSURE(cdo, get_mcn, CDC_MCN);
612 ENSURE(cdo, reset, CDC_RESET);
613 ENSURE(cdo, generic_packet, CDC_GENERIC_PACKET);
Joe Perches40569c62014-05-04 17:05:12 -0700614 cdi->mc_flags = 0;
Joe Perches40569c62014-05-04 17:05:12 -0700615 cdi->options = CDO_USE_FFLAGS;
616
617 if (autoclose == 1 && CDROM_CAN(CDC_CLOSE_TRAY))
618 cdi->options |= (int) CDO_AUTO_CLOSE;
619 if (autoeject == 1 && CDROM_CAN(CDC_OPEN_TRAY))
620 cdi->options |= (int) CDO_AUTO_EJECT;
621 if (lockdoor == 1)
622 cdi->options |= (int) CDO_LOCK;
623 if (check_media_type == 1)
624 cdi->options |= (int) CDO_CHECK_TYPE;
625
626 if (CDROM_CAN(CDC_MRW_W))
627 cdi->exit = cdrom_mrw_exit;
628
629 if (cdi->disk)
630 cdi->cdda_method = CDDA_BPC_FULL;
631 else
632 cdi->cdda_method = CDDA_OLD;
633
Kees Cook853fe1b2017-02-13 16:25:26 -0800634 WARN_ON(!cdo->generic_packet);
Joe Perches40569c62014-05-04 17:05:12 -0700635
636 cd_dbg(CD_REG_UNREG, "drive \"/dev/%s\" registered\n", cdi->name);
637 mutex_lock(&cdrom_mutex);
638 list_add(&cdi->list, &cdrom_list);
639 mutex_unlock(&cdrom_mutex);
640 return 0;
641}
642#undef ENSURE
643
644void unregister_cdrom(struct cdrom_device_info *cdi)
645{
646 cd_dbg(CD_OPEN, "entering unregister_cdrom\n");
647
648 mutex_lock(&cdrom_mutex);
649 list_del(&cdi->list);
650 mutex_unlock(&cdrom_mutex);
651
652 if (cdi->exit)
653 cdi->exit(cdi);
654
Joe Perches40569c62014-05-04 17:05:12 -0700655 cd_dbg(CD_REG_UNREG, "drive \"/dev/%s\" unregistered\n", cdi->name);
656}
657
658int cdrom_get_media_event(struct cdrom_device_info *cdi,
659 struct media_event_desc *med)
660{
661 struct packet_command cgc;
662 unsigned char buffer[8];
663 struct event_header *eh = (struct event_header *)buffer;
664
665 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_READ);
666 cgc.cmd[0] = GPCMD_GET_EVENT_STATUS_NOTIFICATION;
667 cgc.cmd[1] = 1; /* IMMED */
668 cgc.cmd[4] = 1 << 4; /* media event */
669 cgc.cmd[8] = sizeof(buffer);
670 cgc.quiet = 1;
671
672 if (cdi->ops->generic_packet(cdi, &cgc))
673 return 1;
674
675 if (be16_to_cpu(eh->data_len) < sizeof(*med))
676 return 1;
677
678 if (eh->nea || eh->notification_class != 0x4)
679 return 1;
680
681 memcpy(med, &buffer[sizeof(*eh)], sizeof(*med));
682 return 0;
683}
684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685static int cdrom_get_random_writable(struct cdrom_device_info *cdi,
686 struct rwrt_feature_desc *rfd)
687{
688 struct packet_command cgc;
FUJITA Tomonorifce53842008-07-04 09:33:01 +0200689 char buffer[24];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 int ret;
691
FUJITA Tomonorifce53842008-07-04 09:33:01 +0200692 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
694 cgc.cmd[0] = GPCMD_GET_CONFIGURATION; /* often 0x46 */
695 cgc.cmd[3] = CDF_RWRT; /* often 0x0020 */
FUJITA Tomonorifce53842008-07-04 09:33:01 +0200696 cgc.cmd[8] = sizeof(buffer); /* often 0x18 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 cgc.quiet = 1;
698
699 if ((ret = cdi->ops->generic_packet(cdi, &cgc)))
FUJITA Tomonorifce53842008-07-04 09:33:01 +0200700 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
702 memcpy(rfd, &buffer[sizeof(struct feature_header)], sizeof (*rfd));
FUJITA Tomonorifce53842008-07-04 09:33:01 +0200703 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704}
705
706static int cdrom_has_defect_mgt(struct cdrom_device_info *cdi)
707{
708 struct packet_command cgc;
FUJITA Tomonorifce53842008-07-04 09:33:01 +0200709 char buffer[16];
Alexey Dobriyan56052d52005-12-01 17:10:40 -0500710 __be16 *feature_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 int ret;
712
FUJITA Tomonorifce53842008-07-04 09:33:01 +0200713 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
715 cgc.cmd[0] = GPCMD_GET_CONFIGURATION;
716 cgc.cmd[3] = CDF_HWDM;
FUJITA Tomonorifce53842008-07-04 09:33:01 +0200717 cgc.cmd[8] = sizeof(buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 cgc.quiet = 1;
719
720 if ((ret = cdi->ops->generic_packet(cdi, &cgc)))
FUJITA Tomonorifce53842008-07-04 09:33:01 +0200721 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Alexey Dobriyan56052d52005-12-01 17:10:40 -0500723 feature_code = (__be16 *) &buffer[sizeof(struct feature_header)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 if (be16_to_cpu(*feature_code) == CDF_HWDM)
FUJITA Tomonorifce53842008-07-04 09:33:01 +0200725 return 0;
726
727 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728}
729
730
731static int cdrom_is_random_writable(struct cdrom_device_info *cdi, int *write)
732{
733 struct rwrt_feature_desc rfd;
734 int ret;
735
736 *write = 0;
737
738 if ((ret = cdrom_get_random_writable(cdi, &rfd)))
739 return ret;
740
741 if (CDF_RWRT == be16_to_cpu(rfd.feature_code))
742 *write = 1;
743
744 return 0;
745}
746
747static int cdrom_media_erasable(struct cdrom_device_info *cdi)
748{
749 disc_information di;
750 int ret;
751
752 ret = cdrom_get_disc_info(cdi, &di);
753 if (ret < 0 || ret < offsetof(typeof(di), n_first_track))
754 return -1;
755
756 return di.erasable;
757}
758
759/*
760 * FIXME: check RO bit
761 */
762static int cdrom_dvdram_open_write(struct cdrom_device_info *cdi)
763{
764 int ret = cdrom_media_erasable(cdi);
765
766 /*
767 * allow writable open if media info read worked and media is
768 * erasable, _or_ if it fails since not all drives support it
769 */
770 if (!ret)
771 return 1;
772
773 return 0;
774}
775
776static int cdrom_mrw_open_write(struct cdrom_device_info *cdi)
777{
778 disc_information di;
779 int ret;
780
781 /*
782 * always reset to DMA lba space on open
783 */
784 if (cdrom_mrw_set_lba_space(cdi, MRW_LBA_DMA)) {
Joe Perchese597cd02010-07-01 08:24:32 +0200785 pr_err("failed setting lba address space\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 return 1;
787 }
788
789 ret = cdrom_get_disc_info(cdi, &di);
790 if (ret < 0 || ret < offsetof(typeof(di),disc_type))
791 return 1;
792
793 if (!di.erasable)
794 return 1;
795
796 /*
797 * mrw_status
798 * 0 - not MRW formatted
799 * 1 - MRW bgformat started, but not running or complete
800 * 2 - MRW bgformat in progress
801 * 3 - MRW formatting complete
802 */
803 ret = 0;
Joe Perchese597cd02010-07-01 08:24:32 +0200804 pr_info("open: mrw_status '%s'\n", mrw_format_status[di.mrw_status]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 if (!di.mrw_status)
806 ret = 1;
807 else if (di.mrw_status == CDM_MRW_BGFORMAT_INACTIVE &&
808 mrw_format_restart)
809 ret = cdrom_mrw_bgformat(cdi, 1);
810
811 return ret;
812}
813
814static int mo_open_write(struct cdrom_device_info *cdi)
815{
816 struct packet_command cgc;
FUJITA Tomonorifce53842008-07-04 09:33:01 +0200817 char buffer[255];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 int ret;
819
FUJITA Tomonorifce53842008-07-04 09:33:01 +0200820 init_cdrom_command(&cgc, &buffer, 4, CGC_DATA_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 cgc.quiet = 1;
822
823 /*
824 * obtain write protect information as per
825 * drivers/scsi/sd.c:sd_read_write_protect_flag
826 */
827
828 ret = cdrom_mode_sense(cdi, &cgc, GPMODE_ALL_PAGES, 0);
829 if (ret)
830 ret = cdrom_mode_sense(cdi, &cgc, GPMODE_VENDOR_PAGE, 0);
831 if (ret) {
832 cgc.buflen = 255;
833 ret = cdrom_mode_sense(cdi, &cgc, GPMODE_ALL_PAGES, 0);
834 }
835
836 /* drive gave us no info, let the user go ahead */
FUJITA Tomonorifce53842008-07-04 09:33:01 +0200837 if (ret)
838 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
FUJITA Tomonorifce53842008-07-04 09:33:01 +0200840 return buffer[3] & 0x80;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841}
842
843static int cdrom_ram_open_write(struct cdrom_device_info *cdi)
844{
845 struct rwrt_feature_desc rfd;
846 int ret;
847
848 if ((ret = cdrom_has_defect_mgt(cdi)))
849 return ret;
850
851 if ((ret = cdrom_get_random_writable(cdi, &rfd)))
852 return ret;
853 else if (CDF_RWRT == be16_to_cpu(rfd.feature_code))
854 ret = !rfd.curr;
855
Joe Perches5944b2c2014-05-04 17:05:02 -0700856 cd_dbg(CD_OPEN, "can open for random write\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 return ret;
858}
859
860static void cdrom_mmc3_profile(struct cdrom_device_info *cdi)
861{
862 struct packet_command cgc;
FUJITA Tomonorifce53842008-07-04 09:33:01 +0200863 char buffer[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 int ret, mmc3_profile;
865
FUJITA Tomonorifce53842008-07-04 09:33:01 +0200866 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
868 cgc.cmd[0] = GPCMD_GET_CONFIGURATION;
869 cgc.cmd[1] = 0;
870 cgc.cmd[2] = cgc.cmd[3] = 0; /* Starting Feature Number */
FUJITA Tomonorifce53842008-07-04 09:33:01 +0200871 cgc.cmd[8] = sizeof(buffer); /* Allocation Length */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 cgc.quiet = 1;
873
874 if ((ret = cdi->ops->generic_packet(cdi, &cgc)))
875 mmc3_profile = 0xffff;
876 else
877 mmc3_profile = (buffer[6] << 8) | buffer[7];
878
879 cdi->mmc3_profile = mmc3_profile;
880}
881
882static int cdrom_is_dvd_rw(struct cdrom_device_info *cdi)
883{
884 switch (cdi->mmc3_profile) {
885 case 0x12: /* DVD-RAM */
886 case 0x1A: /* DVD+RW */
Georgios Toptsidisf7e78682015-09-25 10:50:08 +0300887 case 0x43: /* BD-RE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 return 0;
889 default:
890 return 1;
891 }
892}
893
894/*
895 * returns 0 for ok to open write, non-0 to disallow
896 */
897static int cdrom_open_write(struct cdrom_device_info *cdi)
898{
899 int mrw, mrw_write, ram_write;
900 int ret = 1;
901
902 mrw = 0;
903 if (!cdrom_is_mrw(cdi, &mrw_write))
904 mrw = 1;
905
906 if (CDROM_CAN(CDC_MO_DRIVE))
907 ram_write = 1;
908 else
909 (void) cdrom_is_random_writable(cdi, &ram_write);
910
911 if (mrw)
912 cdi->mask &= ~CDC_MRW;
913 else
914 cdi->mask |= CDC_MRW;
915
916 if (mrw_write)
917 cdi->mask &= ~CDC_MRW_W;
918 else
919 cdi->mask |= CDC_MRW_W;
920
921 if (ram_write)
922 cdi->mask &= ~CDC_RAM;
923 else
924 cdi->mask |= CDC_RAM;
925
926 if (CDROM_CAN(CDC_MRW_W))
927 ret = cdrom_mrw_open_write(cdi);
928 else if (CDROM_CAN(CDC_DVD_RAM))
929 ret = cdrom_dvdram_open_write(cdi);
930 else if (CDROM_CAN(CDC_RAM) &&
931 !CDROM_CAN(CDC_CD_R|CDC_CD_RW|CDC_DVD|CDC_DVD_R|CDC_MRW|CDC_MO_DRIVE))
932 ret = cdrom_ram_open_write(cdi);
933 else if (CDROM_CAN(CDC_MO_DRIVE))
934 ret = mo_open_write(cdi);
935 else if (!cdrom_is_dvd_rw(cdi))
936 ret = 0;
937
938 return ret;
939}
940
941static void cdrom_dvd_rw_close_write(struct cdrom_device_info *cdi)
942{
943 struct packet_command cgc;
944
945 if (cdi->mmc3_profile != 0x1a) {
Joe Perches5944b2c2014-05-04 17:05:02 -0700946 cd_dbg(CD_CLOSE, "%s: No DVD+RW\n", cdi->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 return;
948 }
949
950 if (!cdi->media_written) {
Joe Perches5944b2c2014-05-04 17:05:02 -0700951 cd_dbg(CD_CLOSE, "%s: DVD+RW media clean\n", cdi->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 return;
953 }
954
Joe Perchese597cd02010-07-01 08:24:32 +0200955 pr_info("%s: dirty DVD+RW media, \"finalizing\"\n", cdi->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
957 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
958 cgc.cmd[0] = GPCMD_FLUSH_CACHE;
959 cgc.timeout = 30*HZ;
960 cdi->ops->generic_packet(cdi, &cgc);
961
962 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
963 cgc.cmd[0] = GPCMD_CLOSE_TRACK;
964 cgc.timeout = 3000*HZ;
965 cgc.quiet = 1;
966 cdi->ops->generic_packet(cdi, &cgc);
967
968 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
969 cgc.cmd[0] = GPCMD_CLOSE_TRACK;
970 cgc.cmd[2] = 2; /* Close session */
971 cgc.quiet = 1;
972 cgc.timeout = 3000*HZ;
973 cdi->ops->generic_packet(cdi, &cgc);
974
975 cdi->media_written = 0;
976}
977
978static int cdrom_close_write(struct cdrom_device_info *cdi)
979{
980#if 0
981 return cdrom_flush_cache(cdi);
982#else
983 return 0;
984#endif
985}
986
Joe Perchesa8033932014-05-04 17:05:11 -0700987/* badly broken, I know. Is due for a fixup anytime. */
988static void cdrom_count_tracks(struct cdrom_device_info *cdi, tracktype *tracks)
989{
990 struct cdrom_tochdr header;
991 struct cdrom_tocentry entry;
992 int ret, i;
993 tracks->data = 0;
994 tracks->audio = 0;
995 tracks->cdi = 0;
996 tracks->xa = 0;
997 tracks->error = 0;
998 cd_dbg(CD_COUNT_TRACKS, "entering cdrom_count_tracks\n");
Diego Elio Pettenò366ba7c2019-11-19 21:37:08 +0000999
1000 if (!CDROM_CAN(CDC_PLAY_AUDIO)) {
1001 tracks->error = CDS_NO_INFO;
1002 return;
1003 }
1004
Joe Perchesa8033932014-05-04 17:05:11 -07001005 /* Grab the TOC header so we can see how many tracks there are */
1006 ret = cdi->ops->audio_ioctl(cdi, CDROMREADTOCHDR, &header);
1007 if (ret) {
1008 if (ret == -ENOMEDIUM)
1009 tracks->error = CDS_NO_DISC;
1010 else
1011 tracks->error = CDS_NO_INFO;
1012 return;
1013 }
1014 /* check what type of tracks are on this disc */
1015 entry.cdte_format = CDROM_MSF;
1016 for (i = header.cdth_trk0; i <= header.cdth_trk1; i++) {
1017 entry.cdte_track = i;
1018 if (cdi->ops->audio_ioctl(cdi, CDROMREADTOCENTRY, &entry)) {
1019 tracks->error = CDS_NO_INFO;
1020 return;
1021 }
1022 if (entry.cdte_ctrl & CDROM_DATA_TRACK) {
1023 if (entry.cdte_format == 0x10)
1024 tracks->cdi++;
1025 else if (entry.cdte_format == 0x20)
1026 tracks->xa++;
1027 else
1028 tracks->data++;
1029 } else {
1030 tracks->audio++;
1031 }
1032 cd_dbg(CD_COUNT_TRACKS, "track %d: format=%d, ctrl=%d\n",
1033 i, entry.cdte_format, entry.cdte_ctrl);
1034 }
1035 cd_dbg(CD_COUNT_TRACKS, "disc has %d tracks: %d=audio %d=data %d=Cd-I %d=XA\n",
1036 header.cdth_trk1, tracks->audio, tracks->data,
1037 tracks->cdi, tracks->xa);
1038}
1039
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040static
Joe Perches82b91542014-05-04 17:05:05 -07001041int open_for_data(struct cdrom_device_info *cdi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042{
1043 int ret;
Kees Cook853fe1b2017-02-13 16:25:26 -08001044 const struct cdrom_device_ops *cdo = cdi->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 tracktype tracks;
Joe Perches5944b2c2014-05-04 17:05:02 -07001046 cd_dbg(CD_OPEN, "entering open_for_data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 /* Check if the driver can report drive status. If it can, we
1048 can do clever things. If it can't, well, we at least tried! */
1049 if (cdo->drive_status != NULL) {
1050 ret = cdo->drive_status(cdi, CDSL_CURRENT);
Joe Perches5944b2c2014-05-04 17:05:02 -07001051 cd_dbg(CD_OPEN, "drive_status=%d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 if (ret == CDS_TRAY_OPEN) {
Joe Perches5944b2c2014-05-04 17:05:02 -07001053 cd_dbg(CD_OPEN, "the tray is open...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 /* can/may i close it? */
1055 if (CDROM_CAN(CDC_CLOSE_TRAY) &&
1056 cdi->options & CDO_AUTO_CLOSE) {
Joe Perches5944b2c2014-05-04 17:05:02 -07001057 cd_dbg(CD_OPEN, "trying to close the tray\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 ret=cdo->tray_move(cdi,0);
1059 if (ret) {
Joe Perches5944b2c2014-05-04 17:05:02 -07001060 cd_dbg(CD_OPEN, "bummer. tried to close the tray but failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 /* Ignore the error from the low
1062 level driver. We don't care why it
1063 couldn't close the tray. We only care
1064 that there is no disc in the drive,
1065 since that is the _REAL_ problem here.*/
1066 ret=-ENOMEDIUM;
1067 goto clean_up_and_return;
1068 }
1069 } else {
Joe Perches5944b2c2014-05-04 17:05:02 -07001070 cd_dbg(CD_OPEN, "bummer. this drive can't close the tray.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 ret=-ENOMEDIUM;
1072 goto clean_up_and_return;
1073 }
1074 /* Ok, the door should be closed now.. Check again */
1075 ret = cdo->drive_status(cdi, CDSL_CURRENT);
1076 if ((ret == CDS_NO_DISC) || (ret==CDS_TRAY_OPEN)) {
Joe Perches5944b2c2014-05-04 17:05:02 -07001077 cd_dbg(CD_OPEN, "bummer. the tray is still not closed.\n");
1078 cd_dbg(CD_OPEN, "tray might not contain a medium\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 ret=-ENOMEDIUM;
1080 goto clean_up_and_return;
1081 }
Joe Perches5944b2c2014-05-04 17:05:02 -07001082 cd_dbg(CD_OPEN, "the tray is now closed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 }
1084 /* the door should be closed now, check for the disc */
1085 ret = cdo->drive_status(cdi, CDSL_CURRENT);
1086 if (ret!=CDS_DISC_OK) {
1087 ret = -ENOMEDIUM;
1088 goto clean_up_and_return;
1089 }
1090 }
1091 cdrom_count_tracks(cdi, &tracks);
1092 if (tracks.error == CDS_NO_DISC) {
Joe Perches5944b2c2014-05-04 17:05:02 -07001093 cd_dbg(CD_OPEN, "bummer. no disc.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 ret=-ENOMEDIUM;
1095 goto clean_up_and_return;
1096 }
1097 /* CD-Players which don't use O_NONBLOCK, workman
1098 * for example, need bit CDO_CHECK_TYPE cleared! */
1099 if (tracks.data==0) {
1100 if (cdi->options & CDO_CHECK_TYPE) {
1101 /* give people a warning shot, now that CDO_CHECK_TYPE
1102 is the default case! */
Joe Perches5944b2c2014-05-04 17:05:02 -07001103 cd_dbg(CD_OPEN, "bummer. wrong media type.\n");
1104 cd_dbg(CD_WARNING, "pid %d must open device O_NONBLOCK!\n",
1105 (unsigned int)task_pid_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 ret=-EMEDIUMTYPE;
1107 goto clean_up_and_return;
1108 }
1109 else {
Joe Perches5944b2c2014-05-04 17:05:02 -07001110 cd_dbg(CD_OPEN, "wrong media type, but CDO_CHECK_TYPE not set\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 }
1112 }
1113
Joe Perches5944b2c2014-05-04 17:05:02 -07001114 cd_dbg(CD_OPEN, "all seems well, opening the devicen");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
1116 /* all seems well, we can open the device */
1117 ret = cdo->open(cdi, 0); /* open for data */
Joe Perches5944b2c2014-05-04 17:05:02 -07001118 cd_dbg(CD_OPEN, "opening the device gave me %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 /* After all this careful checking, we shouldn't have problems
1120 opening the device, but we don't want the device locked if
1121 this somehow fails... */
1122 if (ret) {
Joe Perches5944b2c2014-05-04 17:05:02 -07001123 cd_dbg(CD_OPEN, "open device failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 goto clean_up_and_return;
1125 }
1126 if (CDROM_CAN(CDC_LOCK) && (cdi->options & CDO_LOCK)) {
1127 cdo->lock_door(cdi, 1);
Joe Perches5944b2c2014-05-04 17:05:02 -07001128 cd_dbg(CD_OPEN, "door locked\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 }
Joe Perches5944b2c2014-05-04 17:05:02 -07001130 cd_dbg(CD_OPEN, "device opened successfully\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 return ret;
1132
1133 /* Something failed. Try to unlock the drive, because some drivers
1134 (notably ide-cd) lock the drive after every command. This produced
1135 a nasty bug where after mount failed, the drive would remain locked!
1136 This ensures that the drive gets unlocked after a mount fails. This
1137 is a goto to avoid bloating the driver with redundant code. */
1138clean_up_and_return:
Joe Perches5944b2c2014-05-04 17:05:02 -07001139 cd_dbg(CD_OPEN, "open failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 if (CDROM_CAN(CDC_LOCK) && cdi->options & CDO_LOCK) {
1141 cdo->lock_door(cdi, 0);
Joe Perches5944b2c2014-05-04 17:05:02 -07001142 cd_dbg(CD_OPEN, "door unlocked\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 }
1144 return ret;
1145}
1146
Joe Perches82b91542014-05-04 17:05:05 -07001147/* We use the open-option O_NONBLOCK to indicate that the
1148 * purpose of opening is only for subsequent ioctl() calls; no device
1149 * integrity checks are performed.
1150 *
1151 * We hope that all cd-player programs will adopt this convention. It
1152 * is in their own interest: device control becomes a lot easier
1153 * this way.
1154 */
1155int cdrom_open(struct cdrom_device_info *cdi, struct block_device *bdev,
1156 fmode_t mode)
1157{
1158 int ret;
1159
1160 cd_dbg(CD_OPEN, "entering cdrom_open\n");
1161
Joe Perches82b91542014-05-04 17:05:05 -07001162 /* if this was a O_NONBLOCK open and we should honor the flags,
1163 * do a quick open without drive/disc integrity checks. */
1164 cdi->use_count++;
1165 if ((mode & FMODE_NDELAY) && (cdi->options & CDO_USE_FFLAGS)) {
1166 ret = cdi->ops->open(cdi, 1);
1167 } else {
1168 ret = open_for_data(cdi);
1169 if (ret)
1170 goto err;
Diego Elio Pettenò366ba7c2019-11-19 21:37:08 +00001171 if (CDROM_CAN(CDC_GENERIC_PACKET))
1172 cdrom_mmc3_profile(cdi);
Joe Perches82b91542014-05-04 17:05:05 -07001173 if (mode & FMODE_WRITE) {
1174 ret = -EROFS;
1175 if (cdrom_open_write(cdi))
1176 goto err_release;
1177 if (!CDROM_CAN(CDC_RAM))
1178 goto err_release;
1179 ret = 0;
1180 cdi->media_written = 0;
1181 }
1182 }
1183
1184 if (ret)
1185 goto err;
1186
1187 cd_dbg(CD_OPEN, "Use count for \"/dev/%s\" now %d\n",
1188 cdi->name, cdi->use_count);
1189 return 0;
1190err_release:
1191 if (CDROM_CAN(CDC_LOCK) && cdi->options & CDO_LOCK) {
1192 cdi->ops->lock_door(cdi, 0);
1193 cd_dbg(CD_OPEN, "door unlocked\n");
1194 }
1195 cdi->ops->release(cdi);
1196err:
1197 cdi->use_count--;
1198 return ret;
1199}
1200
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201/* This code is similar to that in open_for_data. The routine is called
1202 whenever an audio play operation is requested.
1203*/
Kees Cook853fe1b2017-02-13 16:25:26 -08001204static int check_for_audio_disc(struct cdrom_device_info *cdi,
1205 const struct cdrom_device_ops *cdo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
1207 int ret;
1208 tracktype tracks;
Joe Perches5944b2c2014-05-04 17:05:02 -07001209 cd_dbg(CD_OPEN, "entering check_for_audio_disc\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 if (!(cdi->options & CDO_CHECK_TYPE))
1211 return 0;
1212 if (cdo->drive_status != NULL) {
1213 ret = cdo->drive_status(cdi, CDSL_CURRENT);
Joe Perches5944b2c2014-05-04 17:05:02 -07001214 cd_dbg(CD_OPEN, "drive_status=%d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 if (ret == CDS_TRAY_OPEN) {
Joe Perches5944b2c2014-05-04 17:05:02 -07001216 cd_dbg(CD_OPEN, "the tray is open...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 /* can/may i close it? */
1218 if (CDROM_CAN(CDC_CLOSE_TRAY) &&
1219 cdi->options & CDO_AUTO_CLOSE) {
Joe Perches5944b2c2014-05-04 17:05:02 -07001220 cd_dbg(CD_OPEN, "trying to close the tray\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 ret=cdo->tray_move(cdi,0);
1222 if (ret) {
Joe Perches5944b2c2014-05-04 17:05:02 -07001223 cd_dbg(CD_OPEN, "bummer. tried to close tray but failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 /* Ignore the error from the low
1225 level driver. We don't care why it
1226 couldn't close the tray. We only care
1227 that there is no disc in the drive,
1228 since that is the _REAL_ problem here.*/
1229 return -ENOMEDIUM;
1230 }
1231 } else {
Joe Perches5944b2c2014-05-04 17:05:02 -07001232 cd_dbg(CD_OPEN, "bummer. this driver can't close the tray.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 return -ENOMEDIUM;
1234 }
1235 /* Ok, the door should be closed now.. Check again */
1236 ret = cdo->drive_status(cdi, CDSL_CURRENT);
1237 if ((ret == CDS_NO_DISC) || (ret==CDS_TRAY_OPEN)) {
Joe Perches5944b2c2014-05-04 17:05:02 -07001238 cd_dbg(CD_OPEN, "bummer. the tray is still not closed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 return -ENOMEDIUM;
1240 }
1241 if (ret!=CDS_DISC_OK) {
Joe Perches5944b2c2014-05-04 17:05:02 -07001242 cd_dbg(CD_OPEN, "bummer. disc isn't ready.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 return -EIO;
1244 }
Joe Perches5944b2c2014-05-04 17:05:02 -07001245 cd_dbg(CD_OPEN, "the tray is now closed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 }
1247 }
1248 cdrom_count_tracks(cdi, &tracks);
1249 if (tracks.error)
1250 return(tracks.error);
1251
1252 if (tracks.audio==0)
1253 return -EMEDIUMTYPE;
1254
1255 return 0;
1256}
1257
Al Virobbc1cc92007-10-07 17:54:28 -04001258void cdrom_release(struct cdrom_device_info *cdi, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259{
Kees Cook853fe1b2017-02-13 16:25:26 -08001260 const struct cdrom_device_ops *cdo = cdi->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 int opened_for_data;
1262
Joe Perches5944b2c2014-05-04 17:05:02 -07001263 cd_dbg(CD_CLOSE, "entering cdrom_release\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
1265 if (cdi->use_count > 0)
1266 cdi->use_count--;
Borislav Petkov35841f72008-02-06 01:38:08 -08001267
1268 if (cdi->use_count == 0) {
Joe Perches5944b2c2014-05-04 17:05:02 -07001269 cd_dbg(CD_CLOSE, "Use count for \"/dev/%s\" now zero\n",
1270 cdi->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 cdrom_dvd_rw_close_write(cdi);
Borislav Petkov35841f72008-02-06 01:38:08 -08001272
Paolo Bonzinicdccaa92012-02-08 20:03:14 +01001273 if ((cdo->capability & CDC_LOCK) && !cdi->keeplocked) {
Joe Perches5944b2c2014-05-04 17:05:02 -07001274 cd_dbg(CD_CLOSE, "Unlocking door!\n");
Borislav Petkov35841f72008-02-06 01:38:08 -08001275 cdo->lock_door(cdi, 0);
1276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 }
Borislav Petkov35841f72008-02-06 01:38:08 -08001278
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 opened_for_data = !(cdi->options & CDO_USE_FFLAGS) ||
Al Virobbc1cc92007-10-07 17:54:28 -04001280 !(mode & FMODE_NDELAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
1282 /*
1283 * flush cache on last write release
1284 */
1285 if (CDROM_CAN(CDC_RAM) && !cdi->use_count && cdi->for_data)
1286 cdrom_close_write(cdi);
1287
1288 cdo->release(cdi);
1289 if (cdi->use_count == 0) { /* last process that closes dev*/
1290 if (opened_for_data &&
1291 cdi->options & CDO_AUTO_EJECT && CDROM_CAN(CDC_OPEN_TRAY))
1292 cdo->tray_move(cdi, 1);
1293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294}
1295
1296static int cdrom_read_mech_status(struct cdrom_device_info *cdi,
1297 struct cdrom_changer_info *buf)
1298{
1299 struct packet_command cgc;
Kees Cook853fe1b2017-02-13 16:25:26 -08001300 const struct cdrom_device_ops *cdo = cdi->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 int length;
1302
1303 /*
1304 * Sanyo changer isn't spec compliant (doesn't use regular change
1305 * LOAD_UNLOAD command, and it doesn't implement the mech status
1306 * command below
1307 */
1308 if (cdi->sanyo_slot) {
1309 buf->hdr.nslots = 3;
1310 buf->hdr.curslot = cdi->sanyo_slot == 3 ? 0 : cdi->sanyo_slot;
1311 for (length = 0; length < 3; length++) {
1312 buf->slots[length].disc_present = 1;
1313 buf->slots[length].change = 0;
1314 }
1315 return 0;
1316 }
1317
1318 length = sizeof(struct cdrom_mechstat_header) +
1319 cdi->capacity * sizeof(struct cdrom_slot);
1320
1321 init_cdrom_command(&cgc, buf, length, CGC_DATA_READ);
1322 cgc.cmd[0] = GPCMD_MECHANISM_STATUS;
1323 cgc.cmd[8] = (length >> 8) & 0xff;
1324 cgc.cmd[9] = length & 0xff;
1325 return cdo->generic_packet(cdi, &cgc);
1326}
1327
1328static int cdrom_slot_status(struct cdrom_device_info *cdi, int slot)
1329{
1330 struct cdrom_changer_info *info;
1331 int ret;
1332
Joe Perches5944b2c2014-05-04 17:05:02 -07001333 cd_dbg(CD_CHANGER, "entering cdrom_slot_status()\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 if (cdi->sanyo_slot)
1335 return CDS_NO_INFO;
1336
1337 info = kmalloc(sizeof(*info), GFP_KERNEL);
1338 if (!info)
1339 return -ENOMEM;
1340
1341 if ((ret = cdrom_read_mech_status(cdi, info)))
1342 goto out_free;
1343
1344 if (info->slots[slot].disc_present)
1345 ret = CDS_DISC_OK;
1346 else
1347 ret = CDS_NO_DISC;
1348
1349out_free:
1350 kfree(info);
1351 return ret;
1352}
1353
1354/* Return the number of slots for an ATAPI/SCSI cdrom,
1355 * return 1 if not a changer.
1356 */
1357int cdrom_number_of_slots(struct cdrom_device_info *cdi)
1358{
1359 int status;
1360 int nslots = 1;
1361 struct cdrom_changer_info *info;
1362
Joe Perches5944b2c2014-05-04 17:05:02 -07001363 cd_dbg(CD_CHANGER, "entering cdrom_number_of_slots()\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 /* cdrom_read_mech_status requires a valid value for capacity: */
1365 cdi->capacity = 0;
1366
1367 info = kmalloc(sizeof(*info), GFP_KERNEL);
1368 if (!info)
1369 return -ENOMEM;
1370
1371 if ((status = cdrom_read_mech_status(cdi, info)) == 0)
1372 nslots = info->hdr.nslots;
1373
1374 kfree(info);
1375 return nslots;
1376}
1377
1378
1379/* If SLOT < 0, unload the current slot. Otherwise, try to load SLOT. */
1380static int cdrom_load_unload(struct cdrom_device_info *cdi, int slot)
1381{
1382 struct packet_command cgc;
1383
Joe Perches5944b2c2014-05-04 17:05:02 -07001384 cd_dbg(CD_CHANGER, "entering cdrom_load_unload()\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 if (cdi->sanyo_slot && slot < 0)
1386 return 0;
1387
1388 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
1389 cgc.cmd[0] = GPCMD_LOAD_UNLOAD;
1390 cgc.cmd[4] = 2 + (slot >= 0);
1391 cgc.cmd[8] = slot;
1392 cgc.timeout = 60 * HZ;
1393
1394 /* The Sanyo 3 CD changer uses byte 7 of the
1395 GPCMD_TEST_UNIT_READY to command to switch CDs instead of
1396 using the GPCMD_LOAD_UNLOAD opcode. */
1397 if (cdi->sanyo_slot && -1 < slot) {
1398 cgc.cmd[0] = GPCMD_TEST_UNIT_READY;
1399 cgc.cmd[7] = slot;
1400 cgc.cmd[4] = cgc.cmd[8] = 0;
1401 cdi->sanyo_slot = slot ? slot : 3;
1402 }
1403
1404 return cdi->ops->generic_packet(cdi, &cgc);
1405}
1406
1407static int cdrom_select_disc(struct cdrom_device_info *cdi, int slot)
1408{
1409 struct cdrom_changer_info *info;
1410 int curslot;
1411 int ret;
1412
Joe Perches5944b2c2014-05-04 17:05:02 -07001413 cd_dbg(CD_CHANGER, "entering cdrom_select_disc()\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 if (!CDROM_CAN(CDC_SELECT_DISC))
1415 return -EDRIVE_CANT_DO_THIS;
1416
Tejun Heo2d921722010-12-08 20:57:38 +01001417 if (cdi->ops->check_events)
1418 cdi->ops->check_events(cdi, 0, slot);
1419 else
1420 cdi->ops->media_changed(cdi, slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
1422 if (slot == CDSL_NONE) {
1423 /* set media changed bits, on both queues */
1424 cdi->mc_flags = 0x3;
1425 return cdrom_load_unload(cdi, -1);
1426 }
1427
1428 info = kmalloc(sizeof(*info), GFP_KERNEL);
1429 if (!info)
1430 return -ENOMEM;
1431
1432 if ((ret = cdrom_read_mech_status(cdi, info))) {
1433 kfree(info);
1434 return ret;
1435 }
1436
1437 curslot = info->hdr.curslot;
1438 kfree(info);
1439
Paolo Bonzinicdccaa92012-02-08 20:03:14 +01001440 if (cdi->use_count > 1 || cdi->keeplocked) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 if (slot == CDSL_CURRENT) {
1442 return curslot;
1443 } else {
1444 return -EBUSY;
1445 }
1446 }
1447
1448 /* Specifying CDSL_CURRENT will attempt to load the currnet slot,
1449 which is useful if it had been previously unloaded.
1450 Whether it can or not, it returns the current slot.
1451 Similarly, if slot happens to be the current one, we still
1452 try and load it. */
1453 if (slot == CDSL_CURRENT)
1454 slot = curslot;
1455
1456 /* set media changed bits on both queues */
1457 cdi->mc_flags = 0x3;
1458 if ((ret = cdrom_load_unload(cdi, slot)))
1459 return ret;
1460
1461 return slot;
1462}
1463
Tejun Heo2d921722010-12-08 20:57:38 +01001464/*
1465 * As cdrom implements an extra ioctl consumer for media changed
1466 * event, it needs to buffer ->check_events() output, such that event
1467 * is not lost for both the usual VFS and ioctl paths.
1468 * cdi->{vfs|ioctl}_events are used to buffer pending events for each
1469 * path.
1470 *
1471 * XXX: Locking is non-existent. cdi->ops->check_events() can be
1472 * called in parallel and buffering fields are accessed without any
1473 * exclusion. The original media_changed code had the same problem.
1474 * It might be better to simply deprecate CDROM_MEDIA_CHANGED ioctl
1475 * and remove this cruft altogether. It doesn't have much usefulness
1476 * at this point.
1477 */
1478static void cdrom_update_events(struct cdrom_device_info *cdi,
1479 unsigned int clearing)
1480{
1481 unsigned int events;
1482
1483 events = cdi->ops->check_events(cdi, clearing, CDSL_CURRENT);
1484 cdi->vfs_events |= events;
1485 cdi->ioctl_events |= events;
1486}
1487
1488unsigned int cdrom_check_events(struct cdrom_device_info *cdi,
1489 unsigned int clearing)
1490{
1491 unsigned int events;
1492
1493 cdrom_update_events(cdi, clearing);
1494 events = cdi->vfs_events;
1495 cdi->vfs_events = 0;
1496 return events;
1497}
Jens Axboe30a7caa2010-12-16 17:58:50 +01001498EXPORT_SYMBOL(cdrom_check_events);
Tejun Heo2d921722010-12-08 20:57:38 +01001499
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500/* We want to make media_changed accessible to the user through an
1501 * ioctl. The main problem now is that we must double-buffer the
1502 * low-level implementation, to assure that the VFS and the user both
1503 * see a medium change once.
1504 */
1505
1506static
1507int media_changed(struct cdrom_device_info *cdi, int queue)
1508{
1509 unsigned int mask = (1 << (queue & 1));
1510 int ret = !!(cdi->mc_flags & mask);
Tejun Heo2d921722010-12-08 20:57:38 +01001511 bool changed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
1513 if (!CDROM_CAN(CDC_MEDIA_CHANGED))
Tejun Heo2d921722010-12-08 20:57:38 +01001514 return ret;
1515
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 /* changed since last call? */
Tejun Heo2d921722010-12-08 20:57:38 +01001517 if (cdi->ops->check_events) {
1518 BUG_ON(!queue); /* shouldn't be called from VFS path */
1519 cdrom_update_events(cdi, DISK_EVENT_MEDIA_CHANGE);
1520 changed = cdi->ioctl_events & DISK_EVENT_MEDIA_CHANGE;
1521 cdi->ioctl_events = 0;
1522 } else
1523 changed = cdi->ops->media_changed(cdi, CDSL_CURRENT);
1524
1525 if (changed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 cdi->mc_flags = 0x3; /* set bit on both queues */
1527 ret |= 1;
1528 cdi->media_written = 0;
1529 }
Tejun Heo2d921722010-12-08 20:57:38 +01001530
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 cdi->mc_flags &= ~mask; /* clear bit */
1532 return ret;
1533}
1534
1535int cdrom_media_changed(struct cdrom_device_info *cdi)
1536{
1537 /* This talks to the VFS, which doesn't like errors - just 1 or 0.
1538 * Returning "0" is always safe (media hasn't been changed). Do that
1539 * if the low-level cdrom driver dosn't support media changed. */
1540 if (cdi == NULL || cdi->ops->media_changed == NULL)
1541 return 0;
1542 if (!CDROM_CAN(CDC_MEDIA_CHANGED))
1543 return 0;
1544 return media_changed(cdi, 0);
1545}
1546
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547/* Requests to the low-level drivers will /always/ be done in the
1548 following format convention:
1549
1550 CDROM_LBA: all data-related requests.
1551 CDROM_MSF: all audio-related requests.
1552
1553 However, a low-level implementation is allowed to refuse this
1554 request, and return information in its own favorite format.
1555
1556 It doesn't make sense /at all/ to ask for a play_audio in LBA
1557 format, or ask for multi-session info in MSF format. However, for
1558 backward compatibility these format requests will be satisfied, but
1559 the requests to the low-level drivers will be sanitized in the more
1560 meaningful format indicated above.
1561 */
1562
1563static
1564void sanitize_format(union cdrom_addr *addr,
1565 u_char * curr, u_char requested)
1566{
1567 if (*curr == requested)
1568 return; /* nothing to be done! */
1569 if (requested == CDROM_LBA) {
1570 addr->lba = (int) addr->msf.frame +
1571 75 * (addr->msf.second - 2 + 60 * addr->msf.minute);
1572 } else { /* CDROM_MSF */
1573 int lba = addr->lba;
1574 addr->msf.frame = lba % 75;
1575 lba /= 75;
1576 lba += 2;
1577 addr->msf.second = lba % 60;
1578 addr->msf.minute = lba / 60;
1579 }
1580 *curr = requested;
1581}
1582
1583void init_cdrom_command(struct packet_command *cgc, void *buf, int len,
1584 int type)
1585{
1586 memset(cgc, 0, sizeof(struct packet_command));
1587 if (buf)
1588 memset(buf, 0, len);
1589 cgc->buffer = (char *) buf;
1590 cgc->buflen = len;
1591 cgc->data_direction = type;
Jens Axboe2e11c202007-01-03 08:10:35 +01001592 cgc->timeout = CDROM_DEF_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593}
1594
1595/* DVD handling */
1596
1597#define copy_key(dest,src) memcpy((dest), (src), sizeof(dvd_key))
1598#define copy_chal(dest,src) memcpy((dest), (src), sizeof(dvd_challenge))
1599
1600static void setup_report_key(struct packet_command *cgc, unsigned agid, unsigned type)
1601{
1602 cgc->cmd[0] = GPCMD_REPORT_KEY;
1603 cgc->cmd[10] = type | (agid << 6);
1604 switch (type) {
1605 case 0: case 8: case 5: {
1606 cgc->buflen = 8;
1607 break;
1608 }
1609 case 1: {
1610 cgc->buflen = 16;
1611 break;
1612 }
1613 case 2: case 4: {
1614 cgc->buflen = 12;
1615 break;
1616 }
1617 }
1618 cgc->cmd[9] = cgc->buflen;
1619 cgc->data_direction = CGC_DATA_READ;
1620}
1621
1622static void setup_send_key(struct packet_command *cgc, unsigned agid, unsigned type)
1623{
1624 cgc->cmd[0] = GPCMD_SEND_KEY;
1625 cgc->cmd[10] = type | (agid << 6);
1626 switch (type) {
1627 case 1: {
1628 cgc->buflen = 16;
1629 break;
1630 }
1631 case 3: {
1632 cgc->buflen = 12;
1633 break;
1634 }
1635 case 6: {
1636 cgc->buflen = 8;
1637 break;
1638 }
1639 }
1640 cgc->cmd[9] = cgc->buflen;
1641 cgc->data_direction = CGC_DATA_WRITE;
1642}
1643
1644static int dvd_do_auth(struct cdrom_device_info *cdi, dvd_authinfo *ai)
1645{
1646 int ret;
FUJITA Tomonorifce53842008-07-04 09:33:01 +02001647 u_char buf[20];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 struct packet_command cgc;
Kees Cook853fe1b2017-02-13 16:25:26 -08001649 const struct cdrom_device_ops *cdo = cdi->ops;
FUJITA Tomonorifce53842008-07-04 09:33:01 +02001650 rpc_state_t rpc_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
FUJITA Tomonorifce53842008-07-04 09:33:01 +02001652 memset(buf, 0, sizeof(buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 init_cdrom_command(&cgc, buf, 0, CGC_DATA_READ);
1654
1655 switch (ai->type) {
1656 /* LU data send */
1657 case DVD_LU_SEND_AGID:
Joe Perches5944b2c2014-05-04 17:05:02 -07001658 cd_dbg(CD_DVD, "entering DVD_LU_SEND_AGID\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 cgc.quiet = 1;
1660 setup_report_key(&cgc, ai->lsa.agid, 0);
1661
1662 if ((ret = cdo->generic_packet(cdi, &cgc)))
FUJITA Tomonorifce53842008-07-04 09:33:01 +02001663 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
1665 ai->lsa.agid = buf[7] >> 6;
1666 /* Returning data, let host change state */
1667 break;
1668
1669 case DVD_LU_SEND_KEY1:
Joe Perches5944b2c2014-05-04 17:05:02 -07001670 cd_dbg(CD_DVD, "entering DVD_LU_SEND_KEY1\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 setup_report_key(&cgc, ai->lsk.agid, 2);
1672
1673 if ((ret = cdo->generic_packet(cdi, &cgc)))
FUJITA Tomonorifce53842008-07-04 09:33:01 +02001674 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
1676 copy_key(ai->lsk.key, &buf[4]);
1677 /* Returning data, let host change state */
1678 break;
1679
1680 case DVD_LU_SEND_CHALLENGE:
Joe Perches5944b2c2014-05-04 17:05:02 -07001681 cd_dbg(CD_DVD, "entering DVD_LU_SEND_CHALLENGE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 setup_report_key(&cgc, ai->lsc.agid, 1);
1683
1684 if ((ret = cdo->generic_packet(cdi, &cgc)))
FUJITA Tomonorifce53842008-07-04 09:33:01 +02001685 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686
1687 copy_chal(ai->lsc.chal, &buf[4]);
1688 /* Returning data, let host change state */
1689 break;
1690
1691 /* Post-auth key */
1692 case DVD_LU_SEND_TITLE_KEY:
Joe Perches5944b2c2014-05-04 17:05:02 -07001693 cd_dbg(CD_DVD, "entering DVD_LU_SEND_TITLE_KEY\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 cgc.quiet = 1;
1695 setup_report_key(&cgc, ai->lstk.agid, 4);
1696 cgc.cmd[5] = ai->lstk.lba;
1697 cgc.cmd[4] = ai->lstk.lba >> 8;
1698 cgc.cmd[3] = ai->lstk.lba >> 16;
1699 cgc.cmd[2] = ai->lstk.lba >> 24;
1700
1701 if ((ret = cdo->generic_packet(cdi, &cgc)))
FUJITA Tomonorifce53842008-07-04 09:33:01 +02001702 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
1704 ai->lstk.cpm = (buf[4] >> 7) & 1;
1705 ai->lstk.cp_sec = (buf[4] >> 6) & 1;
1706 ai->lstk.cgms = (buf[4] >> 4) & 3;
1707 copy_key(ai->lstk.title_key, &buf[5]);
1708 /* Returning data, let host change state */
1709 break;
1710
1711 case DVD_LU_SEND_ASF:
Joe Perches5944b2c2014-05-04 17:05:02 -07001712 cd_dbg(CD_DVD, "entering DVD_LU_SEND_ASF\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 setup_report_key(&cgc, ai->lsasf.agid, 5);
1714
1715 if ((ret = cdo->generic_packet(cdi, &cgc)))
FUJITA Tomonorifce53842008-07-04 09:33:01 +02001716 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
1718 ai->lsasf.asf = buf[7] & 1;
1719 break;
1720
1721 /* LU data receive (LU changes state) */
1722 case DVD_HOST_SEND_CHALLENGE:
Joe Perches5944b2c2014-05-04 17:05:02 -07001723 cd_dbg(CD_DVD, "entering DVD_HOST_SEND_CHALLENGE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 setup_send_key(&cgc, ai->hsc.agid, 1);
1725 buf[1] = 0xe;
1726 copy_chal(&buf[4], ai->hsc.chal);
1727
1728 if ((ret = cdo->generic_packet(cdi, &cgc)))
FUJITA Tomonorifce53842008-07-04 09:33:01 +02001729 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
1731 ai->type = DVD_LU_SEND_KEY1;
1732 break;
1733
1734 case DVD_HOST_SEND_KEY2:
Joe Perches5944b2c2014-05-04 17:05:02 -07001735 cd_dbg(CD_DVD, "entering DVD_HOST_SEND_KEY2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 setup_send_key(&cgc, ai->hsk.agid, 3);
1737 buf[1] = 0xa;
1738 copy_key(&buf[4], ai->hsk.key);
1739
1740 if ((ret = cdo->generic_packet(cdi, &cgc))) {
1741 ai->type = DVD_AUTH_FAILURE;
FUJITA Tomonorifce53842008-07-04 09:33:01 +02001742 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 }
1744 ai->type = DVD_AUTH_ESTABLISHED;
1745 break;
1746
1747 /* Misc */
1748 case DVD_INVALIDATE_AGID:
1749 cgc.quiet = 1;
Joe Perches5944b2c2014-05-04 17:05:02 -07001750 cd_dbg(CD_DVD, "entering DVD_INVALIDATE_AGID\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 setup_report_key(&cgc, ai->lsa.agid, 0x3f);
1752 if ((ret = cdo->generic_packet(cdi, &cgc)))
FUJITA Tomonorifce53842008-07-04 09:33:01 +02001753 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 break;
1755
1756 /* Get region settings */
1757 case DVD_LU_SEND_RPC_STATE:
Joe Perches5944b2c2014-05-04 17:05:02 -07001758 cd_dbg(CD_DVD, "entering DVD_LU_SEND_RPC_STATE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 setup_report_key(&cgc, 0, 8);
FUJITA Tomonorifce53842008-07-04 09:33:01 +02001760 memset(&rpc_state, 0, sizeof(rpc_state_t));
1761 cgc.buffer = (char *) &rpc_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
1763 if ((ret = cdo->generic_packet(cdi, &cgc)))
FUJITA Tomonorifce53842008-07-04 09:33:01 +02001764 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
FUJITA Tomonorifce53842008-07-04 09:33:01 +02001766 ai->lrpcs.type = rpc_state.type_code;
1767 ai->lrpcs.vra = rpc_state.vra;
1768 ai->lrpcs.ucca = rpc_state.ucca;
1769 ai->lrpcs.region_mask = rpc_state.region_mask;
1770 ai->lrpcs.rpc_scheme = rpc_state.rpc_scheme;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 break;
1772
1773 /* Set region settings */
1774 case DVD_HOST_SEND_RPC_STATE:
Joe Perches5944b2c2014-05-04 17:05:02 -07001775 cd_dbg(CD_DVD, "entering DVD_HOST_SEND_RPC_STATE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 setup_send_key(&cgc, 0, 6);
1777 buf[1] = 6;
1778 buf[4] = ai->hrpcs.pdrc;
1779
1780 if ((ret = cdo->generic_packet(cdi, &cgc)))
FUJITA Tomonorifce53842008-07-04 09:33:01 +02001781 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 break;
1783
1784 default:
Joe Perches5944b2c2014-05-04 17:05:02 -07001785 cd_dbg(CD_WARNING, "Invalid DVD key ioctl (%d)\n", ai->type);
FUJITA Tomonorifce53842008-07-04 09:33:01 +02001786 return -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 }
FUJITA Tomonorifce53842008-07-04 09:33:01 +02001788
1789 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790}
1791
Marcin Slusarzd1941392008-11-16 19:06:37 +01001792static int dvd_read_physical(struct cdrom_device_info *cdi, dvd_struct *s,
1793 struct packet_command *cgc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794{
FUJITA Tomonorifce53842008-07-04 09:33:01 +02001795 unsigned char buf[21], *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 struct dvd_layer *layer;
Kees Cook853fe1b2017-02-13 16:25:26 -08001797 const struct cdrom_device_ops *cdo = cdi->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 int ret, layer_num = s->physical.layer_num;
1799
1800 if (layer_num >= DVD_LAYERS)
1801 return -EINVAL;
1802
Marcin Slusarzd1941392008-11-16 19:06:37 +01001803 init_cdrom_command(cgc, buf, sizeof(buf), CGC_DATA_READ);
1804 cgc->cmd[0] = GPCMD_READ_DVD_STRUCTURE;
1805 cgc->cmd[6] = layer_num;
1806 cgc->cmd[7] = s->type;
1807 cgc->cmd[9] = cgc->buflen & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
1809 /*
1810 * refrain from reporting errors on non-existing layers (mainly)
1811 */
Marcin Slusarzd1941392008-11-16 19:06:37 +01001812 cgc->quiet = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
Marcin Slusarzd1941392008-11-16 19:06:37 +01001814 ret = cdo->generic_packet(cdi, cgc);
1815 if (ret)
FUJITA Tomonorifce53842008-07-04 09:33:01 +02001816 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
1818 base = &buf[4];
1819 layer = &s->physical.layer[layer_num];
1820
1821 /*
1822 * place the data... really ugly, but at least we won't have to
1823 * worry about endianess in userspace.
1824 */
1825 memset(layer, 0, sizeof(*layer));
1826 layer->book_version = base[0] & 0xf;
1827 layer->book_type = base[0] >> 4;
1828 layer->min_rate = base[1] & 0xf;
1829 layer->disc_size = base[1] >> 4;
1830 layer->layer_type = base[2] & 0xf;
1831 layer->track_path = (base[2] >> 4) & 1;
1832 layer->nlayers = (base[2] >> 5) & 3;
1833 layer->track_density = base[3] & 0xf;
1834 layer->linear_density = base[3] >> 4;
1835 layer->start_sector = base[5] << 16 | base[6] << 8 | base[7];
1836 layer->end_sector = base[9] << 16 | base[10] << 8 | base[11];
1837 layer->end_sector_l0 = base[13] << 16 | base[14] << 8 | base[15];
1838 layer->bca = base[16] >> 7;
1839
FUJITA Tomonorifce53842008-07-04 09:33:01 +02001840 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841}
1842
Marcin Slusarzd1941392008-11-16 19:06:37 +01001843static int dvd_read_copyright(struct cdrom_device_info *cdi, dvd_struct *s,
1844 struct packet_command *cgc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845{
1846 int ret;
FUJITA Tomonorifce53842008-07-04 09:33:01 +02001847 u_char buf[8];
Kees Cook853fe1b2017-02-13 16:25:26 -08001848 const struct cdrom_device_ops *cdo = cdi->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
Marcin Slusarzd1941392008-11-16 19:06:37 +01001850 init_cdrom_command(cgc, buf, sizeof(buf), CGC_DATA_READ);
1851 cgc->cmd[0] = GPCMD_READ_DVD_STRUCTURE;
1852 cgc->cmd[6] = s->copyright.layer_num;
1853 cgc->cmd[7] = s->type;
1854 cgc->cmd[8] = cgc->buflen >> 8;
1855 cgc->cmd[9] = cgc->buflen & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
Marcin Slusarzd1941392008-11-16 19:06:37 +01001857 ret = cdo->generic_packet(cdi, cgc);
1858 if (ret)
FUJITA Tomonorifce53842008-07-04 09:33:01 +02001859 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
1861 s->copyright.cpst = buf[4];
1862 s->copyright.rmi = buf[5];
1863
FUJITA Tomonorifce53842008-07-04 09:33:01 +02001864 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865}
1866
Marcin Slusarzd1941392008-11-16 19:06:37 +01001867static int dvd_read_disckey(struct cdrom_device_info *cdi, dvd_struct *s,
1868 struct packet_command *cgc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869{
1870 int ret, size;
1871 u_char *buf;
Kees Cook853fe1b2017-02-13 16:25:26 -08001872 const struct cdrom_device_ops *cdo = cdi->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873
1874 size = sizeof(s->disckey.value) + 4;
1875
Marcin Slusarzd1941392008-11-16 19:06:37 +01001876 buf = kmalloc(size, GFP_KERNEL);
1877 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 return -ENOMEM;
1879
Marcin Slusarzd1941392008-11-16 19:06:37 +01001880 init_cdrom_command(cgc, buf, size, CGC_DATA_READ);
1881 cgc->cmd[0] = GPCMD_READ_DVD_STRUCTURE;
1882 cgc->cmd[7] = s->type;
1883 cgc->cmd[8] = size >> 8;
1884 cgc->cmd[9] = size & 0xff;
1885 cgc->cmd[10] = s->disckey.agid << 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
Marcin Slusarzd1941392008-11-16 19:06:37 +01001887 ret = cdo->generic_packet(cdi, cgc);
1888 if (!ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 memcpy(s->disckey.value, &buf[4], sizeof(s->disckey.value));
1890
1891 kfree(buf);
1892 return ret;
1893}
1894
Marcin Slusarzd1941392008-11-16 19:06:37 +01001895static int dvd_read_bca(struct cdrom_device_info *cdi, dvd_struct *s,
1896 struct packet_command *cgc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897{
Marcin Slusarzd1941392008-11-16 19:06:37 +01001898 int ret, size = 4 + 188;
1899 u_char *buf;
Kees Cook853fe1b2017-02-13 16:25:26 -08001900 const struct cdrom_device_ops *cdo = cdi->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
Marcin Slusarzd1941392008-11-16 19:06:37 +01001902 buf = kmalloc(size, GFP_KERNEL);
1903 if (!buf)
1904 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
Marcin Slusarzd1941392008-11-16 19:06:37 +01001906 init_cdrom_command(cgc, buf, size, CGC_DATA_READ);
1907 cgc->cmd[0] = GPCMD_READ_DVD_STRUCTURE;
1908 cgc->cmd[7] = s->type;
1909 cgc->cmd[9] = cgc->buflen & 0xff;
1910
1911 ret = cdo->generic_packet(cdi, cgc);
1912 if (ret)
1913 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
1915 s->bca.len = buf[0] << 8 | buf[1];
1916 if (s->bca.len < 12 || s->bca.len > 188) {
Joe Perches5944b2c2014-05-04 17:05:02 -07001917 cd_dbg(CD_WARNING, "Received invalid BCA length (%d)\n",
1918 s->bca.len);
Marcin Slusarzd1941392008-11-16 19:06:37 +01001919 ret = -EIO;
1920 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 }
1922 memcpy(s->bca.value, &buf[4], s->bca.len);
Marcin Slusarzd1941392008-11-16 19:06:37 +01001923 ret = 0;
1924out:
1925 kfree(buf);
1926 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927}
1928
Marcin Slusarzd1941392008-11-16 19:06:37 +01001929static int dvd_read_manufact(struct cdrom_device_info *cdi, dvd_struct *s,
1930 struct packet_command *cgc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931{
1932 int ret = 0, size;
1933 u_char *buf;
Kees Cook853fe1b2017-02-13 16:25:26 -08001934 const struct cdrom_device_ops *cdo = cdi->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
1936 size = sizeof(s->manufact.value) + 4;
1937
Marcin Slusarzd1941392008-11-16 19:06:37 +01001938 buf = kmalloc(size, GFP_KERNEL);
1939 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 return -ENOMEM;
1941
Marcin Slusarzd1941392008-11-16 19:06:37 +01001942 init_cdrom_command(cgc, buf, size, CGC_DATA_READ);
1943 cgc->cmd[0] = GPCMD_READ_DVD_STRUCTURE;
1944 cgc->cmd[7] = s->type;
1945 cgc->cmd[8] = size >> 8;
1946 cgc->cmd[9] = size & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
Marcin Slusarzd1941392008-11-16 19:06:37 +01001948 ret = cdo->generic_packet(cdi, cgc);
1949 if (ret)
1950 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
1952 s->manufact.len = buf[0] << 8 | buf[1];
Andrew Mortonaec9f372011-08-02 12:43:50 +02001953 if (s->manufact.len < 0) {
Joe Perches5944b2c2014-05-04 17:05:02 -07001954 cd_dbg(CD_WARNING, "Received invalid manufacture info length (%d)\n",
1955 s->manufact.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 ret = -EIO;
1957 } else {
Andrew Mortonaec9f372011-08-02 12:43:50 +02001958 if (s->manufact.len > 2048) {
Joe Perches5944b2c2014-05-04 17:05:02 -07001959 cd_dbg(CD_WARNING, "Received invalid manufacture info length (%d): truncating to 2048\n",
1960 s->manufact.len);
Andrew Mortonaec9f372011-08-02 12:43:50 +02001961 s->manufact.len = 2048;
1962 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 memcpy(s->manufact.value, &buf[4], s->manufact.len);
1964 }
1965
Marcin Slusarzd1941392008-11-16 19:06:37 +01001966out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 kfree(buf);
1968 return ret;
1969}
1970
Marcin Slusarzd1941392008-11-16 19:06:37 +01001971static int dvd_read_struct(struct cdrom_device_info *cdi, dvd_struct *s,
1972 struct packet_command *cgc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973{
1974 switch (s->type) {
1975 case DVD_STRUCT_PHYSICAL:
Marcin Slusarzd1941392008-11-16 19:06:37 +01001976 return dvd_read_physical(cdi, s, cgc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
1978 case DVD_STRUCT_COPYRIGHT:
Marcin Slusarzd1941392008-11-16 19:06:37 +01001979 return dvd_read_copyright(cdi, s, cgc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
1981 case DVD_STRUCT_DISCKEY:
Marcin Slusarzd1941392008-11-16 19:06:37 +01001982 return dvd_read_disckey(cdi, s, cgc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983
1984 case DVD_STRUCT_BCA:
Marcin Slusarzd1941392008-11-16 19:06:37 +01001985 return dvd_read_bca(cdi, s, cgc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
1987 case DVD_STRUCT_MANUFACT:
Marcin Slusarzd1941392008-11-16 19:06:37 +01001988 return dvd_read_manufact(cdi, s, cgc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
1990 default:
Joe Perches5944b2c2014-05-04 17:05:02 -07001991 cd_dbg(CD_WARNING, ": Invalid DVD structure read requested (%d)\n",
1992 s->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 return -EINVAL;
1994 }
1995}
1996
1997int cdrom_mode_sense(struct cdrom_device_info *cdi,
1998 struct packet_command *cgc,
1999 int page_code, int page_control)
2000{
Kees Cook853fe1b2017-02-13 16:25:26 -08002001 const struct cdrom_device_ops *cdo = cdi->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
2003 memset(cgc->cmd, 0, sizeof(cgc->cmd));
2004
2005 cgc->cmd[0] = GPCMD_MODE_SENSE_10;
2006 cgc->cmd[2] = page_code | (page_control << 6);
2007 cgc->cmd[7] = cgc->buflen >> 8;
2008 cgc->cmd[8] = cgc->buflen & 0xff;
2009 cgc->data_direction = CGC_DATA_READ;
2010 return cdo->generic_packet(cdi, cgc);
2011}
2012
2013int cdrom_mode_select(struct cdrom_device_info *cdi,
2014 struct packet_command *cgc)
2015{
Kees Cook853fe1b2017-02-13 16:25:26 -08002016 const struct cdrom_device_ops *cdo = cdi->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
2018 memset(cgc->cmd, 0, sizeof(cgc->cmd));
2019 memset(cgc->buffer, 0, 2);
2020 cgc->cmd[0] = GPCMD_MODE_SELECT_10;
2021 cgc->cmd[1] = 0x10; /* PF */
2022 cgc->cmd[7] = cgc->buflen >> 8;
2023 cgc->cmd[8] = cgc->buflen & 0xff;
2024 cgc->data_direction = CGC_DATA_WRITE;
2025 return cdo->generic_packet(cdi, cgc);
2026}
2027
2028static int cdrom_read_subchannel(struct cdrom_device_info *cdi,
2029 struct cdrom_subchnl *subchnl, int mcn)
2030{
Kees Cook853fe1b2017-02-13 16:25:26 -08002031 const struct cdrom_device_ops *cdo = cdi->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 struct packet_command cgc;
FUJITA Tomonorifce53842008-07-04 09:33:01 +02002033 char buffer[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 int ret;
2035
2036 init_cdrom_command(&cgc, buffer, 16, CGC_DATA_READ);
2037 cgc.cmd[0] = GPCMD_READ_SUBCHANNEL;
vchannaiahbf294e82016-06-29 08:18:25 -04002038 cgc.cmd[1] = subchnl->cdsc_format;/* MSF or LBA addressing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 cgc.cmd[2] = 0x40; /* request subQ data */
2040 cgc.cmd[3] = mcn ? 2 : 1;
2041 cgc.cmd[8] = 16;
2042
2043 if ((ret = cdo->generic_packet(cdi, &cgc)))
FUJITA Tomonorifce53842008-07-04 09:33:01 +02002044 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
2046 subchnl->cdsc_audiostatus = cgc.buffer[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 subchnl->cdsc_ctrl = cgc.buffer[5] & 0xf;
2048 subchnl->cdsc_trk = cgc.buffer[6];
2049 subchnl->cdsc_ind = cgc.buffer[7];
2050
vchannaiahbf294e82016-06-29 08:18:25 -04002051 if (subchnl->cdsc_format == CDROM_LBA) {
2052 subchnl->cdsc_absaddr.lba = ((cgc.buffer[8] << 24) |
2053 (cgc.buffer[9] << 16) |
2054 (cgc.buffer[10] << 8) |
2055 (cgc.buffer[11]));
2056 subchnl->cdsc_reladdr.lba = ((cgc.buffer[12] << 24) |
2057 (cgc.buffer[13] << 16) |
2058 (cgc.buffer[14] << 8) |
2059 (cgc.buffer[15]));
2060 } else {
2061 subchnl->cdsc_reladdr.msf.minute = cgc.buffer[13];
2062 subchnl->cdsc_reladdr.msf.second = cgc.buffer[14];
2063 subchnl->cdsc_reladdr.msf.frame = cgc.buffer[15];
2064 subchnl->cdsc_absaddr.msf.minute = cgc.buffer[9];
2065 subchnl->cdsc_absaddr.msf.second = cgc.buffer[10];
2066 subchnl->cdsc_absaddr.msf.frame = cgc.buffer[11];
2067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
FUJITA Tomonorifce53842008-07-04 09:33:01 +02002069 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070}
2071
2072/*
2073 * Specific READ_10 interface
2074 */
2075static int cdrom_read_cd(struct cdrom_device_info *cdi,
2076 struct packet_command *cgc, int lba,
2077 int blocksize, int nblocks)
2078{
Kees Cook853fe1b2017-02-13 16:25:26 -08002079 const struct cdrom_device_ops *cdo = cdi->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080
2081 memset(&cgc->cmd, 0, sizeof(cgc->cmd));
2082 cgc->cmd[0] = GPCMD_READ_10;
2083 cgc->cmd[2] = (lba >> 24) & 0xff;
2084 cgc->cmd[3] = (lba >> 16) & 0xff;
2085 cgc->cmd[4] = (lba >> 8) & 0xff;
2086 cgc->cmd[5] = lba & 0xff;
2087 cgc->cmd[6] = (nblocks >> 16) & 0xff;
2088 cgc->cmd[7] = (nblocks >> 8) & 0xff;
2089 cgc->cmd[8] = nblocks & 0xff;
2090 cgc->buflen = blocksize * nblocks;
2091 return cdo->generic_packet(cdi, cgc);
2092}
2093
2094/* very generic interface for reading the various types of blocks */
2095static int cdrom_read_block(struct cdrom_device_info *cdi,
2096 struct packet_command *cgc,
2097 int lba, int nblocks, int format, int blksize)
2098{
Kees Cook853fe1b2017-02-13 16:25:26 -08002099 const struct cdrom_device_ops *cdo = cdi->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100
2101 memset(&cgc->cmd, 0, sizeof(cgc->cmd));
2102 cgc->cmd[0] = GPCMD_READ_CD;
2103 /* expected sector size - cdda,mode1,etc. */
2104 cgc->cmd[1] = format << 2;
2105 /* starting address */
2106 cgc->cmd[2] = (lba >> 24) & 0xff;
2107 cgc->cmd[3] = (lba >> 16) & 0xff;
2108 cgc->cmd[4] = (lba >> 8) & 0xff;
2109 cgc->cmd[5] = lba & 0xff;
2110 /* number of blocks */
2111 cgc->cmd[6] = (nblocks >> 16) & 0xff;
2112 cgc->cmd[7] = (nblocks >> 8) & 0xff;
2113 cgc->cmd[8] = nblocks & 0xff;
2114 cgc->buflen = blksize * nblocks;
2115
2116 /* set the header info returned */
2117 switch (blksize) {
2118 case CD_FRAMESIZE_RAW0 : cgc->cmd[9] = 0x58; break;
2119 case CD_FRAMESIZE_RAW1 : cgc->cmd[9] = 0x78; break;
2120 case CD_FRAMESIZE_RAW : cgc->cmd[9] = 0xf8; break;
2121 default : cgc->cmd[9] = 0x10;
2122 }
2123
2124 return cdo->generic_packet(cdi, cgc);
2125}
2126
2127static int cdrom_read_cdda_old(struct cdrom_device_info *cdi, __u8 __user *ubuf,
2128 int lba, int nframes)
2129{
2130 struct packet_command cgc;
2131 int ret = 0;
2132 int nr;
2133
2134 cdi->last_sense = 0;
2135
2136 memset(&cgc, 0, sizeof(cgc));
2137
2138 /*
2139 * start with will ra.nframes size, back down if alloc fails
2140 */
2141 nr = nframes;
2142 do {
Kees Cook6da2ec52018-06-12 13:55:00 -07002143 cgc.buffer = kmalloc_array(nr, CD_FRAMESIZE_RAW, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 if (cgc.buffer)
2145 break;
2146
2147 nr >>= 1;
2148 } while (nr);
2149
2150 if (!nr)
2151 return -ENOMEM;
2152
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 cgc.data_direction = CGC_DATA_READ;
2154 while (nframes > 0) {
2155 if (nr > nframes)
2156 nr = nframes;
2157
2158 ret = cdrom_read_block(cdi, &cgc, lba, nr, 1, CD_FRAMESIZE_RAW);
2159 if (ret)
2160 break;
Dan Carpenter822bfa52012-02-06 10:20:45 +01002161 if (copy_to_user(ubuf, cgc.buffer, CD_FRAMESIZE_RAW * nr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 ret = -EFAULT;
2163 break;
2164 }
2165 ubuf += CD_FRAMESIZE_RAW * nr;
2166 nframes -= nr;
2167 lba += nr;
2168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 kfree(cgc.buffer);
2170 return ret;
2171}
2172
2173static int cdrom_read_cdda_bpc(struct cdrom_device_info *cdi, __u8 __user *ubuf,
2174 int lba, int nframes)
2175{
Jens Axboe165125e2007-07-24 09:28:11 +02002176 struct request_queue *q = cdi->disk->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 struct request *rq;
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01002178 struct scsi_request *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 struct bio *bio;
2180 unsigned int len;
2181 int nr, ret = 0;
2182
2183 if (!q)
2184 return -ENXIO;
2185
Bart Van Assche73d17702017-05-31 14:43:49 -07002186 if (!blk_queue_scsi_passthrough(q)) {
2187 WARN_ONCE(true,
2188 "Attempt read CDDA info through a non-SCSI queue\n");
2189 return -EINVAL;
2190 }
2191
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 cdi->last_sense = 0;
2193
2194 while (nframes) {
2195 nr = nframes;
2196 if (cdi->cdda_method == CDDA_BPC_SINGLE)
2197 nr = 1;
Martin K. Petersenae03bf62009-05-22 17:17:50 -04002198 if (nr * CD_FRAMESIZE_RAW > (queue_max_sectors(q) << 9))
2199 nr = (queue_max_sectors(q) << 9) / CD_FRAMESIZE_RAW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200
2201 len = nr * CD_FRAMESIZE_RAW;
2202
Christoph Hellwigff005a02018-05-09 09:54:05 +02002203 rq = blk_get_request(q, REQ_OP_SCSI_IN, 0);
Joe Lawrencea492f072014-08-28 08:15:21 -06002204 if (IS_ERR(rq)) {
2205 ret = PTR_ERR(rq);
Jens Axboedd1cab92005-06-20 14:06:01 +02002206 break;
Alan D. Brunellefebd7a52008-12-09 15:52:15 +01002207 }
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01002208 req = scsi_req(rq);
Alan D. Brunellefebd7a52008-12-09 15:52:15 +01002209
2210 ret = blk_rq_map_user(q, rq, NULL, ubuf, len, GFP_KERNEL);
2211 if (ret) {
2212 blk_put_request(rq);
2213 break;
2214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01002216 req->cmd[0] = GPCMD_READ_CD;
2217 req->cmd[1] = 1 << 2;
2218 req->cmd[2] = (lba >> 24) & 0xff;
2219 req->cmd[3] = (lba >> 16) & 0xff;
2220 req->cmd[4] = (lba >> 8) & 0xff;
2221 req->cmd[5] = lba & 0xff;
2222 req->cmd[6] = (nr >> 16) & 0xff;
2223 req->cmd[7] = (nr >> 8) & 0xff;
2224 req->cmd[8] = nr & 0xff;
2225 req->cmd[9] = 0xf8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01002227 req->cmd_len = 12;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 rq->timeout = 60 * HZ;
2229 bio = rq->bio;
2230
Christoph Hellwigb7819b92017-04-20 16:02:55 +02002231 blk_execute_rq(q, cdi->disk, rq, 0);
Christoph Hellwig17d53632017-04-20 16:03:01 +02002232 if (scsi_req(rq)->result) {
Kees Cook4e178c12018-07-31 12:51:52 -07002233 struct scsi_sense_hdr sshdr;
2234
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 ret = -EIO;
Kees Cook4e178c12018-07-31 12:51:52 -07002236 scsi_normalize_sense(req->sense, req->sense_len,
2237 &sshdr);
2238 cdi->last_sense = sshdr.sense_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 }
2240
Jens Axboe8e5cfc42006-12-19 11:12:46 +01002241 if (blk_rq_unmap_user(bio))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 ret = -EFAULT;
Alan D. Brunellefebd7a52008-12-09 15:52:15 +01002243 blk_put_request(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244
2245 if (ret)
2246 break;
2247
2248 nframes -= nr;
2249 lba += nr;
2250 ubuf += len;
2251 }
2252
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 return ret;
2254}
2255
2256static int cdrom_read_cdda(struct cdrom_device_info *cdi, __u8 __user *ubuf,
2257 int lba, int nframes)
2258{
2259 int ret;
2260
2261 if (cdi->cdda_method == CDDA_OLD)
2262 return cdrom_read_cdda_old(cdi, ubuf, lba, nframes);
2263
2264retry:
2265 /*
2266 * for anything else than success and io error, we need to retry
2267 */
2268 ret = cdrom_read_cdda_bpc(cdi, ubuf, lba, nframes);
2269 if (!ret || ret != -EIO)
2270 return ret;
2271
2272 /*
2273 * I've seen drives get sense 4/8/3 udma crc errors on multi
2274 * frame dma, so drop to single frame dma if we need to
2275 */
2276 if (cdi->cdda_method == CDDA_BPC_FULL && nframes > 1) {
Joe Perchese597cd02010-07-01 08:24:32 +02002277 pr_info("dropping to single frame dma\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 cdi->cdda_method = CDDA_BPC_SINGLE;
2279 goto retry;
2280 }
2281
2282 /*
2283 * so we have an io error of some sort with multi frame dma. if the
2284 * condition wasn't a hardware error
2285 * problems, not for any error
2286 */
2287 if (cdi->last_sense != 0x04 && cdi->last_sense != 0x0b)
2288 return ret;
2289
Joe Perchese597cd02010-07-01 08:24:32 +02002290 pr_info("dropping to old style cdda (sense=%x)\n", cdi->last_sense);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 cdi->cdda_method = CDDA_OLD;
2292 return cdrom_read_cdda_old(cdi, ubuf, lba, nframes);
2293}
2294
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08002295static int cdrom_ioctl_multisession(struct cdrom_device_info *cdi,
2296 void __user *argp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297{
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08002298 struct cdrom_multisession ms_info;
2299 u8 requested_format;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 int ret;
2301
Joe Perches5944b2c2014-05-04 17:05:02 -07002302 cd_dbg(CD_DO_IOCTL, "entering CDROMMULTISESSION\n");
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08002303
2304 if (!(cdi->ops->capability & CDC_MULTI_SESSION))
2305 return -ENOSYS;
2306
2307 if (copy_from_user(&ms_info, argp, sizeof(ms_info)))
2308 return -EFAULT;
2309
2310 requested_format = ms_info.addr_format;
2311 if (requested_format != CDROM_MSF && requested_format != CDROM_LBA)
2312 return -EINVAL;
2313 ms_info.addr_format = CDROM_LBA;
2314
2315 ret = cdi->ops->get_last_session(cdi, &ms_info);
2316 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 return ret;
2318
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08002319 sanitize_format(&ms_info.addr, &ms_info.addr_format, requested_format);
2320
2321 if (copy_to_user(argp, &ms_info, sizeof(ms_info)))
2322 return -EFAULT;
2323
Joe Perches5944b2c2014-05-04 17:05:02 -07002324 cd_dbg(CD_DO_IOCTL, "CDROMMULTISESSION successful\n");
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08002325 return 0;
2326}
2327
2328static int cdrom_ioctl_eject(struct cdrom_device_info *cdi)
2329{
Joe Perches5944b2c2014-05-04 17:05:02 -07002330 cd_dbg(CD_DO_IOCTL, "entering CDROMEJECT\n");
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08002331
2332 if (!CDROM_CAN(CDC_OPEN_TRAY))
2333 return -ENOSYS;
Paolo Bonzinicdccaa92012-02-08 20:03:14 +01002334 if (cdi->use_count != 1 || cdi->keeplocked)
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08002335 return -EBUSY;
2336 if (CDROM_CAN(CDC_LOCK)) {
2337 int ret = cdi->ops->lock_door(cdi, 0);
2338 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 return ret;
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08002340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08002342 return cdi->ops->tray_move(cdi, 1);
2343}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08002345static int cdrom_ioctl_closetray(struct cdrom_device_info *cdi)
2346{
Joe Perches5944b2c2014-05-04 17:05:02 -07002347 cd_dbg(CD_DO_IOCTL, "entering CDROMCLOSETRAY\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08002349 if (!CDROM_CAN(CDC_CLOSE_TRAY))
2350 return -ENOSYS;
2351 return cdi->ops->tray_move(cdi, 0);
2352}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08002354static int cdrom_ioctl_eject_sw(struct cdrom_device_info *cdi,
2355 unsigned long arg)
2356{
Joe Perches5944b2c2014-05-04 17:05:02 -07002357 cd_dbg(CD_DO_IOCTL, "entering CDROMEJECT_SW\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08002359 if (!CDROM_CAN(CDC_OPEN_TRAY))
2360 return -ENOSYS;
Paolo Bonzinicdccaa92012-02-08 20:03:14 +01002361 if (cdi->keeplocked)
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08002362 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08002364 cdi->options &= ~(CDO_AUTO_CLOSE | CDO_AUTO_EJECT);
2365 if (arg)
2366 cdi->options |= CDO_AUTO_CLOSE | CDO_AUTO_EJECT;
2367 return 0;
2368}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08002370static int cdrom_ioctl_media_changed(struct cdrom_device_info *cdi,
2371 unsigned long arg)
2372{
2373 struct cdrom_changer_info *info;
2374 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375
Joe Perches5944b2c2014-05-04 17:05:02 -07002376 cd_dbg(CD_DO_IOCTL, "entering CDROM_MEDIA_CHANGED\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08002378 if (!CDROM_CAN(CDC_MEDIA_CHANGED))
2379 return -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08002381 /* cannot select disc or select current disc */
2382 if (!CDROM_CAN(CDC_SELECT_DISC) || arg == CDSL_CURRENT)
2383 return media_changed(cdi, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384
Dan Carpenter9de4ee42018-04-18 12:51:31 +03002385 if (arg >= cdi->capacity)
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08002386 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08002388 info = kmalloc(sizeof(*info), GFP_KERNEL);
2389 if (!info)
2390 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08002392 ret = cdrom_read_mech_status(cdi, info);
2393 if (!ret)
2394 ret = info->slots[arg].change;
2395 kfree(info);
2396 return ret;
2397}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08002399static int cdrom_ioctl_set_options(struct cdrom_device_info *cdi,
2400 unsigned long arg)
2401{
Joe Perches5944b2c2014-05-04 17:05:02 -07002402 cd_dbg(CD_DO_IOCTL, "entering CDROM_SET_OPTIONS\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08002404 /*
2405 * Options need to be in sync with capability.
2406 * Too late for that, so we have to check each one separately.
2407 */
2408 switch (arg) {
2409 case CDO_USE_FFLAGS:
2410 case CDO_CHECK_TYPE:
2411 break;
2412 case CDO_LOCK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 if (!CDROM_CAN(CDC_LOCK))
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08002414 return -ENOSYS;
2415 break;
2416 case 0:
2417 return cdi->options;
2418 /* default is basically CDO_[AUTO_CLOSE|AUTO_EJECT] */
2419 default:
2420 if (!CDROM_CAN(arg))
2421 return -ENOSYS;
2422 }
2423 cdi->options |= (int) arg;
2424 return cdi->options;
2425}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08002427static int cdrom_ioctl_clear_options(struct cdrom_device_info *cdi,
2428 unsigned long arg)
2429{
Joe Perches5944b2c2014-05-04 17:05:02 -07002430 cd_dbg(CD_DO_IOCTL, "entering CDROM_CLEAR_OPTIONS\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08002432 cdi->options &= ~(int) arg;
2433 return cdi->options;
2434}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08002436static int cdrom_ioctl_select_speed(struct cdrom_device_info *cdi,
2437 unsigned long arg)
2438{
Joe Perches5944b2c2014-05-04 17:05:02 -07002439 cd_dbg(CD_DO_IOCTL, "entering CDROM_SELECT_SPEED\n");
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08002440
2441 if (!CDROM_CAN(CDC_SELECT_SPEED))
2442 return -ENOSYS;
2443 return cdi->ops->select_speed(cdi, arg);
2444}
2445
2446static int cdrom_ioctl_select_disc(struct cdrom_device_info *cdi,
2447 unsigned long arg)
2448{
Joe Perches5944b2c2014-05-04 17:05:02 -07002449 cd_dbg(CD_DO_IOCTL, "entering CDROM_SELECT_DISC\n");
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08002450
2451 if (!CDROM_CAN(CDC_SELECT_DISC))
2452 return -ENOSYS;
2453
2454 if (arg != CDSL_CURRENT && arg != CDSL_NONE) {
Young_Xe4f3aa22018-10-03 12:54:29 +00002455 if (arg >= cdi->capacity)
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08002456 return -EINVAL;
2457 }
2458
2459 /*
2460 * ->select_disc is a hook to allow a driver-specific way of
2461 * seleting disc. However, since there is no equivalent hook for
2462 * cdrom_slot_status this may not actually be useful...
2463 */
2464 if (cdi->ops->select_disc)
2465 return cdi->ops->select_disc(cdi, arg);
2466
Joe Perches5944b2c2014-05-04 17:05:02 -07002467 cd_dbg(CD_CHANGER, "Using generic cdrom_select_disc()\n");
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08002468 return cdrom_select_disc(cdi, arg);
2469}
2470
2471static int cdrom_ioctl_reset(struct cdrom_device_info *cdi,
2472 struct block_device *bdev)
2473{
Joe Perches5944b2c2014-05-04 17:05:02 -07002474 cd_dbg(CD_DO_IOCTL, "entering CDROM_RESET\n");
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08002475
2476 if (!capable(CAP_SYS_ADMIN))
2477 return -EACCES;
2478 if (!CDROM_CAN(CDC_RESET))
2479 return -ENOSYS;
Peter Zijlstraf98393a2007-05-06 14:49:54 -07002480 invalidate_bdev(bdev);
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08002481 return cdi->ops->reset(cdi);
2482}
2483
2484static int cdrom_ioctl_lock_door(struct cdrom_device_info *cdi,
2485 unsigned long arg)
2486{
Joe Perches5944b2c2014-05-04 17:05:02 -07002487 cd_dbg(CD_DO_IOCTL, "%socking door\n", arg ? "L" : "Unl");
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08002488
2489 if (!CDROM_CAN(CDC_LOCK))
2490 return -EDRIVE_CANT_DO_THIS;
2491
Paolo Bonzinicdccaa92012-02-08 20:03:14 +01002492 cdi->keeplocked = arg ? 1 : 0;
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08002493
2494 /*
2495 * Don't unlock the door on multiple opens by default, but allow
2496 * root to do so.
2497 */
2498 if (cdi->use_count != 1 && !arg && !capable(CAP_SYS_ADMIN))
2499 return -EBUSY;
2500 return cdi->ops->lock_door(cdi, arg);
2501}
2502
2503static int cdrom_ioctl_debug(struct cdrom_device_info *cdi,
2504 unsigned long arg)
2505{
Joe Perches5944b2c2014-05-04 17:05:02 -07002506 cd_dbg(CD_DO_IOCTL, "%sabling debug\n", arg ? "En" : "Dis");
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08002507
2508 if (!capable(CAP_SYS_ADMIN))
2509 return -EACCES;
2510 debug = arg ? 1 : 0;
2511 return debug;
2512}
2513
2514static int cdrom_ioctl_get_capability(struct cdrom_device_info *cdi)
2515{
Joe Perches5944b2c2014-05-04 17:05:02 -07002516 cd_dbg(CD_DO_IOCTL, "entering CDROM_GET_CAPABILITY\n");
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08002517 return (cdi->ops->capability & ~cdi->mask);
2518}
2519
2520/*
2521 * The following function is implemented, although very few audio
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 * discs give Universal Product Code information, which should just be
2523 * the Medium Catalog Number on the box. Note, that the way the code
2524 * is written on the CD is /not/ uniform across all discs!
2525 */
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08002526static int cdrom_ioctl_get_mcn(struct cdrom_device_info *cdi,
2527 void __user *argp)
2528{
2529 struct cdrom_mcn mcn;
2530 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531
Joe Perches5944b2c2014-05-04 17:05:02 -07002532 cd_dbg(CD_DO_IOCTL, "entering CDROM_GET_MCN\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08002534 if (!(cdi->ops->capability & CDC_MCN))
2535 return -ENOSYS;
2536 ret = cdi->ops->get_mcn(cdi, &mcn);
2537 if (ret)
2538 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08002540 if (copy_to_user(argp, &mcn, sizeof(mcn)))
2541 return -EFAULT;
Joe Perches5944b2c2014-05-04 17:05:02 -07002542 cd_dbg(CD_DO_IOCTL, "CDROM_GET_MCN successful\n");
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08002543 return 0;
2544}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08002546static int cdrom_ioctl_drive_status(struct cdrom_device_info *cdi,
2547 unsigned long arg)
2548{
Joe Perches5944b2c2014-05-04 17:05:02 -07002549 cd_dbg(CD_DO_IOCTL, "entering CDROM_DRIVE_STATUS\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08002551 if (!(cdi->ops->capability & CDC_DRIVE_STATUS))
2552 return -ENOSYS;
2553 if (!CDROM_CAN(CDC_SELECT_DISC) ||
2554 (arg == CDSL_CURRENT || arg == CDSL_NONE))
2555 return cdi->ops->drive_status(cdi, CDSL_CURRENT);
Scott Bauer8f3fafc2018-04-26 11:51:08 -06002556 if (arg >= cdi->capacity)
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08002557 return -EINVAL;
2558 return cdrom_slot_status(cdi, arg);
2559}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08002561/*
2562 * Ok, this is where problems start. The current interface for the
2563 * CDROM_DISC_STATUS ioctl is flawed. It makes the false assumption that
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002564 * CDs are all CDS_DATA_1 or all CDS_AUDIO, etc. Unfortunately, while this
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08002565 * is often the case, it is also very common for CDs to have some tracks
2566 * with data, and some tracks with audio. Just because I feel like it,
2567 * I declare the following to be the best way to cope. If the CD has ANY
2568 * data tracks on it, it will be returned as a data CD. If it has any XA
2569 * tracks, I will return it as that. Now I could simplify this interface
2570 * by combining these returns with the above, but this more clearly
2571 * demonstrates the problem with the current interface. Too bad this
2572 * wasn't designed to use bitmasks... -Erik
2573 *
2574 * Well, now we have the option CDS_MIXED: a mixed-type CD.
2575 * User level programmers might feel the ioctl is not very useful.
2576 * ---david
2577 */
2578static int cdrom_ioctl_disc_status(struct cdrom_device_info *cdi)
2579{
2580 tracktype tracks;
2581
Joe Perches5944b2c2014-05-04 17:05:02 -07002582 cd_dbg(CD_DO_IOCTL, "entering CDROM_DISC_STATUS\n");
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08002583
2584 cdrom_count_tracks(cdi, &tracks);
2585 if (tracks.error)
2586 return tracks.error;
2587
2588 /* Policy mode on */
2589 if (tracks.audio > 0) {
2590 if (!tracks.data && !tracks.cdi && !tracks.xa)
2591 return CDS_AUDIO;
2592 else
2593 return CDS_MIXED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 }
2595
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08002596 if (tracks.cdi > 0)
2597 return CDS_XA_2_2;
2598 if (tracks.xa > 0)
2599 return CDS_XA_2_1;
2600 if (tracks.data > 0)
2601 return CDS_DATA_1;
2602 /* Policy mode off */
2603
Joe Perches5944b2c2014-05-04 17:05:02 -07002604 cd_dbg(CD_WARNING, "This disc doesn't have any tracks I recognize!\n");
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08002605 return CDS_NO_INFO;
2606}
2607
2608static int cdrom_ioctl_changer_nslots(struct cdrom_device_info *cdi)
2609{
Joe Perches5944b2c2014-05-04 17:05:02 -07002610 cd_dbg(CD_DO_IOCTL, "entering CDROM_CHANGER_NSLOTS\n");
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08002611 return cdi->capacity;
2612}
2613
2614static int cdrom_ioctl_get_subchnl(struct cdrom_device_info *cdi,
2615 void __user *argp)
2616{
2617 struct cdrom_subchnl q;
2618 u8 requested, back;
2619 int ret;
2620
Joe Perches5944b2c2014-05-04 17:05:02 -07002621 /* cd_dbg(CD_DO_IOCTL,"entering CDROMSUBCHNL\n");*/
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08002622
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08002623 if (copy_from_user(&q, argp, sizeof(q)))
2624 return -EFAULT;
2625
2626 requested = q.cdsc_format;
2627 if (requested != CDROM_MSF && requested != CDROM_LBA)
2628 return -EINVAL;
2629 q.cdsc_format = CDROM_MSF;
2630
2631 ret = cdi->ops->audio_ioctl(cdi, CDROMSUBCHNL, &q);
2632 if (ret)
2633 return ret;
2634
2635 back = q.cdsc_format; /* local copy */
2636 sanitize_format(&q.cdsc_absaddr, &back, requested);
2637 sanitize_format(&q.cdsc_reladdr, &q.cdsc_format, requested);
2638
2639 if (copy_to_user(argp, &q, sizeof(q)))
2640 return -EFAULT;
Joe Perches5944b2c2014-05-04 17:05:02 -07002641 /* cd_dbg(CD_DO_IOCTL, "CDROMSUBCHNL successful\n"); */
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08002642 return 0;
2643}
2644
2645static int cdrom_ioctl_read_tochdr(struct cdrom_device_info *cdi,
2646 void __user *argp)
2647{
2648 struct cdrom_tochdr header;
2649 int ret;
2650
Joe Perches5944b2c2014-05-04 17:05:02 -07002651 /* cd_dbg(CD_DO_IOCTL, "entering CDROMREADTOCHDR\n"); */
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08002652
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08002653 if (copy_from_user(&header, argp, sizeof(header)))
2654 return -EFAULT;
2655
2656 ret = cdi->ops->audio_ioctl(cdi, CDROMREADTOCHDR, &header);
2657 if (ret)
2658 return ret;
2659
2660 if (copy_to_user(argp, &header, sizeof(header)))
2661 return -EFAULT;
Joe Perches5944b2c2014-05-04 17:05:02 -07002662 /* cd_dbg(CD_DO_IOCTL, "CDROMREADTOCHDR successful\n"); */
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08002663 return 0;
2664}
2665
2666static int cdrom_ioctl_read_tocentry(struct cdrom_device_info *cdi,
2667 void __user *argp)
2668{
2669 struct cdrom_tocentry entry;
2670 u8 requested_format;
2671 int ret;
2672
Joe Perches5944b2c2014-05-04 17:05:02 -07002673 /* cd_dbg(CD_DO_IOCTL, "entering CDROMREADTOCENTRY\n"); */
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08002674
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08002675 if (copy_from_user(&entry, argp, sizeof(entry)))
2676 return -EFAULT;
2677
2678 requested_format = entry.cdte_format;
2679 if (requested_format != CDROM_MSF && requested_format != CDROM_LBA)
2680 return -EINVAL;
2681 /* make interface to low-level uniform */
2682 entry.cdte_format = CDROM_MSF;
2683 ret = cdi->ops->audio_ioctl(cdi, CDROMREADTOCENTRY, &entry);
2684 if (ret)
2685 return ret;
2686 sanitize_format(&entry.cdte_addr, &entry.cdte_format, requested_format);
2687
2688 if (copy_to_user(argp, &entry, sizeof(entry)))
2689 return -EFAULT;
Joe Perches5944b2c2014-05-04 17:05:02 -07002690 /* cd_dbg(CD_DO_IOCTL, "CDROMREADTOCENTRY successful\n"); */
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08002691 return 0;
2692}
2693
2694static int cdrom_ioctl_play_msf(struct cdrom_device_info *cdi,
2695 void __user *argp)
2696{
2697 struct cdrom_msf msf;
2698
Joe Perches5944b2c2014-05-04 17:05:02 -07002699 cd_dbg(CD_DO_IOCTL, "entering CDROMPLAYMSF\n");
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08002700
2701 if (!CDROM_CAN(CDC_PLAY_AUDIO))
2702 return -ENOSYS;
2703 if (copy_from_user(&msf, argp, sizeof(msf)))
2704 return -EFAULT;
2705 return cdi->ops->audio_ioctl(cdi, CDROMPLAYMSF, &msf);
2706}
2707
2708static int cdrom_ioctl_play_trkind(struct cdrom_device_info *cdi,
2709 void __user *argp)
2710{
2711 struct cdrom_ti ti;
2712 int ret;
2713
Joe Perches5944b2c2014-05-04 17:05:02 -07002714 cd_dbg(CD_DO_IOCTL, "entering CDROMPLAYTRKIND\n");
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08002715
2716 if (!CDROM_CAN(CDC_PLAY_AUDIO))
2717 return -ENOSYS;
2718 if (copy_from_user(&ti, argp, sizeof(ti)))
2719 return -EFAULT;
2720
2721 ret = check_for_audio_disc(cdi, cdi->ops);
2722 if (ret)
2723 return ret;
2724 return cdi->ops->audio_ioctl(cdi, CDROMPLAYTRKIND, &ti);
2725}
2726static int cdrom_ioctl_volctrl(struct cdrom_device_info *cdi,
2727 void __user *argp)
2728{
2729 struct cdrom_volctrl volume;
2730
Joe Perches5944b2c2014-05-04 17:05:02 -07002731 cd_dbg(CD_DO_IOCTL, "entering CDROMVOLCTRL\n");
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08002732
2733 if (!CDROM_CAN(CDC_PLAY_AUDIO))
2734 return -ENOSYS;
2735 if (copy_from_user(&volume, argp, sizeof(volume)))
2736 return -EFAULT;
2737 return cdi->ops->audio_ioctl(cdi, CDROMVOLCTRL, &volume);
2738}
2739
2740static int cdrom_ioctl_volread(struct cdrom_device_info *cdi,
2741 void __user *argp)
2742{
2743 struct cdrom_volctrl volume;
2744 int ret;
2745
Joe Perches5944b2c2014-05-04 17:05:02 -07002746 cd_dbg(CD_DO_IOCTL, "entering CDROMVOLREAD\n");
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08002747
2748 if (!CDROM_CAN(CDC_PLAY_AUDIO))
2749 return -ENOSYS;
2750
2751 ret = cdi->ops->audio_ioctl(cdi, CDROMVOLREAD, &volume);
2752 if (ret)
2753 return ret;
2754
2755 if (copy_to_user(argp, &volume, sizeof(volume)))
2756 return -EFAULT;
2757 return 0;
2758}
2759
2760static int cdrom_ioctl_audioctl(struct cdrom_device_info *cdi,
2761 unsigned int cmd)
2762{
2763 int ret;
2764
Joe Perches5944b2c2014-05-04 17:05:02 -07002765 cd_dbg(CD_DO_IOCTL, "doing audio ioctl (start/stop/pause/resume)\n");
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08002766
2767 if (!CDROM_CAN(CDC_PLAY_AUDIO))
2768 return -ENOSYS;
2769 ret = check_for_audio_disc(cdi, cdi->ops);
2770 if (ret)
2771 return ret;
2772 return cdi->ops->audio_ioctl(cdi, cmd, NULL);
2773}
2774
2775/*
Joe Perches2e9aa082014-05-04 17:05:08 -07002776 * Required when we need to use READ_10 to issue other than 2048 block
2777 * reads
2778 */
2779static int cdrom_switch_blocksize(struct cdrom_device_info *cdi, int size)
2780{
Kees Cook853fe1b2017-02-13 16:25:26 -08002781 const struct cdrom_device_ops *cdo = cdi->ops;
Joe Perches2e9aa082014-05-04 17:05:08 -07002782 struct packet_command cgc;
2783 struct modesel_head mh;
2784
2785 memset(&mh, 0, sizeof(mh));
2786 mh.block_desc_length = 0x08;
2787 mh.block_length_med = (size >> 8) & 0xff;
2788 mh.block_length_lo = size & 0xff;
2789
2790 memset(&cgc, 0, sizeof(cgc));
2791 cgc.cmd[0] = 0x15;
2792 cgc.cmd[1] = 1 << 4;
2793 cgc.cmd[4] = 12;
2794 cgc.buflen = sizeof(mh);
2795 cgc.buffer = (char *) &mh;
2796 cgc.data_direction = CGC_DATA_WRITE;
2797 mh.block_desc_length = 0x08;
2798 mh.block_length_med = (size >> 8) & 0xff;
2799 mh.block_length_lo = size & 0xff;
2800
2801 return cdo->generic_packet(cdi, &cgc);
2802}
2803
Joe Perchese1e60fd2014-05-04 17:05:09 -07002804static int cdrom_get_track_info(struct cdrom_device_info *cdi,
2805 __u16 track, __u8 type, track_information *ti)
2806{
Kees Cook853fe1b2017-02-13 16:25:26 -08002807 const struct cdrom_device_ops *cdo = cdi->ops;
Joe Perchese1e60fd2014-05-04 17:05:09 -07002808 struct packet_command cgc;
2809 int ret, buflen;
2810
2811 init_cdrom_command(&cgc, ti, 8, CGC_DATA_READ);
2812 cgc.cmd[0] = GPCMD_READ_TRACK_RZONE_INFO;
2813 cgc.cmd[1] = type & 3;
2814 cgc.cmd[4] = (track & 0xff00) >> 8;
2815 cgc.cmd[5] = track & 0xff;
2816 cgc.cmd[8] = 8;
2817 cgc.quiet = 1;
2818
2819 ret = cdo->generic_packet(cdi, &cgc);
2820 if (ret)
2821 return ret;
2822
2823 buflen = be16_to_cpu(ti->track_information_length) +
2824 sizeof(ti->track_information_length);
2825
2826 if (buflen > sizeof(track_information))
2827 buflen = sizeof(track_information);
2828
2829 cgc.cmd[8] = cgc.buflen = buflen;
2830 ret = cdo->generic_packet(cdi, &cgc);
2831 if (ret)
2832 return ret;
2833
2834 /* return actual fill size */
2835 return buflen;
2836}
2837
2838/* return the last written block on the CD-R media. this is for the udf
2839 file system. */
2840int cdrom_get_last_written(struct cdrom_device_info *cdi, long *last_written)
2841{
2842 struct cdrom_tocentry toc;
2843 disc_information di;
2844 track_information ti;
2845 __u32 last_track;
2846 int ret = -1, ti_size;
2847
2848 if (!CDROM_CAN(CDC_GENERIC_PACKET))
2849 goto use_toc;
2850
2851 ret = cdrom_get_disc_info(cdi, &di);
2852 if (ret < (int)(offsetof(typeof(di), last_track_lsb)
2853 + sizeof(di.last_track_lsb)))
2854 goto use_toc;
2855
2856 /* if unit didn't return msb, it's zeroed by cdrom_get_disc_info */
2857 last_track = (di.last_track_msb << 8) | di.last_track_lsb;
2858 ti_size = cdrom_get_track_info(cdi, last_track, 1, &ti);
2859 if (ti_size < (int)offsetof(typeof(ti), track_start))
2860 goto use_toc;
2861
2862 /* if this track is blank, try the previous. */
2863 if (ti.blank) {
2864 if (last_track == 1)
2865 goto use_toc;
2866 last_track--;
2867 ti_size = cdrom_get_track_info(cdi, last_track, 1, &ti);
2868 }
2869
2870 if (ti_size < (int)(offsetof(typeof(ti), track_size)
2871 + sizeof(ti.track_size)))
2872 goto use_toc;
2873
2874 /* if last recorded field is valid, return it. */
2875 if (ti.lra_v && ti_size >= (int)(offsetof(typeof(ti), last_rec_address)
2876 + sizeof(ti.last_rec_address))) {
2877 *last_written = be32_to_cpu(ti.last_rec_address);
2878 } else {
2879 /* make it up instead */
2880 *last_written = be32_to_cpu(ti.track_start) +
2881 be32_to_cpu(ti.track_size);
2882 if (ti.free_blocks)
2883 *last_written -= (be32_to_cpu(ti.free_blocks) + 7);
2884 }
2885 return 0;
2886
2887 /* this is where we end up if the drive either can't do a
2888 GPCMD_READ_DISC_INFO or GPCMD_READ_TRACK_RZONE_INFO or if
2889 it doesn't give enough information or fails. then we return
2890 the toc contents. */
2891use_toc:
Diego Elio Pettenò366ba7c2019-11-19 21:37:08 +00002892 if (!CDROM_CAN(CDC_PLAY_AUDIO))
2893 return -ENOSYS;
2894
Joe Perchese1e60fd2014-05-04 17:05:09 -07002895 toc.cdte_format = CDROM_MSF;
2896 toc.cdte_track = CDROM_LEADOUT;
2897 if ((ret = cdi->ops->audio_ioctl(cdi, CDROMREADTOCENTRY, &toc)))
2898 return ret;
2899 sanitize_format(&toc.cdte_addr, &toc.cdte_format, CDROM_LBA);
2900 *last_written = toc.cdte_addr.lba;
2901 return 0;
2902}
2903
Joe Perchesdac1c5c2014-05-04 17:05:10 -07002904/* return the next writable block. also for udf file system. */
2905static int cdrom_get_next_writable(struct cdrom_device_info *cdi,
2906 long *next_writable)
2907{
2908 disc_information di;
2909 track_information ti;
2910 __u16 last_track;
2911 int ret, ti_size;
2912
2913 if (!CDROM_CAN(CDC_GENERIC_PACKET))
2914 goto use_last_written;
2915
2916 ret = cdrom_get_disc_info(cdi, &di);
2917 if (ret < 0 || ret < offsetof(typeof(di), last_track_lsb)
2918 + sizeof(di.last_track_lsb))
2919 goto use_last_written;
2920
2921 /* if unit didn't return msb, it's zeroed by cdrom_get_disc_info */
2922 last_track = (di.last_track_msb << 8) | di.last_track_lsb;
2923 ti_size = cdrom_get_track_info(cdi, last_track, 1, &ti);
2924 if (ti_size < 0 || ti_size < offsetof(typeof(ti), track_start))
2925 goto use_last_written;
2926
2927 /* if this track is blank, try the previous. */
2928 if (ti.blank) {
2929 if (last_track == 1)
2930 goto use_last_written;
2931 last_track--;
2932 ti_size = cdrom_get_track_info(cdi, last_track, 1, &ti);
2933 if (ti_size < 0)
2934 goto use_last_written;
2935 }
2936
2937 /* if next recordable address field is valid, use it. */
2938 if (ti.nwa_v && ti_size >= offsetof(typeof(ti), next_writable)
2939 + sizeof(ti.next_writable)) {
2940 *next_writable = be32_to_cpu(ti.next_writable);
2941 return 0;
2942 }
2943
2944use_last_written:
2945 ret = cdrom_get_last_written(cdi, next_writable);
2946 if (ret) {
2947 *next_writable = 0;
2948 return ret;
2949 } else {
2950 *next_writable += 7;
2951 return 0;
2952 }
2953}
2954
Joe Perches2e9aa082014-05-04 17:05:08 -07002955static noinline int mmc_ioctl_cdrom_read_data(struct cdrom_device_info *cdi,
2956 void __user *arg,
2957 struct packet_command *cgc,
2958 int cmd)
2959{
Kees Cooke7d07482018-08-02 15:22:13 -06002960 struct scsi_sense_hdr sshdr;
Joe Perches2e9aa082014-05-04 17:05:08 -07002961 struct cdrom_msf msf;
2962 int blocksize = 0, format = 0, lba;
2963 int ret;
2964
2965 switch (cmd) {
2966 case CDROMREADRAW:
2967 blocksize = CD_FRAMESIZE_RAW;
2968 break;
2969 case CDROMREADMODE1:
2970 blocksize = CD_FRAMESIZE;
2971 format = 2;
2972 break;
2973 case CDROMREADMODE2:
2974 blocksize = CD_FRAMESIZE_RAW0;
2975 break;
2976 }
2977 if (copy_from_user(&msf, (struct cdrom_msf __user *)arg, sizeof(msf)))
2978 return -EFAULT;
2979 lba = msf_to_lba(msf.cdmsf_min0, msf.cdmsf_sec0, msf.cdmsf_frame0);
2980 /* FIXME: we need upper bound checking, too!! */
2981 if (lba < 0)
2982 return -EINVAL;
2983
2984 cgc->buffer = kzalloc(blocksize, GFP_KERNEL);
2985 if (cgc->buffer == NULL)
2986 return -ENOMEM;
2987
Kees Cooke7d07482018-08-02 15:22:13 -06002988 memset(&sshdr, 0, sizeof(sshdr));
2989 cgc->sshdr = &sshdr;
Joe Perches2e9aa082014-05-04 17:05:08 -07002990 cgc->data_direction = CGC_DATA_READ;
2991 ret = cdrom_read_block(cdi, cgc, lba, 1, format, blocksize);
Kees Cooke7d07482018-08-02 15:22:13 -06002992 if (ret && sshdr.sense_key == 0x05 &&
2993 sshdr.asc == 0x20 &&
2994 sshdr.ascq == 0x00) {
Joe Perches2e9aa082014-05-04 17:05:08 -07002995 /*
2996 * SCSI-II devices are not required to support
2997 * READ_CD, so let's try switching block size
2998 */
2999 /* FIXME: switch back again... */
3000 ret = cdrom_switch_blocksize(cdi, blocksize);
3001 if (ret)
3002 goto out;
Kees Cooke7d07482018-08-02 15:22:13 -06003003 cgc->sshdr = NULL;
Joe Perches2e9aa082014-05-04 17:05:08 -07003004 ret = cdrom_read_cd(cdi, cgc, lba, blocksize, 1);
3005 ret |= cdrom_switch_blocksize(cdi, blocksize);
3006 }
3007 if (!ret && copy_to_user(arg, cgc->buffer, blocksize))
3008 ret = -EFAULT;
3009out:
3010 kfree(cgc->buffer);
3011 return ret;
3012}
3013
3014static noinline int mmc_ioctl_cdrom_read_audio(struct cdrom_device_info *cdi,
3015 void __user *arg)
3016{
3017 struct cdrom_read_audio ra;
3018 int lba;
3019
Arnd Bergmann8f8f5622019-11-28 14:20:53 +01003020#ifdef CONFIG_COMPAT
3021 if (in_compat_syscall()) {
3022 struct compat_cdrom_read_audio {
3023 union cdrom_addr addr;
3024 u8 addr_format;
3025 compat_int_t nframes;
3026 compat_caddr_t buf;
3027 } ra32;
3028
3029 if (copy_from_user(&ra32, arg, sizeof(ra32)))
3030 return -EFAULT;
3031
3032 ra = (struct cdrom_read_audio) {
3033 .addr = ra32.addr,
3034 .addr_format = ra32.addr_format,
3035 .nframes = ra32.nframes,
3036 .buf = compat_ptr(ra32.buf),
3037 };
3038 } else
3039#endif
3040 {
3041 if (copy_from_user(&ra, (struct cdrom_read_audio __user *)arg,
3042 sizeof(ra)))
3043 return -EFAULT;
3044 }
Joe Perches2e9aa082014-05-04 17:05:08 -07003045
3046 if (ra.addr_format == CDROM_MSF)
3047 lba = msf_to_lba(ra.addr.msf.minute,
3048 ra.addr.msf.second,
3049 ra.addr.msf.frame);
3050 else if (ra.addr_format == CDROM_LBA)
3051 lba = ra.addr.lba;
3052 else
3053 return -EINVAL;
3054
3055 /* FIXME: we need upper bound checking, too!! */
3056 if (lba < 0 || ra.nframes <= 0 || ra.nframes > CD_FRAMES)
3057 return -EINVAL;
3058
3059 return cdrom_read_cdda(cdi, ra.buf, lba, ra.nframes);
3060}
3061
3062static noinline int mmc_ioctl_cdrom_subchannel(struct cdrom_device_info *cdi,
3063 void __user *arg)
3064{
3065 int ret;
3066 struct cdrom_subchnl q;
3067 u_char requested, back;
3068 if (copy_from_user(&q, (struct cdrom_subchnl __user *)arg, sizeof(q)))
3069 return -EFAULT;
3070 requested = q.cdsc_format;
3071 if (!((requested == CDROM_MSF) ||
3072 (requested == CDROM_LBA)))
3073 return -EINVAL;
vchannaiahbf294e82016-06-29 08:18:25 -04003074
Joe Perches2e9aa082014-05-04 17:05:08 -07003075 ret = cdrom_read_subchannel(cdi, &q, 0);
3076 if (ret)
3077 return ret;
3078 back = q.cdsc_format; /* local copy */
3079 sanitize_format(&q.cdsc_absaddr, &back, requested);
3080 sanitize_format(&q.cdsc_reladdr, &q.cdsc_format, requested);
3081 if (copy_to_user((struct cdrom_subchnl __user *)arg, &q, sizeof(q)))
3082 return -EFAULT;
3083 /* cd_dbg(CD_DO_IOCTL, "CDROMSUBCHNL successful\n"); */
3084 return 0;
3085}
3086
3087static noinline int mmc_ioctl_cdrom_play_msf(struct cdrom_device_info *cdi,
3088 void __user *arg,
3089 struct packet_command *cgc)
3090{
Kees Cook853fe1b2017-02-13 16:25:26 -08003091 const struct cdrom_device_ops *cdo = cdi->ops;
Joe Perches2e9aa082014-05-04 17:05:08 -07003092 struct cdrom_msf msf;
3093 cd_dbg(CD_DO_IOCTL, "entering CDROMPLAYMSF\n");
3094 if (copy_from_user(&msf, (struct cdrom_msf __user *)arg, sizeof(msf)))
3095 return -EFAULT;
3096 cgc->cmd[0] = GPCMD_PLAY_AUDIO_MSF;
3097 cgc->cmd[3] = msf.cdmsf_min0;
3098 cgc->cmd[4] = msf.cdmsf_sec0;
3099 cgc->cmd[5] = msf.cdmsf_frame0;
3100 cgc->cmd[6] = msf.cdmsf_min1;
3101 cgc->cmd[7] = msf.cdmsf_sec1;
3102 cgc->cmd[8] = msf.cdmsf_frame1;
3103 cgc->data_direction = CGC_DATA_NONE;
3104 return cdo->generic_packet(cdi, cgc);
3105}
3106
3107static noinline int mmc_ioctl_cdrom_play_blk(struct cdrom_device_info *cdi,
3108 void __user *arg,
3109 struct packet_command *cgc)
3110{
Kees Cook853fe1b2017-02-13 16:25:26 -08003111 const struct cdrom_device_ops *cdo = cdi->ops;
Joe Perches2e9aa082014-05-04 17:05:08 -07003112 struct cdrom_blk blk;
3113 cd_dbg(CD_DO_IOCTL, "entering CDROMPLAYBLK\n");
3114 if (copy_from_user(&blk, (struct cdrom_blk __user *)arg, sizeof(blk)))
3115 return -EFAULT;
3116 cgc->cmd[0] = GPCMD_PLAY_AUDIO_10;
3117 cgc->cmd[2] = (blk.from >> 24) & 0xff;
3118 cgc->cmd[3] = (blk.from >> 16) & 0xff;
3119 cgc->cmd[4] = (blk.from >> 8) & 0xff;
3120 cgc->cmd[5] = blk.from & 0xff;
3121 cgc->cmd[7] = (blk.len >> 8) & 0xff;
3122 cgc->cmd[8] = blk.len & 0xff;
3123 cgc->data_direction = CGC_DATA_NONE;
3124 return cdo->generic_packet(cdi, cgc);
3125}
3126
3127static noinline int mmc_ioctl_cdrom_volume(struct cdrom_device_info *cdi,
3128 void __user *arg,
3129 struct packet_command *cgc,
3130 unsigned int cmd)
3131{
3132 struct cdrom_volctrl volctrl;
3133 unsigned char buffer[32];
3134 char mask[sizeof(buffer)];
3135 unsigned short offset;
3136 int ret;
3137
3138 cd_dbg(CD_DO_IOCTL, "entering CDROMVOLUME\n");
3139
3140 if (copy_from_user(&volctrl, (struct cdrom_volctrl __user *)arg,
3141 sizeof(volctrl)))
3142 return -EFAULT;
3143
3144 cgc->buffer = buffer;
3145 cgc->buflen = 24;
3146 ret = cdrom_mode_sense(cdi, cgc, GPMODE_AUDIO_CTL_PAGE, 0);
3147 if (ret)
3148 return ret;
3149
3150 /* originally the code depended on buffer[1] to determine
3151 how much data is available for transfer. buffer[1] is
3152 unfortunately ambigious and the only reliable way seem
3153 to be to simply skip over the block descriptor... */
3154 offset = 8 + be16_to_cpu(*(__be16 *)(buffer + 6));
3155
3156 if (offset + 16 > sizeof(buffer))
3157 return -E2BIG;
3158
3159 if (offset + 16 > cgc->buflen) {
3160 cgc->buflen = offset + 16;
3161 ret = cdrom_mode_sense(cdi, cgc,
3162 GPMODE_AUDIO_CTL_PAGE, 0);
3163 if (ret)
3164 return ret;
3165 }
3166
3167 /* sanity check */
3168 if ((buffer[offset] & 0x3f) != GPMODE_AUDIO_CTL_PAGE ||
3169 buffer[offset + 1] < 14)
3170 return -EINVAL;
3171
3172 /* now we have the current volume settings. if it was only
3173 a CDROMVOLREAD, return these values */
3174 if (cmd == CDROMVOLREAD) {
3175 volctrl.channel0 = buffer[offset+9];
3176 volctrl.channel1 = buffer[offset+11];
3177 volctrl.channel2 = buffer[offset+13];
3178 volctrl.channel3 = buffer[offset+15];
3179 if (copy_to_user((struct cdrom_volctrl __user *)arg, &volctrl,
3180 sizeof(volctrl)))
3181 return -EFAULT;
3182 return 0;
3183 }
3184
3185 /* get the volume mask */
3186 cgc->buffer = mask;
3187 ret = cdrom_mode_sense(cdi, cgc, GPMODE_AUDIO_CTL_PAGE, 1);
3188 if (ret)
3189 return ret;
3190
3191 buffer[offset + 9] = volctrl.channel0 & mask[offset + 9];
3192 buffer[offset + 11] = volctrl.channel1 & mask[offset + 11];
3193 buffer[offset + 13] = volctrl.channel2 & mask[offset + 13];
3194 buffer[offset + 15] = volctrl.channel3 & mask[offset + 15];
3195
3196 /* set volume */
3197 cgc->buffer = buffer + offset - 8;
3198 memset(cgc->buffer, 0, 8);
3199 return cdrom_mode_select(cdi, cgc);
3200}
3201
3202static noinline int mmc_ioctl_cdrom_start_stop(struct cdrom_device_info *cdi,
3203 struct packet_command *cgc,
3204 int cmd)
3205{
Kees Cook853fe1b2017-02-13 16:25:26 -08003206 const struct cdrom_device_ops *cdo = cdi->ops;
Joe Perches2e9aa082014-05-04 17:05:08 -07003207 cd_dbg(CD_DO_IOCTL, "entering CDROMSTART/CDROMSTOP\n");
3208 cgc->cmd[0] = GPCMD_START_STOP_UNIT;
3209 cgc->cmd[1] = 1;
3210 cgc->cmd[4] = (cmd == CDROMSTART) ? 1 : 0;
3211 cgc->data_direction = CGC_DATA_NONE;
3212 return cdo->generic_packet(cdi, cgc);
3213}
3214
3215static noinline int mmc_ioctl_cdrom_pause_resume(struct cdrom_device_info *cdi,
3216 struct packet_command *cgc,
3217 int cmd)
3218{
Kees Cook853fe1b2017-02-13 16:25:26 -08003219 const struct cdrom_device_ops *cdo = cdi->ops;
Joe Perches2e9aa082014-05-04 17:05:08 -07003220 cd_dbg(CD_DO_IOCTL, "entering CDROMPAUSE/CDROMRESUME\n");
3221 cgc->cmd[0] = GPCMD_PAUSE_RESUME;
3222 cgc->cmd[8] = (cmd == CDROMRESUME) ? 1 : 0;
3223 cgc->data_direction = CGC_DATA_NONE;
3224 return cdo->generic_packet(cdi, cgc);
3225}
3226
3227static noinline int mmc_ioctl_dvd_read_struct(struct cdrom_device_info *cdi,
3228 void __user *arg,
3229 struct packet_command *cgc)
3230{
3231 int ret;
3232 dvd_struct *s;
3233 int size = sizeof(dvd_struct);
3234
3235 if (!CDROM_CAN(CDC_DVD))
3236 return -ENOSYS;
3237
Al Viroabb0f6a2016-01-02 14:59:38 -05003238 s = memdup_user(arg, size);
3239 if (IS_ERR(s))
3240 return PTR_ERR(s);
Joe Perches2e9aa082014-05-04 17:05:08 -07003241
3242 cd_dbg(CD_DO_IOCTL, "entering DVD_READ_STRUCT\n");
Joe Perches2e9aa082014-05-04 17:05:08 -07003243
3244 ret = dvd_read_struct(cdi, s, cgc);
3245 if (ret)
3246 goto out;
3247
3248 if (copy_to_user(arg, s, size))
3249 ret = -EFAULT;
3250out:
3251 kfree(s);
3252 return ret;
3253}
3254
3255static noinline int mmc_ioctl_dvd_auth(struct cdrom_device_info *cdi,
3256 void __user *arg)
3257{
3258 int ret;
3259 dvd_authinfo ai;
3260 if (!CDROM_CAN(CDC_DVD))
3261 return -ENOSYS;
3262 cd_dbg(CD_DO_IOCTL, "entering DVD_AUTH\n");
3263 if (copy_from_user(&ai, (dvd_authinfo __user *)arg, sizeof(ai)))
3264 return -EFAULT;
3265 ret = dvd_do_auth(cdi, &ai);
3266 if (ret)
3267 return ret;
3268 if (copy_to_user((dvd_authinfo __user *)arg, &ai, sizeof(ai)))
3269 return -EFAULT;
3270 return 0;
3271}
3272
3273static noinline int mmc_ioctl_cdrom_next_writable(struct cdrom_device_info *cdi,
3274 void __user *arg)
3275{
3276 int ret;
3277 long next = 0;
3278 cd_dbg(CD_DO_IOCTL, "entering CDROM_NEXT_WRITABLE\n");
3279 ret = cdrom_get_next_writable(cdi, &next);
3280 if (ret)
3281 return ret;
3282 if (copy_to_user((long __user *)arg, &next, sizeof(next)))
3283 return -EFAULT;
3284 return 0;
3285}
3286
3287static noinline int mmc_ioctl_cdrom_last_written(struct cdrom_device_info *cdi,
3288 void __user *arg)
3289{
3290 int ret;
3291 long last = 0;
3292 cd_dbg(CD_DO_IOCTL, "entering CDROM_LAST_WRITTEN\n");
3293 ret = cdrom_get_last_written(cdi, &last);
3294 if (ret)
3295 return ret;
Arnd Bergmannab8bc542019-12-09 16:33:16 +01003296 if (in_compat_syscall())
3297 return put_user(last, (__s32 __user *)arg);
3298
3299 return put_user(last, (long __user *)arg);
Joe Perches2e9aa082014-05-04 17:05:08 -07003300}
3301
3302static int mmc_ioctl(struct cdrom_device_info *cdi, unsigned int cmd,
3303 unsigned long arg)
3304{
3305 struct packet_command cgc;
3306 void __user *userptr = (void __user *)arg;
3307
3308 memset(&cgc, 0, sizeof(cgc));
3309
3310 /* build a unified command and queue it through
3311 cdo->generic_packet() */
3312 switch (cmd) {
3313 case CDROMREADRAW:
3314 case CDROMREADMODE1:
3315 case CDROMREADMODE2:
3316 return mmc_ioctl_cdrom_read_data(cdi, userptr, &cgc, cmd);
3317 case CDROMREADAUDIO:
3318 return mmc_ioctl_cdrom_read_audio(cdi, userptr);
3319 case CDROMSUBCHNL:
3320 return mmc_ioctl_cdrom_subchannel(cdi, userptr);
3321 case CDROMPLAYMSF:
3322 return mmc_ioctl_cdrom_play_msf(cdi, userptr, &cgc);
3323 case CDROMPLAYBLK:
3324 return mmc_ioctl_cdrom_play_blk(cdi, userptr, &cgc);
3325 case CDROMVOLCTRL:
3326 case CDROMVOLREAD:
3327 return mmc_ioctl_cdrom_volume(cdi, userptr, &cgc, cmd);
3328 case CDROMSTART:
3329 case CDROMSTOP:
3330 return mmc_ioctl_cdrom_start_stop(cdi, &cgc, cmd);
3331 case CDROMPAUSE:
3332 case CDROMRESUME:
3333 return mmc_ioctl_cdrom_pause_resume(cdi, &cgc, cmd);
3334 case DVD_READ_STRUCT:
3335 return mmc_ioctl_dvd_read_struct(cdi, userptr, &cgc);
3336 case DVD_AUTH:
3337 return mmc_ioctl_dvd_auth(cdi, userptr);
3338 case CDROM_NEXT_WRITABLE:
3339 return mmc_ioctl_cdrom_next_writable(cdi, userptr);
3340 case CDROM_LAST_WRITTEN:
3341 return mmc_ioctl_cdrom_last_written(cdi, userptr);
3342 }
3343
3344 return -ENOTTY;
3345}
3346
3347/*
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08003348 * Just about every imaginable ioctl is supported in the Uniform layer
3349 * these days.
3350 * ATAPI / SCSI specific code now mainly resides in mmc_ioctl().
3351 */
Al Virobbc1cc92007-10-07 17:54:28 -04003352int cdrom_ioctl(struct cdrom_device_info *cdi, struct block_device *bdev,
3353 fmode_t mode, unsigned int cmd, unsigned long arg)
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08003354{
3355 void __user *argp = (void __user *)arg;
3356 int ret;
3357
3358 /*
3359 * Try the generic SCSI command ioctl's first.
3360 */
Paolo Bonzini577ebb32012-01-12 16:01:27 +01003361 ret = scsi_cmd_blk_ioctl(bdev, mode, cmd, argp);
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08003362 if (ret != -ENOTTY)
3363 return ret;
3364
3365 switch (cmd) {
3366 case CDROMMULTISESSION:
3367 return cdrom_ioctl_multisession(cdi, argp);
3368 case CDROMEJECT:
3369 return cdrom_ioctl_eject(cdi);
3370 case CDROMCLOSETRAY:
3371 return cdrom_ioctl_closetray(cdi);
3372 case CDROMEJECT_SW:
3373 return cdrom_ioctl_eject_sw(cdi, arg);
3374 case CDROM_MEDIA_CHANGED:
3375 return cdrom_ioctl_media_changed(cdi, arg);
3376 case CDROM_SET_OPTIONS:
3377 return cdrom_ioctl_set_options(cdi, arg);
3378 case CDROM_CLEAR_OPTIONS:
3379 return cdrom_ioctl_clear_options(cdi, arg);
3380 case CDROM_SELECT_SPEED:
3381 return cdrom_ioctl_select_speed(cdi, arg);
3382 case CDROM_SELECT_DISC:
3383 return cdrom_ioctl_select_disc(cdi, arg);
3384 case CDROMRESET:
Al Virobbc1cc92007-10-07 17:54:28 -04003385 return cdrom_ioctl_reset(cdi, bdev);
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08003386 case CDROM_LOCKDOOR:
3387 return cdrom_ioctl_lock_door(cdi, arg);
3388 case CDROM_DEBUG:
3389 return cdrom_ioctl_debug(cdi, arg);
3390 case CDROM_GET_CAPABILITY:
3391 return cdrom_ioctl_get_capability(cdi);
3392 case CDROM_GET_MCN:
3393 return cdrom_ioctl_get_mcn(cdi, argp);
3394 case CDROM_DRIVE_STATUS:
3395 return cdrom_ioctl_drive_status(cdi, arg);
3396 case CDROM_DISC_STATUS:
3397 return cdrom_ioctl_disc_status(cdi);
3398 case CDROM_CHANGER_NSLOTS:
3399 return cdrom_ioctl_changer_nslots(cdi);
3400 }
3401
3402 /*
3403 * Use the ioctls that are implemented through the generic_packet()
3404 * interface. this may look at bit funny, but if -ENOTTY is
3405 * returned that particular ioctl is not implemented and we
3406 * let it go through the device specific ones.
3407 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 if (CDROM_CAN(CDC_GENERIC_PACKET)) {
3409 ret = mmc_ioctl(cdi, cmd, arg);
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08003410 if (ret != -ENOTTY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 }
3413
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08003414 /*
Joe Perches5944b2c2014-05-04 17:05:02 -07003415 * Note: most of the cd_dbg() calls are commented out here,
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08003416 * because they fill up the sys log when CD players poll
3417 * the drive.
3418 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 switch (cmd) {
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08003420 case CDROMSUBCHNL:
3421 return cdrom_ioctl_get_subchnl(cdi, argp);
3422 case CDROMREADTOCHDR:
3423 return cdrom_ioctl_read_tochdr(cdi, argp);
3424 case CDROMREADTOCENTRY:
3425 return cdrom_ioctl_read_tocentry(cdi, argp);
3426 case CDROMPLAYMSF:
3427 return cdrom_ioctl_play_msf(cdi, argp);
3428 case CDROMPLAYTRKIND:
3429 return cdrom_ioctl_play_trkind(cdi, argp);
3430 case CDROMVOLCTRL:
3431 return cdrom_ioctl_volctrl(cdi, argp);
3432 case CDROMVOLREAD:
3433 return cdrom_ioctl_volread(cdi, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 case CDROMSTART:
3435 case CDROMSTOP:
3436 case CDROMPAUSE:
Christoph Hellwigd2c5d4f2006-03-23 03:00:14 -08003437 case CDROMRESUME:
3438 return cdrom_ioctl_audioctl(cdi, cmd);
3439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 return -ENOSYS;
3442}
3443
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444EXPORT_SYMBOL(cdrom_get_last_written);
3445EXPORT_SYMBOL(register_cdrom);
3446EXPORT_SYMBOL(unregister_cdrom);
3447EXPORT_SYMBOL(cdrom_open);
3448EXPORT_SYMBOL(cdrom_release);
3449EXPORT_SYMBOL(cdrom_ioctl);
3450EXPORT_SYMBOL(cdrom_media_changed);
3451EXPORT_SYMBOL(cdrom_number_of_slots);
3452EXPORT_SYMBOL(cdrom_mode_select);
3453EXPORT_SYMBOL(cdrom_mode_sense);
3454EXPORT_SYMBOL(init_cdrom_command);
3455EXPORT_SYMBOL(cdrom_get_media_event);
3456
3457#ifdef CONFIG_SYSCTL
3458
3459#define CDROM_STR_SIZE 1000
3460
3461static struct cdrom_sysctl_settings {
3462 char info[CDROM_STR_SIZE]; /* general info */
3463 int autoclose; /* close tray upon mount, etc */
3464 int autoeject; /* eject on umount */
3465 int debug; /* turn on debugging messages */
3466 int lock; /* lock the door on device open */
3467 int check; /* check media type */
3468} cdrom_sysctl_settings;
3469
Dave Young554988d2007-06-19 09:14:26 +02003470enum cdrom_print_option {
3471 CTL_NAME,
3472 CTL_SPEED,
3473 CTL_SLOTS,
3474 CTL_CAPABILITY
3475};
3476
3477static int cdrom_print_info(const char *header, int val, char *info,
3478 int *pos, enum cdrom_print_option option)
3479{
3480 const int max_size = sizeof(cdrom_sysctl_settings.info);
3481 struct cdrom_device_info *cdi;
3482 int ret;
3483
3484 ret = scnprintf(info + *pos, max_size - *pos, header);
3485 if (!ret)
3486 return 1;
3487
3488 *pos += ret;
3489
Akinobu Mita7fd097d2008-03-26 12:09:02 +01003490 list_for_each_entry(cdi, &cdrom_list, list) {
Dave Young554988d2007-06-19 09:14:26 +02003491 switch (option) {
3492 case CTL_NAME:
3493 ret = scnprintf(info + *pos, max_size - *pos,
3494 "\t%s", cdi->name);
3495 break;
3496 case CTL_SPEED:
3497 ret = scnprintf(info + *pos, max_size - *pos,
3498 "\t%d", cdi->speed);
3499 break;
3500 case CTL_SLOTS:
3501 ret = scnprintf(info + *pos, max_size - *pos,
3502 "\t%d", cdi->capacity);
3503 break;
3504 case CTL_CAPABILITY:
3505 ret = scnprintf(info + *pos, max_size - *pos,
3506 "\t%d", CDROM_CAN(val) != 0);
3507 break;
3508 default:
Joe Perchese597cd02010-07-01 08:24:32 +02003509 pr_info("invalid option%d\n", option);
Dave Young554988d2007-06-19 09:14:26 +02003510 return 1;
3511 }
3512 if (!ret)
3513 return 1;
3514 *pos += ret;
3515 }
3516
3517 return 0;
3518}
3519
Joe Perches90a3b892014-06-06 14:37:57 -07003520static int cdrom_sysctl_info(struct ctl_table *ctl, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 void __user *buffer, size_t *lenp, loff_t *ppos)
3522{
Dave Young554988d2007-06-19 09:14:26 +02003523 int pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 char *info = cdrom_sysctl_settings.info;
Dave Young554988d2007-06-19 09:14:26 +02003525 const int max_size = sizeof(cdrom_sysctl_settings.info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526
3527 if (!*lenp || (*ppos && !write)) {
3528 *lenp = 0;
3529 return 0;
3530 }
3531
Dave Young554988d2007-06-19 09:14:26 +02003532 mutex_lock(&cdrom_mutex);
3533
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534 pos = sprintf(info, "CD-ROM information, " VERSION "\n");
3535
Dave Young554988d2007-06-19 09:14:26 +02003536 if (cdrom_print_info("\ndrive name:\t", 0, info, &pos, CTL_NAME))
3537 goto done;
3538 if (cdrom_print_info("\ndrive speed:\t", 0, info, &pos, CTL_SPEED))
3539 goto done;
3540 if (cdrom_print_info("\ndrive # of slots:", 0, info, &pos, CTL_SLOTS))
3541 goto done;
3542 if (cdrom_print_info("\nCan close tray:\t",
3543 CDC_CLOSE_TRAY, info, &pos, CTL_CAPABILITY))
3544 goto done;
3545 if (cdrom_print_info("\nCan open tray:\t",
3546 CDC_OPEN_TRAY, info, &pos, CTL_CAPABILITY))
3547 goto done;
3548 if (cdrom_print_info("\nCan lock tray:\t",
3549 CDC_LOCK, info, &pos, CTL_CAPABILITY))
3550 goto done;
3551 if (cdrom_print_info("\nCan change speed:",
3552 CDC_SELECT_SPEED, info, &pos, CTL_CAPABILITY))
3553 goto done;
3554 if (cdrom_print_info("\nCan select disk:",
3555 CDC_SELECT_DISC, info, &pos, CTL_CAPABILITY))
3556 goto done;
3557 if (cdrom_print_info("\nCan read multisession:",
3558 CDC_MULTI_SESSION, info, &pos, CTL_CAPABILITY))
3559 goto done;
3560 if (cdrom_print_info("\nCan read MCN:\t",
3561 CDC_MCN, info, &pos, CTL_CAPABILITY))
3562 goto done;
3563 if (cdrom_print_info("\nReports media changed:",
3564 CDC_MEDIA_CHANGED, info, &pos, CTL_CAPABILITY))
3565 goto done;
3566 if (cdrom_print_info("\nCan play audio:\t",
3567 CDC_PLAY_AUDIO, info, &pos, CTL_CAPABILITY))
3568 goto done;
3569 if (cdrom_print_info("\nCan write CD-R:\t",
3570 CDC_CD_R, info, &pos, CTL_CAPABILITY))
3571 goto done;
3572 if (cdrom_print_info("\nCan write CD-RW:",
3573 CDC_CD_RW, info, &pos, CTL_CAPABILITY))
3574 goto done;
3575 if (cdrom_print_info("\nCan read DVD:\t",
3576 CDC_DVD, info, &pos, CTL_CAPABILITY))
3577 goto done;
3578 if (cdrom_print_info("\nCan write DVD-R:",
3579 CDC_DVD_R, info, &pos, CTL_CAPABILITY))
3580 goto done;
3581 if (cdrom_print_info("\nCan write DVD-RAM:",
3582 CDC_DVD_RAM, info, &pos, CTL_CAPABILITY))
3583 goto done;
3584 if (cdrom_print_info("\nCan read MRW:\t",
3585 CDC_MRW, info, &pos, CTL_CAPABILITY))
3586 goto done;
3587 if (cdrom_print_info("\nCan write MRW:\t",
3588 CDC_MRW_W, info, &pos, CTL_CAPABILITY))
3589 goto done;
3590 if (cdrom_print_info("\nCan write RAM:\t",
3591 CDC_RAM, info, &pos, CTL_CAPABILITY))
3592 goto done;
3593 if (!scnprintf(info + pos, max_size - pos, "\n\n"))
3594 goto done;
3595doit:
3596 mutex_unlock(&cdrom_mutex);
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07003597 return proc_dostring(ctl, write, buffer, lenp, ppos);
Dave Young554988d2007-06-19 09:14:26 +02003598done:
Joe Perchese597cd02010-07-01 08:24:32 +02003599 pr_info("info buffer too small\n");
Dave Young554988d2007-06-19 09:14:26 +02003600 goto doit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601}
3602
3603/* Unfortunately, per device settings are not implemented through
3604 procfs/sysctl yet. When they are, this will naturally disappear. For now
3605 just update all drives. Later this will become the template on which
3606 new registered drives will be based. */
3607static void cdrom_update_settings(void)
3608{
3609 struct cdrom_device_info *cdi;
3610
Akinobu Mita032d8d92008-03-26 12:09:01 +01003611 mutex_lock(&cdrom_mutex);
Akinobu Mita7fd097d2008-03-26 12:09:02 +01003612 list_for_each_entry(cdi, &cdrom_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 if (autoclose && CDROM_CAN(CDC_CLOSE_TRAY))
3614 cdi->options |= CDO_AUTO_CLOSE;
3615 else if (!autoclose)
3616 cdi->options &= ~CDO_AUTO_CLOSE;
3617 if (autoeject && CDROM_CAN(CDC_OPEN_TRAY))
3618 cdi->options |= CDO_AUTO_EJECT;
3619 else if (!autoeject)
3620 cdi->options &= ~CDO_AUTO_EJECT;
3621 if (lockdoor && CDROM_CAN(CDC_LOCK))
3622 cdi->options |= CDO_LOCK;
3623 else if (!lockdoor)
3624 cdi->options &= ~CDO_LOCK;
3625 if (check_media_type)
3626 cdi->options |= CDO_CHECK_TYPE;
3627 else
3628 cdi->options &= ~CDO_CHECK_TYPE;
3629 }
Akinobu Mita032d8d92008-03-26 12:09:01 +01003630 mutex_unlock(&cdrom_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631}
3632
Joe Perches90a3b892014-06-06 14:37:57 -07003633static int cdrom_sysctl_handler(struct ctl_table *ctl, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634 void __user *buffer, size_t *lenp, loff_t *ppos)
3635{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636 int ret;
3637
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07003638 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639
Eric W. Biederman06489b42007-10-18 03:05:28 -07003640 if (write) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641
3642 /* we only care for 1 or 0. */
Eric W. Biederman06489b42007-10-18 03:05:28 -07003643 autoclose = !!cdrom_sysctl_settings.autoclose;
3644 autoeject = !!cdrom_sysctl_settings.autoeject;
3645 debug = !!cdrom_sysctl_settings.debug;
3646 lockdoor = !!cdrom_sysctl_settings.lock;
3647 check_media_type = !!cdrom_sysctl_settings.check;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649 /* update the option flags according to the changes. we
3650 don't have per device options through sysctl yet,
3651 but we will have and then this will disappear. */
3652 cdrom_update_settings();
3653 }
3654
3655 return ret;
3656}
3657
3658/* Place files in /proc/sys/dev/cdrom */
Joe Perches90a3b892014-06-06 14:37:57 -07003659static struct ctl_table cdrom_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661 .procname = "info",
3662 .data = &cdrom_sysctl_settings.info,
3663 .maxlen = CDROM_STR_SIZE,
3664 .mode = 0444,
Eric W. Biederman6d456112009-11-16 03:11:48 -08003665 .proc_handler = cdrom_sysctl_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 },
3667 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668 .procname = "autoclose",
3669 .data = &cdrom_sysctl_settings.autoclose,
3670 .maxlen = sizeof(int),
3671 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -08003672 .proc_handler = cdrom_sysctl_handler,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 },
3674 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 .procname = "autoeject",
3676 .data = &cdrom_sysctl_settings.autoeject,
3677 .maxlen = sizeof(int),
3678 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -08003679 .proc_handler = cdrom_sysctl_handler,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 },
3681 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 .procname = "debug",
3683 .data = &cdrom_sysctl_settings.debug,
3684 .maxlen = sizeof(int),
3685 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -08003686 .proc_handler = cdrom_sysctl_handler,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 },
3688 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 .procname = "lock",
3690 .data = &cdrom_sysctl_settings.lock,
3691 .maxlen = sizeof(int),
3692 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -08003693 .proc_handler = cdrom_sysctl_handler,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694 },
3695 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 .procname = "check_media",
3697 .data = &cdrom_sysctl_settings.check,
3698 .maxlen = sizeof(int),
3699 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -08003700 .proc_handler = cdrom_sysctl_handler
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701 },
Eric W. Biederman894d2492009-11-05 14:34:02 -08003702 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703};
3704
Joe Perches90a3b892014-06-06 14:37:57 -07003705static struct ctl_table cdrom_cdrom_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 .procname = "cdrom",
3708 .maxlen = 0,
3709 .mode = 0555,
3710 .child = cdrom_table,
3711 },
Eric W. Biederman894d2492009-11-05 14:34:02 -08003712 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713};
3714
3715/* Make sure that /proc/sys/dev is there */
Joe Perches90a3b892014-06-06 14:37:57 -07003716static struct ctl_table cdrom_root_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 .procname = "dev",
3719 .maxlen = 0,
3720 .mode = 0555,
3721 .child = cdrom_cdrom_table,
3722 },
Eric W. Biederman894d2492009-11-05 14:34:02 -08003723 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724};
3725static struct ctl_table_header *cdrom_sysctl_header;
3726
3727static void cdrom_sysctl_register(void)
3728{
Guenter Roeckf25191b2019-02-06 21:13:49 -08003729 static atomic_t initialized = ATOMIC_INIT(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730
Guenter Roeckf25191b2019-02-06 21:13:49 -08003731 if (!atomic_add_unless(&initialized, 1, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 return;
3733
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08003734 cdrom_sysctl_header = register_sysctl_table(cdrom_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735
3736 /* set the defaults */
3737 cdrom_sysctl_settings.autoclose = autoclose;
3738 cdrom_sysctl_settings.autoeject = autoeject;
3739 cdrom_sysctl_settings.debug = debug;
3740 cdrom_sysctl_settings.lock = lockdoor;
3741 cdrom_sysctl_settings.check = check_media_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742}
3743
3744static void cdrom_sysctl_unregister(void)
3745{
3746 if (cdrom_sysctl_header)
3747 unregister_sysctl_table(cdrom_sysctl_header);
3748}
3749
Akinobu Mita17672cf2008-03-26 12:08:59 +01003750#else /* CONFIG_SYSCTL */
3751
3752static void cdrom_sysctl_register(void)
3753{
3754}
3755
3756static void cdrom_sysctl_unregister(void)
3757{
3758}
3759
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760#endif /* CONFIG_SYSCTL */
3761
3762static int __init cdrom_init(void)
3763{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764 cdrom_sysctl_register();
Akinobu Mita17672cf2008-03-26 12:08:59 +01003765
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 return 0;
3767}
3768
3769static void __exit cdrom_exit(void)
3770{
Joe Perchese597cd02010-07-01 08:24:32 +02003771 pr_info("Uniform CD-ROM driver unloaded\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772 cdrom_sysctl_unregister();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773}
3774
3775module_init(cdrom_init);
3776module_exit(cdrom_exit);
3777MODULE_LICENSE("GPL");