1f30c2269SUwe Zeisberger /* linux/drivers/cdrom/cdrom.c
21da177e4SLinus Torvalds Copyright (c) 1996, 1997 David A. van Leeuwen.
31da177e4SLinus Torvalds Copyright (c) 1997, 1998 Erik Andersen <andersee@debian.org>
41da177e4SLinus Torvalds Copyright (c) 1998, 1999 Jens Axboe <axboe@image.dk>
51da177e4SLinus Torvalds
61da177e4SLinus Torvalds May be copied or modified under the terms of the GNU General Public
71da177e4SLinus Torvalds License. See linux/COPYING for more information.
81da177e4SLinus Torvalds
91da177e4SLinus Torvalds Uniform CD-ROM driver for Linux.
108ea61889SMauro Carvalho Chehab See Documentation/cdrom/cdrom-standard.rst for usage information.
111da177e4SLinus Torvalds
121da177e4SLinus Torvalds The routines in the file provide a uniform interface between the
131da177e4SLinus Torvalds software that uses CD-ROMs and the various low-level drivers that
141da177e4SLinus Torvalds actually talk to the hardware. Suggestions are welcome.
151da177e4SLinus Torvalds Patches that work are more welcome though. ;-)
161da177e4SLinus Torvalds
171da177e4SLinus Torvalds Revision History
181da177e4SLinus Torvalds ----------------------------------
191da177e4SLinus Torvalds 1.00 Date Unknown -- David van Leeuwen <david@tm.tno.nl>
201da177e4SLinus Torvalds -- Initial version by David A. van Leeuwen. I don't have a detailed
211da177e4SLinus Torvalds changelog for the 1.x series, David?
221da177e4SLinus Torvalds
231da177e4SLinus Torvalds 2.00 Dec 2, 1997 -- Erik Andersen <andersee@debian.org>
2425985edcSLucas De Marchi -- New maintainer! As David A. van Leeuwen has been too busy to actively
251da177e4SLinus Torvalds maintain and improve this driver, I am now carrying on the torch. If
261da177e4SLinus Torvalds you have a problem with this driver, please feel free to contact me.
271da177e4SLinus Torvalds
281da177e4SLinus Torvalds -- Added (rudimentary) sysctl interface. I realize this is really weak
291da177e4SLinus Torvalds right now, and is _very_ badly implemented. It will be improved...
301da177e4SLinus Torvalds
311da177e4SLinus Torvalds -- Modified CDROM_DISC_STATUS so that it is now incorporated into
321da177e4SLinus Torvalds the Uniform CD-ROM driver via the cdrom_count_tracks function.
331da177e4SLinus Torvalds The cdrom_count_tracks function helps resolve some of the false
341da177e4SLinus Torvalds assumptions of the CDROM_DISC_STATUS ioctl, and is also used to check
351da177e4SLinus Torvalds for the correct media type when mounting or playing audio from a CD.
361da177e4SLinus Torvalds
371da177e4SLinus Torvalds -- Remove the calls to verify_area and only use the copy_from_user and
381da177e4SLinus Torvalds copy_to_user stuff, since these calls now provide their own memory
391da177e4SLinus Torvalds checking with the 2.1.x kernels.
401da177e4SLinus Torvalds
411da177e4SLinus Torvalds -- Major update to return codes so that errors from low-level drivers
421da177e4SLinus Torvalds are passed on through (thanks to Gerd Knorr for pointing out this
431da177e4SLinus Torvalds problem).
441da177e4SLinus Torvalds
451da177e4SLinus Torvalds -- Made it so if a function isn't implemented in a low-level driver,
461da177e4SLinus Torvalds ENOSYS is now returned instead of EINVAL.
471da177e4SLinus Torvalds
481da177e4SLinus Torvalds -- Simplified some complex logic so that the source code is easier to read.
491da177e4SLinus Torvalds
501da177e4SLinus Torvalds -- Other stuff I probably forgot to mention (lots of changes).
511da177e4SLinus Torvalds
521da177e4SLinus Torvalds 2.01 to 2.11 Dec 1997-Jan 1998
531da177e4SLinus Torvalds -- TO-DO! Write changelogs for 2.01 to 2.12.
541da177e4SLinus Torvalds
551da177e4SLinus Torvalds 2.12 Jan 24, 1998 -- Erik Andersen <andersee@debian.org>
561da177e4SLinus Torvalds -- Fixed a bug in the IOCTL_IN and IOCTL_OUT macros. It turns out that
571da177e4SLinus Torvalds copy_*_user does not return EFAULT on error, but instead returns the number
581da177e4SLinus Torvalds of bytes not copied. I was returning whatever non-zero stuff came back from
591da177e4SLinus Torvalds the copy_*_user functions directly, which would result in strange errors.
601da177e4SLinus Torvalds
611da177e4SLinus Torvalds 2.13 July 17, 1998 -- Erik Andersen <andersee@debian.org>
621da177e4SLinus Torvalds -- Fixed a bug in CDROM_SELECT_SPEED where you couldn't lower the speed
631da177e4SLinus Torvalds of the drive. Thanks to Tobias Ringstr|m <tori@prosolvia.se> for pointing
641da177e4SLinus Torvalds this out and providing a simple fix.
651da177e4SLinus Torvalds -- Fixed the procfs-unload-module bug with the fill_inode procfs callback.
661da177e4SLinus Torvalds thanks to Andrea Arcangeli
671da177e4SLinus Torvalds -- Fixed it so that the /proc entry now also shows up when cdrom is
681da177e4SLinus Torvalds compiled into the kernel. Before it only worked when loaded as a module.
691da177e4SLinus Torvalds
701da177e4SLinus Torvalds 2.14 August 17, 1998 -- Erik Andersen <andersee@debian.org>
711da177e4SLinus Torvalds -- Fixed a bug in cdrom_media_changed and handling of reporting that
721da177e4SLinus Torvalds the media had changed for devices that _don't_ implement media_changed.
731da177e4SLinus Torvalds Thanks to Grant R. Guenther <grant@torque.net> for spotting this bug.
741da177e4SLinus Torvalds -- Made a few things more pedanticly correct.
751da177e4SLinus Torvalds
761da177e4SLinus Torvalds 2.50 Oct 19, 1998 - Jens Axboe <axboe@image.dk>
771da177e4SLinus Torvalds -- New maintainers! Erik was too busy to continue the work on the driver,
781da177e4SLinus Torvalds so now Chris Zwilling <chris@cloudnet.com> and Jens Axboe <axboe@image.dk>
791da177e4SLinus Torvalds will do their best to follow in his footsteps
801da177e4SLinus Torvalds
811da177e4SLinus Torvalds 2.51 Dec 20, 1998 - Jens Axboe <axboe@image.dk>
821da177e4SLinus Torvalds -- Check if drive is capable of doing what we ask before blindly changing
831da177e4SLinus Torvalds cdi->options in various ioctl.
841da177e4SLinus Torvalds -- Added version to proc entry.
851da177e4SLinus Torvalds
861da177e4SLinus Torvalds 2.52 Jan 16, 1999 - Jens Axboe <axboe@image.dk>
871da177e4SLinus Torvalds -- Fixed an error in open_for_data where we would sometimes not return
881da177e4SLinus Torvalds the correct error value. Thanks Huba Gaspar <huba@softcell.hu>.
891da177e4SLinus Torvalds -- Fixed module usage count - usage was based on /proc/sys/dev
901da177e4SLinus Torvalds instead of /proc/sys/dev/cdrom. This could lead to an oops when other
911da177e4SLinus Torvalds modules had entries in dev. Feb 02 - real bug was in sysctl.c where
921da177e4SLinus Torvalds dev would be removed even though it was used. cdrom.c just illuminated
931da177e4SLinus Torvalds that bug.
941da177e4SLinus Torvalds
951da177e4SLinus Torvalds 2.53 Feb 22, 1999 - Jens Axboe <axboe@image.dk>
961da177e4SLinus Torvalds -- Fixup of several ioctl calls, in particular CDROM_SET_OPTIONS has
971da177e4SLinus Torvalds been "rewritten" because capabilities and options aren't in sync. They
981da177e4SLinus Torvalds should be...
991da177e4SLinus Torvalds -- Added CDROM_LOCKDOOR ioctl. Locks the door and keeps it that way.
1001da177e4SLinus Torvalds -- Added CDROM_RESET ioctl.
1011da177e4SLinus Torvalds -- Added CDROM_DEBUG ioctl. Enable debug messages on-the-fly.
1021da177e4SLinus Torvalds -- Added CDROM_GET_CAPABILITY ioctl. This relieves userspace programs
1031da177e4SLinus Torvalds from parsing /proc/sys/dev/cdrom/info.
1041da177e4SLinus Torvalds
1051da177e4SLinus Torvalds 2.54 Mar 15, 1999 - Jens Axboe <axboe@image.dk>
1061da177e4SLinus Torvalds -- Check capability mask from low level driver when counting tracks as
1071da177e4SLinus Torvalds per suggestion from Corey J. Scotts <cstotts@blue.weeg.uiowa.edu>.
1081da177e4SLinus Torvalds
1091da177e4SLinus Torvalds 2.55 Apr 25, 1999 - Jens Axboe <axboe@image.dk>
1101da177e4SLinus Torvalds -- autoclose was mistakenly checked against CDC_OPEN_TRAY instead of
1111da177e4SLinus Torvalds CDC_CLOSE_TRAY.
1121da177e4SLinus Torvalds -- proc info didn't mask against capabilities mask.
1131da177e4SLinus Torvalds
1141da177e4SLinus Torvalds 3.00 Aug 5, 1999 - Jens Axboe <axboe@image.dk>
1151da177e4SLinus Torvalds -- Unified audio ioctl handling across CD-ROM drivers. A lot of the
1161da177e4SLinus Torvalds code was duplicated before. Drives that support the generic packet
1171da177e4SLinus Torvalds interface are now being fed packets from here instead.
1181da177e4SLinus Torvalds -- First attempt at adding support for MMC2 commands - for DVD and
1191da177e4SLinus Torvalds CD-R(W) drives. Only the DVD parts are in now - the interface used is
1201da177e4SLinus Torvalds the same as for the audio ioctls.
1211da177e4SLinus Torvalds -- ioctl cleanups. if a drive couldn't play audio, it didn't get
1221da177e4SLinus Torvalds a change to perform device specific ioctls as well.
1231da177e4SLinus Torvalds -- Defined CDROM_CAN(CDC_XXX) for checking the capabilities.
1241da177e4SLinus Torvalds -- Put in sysctl files for autoclose, autoeject, check_media, debug,
1251da177e4SLinus Torvalds and lock.
1261da177e4SLinus Torvalds -- /proc/sys/dev/cdrom/info has been updated to also contain info about
1271da177e4SLinus Torvalds CD-Rx and DVD capabilities.
1281da177e4SLinus Torvalds -- Now default to checking media type.
1291da177e4SLinus Torvalds -- CDROM_SEND_PACKET ioctl added. The infrastructure was in place for
1301da177e4SLinus Torvalds doing this anyway, with the generic_packet addition.
1311da177e4SLinus Torvalds
1321da177e4SLinus Torvalds 3.01 Aug 6, 1999 - Jens Axboe <axboe@image.dk>
1331da177e4SLinus Torvalds -- Fix up the sysctl handling so that the option flags get set
1341da177e4SLinus Torvalds correctly.
1351da177e4SLinus Torvalds -- Fix up ioctl handling so the device specific ones actually get
1361da177e4SLinus Torvalds called :).
1371da177e4SLinus Torvalds
1381da177e4SLinus Torvalds 3.02 Aug 8, 1999 - Jens Axboe <axboe@image.dk>
1391da177e4SLinus Torvalds -- Fixed volume control on SCSI drives (or others with longer audio
1401da177e4SLinus Torvalds page).
1411da177e4SLinus Torvalds -- Fixed a couple of DVD minors. Thanks to Andrew T. Veliath
1421da177e4SLinus Torvalds <andrewtv@usa.net> for telling me and for having defined the various
1431da177e4SLinus Torvalds DVD structures and ioctls in the first place! He designed the original
1441da177e4SLinus Torvalds DVD patches for ide-cd and while I rearranged and unified them, the
1451da177e4SLinus Torvalds interface is still the same.
1461da177e4SLinus Torvalds
1471da177e4SLinus Torvalds 3.03 Sep 1, 1999 - Jens Axboe <axboe@image.dk>
1481da177e4SLinus Torvalds -- Moved the rest of the audio ioctls from the CD-ROM drivers here. Only
1491da177e4SLinus Torvalds CDROMREADTOCENTRY and CDROMREADTOCHDR are left.
1501da177e4SLinus Torvalds -- Moved the CDROMREADxxx ioctls in here.
1511da177e4SLinus Torvalds -- Defined the cdrom_get_last_written and cdrom_get_next_block as ioctls
1521da177e4SLinus Torvalds and exported functions.
1531da177e4SLinus Torvalds -- Erik Andersen <andersen@xmission.com> modified all SCMD_ commands
1541da177e4SLinus Torvalds to now read GPCMD_ for the new generic packet interface. All low level
1551da177e4SLinus Torvalds drivers are updated as well.
1561da177e4SLinus Torvalds -- Various other cleanups.
1571da177e4SLinus Torvalds
1581da177e4SLinus Torvalds 3.04 Sep 12, 1999 - Jens Axboe <axboe@image.dk>
1591da177e4SLinus Torvalds -- Fixed a couple of possible memory leaks (if an operation failed and
1601da177e4SLinus Torvalds we didn't free the buffer before returning the error).
1611da177e4SLinus Torvalds -- Integrated Uniform CD Changer handling from Richard Sharman
1621da177e4SLinus Torvalds <rsharman@pobox.com>.
1631da177e4SLinus Torvalds -- Defined CD_DVD and CD_CHANGER log levels.
1641da177e4SLinus Torvalds -- Fixed the CDROMREADxxx ioctls.
1651da177e4SLinus Torvalds -- CDROMPLAYTRKIND uses the GPCMD_PLAY_AUDIO_MSF command - too few
1661da177e4SLinus Torvalds drives supported it. We lose the index part, however.
1671da177e4SLinus Torvalds -- Small modifications to accommodate opens of /dev/hdc1, required
1681da177e4SLinus Torvalds for ide-cd to handle multisession discs.
1691da177e4SLinus Torvalds -- Export cdrom_mode_sense and cdrom_mode_select.
1701da177e4SLinus Torvalds -- init_cdrom_command() for setting up a cgc command.
1711da177e4SLinus Torvalds
1721da177e4SLinus Torvalds 3.05 Oct 24, 1999 - Jens Axboe <axboe@image.dk>
1731da177e4SLinus Torvalds -- Changed the interface for CDROM_SEND_PACKET. Before it was virtually
1741da177e4SLinus Torvalds impossible to send the drive data in a sensible way.
1751da177e4SLinus Torvalds -- Lowered stack usage in mmc_ioctl(), dvd_read_disckey(), and
1761da177e4SLinus Torvalds dvd_read_manufact.
1771da177e4SLinus Torvalds -- Added setup of write mode for packet writing.
1781da177e4SLinus Torvalds -- Fixed CDDA ripping with cdda2wav - accept much larger requests of
1791da177e4SLinus Torvalds number of frames and split the reads in blocks of 8.
1801da177e4SLinus Torvalds
1811da177e4SLinus Torvalds 3.06 Dec 13, 1999 - Jens Axboe <axboe@image.dk>
1821da177e4SLinus Torvalds -- Added support for changing the region of DVD drives.
1831da177e4SLinus Torvalds -- Added sense data to generic command.
1841da177e4SLinus Torvalds
1851da177e4SLinus Torvalds 3.07 Feb 2, 2000 - Jens Axboe <axboe@suse.de>
1861da177e4SLinus Torvalds -- Do same "read header length" trick in cdrom_get_disc_info() as
1871da177e4SLinus Torvalds we do in cdrom_get_track_info() -- some drive don't obey specs and
1881da177e4SLinus Torvalds fail if they can't supply the full Mt Fuji size table.
1891da177e4SLinus Torvalds -- Deleted stuff related to setting up write modes. It has a different
1901da177e4SLinus Torvalds home now.
1911da177e4SLinus Torvalds -- Clear header length in mode_select unconditionally.
1921da177e4SLinus Torvalds -- Removed the register_disk() that was added, not needed here.
1931da177e4SLinus Torvalds
1941da177e4SLinus Torvalds 3.08 May 1, 2000 - Jens Axboe <axboe@suse.de>
1951da177e4SLinus Torvalds -- Fix direction flag in setup_send_key and setup_report_key. This
1961da177e4SLinus Torvalds gave some SCSI adapters problems.
1971da177e4SLinus Torvalds -- Always return -EROFS for write opens
1981da177e4SLinus Torvalds -- Convert to module_init/module_exit style init and remove some
1991da177e4SLinus Torvalds of the #ifdef MODULE stuff
2001da177e4SLinus Torvalds -- Fix several dvd errors - DVD_LU_SEND_ASF should pass agid,
2011da177e4SLinus Torvalds DVD_HOST_SEND_RPC_STATE did not set buffer size in cdb, and
2021da177e4SLinus Torvalds dvd_do_auth passed uninitialized data to drive because init_cdrom_command
2031da177e4SLinus Torvalds did not clear a 0 sized buffer.
2041da177e4SLinus Torvalds
2051da177e4SLinus Torvalds 3.09 May 12, 2000 - Jens Axboe <axboe@suse.de>
2061da177e4SLinus Torvalds -- Fix Video-CD on SCSI drives that don't support READ_CD command. In
2071da177e4SLinus Torvalds that case switch block size and issue plain READ_10 again, then switch
2081da177e4SLinus Torvalds back.
2091da177e4SLinus Torvalds
2101da177e4SLinus Torvalds 3.10 Jun 10, 2000 - Jens Axboe <axboe@suse.de>
2111da177e4SLinus Torvalds -- Fix volume control on CD's - old SCSI-II drives now use their own
2121da177e4SLinus Torvalds code, as doing MODE6 stuff in here is really not my intention.
2131da177e4SLinus Torvalds -- Use READ_DISC_INFO for more reliable end-of-disc.
2141da177e4SLinus Torvalds
2151da177e4SLinus Torvalds 3.11 Jun 12, 2000 - Jens Axboe <axboe@suse.de>
2161da177e4SLinus Torvalds -- Fix bug in getting rpc phase 2 region info.
2171da177e4SLinus Torvalds -- Reinstate "correct" CDROMPLAYTRKIND
2181da177e4SLinus Torvalds
2191da177e4SLinus Torvalds 3.12 Oct 18, 2000 - Jens Axboe <axboe@suse.de>
2201da177e4SLinus Torvalds -- Use quiet bit on packet commands not known to work
2211da177e4SLinus Torvalds
2221da177e4SLinus Torvalds 3.20 Dec 17, 2003 - Jens Axboe <axboe@suse.de>
2231da177e4SLinus Torvalds -- Various fixes and lots of cleanups not listed :-)
2241da177e4SLinus Torvalds -- Locking fixes
2251da177e4SLinus Torvalds -- Mt Rainier support
2261da177e4SLinus Torvalds -- DVD-RAM write open fixes
2271da177e4SLinus Torvalds
2281da177e4SLinus Torvalds Nov 5 2001, Aug 8 2002. Modified by Andy Polyakov
2291da177e4SLinus Torvalds <appro@fy.chalmers.se> to support MMC-3 compliant DVD+RW units.
2301da177e4SLinus Torvalds
2311da177e4SLinus Torvalds Modified by Nigel Kukard <nkukard@lbsd.net> - support DVD+RW
2321da177e4SLinus Torvalds 2.4.x patch by Andy Polyakov <appro@fy.chalmers.se>
2331da177e4SLinus Torvalds
2341da177e4SLinus Torvalds -------------------------------------------------------------------------*/
2351da177e4SLinus Torvalds
236e597cd09SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
237e597cd09SJoe Perches
2381da177e4SLinus Torvalds #define REVISION "Revision: 3.20"
2391da177e4SLinus Torvalds #define VERSION "Id: cdrom.c 3.20 2003/12/17"
2401da177e4SLinus Torvalds
2411da177e4SLinus Torvalds /* I use an error-log mask to give fine grain control over the type of
2421da177e4SLinus Torvalds messages dumped to the system logs. The available masks include: */
2431da177e4SLinus Torvalds #define CD_NOTHING 0x0
2441da177e4SLinus Torvalds #define CD_WARNING 0x1
2451da177e4SLinus Torvalds #define CD_REG_UNREG 0x2
2461da177e4SLinus Torvalds #define CD_DO_IOCTL 0x4
2471da177e4SLinus Torvalds #define CD_OPEN 0x8
2481da177e4SLinus Torvalds #define CD_CLOSE 0x10
2491da177e4SLinus Torvalds #define CD_COUNT_TRACKS 0x20
2501da177e4SLinus Torvalds #define CD_CHANGER 0x40
2511da177e4SLinus Torvalds #define CD_DVD 0x80
2521da177e4SLinus Torvalds
2531da177e4SLinus Torvalds /* Define this to remove _all_ the debugging messages */
2541da177e4SLinus Torvalds /* #define ERRLOGMASK CD_NOTHING */
2551da177e4SLinus Torvalds #define ERRLOGMASK CD_WARNING
2561da177e4SLinus Torvalds /* #define ERRLOGMASK (CD_WARNING|CD_OPEN|CD_COUNT_TRACKS|CD_CLOSE) */
2571da177e4SLinus Torvalds /* #define ERRLOGMASK (CD_WARNING|CD_REG_UNREG|CD_DO_IOCTL|CD_OPEN|CD_CLOSE|CD_COUNT_TRACKS) */
2581da177e4SLinus Torvalds
259f25191bbSGuenter Roeck #include <linux/atomic.h>
2601da177e4SLinus Torvalds #include <linux/module.h>
2611da177e4SLinus Torvalds #include <linux/fs.h>
2621da177e4SLinus Torvalds #include <linux/major.h>
2631da177e4SLinus Torvalds #include <linux/types.h>
2641da177e4SLinus Torvalds #include <linux/errno.h>
2651da177e4SLinus Torvalds #include <linux/kernel.h>
2661da177e4SLinus Torvalds #include <linux/mm.h>
2678270cb10SJordy Zomer #include <linux/nospec.h>
2681da177e4SLinus Torvalds #include <linux/slab.h>
2691da177e4SLinus Torvalds #include <linux/cdrom.h>
2701da177e4SLinus Torvalds #include <linux/sysctl.h>
2711da177e4SLinus Torvalds #include <linux/proc_fs.h>
2721da177e4SLinus Torvalds #include <linux/blkpg.h>
2731da177e4SLinus Torvalds #include <linux/init.h>
2741da177e4SLinus Torvalds #include <linux/fcntl.h>
2751da177e4SLinus Torvalds #include <linux/blkdev.h>
2761da177e4SLinus Torvalds #include <linux/times.h>
2777c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
2784e178c17SKees Cook #include <scsi/scsi_common.h>
2791da177e4SLinus Torvalds
2801da177e4SLinus Torvalds /* used to tell the module to turn on full debugging messages */
28190ab5ee9SRusty Russell static bool debug;
2821da177e4SLinus Torvalds /* default compatibility mode */
28390ab5ee9SRusty Russell static bool autoclose=1;
28490ab5ee9SRusty Russell static bool autoeject;
28590ab5ee9SRusty Russell static bool lockdoor = 1;
2861da177e4SLinus Torvalds /* will we ever get to use this... sigh. */
28790ab5ee9SRusty Russell static bool check_media_type;
2881da177e4SLinus Torvalds /* automatically restart mrw format */
28990ab5ee9SRusty Russell static bool mrw_format_restart = 1;
2901da177e4SLinus Torvalds module_param(debug, bool, 0);
2911da177e4SLinus Torvalds module_param(autoclose, bool, 0);
2921da177e4SLinus Torvalds module_param(autoeject, bool, 0);
2931da177e4SLinus Torvalds module_param(lockdoor, bool, 0);
2941da177e4SLinus Torvalds module_param(check_media_type, bool, 0);
2951da177e4SLinus Torvalds module_param(mrw_format_restart, bool, 0);
2961da177e4SLinus Torvalds
297554988d6SDave Young static DEFINE_MUTEX(cdrom_mutex);
2981da177e4SLinus Torvalds
2991da177e4SLinus Torvalds static const char *mrw_format_status[] = {
3001da177e4SLinus Torvalds "not mrw",
3011da177e4SLinus Torvalds "bgformat inactive",
3021da177e4SLinus Torvalds "bgformat active",
3031da177e4SLinus Torvalds "mrw complete",
3041da177e4SLinus Torvalds };
3051da177e4SLinus Torvalds
3061da177e4SLinus Torvalds static const char *mrw_address_space[] = { "DMA", "GAA" };
3071da177e4SLinus Torvalds
3081da177e4SLinus Torvalds #if (ERRLOGMASK != CD_NOTHING)
3095944b2ceSJoe Perches #define cd_dbg(type, fmt, ...) \
310e597cd09SJoe Perches do { \
3111da177e4SLinus Torvalds if ((ERRLOGMASK & type) || debug == 1) \
3125944b2ceSJoe Perches pr_debug(fmt, ##__VA_ARGS__); \
313e597cd09SJoe Perches } while (0)
3141da177e4SLinus Torvalds #else
3155944b2ceSJoe Perches #define cd_dbg(type, fmt, ...) \
316e597cd09SJoe Perches do { \
317e597cd09SJoe Perches if (0 && (ERRLOGMASK & type) || debug == 1) \
3185944b2ceSJoe Perches pr_debug(fmt, ##__VA_ARGS__); \
319e597cd09SJoe Perches } while (0)
3201da177e4SLinus Torvalds #endif
3211da177e4SLinus Torvalds
3221da177e4SLinus Torvalds /* The (cdo->capability & ~cdi->mask & CDC_XXX) construct was used in
3231da177e4SLinus Torvalds a lot of places. This macro makes the code more clear. */
3241da177e4SLinus Torvalds #define CDROM_CAN(type) (cdi->ops->capability & ~cdi->mask & (type))
3251da177e4SLinus Torvalds
3262e11c207SJens Axboe /*
3272e11c207SJens Axboe * Another popular OS uses 7 seconds as the hard timeout for default
3282e11c207SJens Axboe * commands, so it is a good choice for us as well.
3292e11c207SJens Axboe */
3302e11c207SJens Axboe #define CDROM_DEF_TIMEOUT (7 * HZ)
3312e11c207SJens Axboe
3321da177e4SLinus Torvalds /* Not-exported routines. */
3331da177e4SLinus Torvalds
3341da177e4SLinus Torvalds static void cdrom_sysctl_register(void);
33517672cf0SAkinobu Mita
3367fd097d4SAkinobu Mita static LIST_HEAD(cdrom_list);
3371da177e4SLinus Torvalds
signal_media_change(struct cdrom_device_info * cdi)33867f1e027SLukas Prediger static void signal_media_change(struct cdrom_device_info *cdi)
33967f1e027SLukas Prediger {
34067f1e027SLukas Prediger cdi->mc_flags = 0x3; /* set media changed bits, on both queues */
34167f1e027SLukas Prediger cdi->last_media_change_ms = ktime_to_ms(ktime_get());
34267f1e027SLukas Prediger }
34367f1e027SLukas Prediger
cdrom_dummy_generic_packet(struct cdrom_device_info * cdi,struct packet_command * cgc)344853fe1bfSKees Cook int cdrom_dummy_generic_packet(struct cdrom_device_info *cdi,
3451da177e4SLinus Torvalds struct packet_command *cgc)
3461da177e4SLinus Torvalds {
347e7d0748dSKees Cook if (cgc->sshdr) {
348e7d0748dSKees Cook cgc->sshdr->sense_key = 0x05;
349e7d0748dSKees Cook cgc->sshdr->asc = 0x20;
350e7d0748dSKees Cook cgc->sshdr->ascq = 0x00;
3511da177e4SLinus Torvalds }
3521da177e4SLinus Torvalds
3531da177e4SLinus Torvalds cgc->stat = -EIO;
3541da177e4SLinus Torvalds return -EIO;
3551da177e4SLinus Torvalds }
356853fe1bfSKees Cook EXPORT_SYMBOL(cdrom_dummy_generic_packet);
3571da177e4SLinus Torvalds
cdrom_flush_cache(struct cdrom_device_info * cdi)35840569c61SJoe Perches static int cdrom_flush_cache(struct cdrom_device_info *cdi)
35940569c61SJoe Perches {
36040569c61SJoe Perches struct packet_command cgc;
36140569c61SJoe Perches
36240569c61SJoe Perches init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
36340569c61SJoe Perches cgc.cmd[0] = GPCMD_FLUSH_CACHE;
36440569c61SJoe Perches
36540569c61SJoe Perches cgc.timeout = 5 * 60 * HZ;
36640569c61SJoe Perches
36740569c61SJoe Perches return cdi->ops->generic_packet(cdi, &cgc);
36840569c61SJoe Perches }
36940569c61SJoe Perches
370bd6f0bbaSJoe Perches /* requires CD R/RW */
cdrom_get_disc_info(struct cdrom_device_info * cdi,disc_information * di)371bd6f0bbaSJoe Perches static int cdrom_get_disc_info(struct cdrom_device_info *cdi,
372bd6f0bbaSJoe Perches disc_information *di)
373bd6f0bbaSJoe Perches {
374853fe1bfSKees Cook const struct cdrom_device_ops *cdo = cdi->ops;
375bd6f0bbaSJoe Perches struct packet_command cgc;
376bd6f0bbaSJoe Perches int ret, buflen;
377bd6f0bbaSJoe Perches
378bd6f0bbaSJoe Perches /* set up command and get the disc info */
379bd6f0bbaSJoe Perches init_cdrom_command(&cgc, di, sizeof(*di), CGC_DATA_READ);
380bd6f0bbaSJoe Perches cgc.cmd[0] = GPCMD_READ_DISC_INFO;
381bd6f0bbaSJoe Perches cgc.cmd[8] = cgc.buflen = 2;
382bd6f0bbaSJoe Perches cgc.quiet = 1;
383bd6f0bbaSJoe Perches
384bd6f0bbaSJoe Perches ret = cdo->generic_packet(cdi, &cgc);
385bd6f0bbaSJoe Perches if (ret)
386bd6f0bbaSJoe Perches return ret;
387bd6f0bbaSJoe Perches
388bd6f0bbaSJoe Perches /* not all drives have the same disc_info length, so requeue
389bd6f0bbaSJoe Perches * packet with the length the drive tells us it can supply
390bd6f0bbaSJoe Perches */
391bd6f0bbaSJoe Perches buflen = be16_to_cpu(di->disc_information_length) +
392bd6f0bbaSJoe Perches sizeof(di->disc_information_length);
393bd6f0bbaSJoe Perches
394bd6f0bbaSJoe Perches if (buflen > sizeof(disc_information))
395bd6f0bbaSJoe Perches buflen = sizeof(disc_information);
396bd6f0bbaSJoe Perches
397bd6f0bbaSJoe Perches cgc.cmd[8] = cgc.buflen = buflen;
398bd6f0bbaSJoe Perches ret = cdo->generic_packet(cdi, &cgc);
399bd6f0bbaSJoe Perches if (ret)
400bd6f0bbaSJoe Perches return ret;
401bd6f0bbaSJoe Perches
402bd6f0bbaSJoe Perches /* return actual fill size */
403bd6f0bbaSJoe Perches return buflen;
404bd6f0bbaSJoe Perches }
405bd6f0bbaSJoe Perches
4061da177e4SLinus Torvalds /* This macro makes sure we don't have to check on cdrom_device_ops
4071da177e4SLinus Torvalds * existence in the run-time routines below. Change_capability is a
4081da177e4SLinus Torvalds * hack to have the capability flags defined const, while we can still
4091da177e4SLinus Torvalds * change it here without gcc complaining at every line.
4101da177e4SLinus Torvalds */
4118f94004eSJens Axboe #define ENSURE(cdo, call, bits) \
41240569c61SJoe Perches do { \
41340569c61SJoe Perches if (cdo->call == NULL) \
4148f94004eSJens Axboe WARN_ON_ONCE((cdo)->capability & (bits)); \
41540569c61SJoe Perches } while (0)
4161da177e4SLinus Torvalds
4171da177e4SLinus Torvalds /*
4181da177e4SLinus Torvalds * the first prototypes used 0x2c as the page code for the mrw mode page,
4191da177e4SLinus Torvalds * subsequently this was changed to 0x03. probe the one used by this drive
4201da177e4SLinus Torvalds */
cdrom_mrw_probe_pc(struct cdrom_device_info * cdi)4211da177e4SLinus Torvalds static int cdrom_mrw_probe_pc(struct cdrom_device_info *cdi)
4221da177e4SLinus Torvalds {
4231da177e4SLinus Torvalds struct packet_command cgc;
424fce53847SFUJITA Tomonori char buffer[16];
4251da177e4SLinus Torvalds
426fce53847SFUJITA Tomonori init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_READ);
4271da177e4SLinus Torvalds
4281da177e4SLinus Torvalds cgc.timeout = HZ;
4291da177e4SLinus Torvalds cgc.quiet = 1;
4301da177e4SLinus Torvalds
4311da177e4SLinus Torvalds if (!cdrom_mode_sense(cdi, &cgc, MRW_MODE_PC, 0)) {
4321da177e4SLinus Torvalds cdi->mrw_mode_page = MRW_MODE_PC;
433fce53847SFUJITA Tomonori return 0;
4341da177e4SLinus Torvalds } else if (!cdrom_mode_sense(cdi, &cgc, MRW_MODE_PC_PRE1, 0)) {
4351da177e4SLinus Torvalds cdi->mrw_mode_page = MRW_MODE_PC_PRE1;
436fce53847SFUJITA Tomonori return 0;
4371da177e4SLinus Torvalds }
438fce53847SFUJITA Tomonori
439fce53847SFUJITA Tomonori return 1;
4401da177e4SLinus Torvalds }
4411da177e4SLinus Torvalds
cdrom_is_mrw(struct cdrom_device_info * cdi,int * write)4421da177e4SLinus Torvalds static int cdrom_is_mrw(struct cdrom_device_info *cdi, int *write)
4431da177e4SLinus Torvalds {
4441da177e4SLinus Torvalds struct packet_command cgc;
4451da177e4SLinus Torvalds struct mrw_feature_desc *mfd;
446fce53847SFUJITA Tomonori unsigned char buffer[16];
4471da177e4SLinus Torvalds int ret;
4481da177e4SLinus Torvalds
4491da177e4SLinus Torvalds *write = 0;
4501da177e4SLinus Torvalds
451fce53847SFUJITA Tomonori init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_READ);
4521da177e4SLinus Torvalds
4531da177e4SLinus Torvalds cgc.cmd[0] = GPCMD_GET_CONFIGURATION;
4541da177e4SLinus Torvalds cgc.cmd[3] = CDF_MRW;
455fce53847SFUJITA Tomonori cgc.cmd[8] = sizeof(buffer);
4561da177e4SLinus Torvalds cgc.quiet = 1;
4571da177e4SLinus Torvalds
4581da177e4SLinus Torvalds if ((ret = cdi->ops->generic_packet(cdi, &cgc)))
459fce53847SFUJITA Tomonori return ret;
4601da177e4SLinus Torvalds
4611da177e4SLinus Torvalds mfd = (struct mrw_feature_desc *)&buffer[sizeof(struct feature_header)];
462fce53847SFUJITA Tomonori if (be16_to_cpu(mfd->feature_code) != CDF_MRW)
463fce53847SFUJITA Tomonori return 1;
4641da177e4SLinus Torvalds *write = mfd->write;
4651da177e4SLinus Torvalds
4661da177e4SLinus Torvalds if ((ret = cdrom_mrw_probe_pc(cdi))) {
4671da177e4SLinus Torvalds *write = 0;
46822a9189fSThomas Bogendoerfer return ret;
4691da177e4SLinus Torvalds }
4701da177e4SLinus Torvalds
471fce53847SFUJITA Tomonori return 0;
472fce53847SFUJITA Tomonori }
473fce53847SFUJITA Tomonori
cdrom_mrw_bgformat(struct cdrom_device_info * cdi,int cont)4741da177e4SLinus Torvalds static int cdrom_mrw_bgformat(struct cdrom_device_info *cdi, int cont)
4751da177e4SLinus Torvalds {
4761da177e4SLinus Torvalds struct packet_command cgc;
477fce53847SFUJITA Tomonori unsigned char buffer[12];
4781da177e4SLinus Torvalds int ret;
4791da177e4SLinus Torvalds
480e597cd09SJoe Perches pr_info("%sstarting format\n", cont ? "Re" : "");
4811da177e4SLinus Torvalds
4821da177e4SLinus Torvalds /*
4831da177e4SLinus Torvalds * FmtData bit set (bit 4), format type is 1
4841da177e4SLinus Torvalds */
485fce53847SFUJITA Tomonori init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_WRITE);
4861da177e4SLinus Torvalds cgc.cmd[0] = GPCMD_FORMAT_UNIT;
4871da177e4SLinus Torvalds cgc.cmd[1] = (1 << 4) | 1;
4881da177e4SLinus Torvalds
4891da177e4SLinus Torvalds cgc.timeout = 5 * 60 * HZ;
4901da177e4SLinus Torvalds
4911da177e4SLinus Torvalds /*
4921da177e4SLinus Torvalds * 4 byte format list header, 8 byte format list descriptor
4931da177e4SLinus Torvalds */
4941da177e4SLinus Torvalds buffer[1] = 1 << 1;
4951da177e4SLinus Torvalds buffer[3] = 8;
4961da177e4SLinus Torvalds
4971da177e4SLinus Torvalds /*
4981da177e4SLinus Torvalds * nr_blocks field
4991da177e4SLinus Torvalds */
5001da177e4SLinus Torvalds buffer[4] = 0xff;
5011da177e4SLinus Torvalds buffer[5] = 0xff;
5021da177e4SLinus Torvalds buffer[6] = 0xff;
5031da177e4SLinus Torvalds buffer[7] = 0xff;
5041da177e4SLinus Torvalds
5051da177e4SLinus Torvalds buffer[8] = 0x24 << 2;
5061da177e4SLinus Torvalds buffer[11] = cont;
5071da177e4SLinus Torvalds
5081da177e4SLinus Torvalds ret = cdi->ops->generic_packet(cdi, &cgc);
5091da177e4SLinus Torvalds if (ret)
510e597cd09SJoe Perches pr_info("bgformat failed\n");
5111da177e4SLinus Torvalds
5121da177e4SLinus Torvalds return ret;
5131da177e4SLinus Torvalds }
5141da177e4SLinus Torvalds
cdrom_mrw_bgformat_susp(struct cdrom_device_info * cdi,int immed)5151da177e4SLinus Torvalds static int cdrom_mrw_bgformat_susp(struct cdrom_device_info *cdi, int immed)
5161da177e4SLinus Torvalds {
5171da177e4SLinus Torvalds struct packet_command cgc;
5181da177e4SLinus Torvalds
5191da177e4SLinus Torvalds init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
5201da177e4SLinus Torvalds cgc.cmd[0] = GPCMD_CLOSE_TRACK;
5211da177e4SLinus Torvalds
5221da177e4SLinus Torvalds /*
5231da177e4SLinus Torvalds * Session = 1, Track = 0
5241da177e4SLinus Torvalds */
5251da177e4SLinus Torvalds cgc.cmd[1] = !!immed;
5261da177e4SLinus Torvalds cgc.cmd[2] = 1 << 1;
5271da177e4SLinus Torvalds
5281da177e4SLinus Torvalds cgc.timeout = 5 * 60 * HZ;
5291da177e4SLinus Torvalds
5301da177e4SLinus Torvalds return cdi->ops->generic_packet(cdi, &cgc);
5311da177e4SLinus Torvalds }
5321da177e4SLinus Torvalds
cdrom_mrw_exit(struct cdrom_device_info * cdi)5331da177e4SLinus Torvalds static int cdrom_mrw_exit(struct cdrom_device_info *cdi)
5341da177e4SLinus Torvalds {
5351da177e4SLinus Torvalds disc_information di;
5361da177e4SLinus Torvalds int ret;
5371da177e4SLinus Torvalds
5381da177e4SLinus Torvalds ret = cdrom_get_disc_info(cdi, &di);
5391da177e4SLinus Torvalds if (ret < 0 || ret < (int)offsetof(typeof(di),disc_type))
5401da177e4SLinus Torvalds return 1;
5411da177e4SLinus Torvalds
5421da177e4SLinus Torvalds ret = 0;
5431da177e4SLinus Torvalds if (di.mrw_status == CDM_MRW_BGFORMAT_ACTIVE) {
544e597cd09SJoe Perches pr_info("issuing MRW background format suspend\n");
5451da177e4SLinus Torvalds ret = cdrom_mrw_bgformat_susp(cdi, 0);
5461da177e4SLinus Torvalds }
5471da177e4SLinus Torvalds
5487da21a02SJens Axboe if (!ret && cdi->media_written)
5491da177e4SLinus Torvalds ret = cdrom_flush_cache(cdi);
5501da177e4SLinus Torvalds
5511da177e4SLinus Torvalds return ret;
5521da177e4SLinus Torvalds }
5531da177e4SLinus Torvalds
cdrom_mrw_set_lba_space(struct cdrom_device_info * cdi,int space)5541da177e4SLinus Torvalds static int cdrom_mrw_set_lba_space(struct cdrom_device_info *cdi, int space)
5551da177e4SLinus Torvalds {
5561da177e4SLinus Torvalds struct packet_command cgc;
5571da177e4SLinus Torvalds struct mode_page_header *mph;
558fce53847SFUJITA Tomonori char buffer[16];
5591da177e4SLinus Torvalds int ret, offset, size;
5601da177e4SLinus Torvalds
561fce53847SFUJITA Tomonori init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_READ);
5621da177e4SLinus Torvalds
563fce53847SFUJITA Tomonori cgc.buffer = buffer;
564fce53847SFUJITA Tomonori cgc.buflen = sizeof(buffer);
5651da177e4SLinus Torvalds
56640569c61SJoe Perches ret = cdrom_mode_sense(cdi, &cgc, cdi->mrw_mode_page, 0);
56740569c61SJoe Perches if (ret)
568fce53847SFUJITA Tomonori return ret;
5691da177e4SLinus Torvalds
5701da177e4SLinus Torvalds mph = (struct mode_page_header *)buffer;
5711da177e4SLinus Torvalds offset = be16_to_cpu(mph->desc_length);
5721da177e4SLinus Torvalds size = be16_to_cpu(mph->mode_data_length) + 2;
5731da177e4SLinus Torvalds
5741da177e4SLinus Torvalds buffer[offset + 3] = space;
5751da177e4SLinus Torvalds cgc.buflen = size;
5761da177e4SLinus Torvalds
57740569c61SJoe Perches ret = cdrom_mode_select(cdi, &cgc);
57840569c61SJoe Perches if (ret)
579fce53847SFUJITA Tomonori return ret;
5801da177e4SLinus Torvalds
581e597cd09SJoe Perches pr_info("%s: mrw address space %s selected\n",
582e597cd09SJoe Perches cdi->name, mrw_address_space[space]);
583fce53847SFUJITA Tomonori return 0;
5841da177e4SLinus Torvalds }
5851da177e4SLinus Torvalds
register_cdrom(struct gendisk * disk,struct cdrom_device_info * cdi)586a711d91cSChristoph Hellwig int register_cdrom(struct gendisk *disk, struct cdrom_device_info *cdi)
58740569c61SJoe Perches {
58840569c61SJoe Perches static char banner_printed;
589853fe1bfSKees Cook const struct cdrom_device_ops *cdo = cdi->ops;
59040569c61SJoe Perches
59140569c61SJoe Perches cd_dbg(CD_OPEN, "entering register_cdrom\n");
59240569c61SJoe Perches
59340569c61SJoe Perches if (cdo->open == NULL || cdo->release == NULL)
59440569c61SJoe Perches return -EINVAL;
59540569c61SJoe Perches if (!banner_printed) {
59640569c61SJoe Perches pr_info("Uniform CD-ROM driver " REVISION "\n");
59740569c61SJoe Perches banner_printed = 1;
59840569c61SJoe Perches cdrom_sysctl_register();
59940569c61SJoe Perches }
60040569c61SJoe Perches
601a711d91cSChristoph Hellwig cdi->disk = disk;
602a711d91cSChristoph Hellwig disk->cdi = cdi;
603a711d91cSChristoph Hellwig
6048f94004eSJens Axboe ENSURE(cdo, drive_status, CDC_DRIVE_STATUS);
6058c22eb3aSChristoph Hellwig if (cdo->check_events == NULL)
6068f94004eSJens Axboe WARN_ON_ONCE(cdo->capability & (CDC_MEDIA_CHANGED | CDC_SELECT_DISC));
6078f94004eSJens Axboe ENSURE(cdo, tray_move, CDC_CLOSE_TRAY | CDC_OPEN_TRAY);
6088f94004eSJens Axboe ENSURE(cdo, lock_door, CDC_LOCK);
6098f94004eSJens Axboe ENSURE(cdo, select_speed, CDC_SELECT_SPEED);
6108f94004eSJens Axboe ENSURE(cdo, get_last_session, CDC_MULTI_SESSION);
6118f94004eSJens Axboe ENSURE(cdo, get_mcn, CDC_MCN);
6128f94004eSJens Axboe ENSURE(cdo, reset, CDC_RESET);
6138f94004eSJens Axboe ENSURE(cdo, generic_packet, CDC_GENERIC_PACKET);
61440569c61SJoe Perches cdi->mc_flags = 0;
61540569c61SJoe Perches cdi->options = CDO_USE_FFLAGS;
61667f1e027SLukas Prediger cdi->last_media_change_ms = ktime_to_ms(ktime_get());
61740569c61SJoe Perches
61840569c61SJoe Perches if (autoclose == 1 && CDROM_CAN(CDC_CLOSE_TRAY))
61940569c61SJoe Perches cdi->options |= (int) CDO_AUTO_CLOSE;
62040569c61SJoe Perches if (autoeject == 1 && CDROM_CAN(CDC_OPEN_TRAY))
62140569c61SJoe Perches cdi->options |= (int) CDO_AUTO_EJECT;
62240569c61SJoe Perches if (lockdoor == 1)
62340569c61SJoe Perches cdi->options |= (int) CDO_LOCK;
62440569c61SJoe Perches if (check_media_type == 1)
62540569c61SJoe Perches cdi->options |= (int) CDO_CHECK_TYPE;
62640569c61SJoe Perches
62740569c61SJoe Perches if (CDROM_CAN(CDC_MRW_W))
62840569c61SJoe Perches cdi->exit = cdrom_mrw_exit;
62940569c61SJoe Perches
630ba51bdafSChristoph Hellwig if (cdi->ops->read_cdda_bpc)
63140569c61SJoe Perches cdi->cdda_method = CDDA_BPC_FULL;
63240569c61SJoe Perches else
63340569c61SJoe Perches cdi->cdda_method = CDDA_OLD;
63440569c61SJoe Perches
635853fe1bfSKees Cook WARN_ON(!cdo->generic_packet);
63640569c61SJoe Perches
63740569c61SJoe Perches cd_dbg(CD_REG_UNREG, "drive \"/dev/%s\" registered\n", cdi->name);
63840569c61SJoe Perches mutex_lock(&cdrom_mutex);
63940569c61SJoe Perches list_add(&cdi->list, &cdrom_list);
64040569c61SJoe Perches mutex_unlock(&cdrom_mutex);
64140569c61SJoe Perches return 0;
64240569c61SJoe Perches }
643eeef7565SEnze Li EXPORT_SYMBOL(register_cdrom);
64440569c61SJoe Perches #undef ENSURE
64540569c61SJoe Perches
unregister_cdrom(struct cdrom_device_info * cdi)64640569c61SJoe Perches void unregister_cdrom(struct cdrom_device_info *cdi)
64740569c61SJoe Perches {
64840569c61SJoe Perches cd_dbg(CD_OPEN, "entering unregister_cdrom\n");
64940569c61SJoe Perches
65040569c61SJoe Perches mutex_lock(&cdrom_mutex);
65140569c61SJoe Perches list_del(&cdi->list);
65240569c61SJoe Perches mutex_unlock(&cdrom_mutex);
65340569c61SJoe Perches
65440569c61SJoe Perches if (cdi->exit)
65540569c61SJoe Perches cdi->exit(cdi);
65640569c61SJoe Perches
65740569c61SJoe Perches cd_dbg(CD_REG_UNREG, "drive \"/dev/%s\" unregistered\n", cdi->name);
65840569c61SJoe Perches }
659eeef7565SEnze Li EXPORT_SYMBOL(unregister_cdrom);
66040569c61SJoe Perches
cdrom_get_media_event(struct cdrom_device_info * cdi,struct media_event_desc * med)66140569c61SJoe Perches int cdrom_get_media_event(struct cdrom_device_info *cdi,
66240569c61SJoe Perches struct media_event_desc *med)
66340569c61SJoe Perches {
66440569c61SJoe Perches struct packet_command cgc;
66540569c61SJoe Perches unsigned char buffer[8];
66640569c61SJoe Perches struct event_header *eh = (struct event_header *)buffer;
66740569c61SJoe Perches
66840569c61SJoe Perches init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_READ);
66940569c61SJoe Perches cgc.cmd[0] = GPCMD_GET_EVENT_STATUS_NOTIFICATION;
67040569c61SJoe Perches cgc.cmd[1] = 1; /* IMMED */
67140569c61SJoe Perches cgc.cmd[4] = 1 << 4; /* media event */
67240569c61SJoe Perches cgc.cmd[8] = sizeof(buffer);
67340569c61SJoe Perches cgc.quiet = 1;
67440569c61SJoe Perches
67540569c61SJoe Perches if (cdi->ops->generic_packet(cdi, &cgc))
67640569c61SJoe Perches return 1;
67740569c61SJoe Perches
67840569c61SJoe Perches if (be16_to_cpu(eh->data_len) < sizeof(*med))
67940569c61SJoe Perches return 1;
68040569c61SJoe Perches
68140569c61SJoe Perches if (eh->nea || eh->notification_class != 0x4)
68240569c61SJoe Perches return 1;
68340569c61SJoe Perches
68440569c61SJoe Perches memcpy(med, &buffer[sizeof(*eh)], sizeof(*med));
68540569c61SJoe Perches return 0;
68640569c61SJoe Perches }
687eeef7565SEnze Li EXPORT_SYMBOL(cdrom_get_media_event);
68840569c61SJoe Perches
cdrom_get_random_writable(struct cdrom_device_info * cdi,struct rwrt_feature_desc * rfd)6891da177e4SLinus Torvalds static int cdrom_get_random_writable(struct cdrom_device_info *cdi,
6901da177e4SLinus Torvalds struct rwrt_feature_desc *rfd)
6911da177e4SLinus Torvalds {
6921da177e4SLinus Torvalds struct packet_command cgc;
693fce53847SFUJITA Tomonori char buffer[24];
6941da177e4SLinus Torvalds int ret;
6951da177e4SLinus Torvalds
696fce53847SFUJITA Tomonori init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_READ);
6971da177e4SLinus Torvalds
6981da177e4SLinus Torvalds cgc.cmd[0] = GPCMD_GET_CONFIGURATION; /* often 0x46 */
6991da177e4SLinus Torvalds cgc.cmd[3] = CDF_RWRT; /* often 0x0020 */
700fce53847SFUJITA Tomonori cgc.cmd[8] = sizeof(buffer); /* often 0x18 */
7011da177e4SLinus Torvalds cgc.quiet = 1;
7021da177e4SLinus Torvalds
7031da177e4SLinus Torvalds if ((ret = cdi->ops->generic_packet(cdi, &cgc)))
704fce53847SFUJITA Tomonori return ret;
7051da177e4SLinus Torvalds
7061da177e4SLinus Torvalds memcpy(rfd, &buffer[sizeof(struct feature_header)], sizeof (*rfd));
707fce53847SFUJITA Tomonori return 0;
7081da177e4SLinus Torvalds }
7091da177e4SLinus Torvalds
cdrom_has_defect_mgt(struct cdrom_device_info * cdi)7101da177e4SLinus Torvalds static int cdrom_has_defect_mgt(struct cdrom_device_info *cdi)
7111da177e4SLinus Torvalds {
7121da177e4SLinus Torvalds struct packet_command cgc;
713fce53847SFUJITA Tomonori char buffer[16];
71456052d52SAlexey Dobriyan __be16 *feature_code;
7151da177e4SLinus Torvalds int ret;
7161da177e4SLinus Torvalds
717fce53847SFUJITA Tomonori init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_READ);
7181da177e4SLinus Torvalds
7191da177e4SLinus Torvalds cgc.cmd[0] = GPCMD_GET_CONFIGURATION;
7201da177e4SLinus Torvalds cgc.cmd[3] = CDF_HWDM;
721fce53847SFUJITA Tomonori cgc.cmd[8] = sizeof(buffer);
7221da177e4SLinus Torvalds cgc.quiet = 1;
7231da177e4SLinus Torvalds
7241da177e4SLinus Torvalds if ((ret = cdi->ops->generic_packet(cdi, &cgc)))
725fce53847SFUJITA Tomonori return ret;
7261da177e4SLinus Torvalds
72756052d52SAlexey Dobriyan feature_code = (__be16 *) &buffer[sizeof(struct feature_header)];
7281da177e4SLinus Torvalds if (be16_to_cpu(*feature_code) == CDF_HWDM)
729fce53847SFUJITA Tomonori return 0;
730fce53847SFUJITA Tomonori
731fce53847SFUJITA Tomonori return 1;
7321da177e4SLinus Torvalds }
7331da177e4SLinus Torvalds
7341da177e4SLinus Torvalds
cdrom_is_random_writable(struct cdrom_device_info * cdi,int * write)7351da177e4SLinus Torvalds static int cdrom_is_random_writable(struct cdrom_device_info *cdi, int *write)
7361da177e4SLinus Torvalds {
7371da177e4SLinus Torvalds struct rwrt_feature_desc rfd;
7381da177e4SLinus Torvalds int ret;
7391da177e4SLinus Torvalds
7401da177e4SLinus Torvalds *write = 0;
7411da177e4SLinus Torvalds
7421da177e4SLinus Torvalds if ((ret = cdrom_get_random_writable(cdi, &rfd)))
7431da177e4SLinus Torvalds return ret;
7441da177e4SLinus Torvalds
7451da177e4SLinus Torvalds if (CDF_RWRT == be16_to_cpu(rfd.feature_code))
7461da177e4SLinus Torvalds *write = 1;
7471da177e4SLinus Torvalds
7481da177e4SLinus Torvalds return 0;
7491da177e4SLinus Torvalds }
7501da177e4SLinus Torvalds
cdrom_media_erasable(struct cdrom_device_info * cdi)7511da177e4SLinus Torvalds static int cdrom_media_erasable(struct cdrom_device_info *cdi)
7521da177e4SLinus Torvalds {
7531da177e4SLinus Torvalds disc_information di;
7541da177e4SLinus Torvalds int ret;
7551da177e4SLinus Torvalds
7561da177e4SLinus Torvalds ret = cdrom_get_disc_info(cdi, &di);
7571da177e4SLinus Torvalds if (ret < 0 || ret < offsetof(typeof(di), n_first_track))
7581da177e4SLinus Torvalds return -1;
7591da177e4SLinus Torvalds
7601da177e4SLinus Torvalds return di.erasable;
7611da177e4SLinus Torvalds }
7621da177e4SLinus Torvalds
7631da177e4SLinus Torvalds /*
7641da177e4SLinus Torvalds * FIXME: check RO bit
7651da177e4SLinus Torvalds */
cdrom_dvdram_open_write(struct cdrom_device_info * cdi)7661da177e4SLinus Torvalds static int cdrom_dvdram_open_write(struct cdrom_device_info *cdi)
7671da177e4SLinus Torvalds {
7681da177e4SLinus Torvalds int ret = cdrom_media_erasable(cdi);
7691da177e4SLinus Torvalds
7701da177e4SLinus Torvalds /*
7711da177e4SLinus Torvalds * allow writable open if media info read worked and media is
7721da177e4SLinus Torvalds * erasable, _or_ if it fails since not all drives support it
7731da177e4SLinus Torvalds */
7741da177e4SLinus Torvalds if (!ret)
7751da177e4SLinus Torvalds return 1;
7761da177e4SLinus Torvalds
7771da177e4SLinus Torvalds return 0;
7781da177e4SLinus Torvalds }
7791da177e4SLinus Torvalds
cdrom_mrw_open_write(struct cdrom_device_info * cdi)7801da177e4SLinus Torvalds static int cdrom_mrw_open_write(struct cdrom_device_info *cdi)
7811da177e4SLinus Torvalds {
7821da177e4SLinus Torvalds disc_information di;
7831da177e4SLinus Torvalds int ret;
7841da177e4SLinus Torvalds
7851da177e4SLinus Torvalds /*
7861da177e4SLinus Torvalds * always reset to DMA lba space on open
7871da177e4SLinus Torvalds */
7881da177e4SLinus Torvalds if (cdrom_mrw_set_lba_space(cdi, MRW_LBA_DMA)) {
789e597cd09SJoe Perches pr_err("failed setting lba address space\n");
7901da177e4SLinus Torvalds return 1;
7911da177e4SLinus Torvalds }
7921da177e4SLinus Torvalds
7931da177e4SLinus Torvalds ret = cdrom_get_disc_info(cdi, &di);
7941da177e4SLinus Torvalds if (ret < 0 || ret < offsetof(typeof(di),disc_type))
7951da177e4SLinus Torvalds return 1;
7961da177e4SLinus Torvalds
7971da177e4SLinus Torvalds if (!di.erasable)
7981da177e4SLinus Torvalds return 1;
7991da177e4SLinus Torvalds
8001da177e4SLinus Torvalds /*
8011da177e4SLinus Torvalds * mrw_status
8021da177e4SLinus Torvalds * 0 - not MRW formatted
8031da177e4SLinus Torvalds * 1 - MRW bgformat started, but not running or complete
8041da177e4SLinus Torvalds * 2 - MRW bgformat in progress
8051da177e4SLinus Torvalds * 3 - MRW formatting complete
8061da177e4SLinus Torvalds */
8071da177e4SLinus Torvalds ret = 0;
808e597cd09SJoe Perches pr_info("open: mrw_status '%s'\n", mrw_format_status[di.mrw_status]);
8091da177e4SLinus Torvalds if (!di.mrw_status)
8101da177e4SLinus Torvalds ret = 1;
8111da177e4SLinus Torvalds else if (di.mrw_status == CDM_MRW_BGFORMAT_INACTIVE &&
8121da177e4SLinus Torvalds mrw_format_restart)
8131da177e4SLinus Torvalds ret = cdrom_mrw_bgformat(cdi, 1);
8141da177e4SLinus Torvalds
8151da177e4SLinus Torvalds return ret;
8161da177e4SLinus Torvalds }
8171da177e4SLinus Torvalds
mo_open_write(struct cdrom_device_info * cdi)8181da177e4SLinus Torvalds static int mo_open_write(struct cdrom_device_info *cdi)
8191da177e4SLinus Torvalds {
8201da177e4SLinus Torvalds struct packet_command cgc;
821fce53847SFUJITA Tomonori char buffer[255];
8221da177e4SLinus Torvalds int ret;
8231da177e4SLinus Torvalds
824fce53847SFUJITA Tomonori init_cdrom_command(&cgc, &buffer, 4, CGC_DATA_READ);
8251da177e4SLinus Torvalds cgc.quiet = 1;
8261da177e4SLinus Torvalds
8271da177e4SLinus Torvalds /*
8281da177e4SLinus Torvalds * obtain write protect information as per
8291da177e4SLinus Torvalds * drivers/scsi/sd.c:sd_read_write_protect_flag
8301da177e4SLinus Torvalds */
8311da177e4SLinus Torvalds
8321da177e4SLinus Torvalds ret = cdrom_mode_sense(cdi, &cgc, GPMODE_ALL_PAGES, 0);
8331da177e4SLinus Torvalds if (ret)
8341da177e4SLinus Torvalds ret = cdrom_mode_sense(cdi, &cgc, GPMODE_VENDOR_PAGE, 0);
8351da177e4SLinus Torvalds if (ret) {
8361da177e4SLinus Torvalds cgc.buflen = 255;
8371da177e4SLinus Torvalds ret = cdrom_mode_sense(cdi, &cgc, GPMODE_ALL_PAGES, 0);
8381da177e4SLinus Torvalds }
8391da177e4SLinus Torvalds
8401da177e4SLinus Torvalds /* drive gave us no info, let the user go ahead */
841fce53847SFUJITA Tomonori if (ret)
842fce53847SFUJITA Tomonori return 0;
8431da177e4SLinus Torvalds
844fce53847SFUJITA Tomonori return buffer[3] & 0x80;
8451da177e4SLinus Torvalds }
8461da177e4SLinus Torvalds
cdrom_ram_open_write(struct cdrom_device_info * cdi)8471da177e4SLinus Torvalds static int cdrom_ram_open_write(struct cdrom_device_info *cdi)
8481da177e4SLinus Torvalds {
8491da177e4SLinus Torvalds struct rwrt_feature_desc rfd;
8501da177e4SLinus Torvalds int ret;
8511da177e4SLinus Torvalds
8521da177e4SLinus Torvalds if ((ret = cdrom_has_defect_mgt(cdi)))
8531da177e4SLinus Torvalds return ret;
8541da177e4SLinus Torvalds
8551da177e4SLinus Torvalds if ((ret = cdrom_get_random_writable(cdi, &rfd)))
8561da177e4SLinus Torvalds return ret;
8571da177e4SLinus Torvalds else if (CDF_RWRT == be16_to_cpu(rfd.feature_code))
8581da177e4SLinus Torvalds ret = !rfd.curr;
8591da177e4SLinus Torvalds
8605944b2ceSJoe Perches cd_dbg(CD_OPEN, "can open for random write\n");
8611da177e4SLinus Torvalds return ret;
8621da177e4SLinus Torvalds }
8631da177e4SLinus Torvalds
cdrom_mmc3_profile(struct cdrom_device_info * cdi)8641da177e4SLinus Torvalds static void cdrom_mmc3_profile(struct cdrom_device_info *cdi)
8651da177e4SLinus Torvalds {
8661da177e4SLinus Torvalds struct packet_command cgc;
867fce53847SFUJITA Tomonori char buffer[32];
868bbc3925cSluo penghao int mmc3_profile;
8691da177e4SLinus Torvalds
870fce53847SFUJITA Tomonori init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_READ);
8711da177e4SLinus Torvalds
8721da177e4SLinus Torvalds cgc.cmd[0] = GPCMD_GET_CONFIGURATION;
8731da177e4SLinus Torvalds cgc.cmd[1] = 0;
8741da177e4SLinus Torvalds cgc.cmd[2] = cgc.cmd[3] = 0; /* Starting Feature Number */
875fce53847SFUJITA Tomonori cgc.cmd[8] = sizeof(buffer); /* Allocation Length */
8761da177e4SLinus Torvalds cgc.quiet = 1;
8771da177e4SLinus Torvalds
878bbc3925cSluo penghao if (cdi->ops->generic_packet(cdi, &cgc))
8791da177e4SLinus Torvalds mmc3_profile = 0xffff;
8801da177e4SLinus Torvalds else
8811da177e4SLinus Torvalds mmc3_profile = (buffer[6] << 8) | buffer[7];
8821da177e4SLinus Torvalds
8831da177e4SLinus Torvalds cdi->mmc3_profile = mmc3_profile;
8841da177e4SLinus Torvalds }
8851da177e4SLinus Torvalds
cdrom_is_dvd_rw(struct cdrom_device_info * cdi)8861da177e4SLinus Torvalds static int cdrom_is_dvd_rw(struct cdrom_device_info *cdi)
8871da177e4SLinus Torvalds {
8881da177e4SLinus Torvalds switch (cdi->mmc3_profile) {
8891da177e4SLinus Torvalds case 0x12: /* DVD-RAM */
8901da177e4SLinus Torvalds case 0x1A: /* DVD+RW */
891f7e7868bSGeorgios Toptsidis case 0x43: /* BD-RE */
8921da177e4SLinus Torvalds return 0;
8931da177e4SLinus Torvalds default:
8941da177e4SLinus Torvalds return 1;
8951da177e4SLinus Torvalds }
8961da177e4SLinus Torvalds }
8971da177e4SLinus Torvalds
8981da177e4SLinus Torvalds /*
8991da177e4SLinus Torvalds * returns 0 for ok to open write, non-0 to disallow
9001da177e4SLinus Torvalds */
cdrom_open_write(struct cdrom_device_info * cdi)9011da177e4SLinus Torvalds static int cdrom_open_write(struct cdrom_device_info *cdi)
9021da177e4SLinus Torvalds {
9031da177e4SLinus Torvalds int mrw, mrw_write, ram_write;
9041da177e4SLinus Torvalds int ret = 1;
9051da177e4SLinus Torvalds
9061da177e4SLinus Torvalds mrw = 0;
9071da177e4SLinus Torvalds if (!cdrom_is_mrw(cdi, &mrw_write))
9081da177e4SLinus Torvalds mrw = 1;
9091da177e4SLinus Torvalds
9101da177e4SLinus Torvalds if (CDROM_CAN(CDC_MO_DRIVE))
9111da177e4SLinus Torvalds ram_write = 1;
9121da177e4SLinus Torvalds else
9131da177e4SLinus Torvalds (void) cdrom_is_random_writable(cdi, &ram_write);
9141da177e4SLinus Torvalds
9151da177e4SLinus Torvalds if (mrw)
9161da177e4SLinus Torvalds cdi->mask &= ~CDC_MRW;
9171da177e4SLinus Torvalds else
9181da177e4SLinus Torvalds cdi->mask |= CDC_MRW;
9191da177e4SLinus Torvalds
9201da177e4SLinus Torvalds if (mrw_write)
9211da177e4SLinus Torvalds cdi->mask &= ~CDC_MRW_W;
9221da177e4SLinus Torvalds else
9231da177e4SLinus Torvalds cdi->mask |= CDC_MRW_W;
9241da177e4SLinus Torvalds
9251da177e4SLinus Torvalds if (ram_write)
9261da177e4SLinus Torvalds cdi->mask &= ~CDC_RAM;
9271da177e4SLinus Torvalds else
9281da177e4SLinus Torvalds cdi->mask |= CDC_RAM;
9291da177e4SLinus Torvalds
9301da177e4SLinus Torvalds if (CDROM_CAN(CDC_MRW_W))
9311da177e4SLinus Torvalds ret = cdrom_mrw_open_write(cdi);
9321da177e4SLinus Torvalds else if (CDROM_CAN(CDC_DVD_RAM))
9331da177e4SLinus Torvalds ret = cdrom_dvdram_open_write(cdi);
9341da177e4SLinus Torvalds else if (CDROM_CAN(CDC_RAM) &&
9351da177e4SLinus Torvalds !CDROM_CAN(CDC_CD_R|CDC_CD_RW|CDC_DVD|CDC_DVD_R|CDC_MRW|CDC_MO_DRIVE))
9361da177e4SLinus Torvalds ret = cdrom_ram_open_write(cdi);
9371da177e4SLinus Torvalds else if (CDROM_CAN(CDC_MO_DRIVE))
9381da177e4SLinus Torvalds ret = mo_open_write(cdi);
9391da177e4SLinus Torvalds else if (!cdrom_is_dvd_rw(cdi))
9401da177e4SLinus Torvalds ret = 0;
9411da177e4SLinus Torvalds
9421da177e4SLinus Torvalds return ret;
9431da177e4SLinus Torvalds }
9441da177e4SLinus Torvalds
cdrom_dvd_rw_close_write(struct cdrom_device_info * cdi)9451da177e4SLinus Torvalds static void cdrom_dvd_rw_close_write(struct cdrom_device_info *cdi)
9461da177e4SLinus Torvalds {
9471da177e4SLinus Torvalds struct packet_command cgc;
9481da177e4SLinus Torvalds
9491da177e4SLinus Torvalds if (cdi->mmc3_profile != 0x1a) {
9505944b2ceSJoe Perches cd_dbg(CD_CLOSE, "%s: No DVD+RW\n", cdi->name);
9511da177e4SLinus Torvalds return;
9521da177e4SLinus Torvalds }
9531da177e4SLinus Torvalds
9541da177e4SLinus Torvalds if (!cdi->media_written) {
9555944b2ceSJoe Perches cd_dbg(CD_CLOSE, "%s: DVD+RW media clean\n", cdi->name);
9561da177e4SLinus Torvalds return;
9571da177e4SLinus Torvalds }
9581da177e4SLinus Torvalds
959e597cd09SJoe Perches pr_info("%s: dirty DVD+RW media, \"finalizing\"\n", cdi->name);
9601da177e4SLinus Torvalds
9611da177e4SLinus Torvalds init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
9621da177e4SLinus Torvalds cgc.cmd[0] = GPCMD_FLUSH_CACHE;
9631da177e4SLinus Torvalds cgc.timeout = 30*HZ;
9641da177e4SLinus Torvalds cdi->ops->generic_packet(cdi, &cgc);
9651da177e4SLinus Torvalds
9661da177e4SLinus Torvalds init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
9671da177e4SLinus Torvalds cgc.cmd[0] = GPCMD_CLOSE_TRACK;
9681da177e4SLinus Torvalds cgc.timeout = 3000*HZ;
9691da177e4SLinus Torvalds cgc.quiet = 1;
9701da177e4SLinus Torvalds cdi->ops->generic_packet(cdi, &cgc);
9711da177e4SLinus Torvalds
9721da177e4SLinus Torvalds init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
9731da177e4SLinus Torvalds cgc.cmd[0] = GPCMD_CLOSE_TRACK;
9741da177e4SLinus Torvalds cgc.cmd[2] = 2; /* Close session */
9751da177e4SLinus Torvalds cgc.quiet = 1;
9761da177e4SLinus Torvalds cgc.timeout = 3000*HZ;
9771da177e4SLinus Torvalds cdi->ops->generic_packet(cdi, &cgc);
9781da177e4SLinus Torvalds
9791da177e4SLinus Torvalds cdi->media_written = 0;
9801da177e4SLinus Torvalds }
9811da177e4SLinus Torvalds
982a803393bSJoe Perches /* badly broken, I know. Is due for a fixup anytime. */
cdrom_count_tracks(struct cdrom_device_info * cdi,tracktype * tracks)983a803393bSJoe Perches static void cdrom_count_tracks(struct cdrom_device_info *cdi, tracktype *tracks)
984a803393bSJoe Perches {
985a803393bSJoe Perches struct cdrom_tochdr header;
986a803393bSJoe Perches struct cdrom_tocentry entry;
987a803393bSJoe Perches int ret, i;
988a803393bSJoe Perches tracks->data = 0;
989a803393bSJoe Perches tracks->audio = 0;
990a803393bSJoe Perches tracks->cdi = 0;
991a803393bSJoe Perches tracks->xa = 0;
992a803393bSJoe Perches tracks->error = 0;
993a803393bSJoe Perches cd_dbg(CD_COUNT_TRACKS, "entering cdrom_count_tracks\n");
994366ba7c7SDiego Elio Pettenò
995366ba7c7SDiego Elio Pettenò if (!CDROM_CAN(CDC_PLAY_AUDIO)) {
996366ba7c7SDiego Elio Pettenò tracks->error = CDS_NO_INFO;
997366ba7c7SDiego Elio Pettenò return;
998366ba7c7SDiego Elio Pettenò }
999366ba7c7SDiego Elio Pettenò
1000a803393bSJoe Perches /* Grab the TOC header so we can see how many tracks there are */
1001a803393bSJoe Perches ret = cdi->ops->audio_ioctl(cdi, CDROMREADTOCHDR, &header);
1002a803393bSJoe Perches if (ret) {
1003a803393bSJoe Perches if (ret == -ENOMEDIUM)
1004a803393bSJoe Perches tracks->error = CDS_NO_DISC;
1005a803393bSJoe Perches else
1006a803393bSJoe Perches tracks->error = CDS_NO_INFO;
1007a803393bSJoe Perches return;
1008a803393bSJoe Perches }
1009a803393bSJoe Perches /* check what type of tracks are on this disc */
1010a803393bSJoe Perches entry.cdte_format = CDROM_MSF;
1011a803393bSJoe Perches for (i = header.cdth_trk0; i <= header.cdth_trk1; i++) {
1012a803393bSJoe Perches entry.cdte_track = i;
1013a803393bSJoe Perches if (cdi->ops->audio_ioctl(cdi, CDROMREADTOCENTRY, &entry)) {
1014a803393bSJoe Perches tracks->error = CDS_NO_INFO;
1015a803393bSJoe Perches return;
1016a803393bSJoe Perches }
1017a803393bSJoe Perches if (entry.cdte_ctrl & CDROM_DATA_TRACK) {
1018a803393bSJoe Perches if (entry.cdte_format == 0x10)
1019a803393bSJoe Perches tracks->cdi++;
1020a803393bSJoe Perches else if (entry.cdte_format == 0x20)
1021a803393bSJoe Perches tracks->xa++;
1022a803393bSJoe Perches else
1023a803393bSJoe Perches tracks->data++;
1024a803393bSJoe Perches } else {
1025a803393bSJoe Perches tracks->audio++;
1026a803393bSJoe Perches }
1027a803393bSJoe Perches cd_dbg(CD_COUNT_TRACKS, "track %d: format=%d, ctrl=%d\n",
1028a803393bSJoe Perches i, entry.cdte_format, entry.cdte_ctrl);
1029a803393bSJoe Perches }
1030a803393bSJoe Perches cd_dbg(CD_COUNT_TRACKS, "disc has %d tracks: %d=audio %d=data %d=Cd-I %d=XA\n",
1031a803393bSJoe Perches header.cdth_trk1, tracks->audio, tracks->data,
1032a803393bSJoe Perches tracks->cdi, tracks->xa);
1033a803393bSJoe Perches }
1034a803393bSJoe Perches
10351da177e4SLinus Torvalds static
open_for_data(struct cdrom_device_info * cdi)10361da177e4SLinus Torvalds int open_for_data(struct cdrom_device_info *cdi)
10371da177e4SLinus Torvalds {
10381da177e4SLinus Torvalds int ret;
1039853fe1bfSKees Cook const struct cdrom_device_ops *cdo = cdi->ops;
10401da177e4SLinus Torvalds tracktype tracks;
10415944b2ceSJoe Perches cd_dbg(CD_OPEN, "entering open_for_data\n");
10421da177e4SLinus Torvalds /* Check if the driver can report drive status. If it can, we
10431da177e4SLinus Torvalds can do clever things. If it can't, well, we at least tried! */
10441da177e4SLinus Torvalds if (cdo->drive_status != NULL) {
10451da177e4SLinus Torvalds ret = cdo->drive_status(cdi, CDSL_CURRENT);
10465944b2ceSJoe Perches cd_dbg(CD_OPEN, "drive_status=%d\n", ret);
10471da177e4SLinus Torvalds if (ret == CDS_TRAY_OPEN) {
10485944b2ceSJoe Perches cd_dbg(CD_OPEN, "the tray is open...\n");
10491da177e4SLinus Torvalds /* can/may i close it? */
10501da177e4SLinus Torvalds if (CDROM_CAN(CDC_CLOSE_TRAY) &&
10511da177e4SLinus Torvalds cdi->options & CDO_AUTO_CLOSE) {
10525944b2ceSJoe Perches cd_dbg(CD_OPEN, "trying to close the tray\n");
10531da177e4SLinus Torvalds ret=cdo->tray_move(cdi,0);
10541da177e4SLinus Torvalds if (ret) {
10555944b2ceSJoe Perches cd_dbg(CD_OPEN, "bummer. tried to close the tray but failed.\n");
10561da177e4SLinus Torvalds /* Ignore the error from the low
10571da177e4SLinus Torvalds level driver. We don't care why it
10581da177e4SLinus Torvalds couldn't close the tray. We only care
10591da177e4SLinus Torvalds that there is no disc in the drive,
10601da177e4SLinus Torvalds since that is the _REAL_ problem here.*/
10611da177e4SLinus Torvalds ret=-ENOMEDIUM;
10621da177e4SLinus Torvalds goto clean_up_and_return;
10631da177e4SLinus Torvalds }
10641da177e4SLinus Torvalds } else {
10655944b2ceSJoe Perches cd_dbg(CD_OPEN, "bummer. this drive can't close the tray.\n");
10661da177e4SLinus Torvalds ret=-ENOMEDIUM;
10671da177e4SLinus Torvalds goto clean_up_and_return;
10681da177e4SLinus Torvalds }
10691da177e4SLinus Torvalds /* Ok, the door should be closed now.. Check again */
10701da177e4SLinus Torvalds ret = cdo->drive_status(cdi, CDSL_CURRENT);
10711da177e4SLinus Torvalds if ((ret == CDS_NO_DISC) || (ret==CDS_TRAY_OPEN)) {
10725944b2ceSJoe Perches cd_dbg(CD_OPEN, "bummer. the tray is still not closed.\n");
10735944b2ceSJoe Perches cd_dbg(CD_OPEN, "tray might not contain a medium\n");
10741da177e4SLinus Torvalds ret=-ENOMEDIUM;
10751da177e4SLinus Torvalds goto clean_up_and_return;
10761da177e4SLinus Torvalds }
10775944b2ceSJoe Perches cd_dbg(CD_OPEN, "the tray is now closed\n");
10781da177e4SLinus Torvalds }
10791da177e4SLinus Torvalds /* the door should be closed now, check for the disc */
10801da177e4SLinus Torvalds ret = cdo->drive_status(cdi, CDSL_CURRENT);
10811da177e4SLinus Torvalds if (ret!=CDS_DISC_OK) {
10821da177e4SLinus Torvalds ret = -ENOMEDIUM;
10831da177e4SLinus Torvalds goto clean_up_and_return;
10841da177e4SLinus Torvalds }
10851da177e4SLinus Torvalds }
10861da177e4SLinus Torvalds cdrom_count_tracks(cdi, &tracks);
10871da177e4SLinus Torvalds if (tracks.error == CDS_NO_DISC) {
10885944b2ceSJoe Perches cd_dbg(CD_OPEN, "bummer. no disc.\n");
10891da177e4SLinus Torvalds ret=-ENOMEDIUM;
10901da177e4SLinus Torvalds goto clean_up_and_return;
10911da177e4SLinus Torvalds }
10921da177e4SLinus Torvalds /* CD-Players which don't use O_NONBLOCK, workman
10931da177e4SLinus Torvalds * for example, need bit CDO_CHECK_TYPE cleared! */
10941da177e4SLinus Torvalds if (tracks.data==0) {
10951da177e4SLinus Torvalds if (cdi->options & CDO_CHECK_TYPE) {
10961da177e4SLinus Torvalds /* give people a warning shot, now that CDO_CHECK_TYPE
10971da177e4SLinus Torvalds is the default case! */
10985944b2ceSJoe Perches cd_dbg(CD_OPEN, "bummer. wrong media type.\n");
10995944b2ceSJoe Perches cd_dbg(CD_WARNING, "pid %d must open device O_NONBLOCK!\n",
1100ba25f9dcSPavel Emelyanov (unsigned int)task_pid_nr(current));
11011da177e4SLinus Torvalds ret=-EMEDIUMTYPE;
11021da177e4SLinus Torvalds goto clean_up_and_return;
11031da177e4SLinus Torvalds }
11041da177e4SLinus Torvalds else {
11055944b2ceSJoe Perches cd_dbg(CD_OPEN, "wrong media type, but CDO_CHECK_TYPE not set\n");
11061da177e4SLinus Torvalds }
11071da177e4SLinus Torvalds }
11081da177e4SLinus Torvalds
11095944b2ceSJoe Perches cd_dbg(CD_OPEN, "all seems well, opening the devicen");
11101da177e4SLinus Torvalds
11111da177e4SLinus Torvalds /* all seems well, we can open the device */
11121da177e4SLinus Torvalds ret = cdo->open(cdi, 0); /* open for data */
11135944b2ceSJoe Perches cd_dbg(CD_OPEN, "opening the device gave me %d\n", ret);
11141da177e4SLinus Torvalds /* After all this careful checking, we shouldn't have problems
11151da177e4SLinus Torvalds opening the device, but we don't want the device locked if
11161da177e4SLinus Torvalds this somehow fails... */
11171da177e4SLinus Torvalds if (ret) {
11185944b2ceSJoe Perches cd_dbg(CD_OPEN, "open device failed\n");
11191da177e4SLinus Torvalds goto clean_up_and_return;
11201da177e4SLinus Torvalds }
11211da177e4SLinus Torvalds if (CDROM_CAN(CDC_LOCK) && (cdi->options & CDO_LOCK)) {
11221da177e4SLinus Torvalds cdo->lock_door(cdi, 1);
11235944b2ceSJoe Perches cd_dbg(CD_OPEN, "door locked\n");
11241da177e4SLinus Torvalds }
11255944b2ceSJoe Perches cd_dbg(CD_OPEN, "device opened successfully\n");
11261da177e4SLinus Torvalds return ret;
11271da177e4SLinus Torvalds
11281da177e4SLinus Torvalds /* Something failed. Try to unlock the drive, because some drivers
11291da177e4SLinus Torvalds (notably ide-cd) lock the drive after every command. This produced
11301da177e4SLinus Torvalds a nasty bug where after mount failed, the drive would remain locked!
11311da177e4SLinus Torvalds This ensures that the drive gets unlocked after a mount fails. This
11321da177e4SLinus Torvalds is a goto to avoid bloating the driver with redundant code. */
11331da177e4SLinus Torvalds clean_up_and_return:
11345944b2ceSJoe Perches cd_dbg(CD_OPEN, "open failed\n");
11351da177e4SLinus Torvalds if (CDROM_CAN(CDC_LOCK) && cdi->options & CDO_LOCK) {
11361da177e4SLinus Torvalds cdo->lock_door(cdi, 0);
11375944b2ceSJoe Perches cd_dbg(CD_OPEN, "door unlocked\n");
11381da177e4SLinus Torvalds }
11391da177e4SLinus Torvalds return ret;
11401da177e4SLinus Torvalds }
11411da177e4SLinus Torvalds
114282b91540SJoe Perches /* We use the open-option O_NONBLOCK to indicate that the
114382b91540SJoe Perches * purpose of opening is only for subsequent ioctl() calls; no device
114482b91540SJoe Perches * integrity checks are performed.
114582b91540SJoe Perches *
114682b91540SJoe Perches * We hope that all cd-player programs will adopt this convention. It
114782b91540SJoe Perches * is in their own interest: device control becomes a lot easier
114882b91540SJoe Perches * this way.
114982b91540SJoe Perches */
cdrom_open(struct cdrom_device_info * cdi,blk_mode_t mode)115005bdb996SChristoph Hellwig int cdrom_open(struct cdrom_device_info *cdi, blk_mode_t mode)
115182b91540SJoe Perches {
115282b91540SJoe Perches int ret;
115382b91540SJoe Perches
115482b91540SJoe Perches cd_dbg(CD_OPEN, "entering cdrom_open\n");
115582b91540SJoe Perches
115682b91540SJoe Perches /* if this was a O_NONBLOCK open and we should honor the flags,
115782b91540SJoe Perches * do a quick open without drive/disc integrity checks. */
115882b91540SJoe Perches cdi->use_count++;
115905bdb996SChristoph Hellwig if ((mode & BLK_OPEN_NDELAY) && (cdi->options & CDO_USE_FFLAGS)) {
116082b91540SJoe Perches ret = cdi->ops->open(cdi, 1);
116182b91540SJoe Perches } else {
116282b91540SJoe Perches ret = open_for_data(cdi);
116382b91540SJoe Perches if (ret)
116482b91540SJoe Perches goto err;
1165366ba7c7SDiego Elio Pettenò if (CDROM_CAN(CDC_GENERIC_PACKET))
116682b91540SJoe Perches cdrom_mmc3_profile(cdi);
116705bdb996SChristoph Hellwig if (mode & BLK_OPEN_WRITE) {
116882b91540SJoe Perches ret = -EROFS;
116982b91540SJoe Perches if (cdrom_open_write(cdi))
117082b91540SJoe Perches goto err_release;
117182b91540SJoe Perches if (!CDROM_CAN(CDC_RAM))
117282b91540SJoe Perches goto err_release;
117382b91540SJoe Perches ret = 0;
117482b91540SJoe Perches cdi->media_written = 0;
117582b91540SJoe Perches }
11768cdf433eSChristoph Hellwig cdi->opened_for_data = true;
117782b91540SJoe Perches }
117882b91540SJoe Perches
117982b91540SJoe Perches if (ret)
118082b91540SJoe Perches goto err;
118182b91540SJoe Perches
118282b91540SJoe Perches cd_dbg(CD_OPEN, "Use count for \"/dev/%s\" now %d\n",
118382b91540SJoe Perches cdi->name, cdi->use_count);
118482b91540SJoe Perches return 0;
118582b91540SJoe Perches err_release:
118682b91540SJoe Perches if (CDROM_CAN(CDC_LOCK) && cdi->options & CDO_LOCK) {
118782b91540SJoe Perches cdi->ops->lock_door(cdi, 0);
118882b91540SJoe Perches cd_dbg(CD_OPEN, "door unlocked\n");
118982b91540SJoe Perches }
119082b91540SJoe Perches cdi->ops->release(cdi);
119182b91540SJoe Perches err:
119282b91540SJoe Perches cdi->use_count--;
119382b91540SJoe Perches return ret;
119482b91540SJoe Perches }
1195eeef7565SEnze Li EXPORT_SYMBOL(cdrom_open);
119682b91540SJoe Perches
11971da177e4SLinus Torvalds /* This code is similar to that in open_for_data. The routine is called
11981da177e4SLinus Torvalds whenever an audio play operation is requested.
11991da177e4SLinus Torvalds */
check_for_audio_disc(struct cdrom_device_info * cdi,const struct cdrom_device_ops * cdo)1200ecb80c6aSAdrian Bunk static int check_for_audio_disc(struct cdrom_device_info *cdi,
1201853fe1bfSKees Cook const struct cdrom_device_ops *cdo)
12021da177e4SLinus Torvalds {
12031da177e4SLinus Torvalds int ret;
12041da177e4SLinus Torvalds tracktype tracks;
12055944b2ceSJoe Perches cd_dbg(CD_OPEN, "entering check_for_audio_disc\n");
12061da177e4SLinus Torvalds if (!(cdi->options & CDO_CHECK_TYPE))
12071da177e4SLinus Torvalds return 0;
12081da177e4SLinus Torvalds if (cdo->drive_status != NULL) {
12091da177e4SLinus Torvalds ret = cdo->drive_status(cdi, CDSL_CURRENT);
12105944b2ceSJoe Perches cd_dbg(CD_OPEN, "drive_status=%d\n", ret);
12111da177e4SLinus Torvalds if (ret == CDS_TRAY_OPEN) {
12125944b2ceSJoe Perches cd_dbg(CD_OPEN, "the tray is open...\n");
12131da177e4SLinus Torvalds /* can/may i close it? */
12141da177e4SLinus Torvalds if (CDROM_CAN(CDC_CLOSE_TRAY) &&
12151da177e4SLinus Torvalds cdi->options & CDO_AUTO_CLOSE) {
12165944b2ceSJoe Perches cd_dbg(CD_OPEN, "trying to close the tray\n");
12171da177e4SLinus Torvalds ret=cdo->tray_move(cdi,0);
12181da177e4SLinus Torvalds if (ret) {
12195944b2ceSJoe Perches cd_dbg(CD_OPEN, "bummer. tried to close tray but failed.\n");
12201da177e4SLinus Torvalds /* Ignore the error from the low
12211da177e4SLinus Torvalds level driver. We don't care why it
12221da177e4SLinus Torvalds couldn't close the tray. We only care
12231da177e4SLinus Torvalds that there is no disc in the drive,
12241da177e4SLinus Torvalds since that is the _REAL_ problem here.*/
12251da177e4SLinus Torvalds return -ENOMEDIUM;
12261da177e4SLinus Torvalds }
12271da177e4SLinus Torvalds } else {
12285944b2ceSJoe Perches cd_dbg(CD_OPEN, "bummer. this driver can't close the tray.\n");
12291da177e4SLinus Torvalds return -ENOMEDIUM;
12301da177e4SLinus Torvalds }
12311da177e4SLinus Torvalds /* Ok, the door should be closed now.. Check again */
12321da177e4SLinus Torvalds ret = cdo->drive_status(cdi, CDSL_CURRENT);
12331da177e4SLinus Torvalds if ((ret == CDS_NO_DISC) || (ret==CDS_TRAY_OPEN)) {
12345944b2ceSJoe Perches cd_dbg(CD_OPEN, "bummer. the tray is still not closed.\n");
12351da177e4SLinus Torvalds return -ENOMEDIUM;
12361da177e4SLinus Torvalds }
12371da177e4SLinus Torvalds if (ret!=CDS_DISC_OK) {
12385944b2ceSJoe Perches cd_dbg(CD_OPEN, "bummer. disc isn't ready.\n");
12391da177e4SLinus Torvalds return -EIO;
12401da177e4SLinus Torvalds }
12415944b2ceSJoe Perches cd_dbg(CD_OPEN, "the tray is now closed\n");
12421da177e4SLinus Torvalds }
12431da177e4SLinus Torvalds }
12441da177e4SLinus Torvalds cdrom_count_tracks(cdi, &tracks);
12451da177e4SLinus Torvalds if (tracks.error)
12461da177e4SLinus Torvalds return(tracks.error);
12471da177e4SLinus Torvalds
12481da177e4SLinus Torvalds if (tracks.audio==0)
12491da177e4SLinus Torvalds return -EMEDIUMTYPE;
12501da177e4SLinus Torvalds
12511da177e4SLinus Torvalds return 0;
12521da177e4SLinus Torvalds }
12531da177e4SLinus Torvalds
cdrom_release(struct cdrom_device_info * cdi)12547ae24fceSChristoph Hellwig void cdrom_release(struct cdrom_device_info *cdi)
12551da177e4SLinus Torvalds {
1256853fe1bfSKees Cook const struct cdrom_device_ops *cdo = cdi->ops;
12571da177e4SLinus Torvalds
12585944b2ceSJoe Perches cd_dbg(CD_CLOSE, "entering cdrom_release\n");
12591da177e4SLinus Torvalds
12601da177e4SLinus Torvalds if (cdi->use_count > 0)
12611da177e4SLinus Torvalds cdi->use_count--;
126235841f70SBorislav Petkov
126335841f70SBorislav Petkov if (cdi->use_count == 0) {
12645944b2ceSJoe Perches cd_dbg(CD_CLOSE, "Use count for \"/dev/%s\" now zero\n",
12655944b2ceSJoe Perches cdi->name);
12661da177e4SLinus Torvalds cdrom_dvd_rw_close_write(cdi);
126735841f70SBorislav Petkov
1268cdccaa94SPaolo Bonzini if ((cdo->capability & CDC_LOCK) && !cdi->keeplocked) {
12695944b2ceSJoe Perches cd_dbg(CD_CLOSE, "Unlocking door!\n");
12701da177e4SLinus Torvalds cdo->lock_door(cdi, 0);
12711da177e4SLinus Torvalds }
127235841f70SBorislav Petkov }
127335841f70SBorislav Petkov
12741da177e4SLinus Torvalds cdo->release(cdi);
12758cdf433eSChristoph Hellwig
12768cdf433eSChristoph Hellwig if (cdi->use_count == 0 && cdi->opened_for_data) {
12778cdf433eSChristoph Hellwig if (cdi->options & CDO_AUTO_EJECT && CDROM_CAN(CDC_OPEN_TRAY))
12781da177e4SLinus Torvalds cdo->tray_move(cdi, 1);
12798cdf433eSChristoph Hellwig cdi->opened_for_data = false;
12801da177e4SLinus Torvalds }
12811da177e4SLinus Torvalds }
1282eeef7565SEnze Li EXPORT_SYMBOL(cdrom_release);
12831da177e4SLinus Torvalds
cdrom_read_mech_status(struct cdrom_device_info * cdi,struct cdrom_changer_info * buf)12841da177e4SLinus Torvalds static int cdrom_read_mech_status(struct cdrom_device_info *cdi,
12851da177e4SLinus Torvalds struct cdrom_changer_info *buf)
12861da177e4SLinus Torvalds {
12871da177e4SLinus Torvalds struct packet_command cgc;
1288853fe1bfSKees Cook const struct cdrom_device_ops *cdo = cdi->ops;
12891da177e4SLinus Torvalds int length;
12901da177e4SLinus Torvalds
12911da177e4SLinus Torvalds /*
12921da177e4SLinus Torvalds * Sanyo changer isn't spec compliant (doesn't use regular change
12931da177e4SLinus Torvalds * LOAD_UNLOAD command, and it doesn't implement the mech status
12941da177e4SLinus Torvalds * command below
12951da177e4SLinus Torvalds */
12961da177e4SLinus Torvalds if (cdi->sanyo_slot) {
12971da177e4SLinus Torvalds buf->hdr.nslots = 3;
12981da177e4SLinus Torvalds buf->hdr.curslot = cdi->sanyo_slot == 3 ? 0 : cdi->sanyo_slot;
12991da177e4SLinus Torvalds for (length = 0; length < 3; length++) {
13001da177e4SLinus Torvalds buf->slots[length].disc_present = 1;
13011da177e4SLinus Torvalds buf->slots[length].change = 0;
13021da177e4SLinus Torvalds }
13031da177e4SLinus Torvalds return 0;
13041da177e4SLinus Torvalds }
13051da177e4SLinus Torvalds
13061da177e4SLinus Torvalds length = sizeof(struct cdrom_mechstat_header) +
13071da177e4SLinus Torvalds cdi->capacity * sizeof(struct cdrom_slot);
13081da177e4SLinus Torvalds
13091da177e4SLinus Torvalds init_cdrom_command(&cgc, buf, length, CGC_DATA_READ);
13101da177e4SLinus Torvalds cgc.cmd[0] = GPCMD_MECHANISM_STATUS;
13111da177e4SLinus Torvalds cgc.cmd[8] = (length >> 8) & 0xff;
13121da177e4SLinus Torvalds cgc.cmd[9] = length & 0xff;
13131da177e4SLinus Torvalds return cdo->generic_packet(cdi, &cgc);
13141da177e4SLinus Torvalds }
13151da177e4SLinus Torvalds
cdrom_slot_status(struct cdrom_device_info * cdi,int slot)13161da177e4SLinus Torvalds static int cdrom_slot_status(struct cdrom_device_info *cdi, int slot)
13171da177e4SLinus Torvalds {
13181da177e4SLinus Torvalds struct cdrom_changer_info *info;
13191da177e4SLinus Torvalds int ret;
13201da177e4SLinus Torvalds
13215944b2ceSJoe Perches cd_dbg(CD_CHANGER, "entering cdrom_slot_status()\n");
13221da177e4SLinus Torvalds if (cdi->sanyo_slot)
13231da177e4SLinus Torvalds return CDS_NO_INFO;
13241da177e4SLinus Torvalds
13251da177e4SLinus Torvalds info = kmalloc(sizeof(*info), GFP_KERNEL);
13261da177e4SLinus Torvalds if (!info)
13271da177e4SLinus Torvalds return -ENOMEM;
13281da177e4SLinus Torvalds
13291da177e4SLinus Torvalds if ((ret = cdrom_read_mech_status(cdi, info)))
13301da177e4SLinus Torvalds goto out_free;
13311da177e4SLinus Torvalds
13321da177e4SLinus Torvalds if (info->slots[slot].disc_present)
13331da177e4SLinus Torvalds ret = CDS_DISC_OK;
13341da177e4SLinus Torvalds else
13351da177e4SLinus Torvalds ret = CDS_NO_DISC;
13361da177e4SLinus Torvalds
13371da177e4SLinus Torvalds out_free:
13381da177e4SLinus Torvalds kfree(info);
13391da177e4SLinus Torvalds return ret;
13401da177e4SLinus Torvalds }
13411da177e4SLinus Torvalds
13421da177e4SLinus Torvalds /* Return the number of slots for an ATAPI/SCSI cdrom,
13431da177e4SLinus Torvalds * return 1 if not a changer.
13441da177e4SLinus Torvalds */
cdrom_number_of_slots(struct cdrom_device_info * cdi)13451da177e4SLinus Torvalds int cdrom_number_of_slots(struct cdrom_device_info *cdi)
13461da177e4SLinus Torvalds {
13471da177e4SLinus Torvalds int nslots = 1;
13481da177e4SLinus Torvalds struct cdrom_changer_info *info;
13491da177e4SLinus Torvalds
13505944b2ceSJoe Perches cd_dbg(CD_CHANGER, "entering cdrom_number_of_slots()\n");
13511da177e4SLinus Torvalds /* cdrom_read_mech_status requires a valid value for capacity: */
13521da177e4SLinus Torvalds cdi->capacity = 0;
13531da177e4SLinus Torvalds
13541da177e4SLinus Torvalds info = kmalloc(sizeof(*info), GFP_KERNEL);
13551da177e4SLinus Torvalds if (!info)
13561da177e4SLinus Torvalds return -ENOMEM;
13571da177e4SLinus Torvalds
13584ded53eaSEnze Li if (cdrom_read_mech_status(cdi, info) == 0)
13591da177e4SLinus Torvalds nslots = info->hdr.nslots;
13601da177e4SLinus Torvalds
13611da177e4SLinus Torvalds kfree(info);
13621da177e4SLinus Torvalds return nslots;
13631da177e4SLinus Torvalds }
1364eeef7565SEnze Li EXPORT_SYMBOL(cdrom_number_of_slots);
13651da177e4SLinus Torvalds
13661da177e4SLinus Torvalds
13671da177e4SLinus Torvalds /* If SLOT < 0, unload the current slot. Otherwise, try to load SLOT. */
cdrom_load_unload(struct cdrom_device_info * cdi,int slot)13681da177e4SLinus Torvalds static int cdrom_load_unload(struct cdrom_device_info *cdi, int slot)
13691da177e4SLinus Torvalds {
13701da177e4SLinus Torvalds struct packet_command cgc;
13711da177e4SLinus Torvalds
13725944b2ceSJoe Perches cd_dbg(CD_CHANGER, "entering cdrom_load_unload()\n");
13731da177e4SLinus Torvalds if (cdi->sanyo_slot && slot < 0)
13741da177e4SLinus Torvalds return 0;
13751da177e4SLinus Torvalds
13761da177e4SLinus Torvalds init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
13771da177e4SLinus Torvalds cgc.cmd[0] = GPCMD_LOAD_UNLOAD;
13781da177e4SLinus Torvalds cgc.cmd[4] = 2 + (slot >= 0);
13791da177e4SLinus Torvalds cgc.cmd[8] = slot;
13801da177e4SLinus Torvalds cgc.timeout = 60 * HZ;
13811da177e4SLinus Torvalds
13821da177e4SLinus Torvalds /* The Sanyo 3 CD changer uses byte 7 of the
13831da177e4SLinus Torvalds GPCMD_TEST_UNIT_READY to command to switch CDs instead of
13841da177e4SLinus Torvalds using the GPCMD_LOAD_UNLOAD opcode. */
13851da177e4SLinus Torvalds if (cdi->sanyo_slot && -1 < slot) {
13861da177e4SLinus Torvalds cgc.cmd[0] = GPCMD_TEST_UNIT_READY;
13871da177e4SLinus Torvalds cgc.cmd[7] = slot;
13881da177e4SLinus Torvalds cgc.cmd[4] = cgc.cmd[8] = 0;
13891da177e4SLinus Torvalds cdi->sanyo_slot = slot ? slot : 3;
13901da177e4SLinus Torvalds }
13911da177e4SLinus Torvalds
13921da177e4SLinus Torvalds return cdi->ops->generic_packet(cdi, &cgc);
13931da177e4SLinus Torvalds }
13941da177e4SLinus Torvalds
cdrom_select_disc(struct cdrom_device_info * cdi,int slot)13951da177e4SLinus Torvalds static int cdrom_select_disc(struct cdrom_device_info *cdi, int slot)
13961da177e4SLinus Torvalds {
13971da177e4SLinus Torvalds struct cdrom_changer_info *info;
13981da177e4SLinus Torvalds int curslot;
13991da177e4SLinus Torvalds int ret;
14001da177e4SLinus Torvalds
14015944b2ceSJoe Perches cd_dbg(CD_CHANGER, "entering cdrom_select_disc()\n");
14021da177e4SLinus Torvalds if (!CDROM_CAN(CDC_SELECT_DISC))
14031da177e4SLinus Torvalds return -EDRIVE_CANT_DO_THIS;
14041da177e4SLinus Torvalds
14052d921729STejun Heo if (cdi->ops->check_events)
14062d921729STejun Heo cdi->ops->check_events(cdi, 0, slot);
14071da177e4SLinus Torvalds
14081da177e4SLinus Torvalds if (slot == CDSL_NONE) {
140967f1e027SLukas Prediger signal_media_change(cdi);
14101da177e4SLinus Torvalds return cdrom_load_unload(cdi, -1);
14111da177e4SLinus Torvalds }
14121da177e4SLinus Torvalds
14131da177e4SLinus Torvalds info = kmalloc(sizeof(*info), GFP_KERNEL);
14141da177e4SLinus Torvalds if (!info)
14151da177e4SLinus Torvalds return -ENOMEM;
14161da177e4SLinus Torvalds
14171da177e4SLinus Torvalds if ((ret = cdrom_read_mech_status(cdi, info))) {
14181da177e4SLinus Torvalds kfree(info);
14191da177e4SLinus Torvalds return ret;
14201da177e4SLinus Torvalds }
14211da177e4SLinus Torvalds
14221da177e4SLinus Torvalds curslot = info->hdr.curslot;
14231da177e4SLinus Torvalds kfree(info);
14241da177e4SLinus Torvalds
1425cdccaa94SPaolo Bonzini if (cdi->use_count > 1 || cdi->keeplocked) {
14261da177e4SLinus Torvalds if (slot == CDSL_CURRENT) {
14271da177e4SLinus Torvalds return curslot;
14281da177e4SLinus Torvalds } else {
14291da177e4SLinus Torvalds return -EBUSY;
14301da177e4SLinus Torvalds }
14311da177e4SLinus Torvalds }
14321da177e4SLinus Torvalds
14331da177e4SLinus Torvalds /* Specifying CDSL_CURRENT will attempt to load the currnet slot,
14341da177e4SLinus Torvalds which is useful if it had been previously unloaded.
14351da177e4SLinus Torvalds Whether it can or not, it returns the current slot.
14361da177e4SLinus Torvalds Similarly, if slot happens to be the current one, we still
14371da177e4SLinus Torvalds try and load it. */
14381da177e4SLinus Torvalds if (slot == CDSL_CURRENT)
14391da177e4SLinus Torvalds slot = curslot;
14401da177e4SLinus Torvalds
14411da177e4SLinus Torvalds /* set media changed bits on both queues */
144267f1e027SLukas Prediger signal_media_change(cdi);
14431da177e4SLinus Torvalds if ((ret = cdrom_load_unload(cdi, slot)))
14441da177e4SLinus Torvalds return ret;
14451da177e4SLinus Torvalds
14461da177e4SLinus Torvalds return slot;
14471da177e4SLinus Torvalds }
14481da177e4SLinus Torvalds
14492d921729STejun Heo /*
14502d921729STejun Heo * As cdrom implements an extra ioctl consumer for media changed
14512d921729STejun Heo * event, it needs to buffer ->check_events() output, such that event
14522d921729STejun Heo * is not lost for both the usual VFS and ioctl paths.
14532d921729STejun Heo * cdi->{vfs|ioctl}_events are used to buffer pending events for each
14542d921729STejun Heo * path.
14552d921729STejun Heo *
14562d921729STejun Heo * XXX: Locking is non-existent. cdi->ops->check_events() can be
14572d921729STejun Heo * called in parallel and buffering fields are accessed without any
14582d921729STejun Heo * exclusion. The original media_changed code had the same problem.
14592d921729STejun Heo * It might be better to simply deprecate CDROM_MEDIA_CHANGED ioctl
14602d921729STejun Heo * and remove this cruft altogether. It doesn't have much usefulness
14612d921729STejun Heo * at this point.
14622d921729STejun Heo */
cdrom_update_events(struct cdrom_device_info * cdi,unsigned int clearing)14632d921729STejun Heo static void cdrom_update_events(struct cdrom_device_info *cdi,
14642d921729STejun Heo unsigned int clearing)
14652d921729STejun Heo {
14662d921729STejun Heo unsigned int events;
14672d921729STejun Heo
14682d921729STejun Heo events = cdi->ops->check_events(cdi, clearing, CDSL_CURRENT);
14692d921729STejun Heo cdi->vfs_events |= events;
14702d921729STejun Heo cdi->ioctl_events |= events;
14712d921729STejun Heo }
14722d921729STejun Heo
cdrom_check_events(struct cdrom_device_info * cdi,unsigned int clearing)14732d921729STejun Heo unsigned int cdrom_check_events(struct cdrom_device_info *cdi,
14742d921729STejun Heo unsigned int clearing)
14752d921729STejun Heo {
14762d921729STejun Heo unsigned int events;
14772d921729STejun Heo
14782d921729STejun Heo cdrom_update_events(cdi, clearing);
14792d921729STejun Heo events = cdi->vfs_events;
14802d921729STejun Heo cdi->vfs_events = 0;
14812d921729STejun Heo return events;
14822d921729STejun Heo }
148330a7caabSJens Axboe EXPORT_SYMBOL(cdrom_check_events);
14842d921729STejun Heo
14851da177e4SLinus Torvalds /* We want to make media_changed accessible to the user through an
14861da177e4SLinus Torvalds * ioctl. The main problem now is that we must double-buffer the
14871da177e4SLinus Torvalds * low-level implementation, to assure that the VFS and the user both
14881da177e4SLinus Torvalds * see a medium change once.
14891da177e4SLinus Torvalds */
14901da177e4SLinus Torvalds
14911da177e4SLinus Torvalds static
media_changed(struct cdrom_device_info * cdi,int queue)14921da177e4SLinus Torvalds int media_changed(struct cdrom_device_info *cdi, int queue)
14931da177e4SLinus Torvalds {
14941da177e4SLinus Torvalds unsigned int mask = (1 << (queue & 1));
14951da177e4SLinus Torvalds int ret = !!(cdi->mc_flags & mask);
14962d921729STejun Heo bool changed;
14971da177e4SLinus Torvalds
14981da177e4SLinus Torvalds if (!CDROM_CAN(CDC_MEDIA_CHANGED))
14991da177e4SLinus Torvalds return ret;
15002d921729STejun Heo
15011da177e4SLinus Torvalds /* changed since last call? */
15022d921729STejun Heo BUG_ON(!queue); /* shouldn't be called from VFS path */
15032d921729STejun Heo cdrom_update_events(cdi, DISK_EVENT_MEDIA_CHANGE);
15042d921729STejun Heo changed = cdi->ioctl_events & DISK_EVENT_MEDIA_CHANGE;
15052d921729STejun Heo cdi->ioctl_events = 0;
15062d921729STejun Heo
15072d921729STejun Heo if (changed) {
150867f1e027SLukas Prediger signal_media_change(cdi);
15091da177e4SLinus Torvalds ret |= 1;
15101da177e4SLinus Torvalds cdi->media_written = 0;
15111da177e4SLinus Torvalds }
15122d921729STejun Heo
15131da177e4SLinus Torvalds cdi->mc_flags &= ~mask; /* clear bit */
15141da177e4SLinus Torvalds return ret;
15151da177e4SLinus Torvalds }
15161da177e4SLinus Torvalds
15171da177e4SLinus Torvalds /* Requests to the low-level drivers will /always/ be done in the
15181da177e4SLinus Torvalds following format convention:
15191da177e4SLinus Torvalds
15201da177e4SLinus Torvalds CDROM_LBA: all data-related requests.
15211da177e4SLinus Torvalds CDROM_MSF: all audio-related requests.
15221da177e4SLinus Torvalds
15231da177e4SLinus Torvalds However, a low-level implementation is allowed to refuse this
15241da177e4SLinus Torvalds request, and return information in its own favorite format.
15251da177e4SLinus Torvalds
15261da177e4SLinus Torvalds It doesn't make sense /at all/ to ask for a play_audio in LBA
15271da177e4SLinus Torvalds format, or ask for multi-session info in MSF format. However, for
15281da177e4SLinus Torvalds backward compatibility these format requests will be satisfied, but
15291da177e4SLinus Torvalds the requests to the low-level drivers will be sanitized in the more
15301da177e4SLinus Torvalds meaningful format indicated above.
15311da177e4SLinus Torvalds */
15321da177e4SLinus Torvalds
15331da177e4SLinus Torvalds static
sanitize_format(union cdrom_addr * addr,u_char * curr,u_char requested)15341da177e4SLinus Torvalds void sanitize_format(union cdrom_addr *addr,
15351da177e4SLinus Torvalds u_char * curr, u_char requested)
15361da177e4SLinus Torvalds {
15371da177e4SLinus Torvalds if (*curr == requested)
15381da177e4SLinus Torvalds return; /* nothing to be done! */
15391da177e4SLinus Torvalds if (requested == CDROM_LBA) {
15401da177e4SLinus Torvalds addr->lba = (int) addr->msf.frame +
15411da177e4SLinus Torvalds 75 * (addr->msf.second - 2 + 60 * addr->msf.minute);
15421da177e4SLinus Torvalds } else { /* CDROM_MSF */
15431da177e4SLinus Torvalds int lba = addr->lba;
15441da177e4SLinus Torvalds addr->msf.frame = lba % 75;
15451da177e4SLinus Torvalds lba /= 75;
15461da177e4SLinus Torvalds lba += 2;
15471da177e4SLinus Torvalds addr->msf.second = lba % 60;
15481da177e4SLinus Torvalds addr->msf.minute = lba / 60;
15491da177e4SLinus Torvalds }
15501da177e4SLinus Torvalds *curr = requested;
15511da177e4SLinus Torvalds }
15521da177e4SLinus Torvalds
init_cdrom_command(struct packet_command * cgc,void * buf,int len,int type)15531da177e4SLinus Torvalds void init_cdrom_command(struct packet_command *cgc, void *buf, int len,
15541da177e4SLinus Torvalds int type)
15551da177e4SLinus Torvalds {
15561da177e4SLinus Torvalds memset(cgc, 0, sizeof(struct packet_command));
15571da177e4SLinus Torvalds if (buf)
15581da177e4SLinus Torvalds memset(buf, 0, len);
15591da177e4SLinus Torvalds cgc->buffer = (char *) buf;
15601da177e4SLinus Torvalds cgc->buflen = len;
15611da177e4SLinus Torvalds cgc->data_direction = type;
15622e11c207SJens Axboe cgc->timeout = CDROM_DEF_TIMEOUT;
15631da177e4SLinus Torvalds }
1564eeef7565SEnze Li EXPORT_SYMBOL(init_cdrom_command);
15651da177e4SLinus Torvalds
15661da177e4SLinus Torvalds /* DVD handling */
15671da177e4SLinus Torvalds
15681da177e4SLinus Torvalds #define copy_key(dest,src) memcpy((dest), (src), sizeof(dvd_key))
15691da177e4SLinus Torvalds #define copy_chal(dest,src) memcpy((dest), (src), sizeof(dvd_challenge))
15701da177e4SLinus Torvalds
setup_report_key(struct packet_command * cgc,unsigned agid,unsigned type)15711da177e4SLinus Torvalds static void setup_report_key(struct packet_command *cgc, unsigned agid, unsigned type)
15721da177e4SLinus Torvalds {
15731da177e4SLinus Torvalds cgc->cmd[0] = GPCMD_REPORT_KEY;
15741da177e4SLinus Torvalds cgc->cmd[10] = type | (agid << 6);
15751da177e4SLinus Torvalds switch (type) {
15761da177e4SLinus Torvalds case 0: case 8: case 5: {
15771da177e4SLinus Torvalds cgc->buflen = 8;
15781da177e4SLinus Torvalds break;
15791da177e4SLinus Torvalds }
15801da177e4SLinus Torvalds case 1: {
15811da177e4SLinus Torvalds cgc->buflen = 16;
15821da177e4SLinus Torvalds break;
15831da177e4SLinus Torvalds }
15841da177e4SLinus Torvalds case 2: case 4: {
15851da177e4SLinus Torvalds cgc->buflen = 12;
15861da177e4SLinus Torvalds break;
15871da177e4SLinus Torvalds }
15881da177e4SLinus Torvalds }
15891da177e4SLinus Torvalds cgc->cmd[9] = cgc->buflen;
15901da177e4SLinus Torvalds cgc->data_direction = CGC_DATA_READ;
15911da177e4SLinus Torvalds }
15921da177e4SLinus Torvalds
setup_send_key(struct packet_command * cgc,unsigned agid,unsigned type)15931da177e4SLinus Torvalds static void setup_send_key(struct packet_command *cgc, unsigned agid, unsigned type)
15941da177e4SLinus Torvalds {
15951da177e4SLinus Torvalds cgc->cmd[0] = GPCMD_SEND_KEY;
15961da177e4SLinus Torvalds cgc->cmd[10] = type | (agid << 6);
15971da177e4SLinus Torvalds switch (type) {
15981da177e4SLinus Torvalds case 1: {
15991da177e4SLinus Torvalds cgc->buflen = 16;
16001da177e4SLinus Torvalds break;
16011da177e4SLinus Torvalds }
16021da177e4SLinus Torvalds case 3: {
16031da177e4SLinus Torvalds cgc->buflen = 12;
16041da177e4SLinus Torvalds break;
16051da177e4SLinus Torvalds }
16061da177e4SLinus Torvalds case 6: {
16071da177e4SLinus Torvalds cgc->buflen = 8;
16081da177e4SLinus Torvalds break;
16091da177e4SLinus Torvalds }
16101da177e4SLinus Torvalds }
16111da177e4SLinus Torvalds cgc->cmd[9] = cgc->buflen;
16121da177e4SLinus Torvalds cgc->data_direction = CGC_DATA_WRITE;
16131da177e4SLinus Torvalds }
16141da177e4SLinus Torvalds
dvd_do_auth(struct cdrom_device_info * cdi,dvd_authinfo * ai)16151da177e4SLinus Torvalds static int dvd_do_auth(struct cdrom_device_info *cdi, dvd_authinfo *ai)
16161da177e4SLinus Torvalds {
16171da177e4SLinus Torvalds int ret;
1618fce53847SFUJITA Tomonori u_char buf[20];
16191da177e4SLinus Torvalds struct packet_command cgc;
1620853fe1bfSKees Cook const struct cdrom_device_ops *cdo = cdi->ops;
1621fce53847SFUJITA Tomonori rpc_state_t rpc_state;
16221da177e4SLinus Torvalds
1623fce53847SFUJITA Tomonori memset(buf, 0, sizeof(buf));
16241da177e4SLinus Torvalds init_cdrom_command(&cgc, buf, 0, CGC_DATA_READ);
16251da177e4SLinus Torvalds
16261da177e4SLinus Torvalds switch (ai->type) {
16271da177e4SLinus Torvalds /* LU data send */
16281da177e4SLinus Torvalds case DVD_LU_SEND_AGID:
16295944b2ceSJoe Perches cd_dbg(CD_DVD, "entering DVD_LU_SEND_AGID\n");
16301da177e4SLinus Torvalds cgc.quiet = 1;
16311da177e4SLinus Torvalds setup_report_key(&cgc, ai->lsa.agid, 0);
16321da177e4SLinus Torvalds
16331da177e4SLinus Torvalds if ((ret = cdo->generic_packet(cdi, &cgc)))
1634fce53847SFUJITA Tomonori return ret;
16351da177e4SLinus Torvalds
16361da177e4SLinus Torvalds ai->lsa.agid = buf[7] >> 6;
16371da177e4SLinus Torvalds /* Returning data, let host change state */
16381da177e4SLinus Torvalds break;
16391da177e4SLinus Torvalds
16401da177e4SLinus Torvalds case DVD_LU_SEND_KEY1:
16415944b2ceSJoe Perches cd_dbg(CD_DVD, "entering DVD_LU_SEND_KEY1\n");
16421da177e4SLinus Torvalds setup_report_key(&cgc, ai->lsk.agid, 2);
16431da177e4SLinus Torvalds
16441da177e4SLinus Torvalds if ((ret = cdo->generic_packet(cdi, &cgc)))
1645fce53847SFUJITA Tomonori return ret;
16461da177e4SLinus Torvalds
16471da177e4SLinus Torvalds copy_key(ai->lsk.key, &buf[4]);
16481da177e4SLinus Torvalds /* Returning data, let host change state */
16491da177e4SLinus Torvalds break;
16501da177e4SLinus Torvalds
16511da177e4SLinus Torvalds case DVD_LU_SEND_CHALLENGE:
16525944b2ceSJoe Perches cd_dbg(CD_DVD, "entering DVD_LU_SEND_CHALLENGE\n");
16531da177e4SLinus Torvalds setup_report_key(&cgc, ai->lsc.agid, 1);
16541da177e4SLinus Torvalds
16551da177e4SLinus Torvalds if ((ret = cdo->generic_packet(cdi, &cgc)))
1656fce53847SFUJITA Tomonori return ret;
16571da177e4SLinus Torvalds
16581da177e4SLinus Torvalds copy_chal(ai->lsc.chal, &buf[4]);
16591da177e4SLinus Torvalds /* Returning data, let host change state */
16601da177e4SLinus Torvalds break;
16611da177e4SLinus Torvalds
16621da177e4SLinus Torvalds /* Post-auth key */
16631da177e4SLinus Torvalds case DVD_LU_SEND_TITLE_KEY:
16645944b2ceSJoe Perches cd_dbg(CD_DVD, "entering DVD_LU_SEND_TITLE_KEY\n");
16651da177e4SLinus Torvalds cgc.quiet = 1;
16661da177e4SLinus Torvalds setup_report_key(&cgc, ai->lstk.agid, 4);
16671da177e4SLinus Torvalds cgc.cmd[5] = ai->lstk.lba;
16681da177e4SLinus Torvalds cgc.cmd[4] = ai->lstk.lba >> 8;
16691da177e4SLinus Torvalds cgc.cmd[3] = ai->lstk.lba >> 16;
16701da177e4SLinus Torvalds cgc.cmd[2] = ai->lstk.lba >> 24;
16711da177e4SLinus Torvalds
16721da177e4SLinus Torvalds if ((ret = cdo->generic_packet(cdi, &cgc)))
1673fce53847SFUJITA Tomonori return ret;
16741da177e4SLinus Torvalds
16751da177e4SLinus Torvalds ai->lstk.cpm = (buf[4] >> 7) & 1;
16761da177e4SLinus Torvalds ai->lstk.cp_sec = (buf[4] >> 6) & 1;
16771da177e4SLinus Torvalds ai->lstk.cgms = (buf[4] >> 4) & 3;
16781da177e4SLinus Torvalds copy_key(ai->lstk.title_key, &buf[5]);
16791da177e4SLinus Torvalds /* Returning data, let host change state */
16801da177e4SLinus Torvalds break;
16811da177e4SLinus Torvalds
16821da177e4SLinus Torvalds case DVD_LU_SEND_ASF:
16835944b2ceSJoe Perches cd_dbg(CD_DVD, "entering DVD_LU_SEND_ASF\n");
16841da177e4SLinus Torvalds setup_report_key(&cgc, ai->lsasf.agid, 5);
16851da177e4SLinus Torvalds
16861da177e4SLinus Torvalds if ((ret = cdo->generic_packet(cdi, &cgc)))
1687fce53847SFUJITA Tomonori return ret;
16881da177e4SLinus Torvalds
16891da177e4SLinus Torvalds ai->lsasf.asf = buf[7] & 1;
16901da177e4SLinus Torvalds break;
16911da177e4SLinus Torvalds
16921da177e4SLinus Torvalds /* LU data receive (LU changes state) */
16931da177e4SLinus Torvalds case DVD_HOST_SEND_CHALLENGE:
16945944b2ceSJoe Perches cd_dbg(CD_DVD, "entering DVD_HOST_SEND_CHALLENGE\n");
16951da177e4SLinus Torvalds setup_send_key(&cgc, ai->hsc.agid, 1);
16961da177e4SLinus Torvalds buf[1] = 0xe;
16971da177e4SLinus Torvalds copy_chal(&buf[4], ai->hsc.chal);
16981da177e4SLinus Torvalds
16991da177e4SLinus Torvalds if ((ret = cdo->generic_packet(cdi, &cgc)))
1700fce53847SFUJITA Tomonori return ret;
17011da177e4SLinus Torvalds
17021da177e4SLinus Torvalds ai->type = DVD_LU_SEND_KEY1;
17031da177e4SLinus Torvalds break;
17041da177e4SLinus Torvalds
17051da177e4SLinus Torvalds case DVD_HOST_SEND_KEY2:
17065944b2ceSJoe Perches cd_dbg(CD_DVD, "entering DVD_HOST_SEND_KEY2\n");
17071da177e4SLinus Torvalds setup_send_key(&cgc, ai->hsk.agid, 3);
17081da177e4SLinus Torvalds buf[1] = 0xa;
17091da177e4SLinus Torvalds copy_key(&buf[4], ai->hsk.key);
17101da177e4SLinus Torvalds
17111da177e4SLinus Torvalds if ((ret = cdo->generic_packet(cdi, &cgc))) {
17121da177e4SLinus Torvalds ai->type = DVD_AUTH_FAILURE;
1713fce53847SFUJITA Tomonori return ret;
17141da177e4SLinus Torvalds }
17151da177e4SLinus Torvalds ai->type = DVD_AUTH_ESTABLISHED;
17161da177e4SLinus Torvalds break;
17171da177e4SLinus Torvalds
17181da177e4SLinus Torvalds /* Misc */
17191da177e4SLinus Torvalds case DVD_INVALIDATE_AGID:
17201da177e4SLinus Torvalds cgc.quiet = 1;
17215944b2ceSJoe Perches cd_dbg(CD_DVD, "entering DVD_INVALIDATE_AGID\n");
17221da177e4SLinus Torvalds setup_report_key(&cgc, ai->lsa.agid, 0x3f);
17231da177e4SLinus Torvalds if ((ret = cdo->generic_packet(cdi, &cgc)))
1724fce53847SFUJITA Tomonori return ret;
17251da177e4SLinus Torvalds break;
17261da177e4SLinus Torvalds
17271da177e4SLinus Torvalds /* Get region settings */
17281da177e4SLinus Torvalds case DVD_LU_SEND_RPC_STATE:
17295944b2ceSJoe Perches cd_dbg(CD_DVD, "entering DVD_LU_SEND_RPC_STATE\n");
17301da177e4SLinus Torvalds setup_report_key(&cgc, 0, 8);
1731fce53847SFUJITA Tomonori memset(&rpc_state, 0, sizeof(rpc_state_t));
1732fce53847SFUJITA Tomonori cgc.buffer = (char *) &rpc_state;
17331da177e4SLinus Torvalds
17341da177e4SLinus Torvalds if ((ret = cdo->generic_packet(cdi, &cgc)))
1735fce53847SFUJITA Tomonori return ret;
17361da177e4SLinus Torvalds
1737fce53847SFUJITA Tomonori ai->lrpcs.type = rpc_state.type_code;
1738fce53847SFUJITA Tomonori ai->lrpcs.vra = rpc_state.vra;
1739fce53847SFUJITA Tomonori ai->lrpcs.ucca = rpc_state.ucca;
1740fce53847SFUJITA Tomonori ai->lrpcs.region_mask = rpc_state.region_mask;
1741fce53847SFUJITA Tomonori ai->lrpcs.rpc_scheme = rpc_state.rpc_scheme;
17421da177e4SLinus Torvalds break;
17431da177e4SLinus Torvalds
17441da177e4SLinus Torvalds /* Set region settings */
17451da177e4SLinus Torvalds case DVD_HOST_SEND_RPC_STATE:
17465944b2ceSJoe Perches cd_dbg(CD_DVD, "entering DVD_HOST_SEND_RPC_STATE\n");
17471da177e4SLinus Torvalds setup_send_key(&cgc, 0, 6);
17481da177e4SLinus Torvalds buf[1] = 6;
17491da177e4SLinus Torvalds buf[4] = ai->hrpcs.pdrc;
17501da177e4SLinus Torvalds
17511da177e4SLinus Torvalds if ((ret = cdo->generic_packet(cdi, &cgc)))
1752fce53847SFUJITA Tomonori return ret;
17531da177e4SLinus Torvalds break;
17541da177e4SLinus Torvalds
17551da177e4SLinus Torvalds default:
17565944b2ceSJoe Perches cd_dbg(CD_WARNING, "Invalid DVD key ioctl (%d)\n", ai->type);
1757fce53847SFUJITA Tomonori return -ENOTTY;
17581da177e4SLinus Torvalds }
1759fce53847SFUJITA Tomonori
1760fce53847SFUJITA Tomonori return 0;
17611da177e4SLinus Torvalds }
17621da177e4SLinus Torvalds
dvd_read_physical(struct cdrom_device_info * cdi,dvd_struct * s,struct packet_command * cgc)1763d194139cSMarcin Slusarz static int dvd_read_physical(struct cdrom_device_info *cdi, dvd_struct *s,
1764d194139cSMarcin Slusarz struct packet_command *cgc)
17651da177e4SLinus Torvalds {
1766fce53847SFUJITA Tomonori unsigned char buf[21], *base;
17671da177e4SLinus Torvalds struct dvd_layer *layer;
1768853fe1bfSKees Cook const struct cdrom_device_ops *cdo = cdi->ops;
17691da177e4SLinus Torvalds int ret, layer_num = s->physical.layer_num;
17701da177e4SLinus Torvalds
17711da177e4SLinus Torvalds if (layer_num >= DVD_LAYERS)
17721da177e4SLinus Torvalds return -EINVAL;
17731da177e4SLinus Torvalds
1774d194139cSMarcin Slusarz init_cdrom_command(cgc, buf, sizeof(buf), CGC_DATA_READ);
1775d194139cSMarcin Slusarz cgc->cmd[0] = GPCMD_READ_DVD_STRUCTURE;
1776d194139cSMarcin Slusarz cgc->cmd[6] = layer_num;
1777d194139cSMarcin Slusarz cgc->cmd[7] = s->type;
1778d194139cSMarcin Slusarz cgc->cmd[9] = cgc->buflen & 0xff;
17791da177e4SLinus Torvalds
17801da177e4SLinus Torvalds /*
17811da177e4SLinus Torvalds * refrain from reporting errors on non-existing layers (mainly)
17821da177e4SLinus Torvalds */
1783d194139cSMarcin Slusarz cgc->quiet = 1;
17841da177e4SLinus Torvalds
1785d194139cSMarcin Slusarz ret = cdo->generic_packet(cdi, cgc);
1786d194139cSMarcin Slusarz if (ret)
1787fce53847SFUJITA Tomonori return ret;
17881da177e4SLinus Torvalds
17891da177e4SLinus Torvalds base = &buf[4];
17901da177e4SLinus Torvalds layer = &s->physical.layer[layer_num];
17911da177e4SLinus Torvalds
17921da177e4SLinus Torvalds /*
17931da177e4SLinus Torvalds * place the data... really ugly, but at least we won't have to
17941da177e4SLinus Torvalds * worry about endianess in userspace.
17951da177e4SLinus Torvalds */
17961da177e4SLinus Torvalds memset(layer, 0, sizeof(*layer));
17971da177e4SLinus Torvalds layer->book_version = base[0] & 0xf;
17981da177e4SLinus Torvalds layer->book_type = base[0] >> 4;
17991da177e4SLinus Torvalds layer->min_rate = base[1] & 0xf;
18001da177e4SLinus Torvalds layer->disc_size = base[1] >> 4;
18011da177e4SLinus Torvalds layer->layer_type = base[2] & 0xf;
18021da177e4SLinus Torvalds layer->track_path = (base[2] >> 4) & 1;
18031da177e4SLinus Torvalds layer->nlayers = (base[2] >> 5) & 3;
18041da177e4SLinus Torvalds layer->track_density = base[3] & 0xf;
18051da177e4SLinus Torvalds layer->linear_density = base[3] >> 4;
18061da177e4SLinus Torvalds layer->start_sector = base[5] << 16 | base[6] << 8 | base[7];
18071da177e4SLinus Torvalds layer->end_sector = base[9] << 16 | base[10] << 8 | base[11];
18081da177e4SLinus Torvalds layer->end_sector_l0 = base[13] << 16 | base[14] << 8 | base[15];
18091da177e4SLinus Torvalds layer->bca = base[16] >> 7;
18101da177e4SLinus Torvalds
1811fce53847SFUJITA Tomonori return 0;
18121da177e4SLinus Torvalds }
18131da177e4SLinus Torvalds
dvd_read_copyright(struct cdrom_device_info * cdi,dvd_struct * s,struct packet_command * cgc)1814d194139cSMarcin Slusarz static int dvd_read_copyright(struct cdrom_device_info *cdi, dvd_struct *s,
1815d194139cSMarcin Slusarz struct packet_command *cgc)
18161da177e4SLinus Torvalds {
18171da177e4SLinus Torvalds int ret;
1818fce53847SFUJITA Tomonori u_char buf[8];
1819853fe1bfSKees Cook const struct cdrom_device_ops *cdo = cdi->ops;
18201da177e4SLinus Torvalds
1821d194139cSMarcin Slusarz init_cdrom_command(cgc, buf, sizeof(buf), CGC_DATA_READ);
1822d194139cSMarcin Slusarz cgc->cmd[0] = GPCMD_READ_DVD_STRUCTURE;
1823d194139cSMarcin Slusarz cgc->cmd[6] = s->copyright.layer_num;
1824d194139cSMarcin Slusarz cgc->cmd[7] = s->type;
1825d194139cSMarcin Slusarz cgc->cmd[8] = cgc->buflen >> 8;
1826d194139cSMarcin Slusarz cgc->cmd[9] = cgc->buflen & 0xff;
18271da177e4SLinus Torvalds
1828d194139cSMarcin Slusarz ret = cdo->generic_packet(cdi, cgc);
1829d194139cSMarcin Slusarz if (ret)
1830fce53847SFUJITA Tomonori return ret;
18311da177e4SLinus Torvalds
18321da177e4SLinus Torvalds s->copyright.cpst = buf[4];
18331da177e4SLinus Torvalds s->copyright.rmi = buf[5];
18341da177e4SLinus Torvalds
1835fce53847SFUJITA Tomonori return 0;
18361da177e4SLinus Torvalds }
18371da177e4SLinus Torvalds
dvd_read_disckey(struct cdrom_device_info * cdi,dvd_struct * s,struct packet_command * cgc)1838d194139cSMarcin Slusarz static int dvd_read_disckey(struct cdrom_device_info *cdi, dvd_struct *s,
1839d194139cSMarcin Slusarz struct packet_command *cgc)
18401da177e4SLinus Torvalds {
18411da177e4SLinus Torvalds int ret, size;
18421da177e4SLinus Torvalds u_char *buf;
1843853fe1bfSKees Cook const struct cdrom_device_ops *cdo = cdi->ops;
18441da177e4SLinus Torvalds
18451da177e4SLinus Torvalds size = sizeof(s->disckey.value) + 4;
18461da177e4SLinus Torvalds
1847d194139cSMarcin Slusarz buf = kmalloc(size, GFP_KERNEL);
1848d194139cSMarcin Slusarz if (!buf)
18491da177e4SLinus Torvalds return -ENOMEM;
18501da177e4SLinus Torvalds
1851d194139cSMarcin Slusarz init_cdrom_command(cgc, buf, size, CGC_DATA_READ);
1852d194139cSMarcin Slusarz cgc->cmd[0] = GPCMD_READ_DVD_STRUCTURE;
1853d194139cSMarcin Slusarz cgc->cmd[7] = s->type;
1854d194139cSMarcin Slusarz cgc->cmd[8] = size >> 8;
1855d194139cSMarcin Slusarz cgc->cmd[9] = size & 0xff;
1856d194139cSMarcin Slusarz cgc->cmd[10] = s->disckey.agid << 6;
18571da177e4SLinus Torvalds
1858d194139cSMarcin Slusarz ret = cdo->generic_packet(cdi, cgc);
1859d194139cSMarcin Slusarz if (!ret)
18601da177e4SLinus Torvalds memcpy(s->disckey.value, &buf[4], sizeof(s->disckey.value));
18611da177e4SLinus Torvalds
18621da177e4SLinus Torvalds kfree(buf);
18631da177e4SLinus Torvalds return ret;
18641da177e4SLinus Torvalds }
18651da177e4SLinus Torvalds
dvd_read_bca(struct cdrom_device_info * cdi,dvd_struct * s,struct packet_command * cgc)1866d194139cSMarcin Slusarz static int dvd_read_bca(struct cdrom_device_info *cdi, dvd_struct *s,
1867d194139cSMarcin Slusarz struct packet_command *cgc)
18681da177e4SLinus Torvalds {
1869d194139cSMarcin Slusarz int ret, size = 4 + 188;
1870d194139cSMarcin Slusarz u_char *buf;
1871853fe1bfSKees Cook const struct cdrom_device_ops *cdo = cdi->ops;
18721da177e4SLinus Torvalds
1873d194139cSMarcin Slusarz buf = kmalloc(size, GFP_KERNEL);
1874d194139cSMarcin Slusarz if (!buf)
1875d194139cSMarcin Slusarz return -ENOMEM;
18761da177e4SLinus Torvalds
1877d194139cSMarcin Slusarz init_cdrom_command(cgc, buf, size, CGC_DATA_READ);
1878d194139cSMarcin Slusarz cgc->cmd[0] = GPCMD_READ_DVD_STRUCTURE;
1879d194139cSMarcin Slusarz cgc->cmd[7] = s->type;
1880d194139cSMarcin Slusarz cgc->cmd[9] = cgc->buflen & 0xff;
1881d194139cSMarcin Slusarz
1882d194139cSMarcin Slusarz ret = cdo->generic_packet(cdi, cgc);
1883d194139cSMarcin Slusarz if (ret)
1884d194139cSMarcin Slusarz goto out;
18851da177e4SLinus Torvalds
18861da177e4SLinus Torvalds s->bca.len = buf[0] << 8 | buf[1];
18871da177e4SLinus Torvalds if (s->bca.len < 12 || s->bca.len > 188) {
18885944b2ceSJoe Perches cd_dbg(CD_WARNING, "Received invalid BCA length (%d)\n",
18895944b2ceSJoe Perches s->bca.len);
1890d194139cSMarcin Slusarz ret = -EIO;
1891d194139cSMarcin Slusarz goto out;
18921da177e4SLinus Torvalds }
18931da177e4SLinus Torvalds memcpy(s->bca.value, &buf[4], s->bca.len);
1894d194139cSMarcin Slusarz ret = 0;
1895d194139cSMarcin Slusarz out:
1896d194139cSMarcin Slusarz kfree(buf);
1897d194139cSMarcin Slusarz return ret;
18981da177e4SLinus Torvalds }
18991da177e4SLinus Torvalds
dvd_read_manufact(struct cdrom_device_info * cdi,dvd_struct * s,struct packet_command * cgc)1900d194139cSMarcin Slusarz static int dvd_read_manufact(struct cdrom_device_info *cdi, dvd_struct *s,
1901d194139cSMarcin Slusarz struct packet_command *cgc)
19021da177e4SLinus Torvalds {
19031da177e4SLinus Torvalds int ret = 0, size;
19041da177e4SLinus Torvalds u_char *buf;
1905853fe1bfSKees Cook const struct cdrom_device_ops *cdo = cdi->ops;
19061da177e4SLinus Torvalds
19071da177e4SLinus Torvalds size = sizeof(s->manufact.value) + 4;
19081da177e4SLinus Torvalds
1909d194139cSMarcin Slusarz buf = kmalloc(size, GFP_KERNEL);
1910d194139cSMarcin Slusarz if (!buf)
19111da177e4SLinus Torvalds return -ENOMEM;
19121da177e4SLinus Torvalds
1913d194139cSMarcin Slusarz init_cdrom_command(cgc, buf, size, CGC_DATA_READ);
1914d194139cSMarcin Slusarz cgc->cmd[0] = GPCMD_READ_DVD_STRUCTURE;
1915d194139cSMarcin Slusarz cgc->cmd[7] = s->type;
1916d194139cSMarcin Slusarz cgc->cmd[8] = size >> 8;
1917d194139cSMarcin Slusarz cgc->cmd[9] = size & 0xff;
19181da177e4SLinus Torvalds
1919d194139cSMarcin Slusarz ret = cdo->generic_packet(cdi, cgc);
1920d194139cSMarcin Slusarz if (ret)
1921d194139cSMarcin Slusarz goto out;
19221da177e4SLinus Torvalds
19231da177e4SLinus Torvalds s->manufact.len = buf[0] << 8 | buf[1];
1924aec9f377SAndrew Morton if (s->manufact.len < 0) {
19255944b2ceSJoe Perches cd_dbg(CD_WARNING, "Received invalid manufacture info length (%d)\n",
19265944b2ceSJoe Perches s->manufact.len);
19271da177e4SLinus Torvalds ret = -EIO;
19281da177e4SLinus Torvalds } else {
1929aec9f377SAndrew Morton if (s->manufact.len > 2048) {
19305944b2ceSJoe Perches cd_dbg(CD_WARNING, "Received invalid manufacture info length (%d): truncating to 2048\n",
1931aec9f377SAndrew Morton s->manufact.len);
1932aec9f377SAndrew Morton s->manufact.len = 2048;
1933aec9f377SAndrew Morton }
19341da177e4SLinus Torvalds memcpy(s->manufact.value, &buf[4], s->manufact.len);
19351da177e4SLinus Torvalds }
19361da177e4SLinus Torvalds
1937d194139cSMarcin Slusarz out:
19381da177e4SLinus Torvalds kfree(buf);
19391da177e4SLinus Torvalds return ret;
19401da177e4SLinus Torvalds }
19411da177e4SLinus Torvalds
dvd_read_struct(struct cdrom_device_info * cdi,dvd_struct * s,struct packet_command * cgc)1942d194139cSMarcin Slusarz static int dvd_read_struct(struct cdrom_device_info *cdi, dvd_struct *s,
1943d194139cSMarcin Slusarz struct packet_command *cgc)
19441da177e4SLinus Torvalds {
19451da177e4SLinus Torvalds switch (s->type) {
19461da177e4SLinus Torvalds case DVD_STRUCT_PHYSICAL:
1947d194139cSMarcin Slusarz return dvd_read_physical(cdi, s, cgc);
19481da177e4SLinus Torvalds
19491da177e4SLinus Torvalds case DVD_STRUCT_COPYRIGHT:
1950d194139cSMarcin Slusarz return dvd_read_copyright(cdi, s, cgc);
19511da177e4SLinus Torvalds
19521da177e4SLinus Torvalds case DVD_STRUCT_DISCKEY:
1953d194139cSMarcin Slusarz return dvd_read_disckey(cdi, s, cgc);
19541da177e4SLinus Torvalds
19551da177e4SLinus Torvalds case DVD_STRUCT_BCA:
1956d194139cSMarcin Slusarz return dvd_read_bca(cdi, s, cgc);
19571da177e4SLinus Torvalds
19581da177e4SLinus Torvalds case DVD_STRUCT_MANUFACT:
1959d194139cSMarcin Slusarz return dvd_read_manufact(cdi, s, cgc);
19601da177e4SLinus Torvalds
19611da177e4SLinus Torvalds default:
19625944b2ceSJoe Perches cd_dbg(CD_WARNING, ": Invalid DVD structure read requested (%d)\n",
19631da177e4SLinus Torvalds s->type);
19641da177e4SLinus Torvalds return -EINVAL;
19651da177e4SLinus Torvalds }
19661da177e4SLinus Torvalds }
19671da177e4SLinus Torvalds
cdrom_mode_sense(struct cdrom_device_info * cdi,struct packet_command * cgc,int page_code,int page_control)19681da177e4SLinus Torvalds int cdrom_mode_sense(struct cdrom_device_info *cdi,
19691da177e4SLinus Torvalds struct packet_command *cgc,
19701da177e4SLinus Torvalds int page_code, int page_control)
19711da177e4SLinus Torvalds {
1972853fe1bfSKees Cook const struct cdrom_device_ops *cdo = cdi->ops;
19731da177e4SLinus Torvalds
19741da177e4SLinus Torvalds memset(cgc->cmd, 0, sizeof(cgc->cmd));
19751da177e4SLinus Torvalds
19761da177e4SLinus Torvalds cgc->cmd[0] = GPCMD_MODE_SENSE_10;
19771da177e4SLinus Torvalds cgc->cmd[2] = page_code | (page_control << 6);
19781da177e4SLinus Torvalds cgc->cmd[7] = cgc->buflen >> 8;
19791da177e4SLinus Torvalds cgc->cmd[8] = cgc->buflen & 0xff;
19801da177e4SLinus Torvalds cgc->data_direction = CGC_DATA_READ;
19811da177e4SLinus Torvalds return cdo->generic_packet(cdi, cgc);
19821da177e4SLinus Torvalds }
1983eeef7565SEnze Li EXPORT_SYMBOL(cdrom_mode_sense);
19841da177e4SLinus Torvalds
cdrom_mode_select(struct cdrom_device_info * cdi,struct packet_command * cgc)19851da177e4SLinus Torvalds int cdrom_mode_select(struct cdrom_device_info *cdi,
19861da177e4SLinus Torvalds struct packet_command *cgc)
19871da177e4SLinus Torvalds {
1988853fe1bfSKees Cook const struct cdrom_device_ops *cdo = cdi->ops;
19891da177e4SLinus Torvalds
19901da177e4SLinus Torvalds memset(cgc->cmd, 0, sizeof(cgc->cmd));
19911da177e4SLinus Torvalds memset(cgc->buffer, 0, 2);
19921da177e4SLinus Torvalds cgc->cmd[0] = GPCMD_MODE_SELECT_10;
19931da177e4SLinus Torvalds cgc->cmd[1] = 0x10; /* PF */
19941da177e4SLinus Torvalds cgc->cmd[7] = cgc->buflen >> 8;
19951da177e4SLinus Torvalds cgc->cmd[8] = cgc->buflen & 0xff;
19961da177e4SLinus Torvalds cgc->data_direction = CGC_DATA_WRITE;
19971da177e4SLinus Torvalds return cdo->generic_packet(cdi, cgc);
19981da177e4SLinus Torvalds }
1999eeef7565SEnze Li EXPORT_SYMBOL(cdrom_mode_select);
20001da177e4SLinus Torvalds
cdrom_read_subchannel(struct cdrom_device_info * cdi,struct cdrom_subchnl * subchnl,int mcn)20011da177e4SLinus Torvalds static int cdrom_read_subchannel(struct cdrom_device_info *cdi,
20021da177e4SLinus Torvalds struct cdrom_subchnl *subchnl, int mcn)
20031da177e4SLinus Torvalds {
2004853fe1bfSKees Cook const struct cdrom_device_ops *cdo = cdi->ops;
20051da177e4SLinus Torvalds struct packet_command cgc;
2006fce53847SFUJITA Tomonori char buffer[32];
20071da177e4SLinus Torvalds int ret;
20081da177e4SLinus Torvalds
20091da177e4SLinus Torvalds init_cdrom_command(&cgc, buffer, 16, CGC_DATA_READ);
20101da177e4SLinus Torvalds cgc.cmd[0] = GPCMD_READ_SUBCHANNEL;
2011bf294e83Svchannaiah cgc.cmd[1] = subchnl->cdsc_format;/* MSF or LBA addressing */
20121da177e4SLinus Torvalds cgc.cmd[2] = 0x40; /* request subQ data */
20131da177e4SLinus Torvalds cgc.cmd[3] = mcn ? 2 : 1;
20141da177e4SLinus Torvalds cgc.cmd[8] = 16;
20151da177e4SLinus Torvalds
20161da177e4SLinus Torvalds if ((ret = cdo->generic_packet(cdi, &cgc)))
2017fce53847SFUJITA Tomonori return ret;
20181da177e4SLinus Torvalds
20191da177e4SLinus Torvalds subchnl->cdsc_audiostatus = cgc.buffer[1];
20201da177e4SLinus Torvalds subchnl->cdsc_ctrl = cgc.buffer[5] & 0xf;
20211da177e4SLinus Torvalds subchnl->cdsc_trk = cgc.buffer[6];
20221da177e4SLinus Torvalds subchnl->cdsc_ind = cgc.buffer[7];
20231da177e4SLinus Torvalds
2024bf294e83Svchannaiah if (subchnl->cdsc_format == CDROM_LBA) {
2025bf294e83Svchannaiah subchnl->cdsc_absaddr.lba = ((cgc.buffer[8] << 24) |
2026bf294e83Svchannaiah (cgc.buffer[9] << 16) |
2027bf294e83Svchannaiah (cgc.buffer[10] << 8) |
2028bf294e83Svchannaiah (cgc.buffer[11]));
2029bf294e83Svchannaiah subchnl->cdsc_reladdr.lba = ((cgc.buffer[12] << 24) |
2030bf294e83Svchannaiah (cgc.buffer[13] << 16) |
2031bf294e83Svchannaiah (cgc.buffer[14] << 8) |
2032bf294e83Svchannaiah (cgc.buffer[15]));
2033bf294e83Svchannaiah } else {
20341da177e4SLinus Torvalds subchnl->cdsc_reladdr.msf.minute = cgc.buffer[13];
20351da177e4SLinus Torvalds subchnl->cdsc_reladdr.msf.second = cgc.buffer[14];
20361da177e4SLinus Torvalds subchnl->cdsc_reladdr.msf.frame = cgc.buffer[15];
20371da177e4SLinus Torvalds subchnl->cdsc_absaddr.msf.minute = cgc.buffer[9];
20381da177e4SLinus Torvalds subchnl->cdsc_absaddr.msf.second = cgc.buffer[10];
20391da177e4SLinus Torvalds subchnl->cdsc_absaddr.msf.frame = cgc.buffer[11];
2040bf294e83Svchannaiah }
20411da177e4SLinus Torvalds
2042fce53847SFUJITA Tomonori return 0;
20431da177e4SLinus Torvalds }
20441da177e4SLinus Torvalds
20451da177e4SLinus Torvalds /*
20461da177e4SLinus Torvalds * Specific READ_10 interface
20471da177e4SLinus Torvalds */
cdrom_read_cd(struct cdrom_device_info * cdi,struct packet_command * cgc,int lba,int blocksize,int nblocks)20481da177e4SLinus Torvalds static int cdrom_read_cd(struct cdrom_device_info *cdi,
20491da177e4SLinus Torvalds struct packet_command *cgc, int lba,
20501da177e4SLinus Torvalds int blocksize, int nblocks)
20511da177e4SLinus Torvalds {
2052853fe1bfSKees Cook const struct cdrom_device_ops *cdo = cdi->ops;
20531da177e4SLinus Torvalds
20541da177e4SLinus Torvalds memset(&cgc->cmd, 0, sizeof(cgc->cmd));
20551da177e4SLinus Torvalds cgc->cmd[0] = GPCMD_READ_10;
20561da177e4SLinus Torvalds cgc->cmd[2] = (lba >> 24) & 0xff;
20571da177e4SLinus Torvalds cgc->cmd[3] = (lba >> 16) & 0xff;
20581da177e4SLinus Torvalds cgc->cmd[4] = (lba >> 8) & 0xff;
20591da177e4SLinus Torvalds cgc->cmd[5] = lba & 0xff;
20601da177e4SLinus Torvalds cgc->cmd[6] = (nblocks >> 16) & 0xff;
20611da177e4SLinus Torvalds cgc->cmd[7] = (nblocks >> 8) & 0xff;
20621da177e4SLinus Torvalds cgc->cmd[8] = nblocks & 0xff;
20631da177e4SLinus Torvalds cgc->buflen = blocksize * nblocks;
20641da177e4SLinus Torvalds return cdo->generic_packet(cdi, cgc);
20651da177e4SLinus Torvalds }
20661da177e4SLinus Torvalds
20671da177e4SLinus Torvalds /* very generic interface for reading the various types of blocks */
cdrom_read_block(struct cdrom_device_info * cdi,struct packet_command * cgc,int lba,int nblocks,int format,int blksize)20681da177e4SLinus Torvalds static int cdrom_read_block(struct cdrom_device_info *cdi,
20691da177e4SLinus Torvalds struct packet_command *cgc,
20701da177e4SLinus Torvalds int lba, int nblocks, int format, int blksize)
20711da177e4SLinus Torvalds {
2072853fe1bfSKees Cook const struct cdrom_device_ops *cdo = cdi->ops;
20731da177e4SLinus Torvalds
20741da177e4SLinus Torvalds memset(&cgc->cmd, 0, sizeof(cgc->cmd));
20751da177e4SLinus Torvalds cgc->cmd[0] = GPCMD_READ_CD;
20761da177e4SLinus Torvalds /* expected sector size - cdda,mode1,etc. */
20771da177e4SLinus Torvalds cgc->cmd[1] = format << 2;
20781da177e4SLinus Torvalds /* starting address */
20791da177e4SLinus Torvalds cgc->cmd[2] = (lba >> 24) & 0xff;
20801da177e4SLinus Torvalds cgc->cmd[3] = (lba >> 16) & 0xff;
20811da177e4SLinus Torvalds cgc->cmd[4] = (lba >> 8) & 0xff;
20821da177e4SLinus Torvalds cgc->cmd[5] = lba & 0xff;
20831da177e4SLinus Torvalds /* number of blocks */
20841da177e4SLinus Torvalds cgc->cmd[6] = (nblocks >> 16) & 0xff;
20851da177e4SLinus Torvalds cgc->cmd[7] = (nblocks >> 8) & 0xff;
20861da177e4SLinus Torvalds cgc->cmd[8] = nblocks & 0xff;
20871da177e4SLinus Torvalds cgc->buflen = blksize * nblocks;
20881da177e4SLinus Torvalds
20891da177e4SLinus Torvalds /* set the header info returned */
20901da177e4SLinus Torvalds switch (blksize) {
20911da177e4SLinus Torvalds case CD_FRAMESIZE_RAW0 : cgc->cmd[9] = 0x58; break;
20921da177e4SLinus Torvalds case CD_FRAMESIZE_RAW1 : cgc->cmd[9] = 0x78; break;
20931da177e4SLinus Torvalds case CD_FRAMESIZE_RAW : cgc->cmd[9] = 0xf8; break;
20941da177e4SLinus Torvalds default : cgc->cmd[9] = 0x10;
20951da177e4SLinus Torvalds }
20961da177e4SLinus Torvalds
20971da177e4SLinus Torvalds return cdo->generic_packet(cdi, cgc);
20981da177e4SLinus Torvalds }
20991da177e4SLinus Torvalds
cdrom_read_cdda_old(struct cdrom_device_info * cdi,__u8 __user * ubuf,int lba,int nframes)21001da177e4SLinus Torvalds static int cdrom_read_cdda_old(struct cdrom_device_info *cdi, __u8 __user *ubuf,
21011da177e4SLinus Torvalds int lba, int nframes)
21021da177e4SLinus Torvalds {
21031da177e4SLinus Torvalds struct packet_command cgc;
21041da177e4SLinus Torvalds int ret = 0;
21051da177e4SLinus Torvalds int nr;
21061da177e4SLinus Torvalds
21071da177e4SLinus Torvalds cdi->last_sense = 0;
21081da177e4SLinus Torvalds
21091da177e4SLinus Torvalds memset(&cgc, 0, sizeof(cgc));
21101da177e4SLinus Torvalds
21111da177e4SLinus Torvalds /*
21121da177e4SLinus Torvalds * start with will ra.nframes size, back down if alloc fails
21131da177e4SLinus Torvalds */
21141da177e4SLinus Torvalds nr = nframes;
21151da177e4SLinus Torvalds do {
21166da2ec56SKees Cook cgc.buffer = kmalloc_array(nr, CD_FRAMESIZE_RAW, GFP_KERNEL);
21171da177e4SLinus Torvalds if (cgc.buffer)
21181da177e4SLinus Torvalds break;
21191da177e4SLinus Torvalds
21201da177e4SLinus Torvalds nr >>= 1;
21211da177e4SLinus Torvalds } while (nr);
21221da177e4SLinus Torvalds
21231da177e4SLinus Torvalds if (!nr)
21241da177e4SLinus Torvalds return -ENOMEM;
21251da177e4SLinus Torvalds
21261da177e4SLinus Torvalds cgc.data_direction = CGC_DATA_READ;
21271da177e4SLinus Torvalds while (nframes > 0) {
21281da177e4SLinus Torvalds if (nr > nframes)
21291da177e4SLinus Torvalds nr = nframes;
21301da177e4SLinus Torvalds
21311da177e4SLinus Torvalds ret = cdrom_read_block(cdi, &cgc, lba, nr, 1, CD_FRAMESIZE_RAW);
21321da177e4SLinus Torvalds if (ret)
21331da177e4SLinus Torvalds break;
2134822bfa51SDan Carpenter if (copy_to_user(ubuf, cgc.buffer, CD_FRAMESIZE_RAW * nr)) {
21351da177e4SLinus Torvalds ret = -EFAULT;
21361da177e4SLinus Torvalds break;
21371da177e4SLinus Torvalds }
21381da177e4SLinus Torvalds ubuf += CD_FRAMESIZE_RAW * nr;
21391da177e4SLinus Torvalds nframes -= nr;
21401da177e4SLinus Torvalds lba += nr;
21411da177e4SLinus Torvalds }
21421da177e4SLinus Torvalds kfree(cgc.buffer);
21431da177e4SLinus Torvalds return ret;
21441da177e4SLinus Torvalds }
21451da177e4SLinus Torvalds
cdrom_read_cdda_bpc(struct cdrom_device_info * cdi,__u8 __user * ubuf,int lba,int nframes)21461da177e4SLinus Torvalds static int cdrom_read_cdda_bpc(struct cdrom_device_info *cdi, __u8 __user *ubuf,
21471da177e4SLinus Torvalds int lba, int nframes)
21481da177e4SLinus Torvalds {
2149ba51bdafSChristoph Hellwig int max_frames = (queue_max_sectors(cdi->disk->queue) << 9) /
2150ba51bdafSChristoph Hellwig CD_FRAMESIZE_RAW;
21511da177e4SLinus Torvalds int nr, ret = 0;
21521da177e4SLinus Torvalds
21531da177e4SLinus Torvalds cdi->last_sense = 0;
21541da177e4SLinus Torvalds
21551da177e4SLinus Torvalds while (nframes) {
21561da177e4SLinus Torvalds if (cdi->cdda_method == CDDA_BPC_SINGLE)
21571da177e4SLinus Torvalds nr = 1;
2158ba51bdafSChristoph Hellwig else
2159ba51bdafSChristoph Hellwig nr = min(nframes, max_frames);
21601da177e4SLinus Torvalds
2161ba51bdafSChristoph Hellwig ret = cdi->ops->read_cdda_bpc(cdi, ubuf, lba, nr,
2162ba51bdafSChristoph Hellwig &cdi->last_sense);
21631da177e4SLinus Torvalds if (ret)
21641da177e4SLinus Torvalds break;
21651da177e4SLinus Torvalds
21661da177e4SLinus Torvalds nframes -= nr;
21671da177e4SLinus Torvalds lba += nr;
2168ba51bdafSChristoph Hellwig ubuf += (nr * CD_FRAMESIZE_RAW);
21691da177e4SLinus Torvalds }
21701da177e4SLinus Torvalds
21711da177e4SLinus Torvalds return ret;
21721da177e4SLinus Torvalds }
21731da177e4SLinus Torvalds
cdrom_read_cdda(struct cdrom_device_info * cdi,__u8 __user * ubuf,int lba,int nframes)21741da177e4SLinus Torvalds static int cdrom_read_cdda(struct cdrom_device_info *cdi, __u8 __user *ubuf,
21751da177e4SLinus Torvalds int lba, int nframes)
21761da177e4SLinus Torvalds {
21771da177e4SLinus Torvalds int ret;
21781da177e4SLinus Torvalds
21791da177e4SLinus Torvalds if (cdi->cdda_method == CDDA_OLD)
21801da177e4SLinus Torvalds return cdrom_read_cdda_old(cdi, ubuf, lba, nframes);
21811da177e4SLinus Torvalds
21821da177e4SLinus Torvalds retry:
21831da177e4SLinus Torvalds /*
21841da177e4SLinus Torvalds * for anything else than success and io error, we need to retry
21851da177e4SLinus Torvalds */
21861da177e4SLinus Torvalds ret = cdrom_read_cdda_bpc(cdi, ubuf, lba, nframes);
21871da177e4SLinus Torvalds if (!ret || ret != -EIO)
21881da177e4SLinus Torvalds return ret;
21891da177e4SLinus Torvalds
21901da177e4SLinus Torvalds /*
21911da177e4SLinus Torvalds * I've seen drives get sense 4/8/3 udma crc errors on multi
21921da177e4SLinus Torvalds * frame dma, so drop to single frame dma if we need to
21931da177e4SLinus Torvalds */
21941da177e4SLinus Torvalds if (cdi->cdda_method == CDDA_BPC_FULL && nframes > 1) {
2195e597cd09SJoe Perches pr_info("dropping to single frame dma\n");
21961da177e4SLinus Torvalds cdi->cdda_method = CDDA_BPC_SINGLE;
21971da177e4SLinus Torvalds goto retry;
21981da177e4SLinus Torvalds }
21991da177e4SLinus Torvalds
22001da177e4SLinus Torvalds /*
22011da177e4SLinus Torvalds * so we have an io error of some sort with multi frame dma. if the
22021da177e4SLinus Torvalds * condition wasn't a hardware error
22031da177e4SLinus Torvalds * problems, not for any error
22041da177e4SLinus Torvalds */
22051da177e4SLinus Torvalds if (cdi->last_sense != 0x04 && cdi->last_sense != 0x0b)
22061da177e4SLinus Torvalds return ret;
22071da177e4SLinus Torvalds
2208e597cd09SJoe Perches pr_info("dropping to old style cdda (sense=%x)\n", cdi->last_sense);
22091da177e4SLinus Torvalds cdi->cdda_method = CDDA_OLD;
22101da177e4SLinus Torvalds return cdrom_read_cdda_old(cdi, ubuf, lba, nframes);
22111da177e4SLinus Torvalds }
22121da177e4SLinus Torvalds
cdrom_multisession(struct cdrom_device_info * cdi,struct cdrom_multisession * info)2213eaf8e3e4SChristoph Hellwig int cdrom_multisession(struct cdrom_device_info *cdi,
2214eaf8e3e4SChristoph Hellwig struct cdrom_multisession *info)
22151da177e4SLinus Torvalds {
2216d2c5d4fcSChristoph Hellwig u8 requested_format;
22171da177e4SLinus Torvalds int ret;
22181da177e4SLinus Torvalds
2219d2c5d4fcSChristoph Hellwig if (!(cdi->ops->capability & CDC_MULTI_SESSION))
22201da177e4SLinus Torvalds return -ENOSYS;
2221d2c5d4fcSChristoph Hellwig
2222eaf8e3e4SChristoph Hellwig requested_format = info->addr_format;
2223d2c5d4fcSChristoph Hellwig if (requested_format != CDROM_MSF && requested_format != CDROM_LBA)
22241da177e4SLinus Torvalds return -EINVAL;
2225eaf8e3e4SChristoph Hellwig info->addr_format = CDROM_LBA;
2226d2c5d4fcSChristoph Hellwig
2227eaf8e3e4SChristoph Hellwig ret = cdi->ops->get_last_session(cdi, info);
2228eaf8e3e4SChristoph Hellwig if (!ret)
2229eaf8e3e4SChristoph Hellwig sanitize_format(&info->addr, &info->addr_format,
2230eaf8e3e4SChristoph Hellwig requested_format);
2231eaf8e3e4SChristoph Hellwig return ret;
2232eaf8e3e4SChristoph Hellwig }
2233eaf8e3e4SChristoph Hellwig EXPORT_SYMBOL_GPL(cdrom_multisession);
2234eaf8e3e4SChristoph Hellwig
cdrom_ioctl_multisession(struct cdrom_device_info * cdi,void __user * argp)2235eaf8e3e4SChristoph Hellwig static int cdrom_ioctl_multisession(struct cdrom_device_info *cdi,
2236eaf8e3e4SChristoph Hellwig void __user *argp)
2237eaf8e3e4SChristoph Hellwig {
2238eaf8e3e4SChristoph Hellwig struct cdrom_multisession info;
2239eaf8e3e4SChristoph Hellwig int ret;
2240eaf8e3e4SChristoph Hellwig
2241eaf8e3e4SChristoph Hellwig cd_dbg(CD_DO_IOCTL, "entering CDROMMULTISESSION\n");
2242eaf8e3e4SChristoph Hellwig
2243eaf8e3e4SChristoph Hellwig if (copy_from_user(&info, argp, sizeof(info)))
2244eaf8e3e4SChristoph Hellwig return -EFAULT;
2245eaf8e3e4SChristoph Hellwig ret = cdrom_multisession(cdi, &info);
2246d2c5d4fcSChristoph Hellwig if (ret)
22471da177e4SLinus Torvalds return ret;
2248eaf8e3e4SChristoph Hellwig if (copy_to_user(argp, &info, sizeof(info)))
2249d2c5d4fcSChristoph Hellwig return -EFAULT;
2250d2c5d4fcSChristoph Hellwig
22515944b2ceSJoe Perches cd_dbg(CD_DO_IOCTL, "CDROMMULTISESSION successful\n");
2252eaf8e3e4SChristoph Hellwig return ret;
22531da177e4SLinus Torvalds }
22541da177e4SLinus Torvalds
cdrom_ioctl_eject(struct cdrom_device_info * cdi)2255d2c5d4fcSChristoph Hellwig static int cdrom_ioctl_eject(struct cdrom_device_info *cdi)
2256d2c5d4fcSChristoph Hellwig {
22575944b2ceSJoe Perches cd_dbg(CD_DO_IOCTL, "entering CDROMEJECT\n");
2258d2c5d4fcSChristoph Hellwig
22591da177e4SLinus Torvalds if (!CDROM_CAN(CDC_OPEN_TRAY))
22601da177e4SLinus Torvalds return -ENOSYS;
2261cdccaa94SPaolo Bonzini if (cdi->use_count != 1 || cdi->keeplocked)
22621da177e4SLinus Torvalds return -EBUSY;
2263d2c5d4fcSChristoph Hellwig if (CDROM_CAN(CDC_LOCK)) {
2264d2c5d4fcSChristoph Hellwig int ret = cdi->ops->lock_door(cdi, 0);
2265d2c5d4fcSChristoph Hellwig if (ret)
22661da177e4SLinus Torvalds return ret;
22671da177e4SLinus Torvalds }
22681da177e4SLinus Torvalds
2269d2c5d4fcSChristoph Hellwig return cdi->ops->tray_move(cdi, 1);
2270d2c5d4fcSChristoph Hellwig }
2271d2c5d4fcSChristoph Hellwig
cdrom_ioctl_closetray(struct cdrom_device_info * cdi)2272d2c5d4fcSChristoph Hellwig static int cdrom_ioctl_closetray(struct cdrom_device_info *cdi)
2273d2c5d4fcSChristoph Hellwig {
22745944b2ceSJoe Perches cd_dbg(CD_DO_IOCTL, "entering CDROMCLOSETRAY\n");
2275d2c5d4fcSChristoph Hellwig
22761da177e4SLinus Torvalds if (!CDROM_CAN(CDC_CLOSE_TRAY))
22771da177e4SLinus Torvalds return -ENOSYS;
2278d2c5d4fcSChristoph Hellwig return cdi->ops->tray_move(cdi, 0);
22791da177e4SLinus Torvalds }
22801da177e4SLinus Torvalds
cdrom_ioctl_eject_sw(struct cdrom_device_info * cdi,unsigned long arg)2281d2c5d4fcSChristoph Hellwig static int cdrom_ioctl_eject_sw(struct cdrom_device_info *cdi,
2282d2c5d4fcSChristoph Hellwig unsigned long arg)
2283d2c5d4fcSChristoph Hellwig {
22845944b2ceSJoe Perches cd_dbg(CD_DO_IOCTL, "entering CDROMEJECT_SW\n");
2285d2c5d4fcSChristoph Hellwig
22861da177e4SLinus Torvalds if (!CDROM_CAN(CDC_OPEN_TRAY))
22871da177e4SLinus Torvalds return -ENOSYS;
2288cdccaa94SPaolo Bonzini if (cdi->keeplocked)
22891da177e4SLinus Torvalds return -EBUSY;
2290d2c5d4fcSChristoph Hellwig
22911da177e4SLinus Torvalds cdi->options &= ~(CDO_AUTO_CLOSE | CDO_AUTO_EJECT);
22921da177e4SLinus Torvalds if (arg)
22931da177e4SLinus Torvalds cdi->options |= CDO_AUTO_CLOSE | CDO_AUTO_EJECT;
22941da177e4SLinus Torvalds return 0;
22951da177e4SLinus Torvalds }
22961da177e4SLinus Torvalds
cdrom_ioctl_media_changed(struct cdrom_device_info * cdi,unsigned long arg)2297d2c5d4fcSChristoph Hellwig static int cdrom_ioctl_media_changed(struct cdrom_device_info *cdi,
2298d2c5d4fcSChristoph Hellwig unsigned long arg)
2299d2c5d4fcSChristoph Hellwig {
23001da177e4SLinus Torvalds struct cdrom_changer_info *info;
2301d2c5d4fcSChristoph Hellwig int ret;
23021da177e4SLinus Torvalds
23035944b2ceSJoe Perches cd_dbg(CD_DO_IOCTL, "entering CDROM_MEDIA_CHANGED\n");
2304d2c5d4fcSChristoph Hellwig
23051da177e4SLinus Torvalds if (!CDROM_CAN(CDC_MEDIA_CHANGED))
23061da177e4SLinus Torvalds return -ENOSYS;
23071da177e4SLinus Torvalds
23081da177e4SLinus Torvalds /* cannot select disc or select current disc */
23091da177e4SLinus Torvalds if (!CDROM_CAN(CDC_SELECT_DISC) || arg == CDSL_CURRENT)
23101da177e4SLinus Torvalds return media_changed(cdi, 1);
23111da177e4SLinus Torvalds
23129de4ee40SDan Carpenter if (arg >= cdi->capacity)
23131da177e4SLinus Torvalds return -EINVAL;
23141da177e4SLinus Torvalds
23158270cb10SJordy Zomer /* Prevent arg from speculatively bypassing the length check */
2316*cf2bb1bfSJosh Poimboeuf arg = array_index_nospec(arg, cdi->capacity);
23178270cb10SJordy Zomer
23181da177e4SLinus Torvalds info = kmalloc(sizeof(*info), GFP_KERNEL);
23191da177e4SLinus Torvalds if (!info)
23201da177e4SLinus Torvalds return -ENOMEM;
23211da177e4SLinus Torvalds
2322d2c5d4fcSChristoph Hellwig ret = cdrom_read_mech_status(cdi, info);
2323d2c5d4fcSChristoph Hellwig if (!ret)
2324d2c5d4fcSChristoph Hellwig ret = info->slots[arg].change;
23251da177e4SLinus Torvalds kfree(info);
23261da177e4SLinus Torvalds return ret;
23271da177e4SLinus Torvalds }
23281da177e4SLinus Torvalds
232967f1e027SLukas Prediger /*
233067f1e027SLukas Prediger * Media change detection with timing information.
233167f1e027SLukas Prediger *
233267f1e027SLukas Prediger * arg is a pointer to a cdrom_timed_media_change_info struct.
233367f1e027SLukas Prediger * arg->last_media_change may be set by calling code to signal
233467f1e027SLukas Prediger * the timestamp (in ms) of the last known media change (by the caller).
233567f1e027SLukas Prediger * Upon successful return, ioctl call will set arg->last_media_change
233667f1e027SLukas Prediger * to the latest media change timestamp known by the kernel/driver
233767f1e027SLukas Prediger * and set arg->has_changed to 1 if that timestamp is more recent
233867f1e027SLukas Prediger * than the timestamp set by the caller.
233967f1e027SLukas Prediger */
cdrom_ioctl_timed_media_change(struct cdrom_device_info * cdi,unsigned long arg)234067f1e027SLukas Prediger static int cdrom_ioctl_timed_media_change(struct cdrom_device_info *cdi,
234167f1e027SLukas Prediger unsigned long arg)
234267f1e027SLukas Prediger {
234367f1e027SLukas Prediger int ret;
234467f1e027SLukas Prediger struct cdrom_timed_media_change_info __user *info;
234567f1e027SLukas Prediger struct cdrom_timed_media_change_info tmp_info;
234667f1e027SLukas Prediger
234767f1e027SLukas Prediger if (!CDROM_CAN(CDC_MEDIA_CHANGED))
234867f1e027SLukas Prediger return -ENOSYS;
234967f1e027SLukas Prediger
235067f1e027SLukas Prediger info = (struct cdrom_timed_media_change_info __user *)arg;
235167f1e027SLukas Prediger cd_dbg(CD_DO_IOCTL, "entering CDROM_TIMED_MEDIA_CHANGE\n");
235267f1e027SLukas Prediger
235367f1e027SLukas Prediger ret = cdrom_ioctl_media_changed(cdi, CDSL_CURRENT);
235467f1e027SLukas Prediger if (ret < 0)
235567f1e027SLukas Prediger return ret;
235667f1e027SLukas Prediger
235767f1e027SLukas Prediger if (copy_from_user(&tmp_info, info, sizeof(tmp_info)) != 0)
235867f1e027SLukas Prediger return -EFAULT;
235967f1e027SLukas Prediger
236067f1e027SLukas Prediger tmp_info.media_flags = 0;
23613ee21e14SJustin Stitt if (cdi->last_media_change_ms > tmp_info.last_media_change)
236267f1e027SLukas Prediger tmp_info.media_flags |= MEDIA_CHANGED_FLAG;
236367f1e027SLukas Prediger
236467f1e027SLukas Prediger tmp_info.last_media_change = cdi->last_media_change_ms;
236567f1e027SLukas Prediger
236667f1e027SLukas Prediger if (copy_to_user(info, &tmp_info, sizeof(*info)) != 0)
236767f1e027SLukas Prediger return -EFAULT;
236867f1e027SLukas Prediger
236967f1e027SLukas Prediger return 0;
237067f1e027SLukas Prediger }
237167f1e027SLukas Prediger
cdrom_ioctl_set_options(struct cdrom_device_info * cdi,unsigned long arg)2372d2c5d4fcSChristoph Hellwig static int cdrom_ioctl_set_options(struct cdrom_device_info *cdi,
2373d2c5d4fcSChristoph Hellwig unsigned long arg)
2374d2c5d4fcSChristoph Hellwig {
23755944b2ceSJoe Perches cd_dbg(CD_DO_IOCTL, "entering CDROM_SET_OPTIONS\n");
2376d2c5d4fcSChristoph Hellwig
2377d2c5d4fcSChristoph Hellwig /*
2378d2c5d4fcSChristoph Hellwig * Options need to be in sync with capability.
2379d2c5d4fcSChristoph Hellwig * Too late for that, so we have to check each one separately.
2380d2c5d4fcSChristoph Hellwig */
23811da177e4SLinus Torvalds switch (arg) {
23821da177e4SLinus Torvalds case CDO_USE_FFLAGS:
23831da177e4SLinus Torvalds case CDO_CHECK_TYPE:
23841da177e4SLinus Torvalds break;
23851da177e4SLinus Torvalds case CDO_LOCK:
23861da177e4SLinus Torvalds if (!CDROM_CAN(CDC_LOCK))
23871da177e4SLinus Torvalds return -ENOSYS;
23881da177e4SLinus Torvalds break;
23891da177e4SLinus Torvalds case 0:
23901da177e4SLinus Torvalds return cdi->options;
23911da177e4SLinus Torvalds /* default is basically CDO_[AUTO_CLOSE|AUTO_EJECT] */
23921da177e4SLinus Torvalds default:
23931da177e4SLinus Torvalds if (!CDROM_CAN(arg))
23941da177e4SLinus Torvalds return -ENOSYS;
23951da177e4SLinus Torvalds }
23961da177e4SLinus Torvalds cdi->options |= (int) arg;
23971da177e4SLinus Torvalds return cdi->options;
23981da177e4SLinus Torvalds }
23991da177e4SLinus Torvalds
cdrom_ioctl_clear_options(struct cdrom_device_info * cdi,unsigned long arg)2400d2c5d4fcSChristoph Hellwig static int cdrom_ioctl_clear_options(struct cdrom_device_info *cdi,
2401d2c5d4fcSChristoph Hellwig unsigned long arg)
2402d2c5d4fcSChristoph Hellwig {
24035944b2ceSJoe Perches cd_dbg(CD_DO_IOCTL, "entering CDROM_CLEAR_OPTIONS\n");
2404d2c5d4fcSChristoph Hellwig
24051da177e4SLinus Torvalds cdi->options &= ~(int) arg;
24061da177e4SLinus Torvalds return cdi->options;
24071da177e4SLinus Torvalds }
24081da177e4SLinus Torvalds
cdrom_ioctl_select_speed(struct cdrom_device_info * cdi,unsigned long arg)2409d2c5d4fcSChristoph Hellwig static int cdrom_ioctl_select_speed(struct cdrom_device_info *cdi,
2410d2c5d4fcSChristoph Hellwig unsigned long arg)
2411d2c5d4fcSChristoph Hellwig {
24125944b2ceSJoe Perches cd_dbg(CD_DO_IOCTL, "entering CDROM_SELECT_SPEED\n");
2413d2c5d4fcSChristoph Hellwig
24141da177e4SLinus Torvalds if (!CDROM_CAN(CDC_SELECT_SPEED))
24151da177e4SLinus Torvalds return -ENOSYS;
2416d2c5d4fcSChristoph Hellwig return cdi->ops->select_speed(cdi, arg);
24171da177e4SLinus Torvalds }
24181da177e4SLinus Torvalds
cdrom_ioctl_select_disc(struct cdrom_device_info * cdi,unsigned long arg)2419d2c5d4fcSChristoph Hellwig static int cdrom_ioctl_select_disc(struct cdrom_device_info *cdi,
2420d2c5d4fcSChristoph Hellwig unsigned long arg)
2421d2c5d4fcSChristoph Hellwig {
24225944b2ceSJoe Perches cd_dbg(CD_DO_IOCTL, "entering CDROM_SELECT_DISC\n");
2423d2c5d4fcSChristoph Hellwig
24241da177e4SLinus Torvalds if (!CDROM_CAN(CDC_SELECT_DISC))
24251da177e4SLinus Torvalds return -ENOSYS;
24261da177e4SLinus Torvalds
2427d2c5d4fcSChristoph Hellwig if (arg != CDSL_CURRENT && arg != CDSL_NONE) {
2428e4f3aa2eSYoung_X if (arg >= cdi->capacity)
24291da177e4SLinus Torvalds return -EINVAL;
2430d2c5d4fcSChristoph Hellwig }
24311da177e4SLinus Torvalds
24325944b2ceSJoe Perches cd_dbg(CD_CHANGER, "Using generic cdrom_select_disc()\n");
24331da177e4SLinus Torvalds return cdrom_select_disc(cdi, arg);
24341da177e4SLinus Torvalds }
24351da177e4SLinus Torvalds
cdrom_ioctl_reset(struct cdrom_device_info * cdi,struct block_device * bdev)2436d2c5d4fcSChristoph Hellwig static int cdrom_ioctl_reset(struct cdrom_device_info *cdi,
2437d2c5d4fcSChristoph Hellwig struct block_device *bdev)
2438d2c5d4fcSChristoph Hellwig {
24395944b2ceSJoe Perches cd_dbg(CD_DO_IOCTL, "entering CDROM_RESET\n");
2440d2c5d4fcSChristoph Hellwig
24411da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN))
24421da177e4SLinus Torvalds return -EACCES;
24431da177e4SLinus Torvalds if (!CDROM_CAN(CDC_RESET))
24441da177e4SLinus Torvalds return -ENOSYS;
2445f98393a6SPeter Zijlstra invalidate_bdev(bdev);
2446d2c5d4fcSChristoph Hellwig return cdi->ops->reset(cdi);
24471da177e4SLinus Torvalds }
24481da177e4SLinus Torvalds
cdrom_ioctl_lock_door(struct cdrom_device_info * cdi,unsigned long arg)2449d2c5d4fcSChristoph Hellwig static int cdrom_ioctl_lock_door(struct cdrom_device_info *cdi,
2450d2c5d4fcSChristoph Hellwig unsigned long arg)
2451d2c5d4fcSChristoph Hellwig {
24525944b2ceSJoe Perches cd_dbg(CD_DO_IOCTL, "%socking door\n", arg ? "L" : "Unl");
2453d2c5d4fcSChristoph Hellwig
24541da177e4SLinus Torvalds if (!CDROM_CAN(CDC_LOCK))
24551da177e4SLinus Torvalds return -EDRIVE_CANT_DO_THIS;
2456d2c5d4fcSChristoph Hellwig
2457cdccaa94SPaolo Bonzini cdi->keeplocked = arg ? 1 : 0;
2458d2c5d4fcSChristoph Hellwig
2459d2c5d4fcSChristoph Hellwig /*
2460d2c5d4fcSChristoph Hellwig * Don't unlock the door on multiple opens by default, but allow
2461d2c5d4fcSChristoph Hellwig * root to do so.
2462d2c5d4fcSChristoph Hellwig */
2463d2c5d4fcSChristoph Hellwig if (cdi->use_count != 1 && !arg && !capable(CAP_SYS_ADMIN))
24641da177e4SLinus Torvalds return -EBUSY;
2465d2c5d4fcSChristoph Hellwig return cdi->ops->lock_door(cdi, arg);
24661da177e4SLinus Torvalds }
24671da177e4SLinus Torvalds
cdrom_ioctl_debug(struct cdrom_device_info * cdi,unsigned long arg)2468d2c5d4fcSChristoph Hellwig static int cdrom_ioctl_debug(struct cdrom_device_info *cdi,
2469d2c5d4fcSChristoph Hellwig unsigned long arg)
2470d2c5d4fcSChristoph Hellwig {
24715944b2ceSJoe Perches cd_dbg(CD_DO_IOCTL, "%sabling debug\n", arg ? "En" : "Dis");
2472d2c5d4fcSChristoph Hellwig
24731da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN))
24741da177e4SLinus Torvalds return -EACCES;
24751da177e4SLinus Torvalds debug = arg ? 1 : 0;
24761da177e4SLinus Torvalds return debug;
24771da177e4SLinus Torvalds }
24781da177e4SLinus Torvalds
cdrom_ioctl_get_capability(struct cdrom_device_info * cdi)2479d2c5d4fcSChristoph Hellwig static int cdrom_ioctl_get_capability(struct cdrom_device_info *cdi)
2480d2c5d4fcSChristoph Hellwig {
24815944b2ceSJoe Perches cd_dbg(CD_DO_IOCTL, "entering CDROM_GET_CAPABILITY\n");
2482d2c5d4fcSChristoph Hellwig return (cdi->ops->capability & ~cdi->mask);
24831da177e4SLinus Torvalds }
24841da177e4SLinus Torvalds
2485d2c5d4fcSChristoph Hellwig /*
2486d2c5d4fcSChristoph Hellwig * The following function is implemented, although very few audio
24871da177e4SLinus Torvalds * discs give Universal Product Code information, which should just be
24881da177e4SLinus Torvalds * the Medium Catalog Number on the box. Note, that the way the code
24891da177e4SLinus Torvalds * is written on the CD is /not/ uniform across all discs!
24901da177e4SLinus Torvalds */
cdrom_ioctl_get_mcn(struct cdrom_device_info * cdi,void __user * argp)2491d2c5d4fcSChristoph Hellwig static int cdrom_ioctl_get_mcn(struct cdrom_device_info *cdi,
2492d2c5d4fcSChristoph Hellwig void __user *argp)
2493d2c5d4fcSChristoph Hellwig {
24941da177e4SLinus Torvalds struct cdrom_mcn mcn;
2495d2c5d4fcSChristoph Hellwig int ret;
2496d2c5d4fcSChristoph Hellwig
24975944b2ceSJoe Perches cd_dbg(CD_DO_IOCTL, "entering CDROM_GET_MCN\n");
2498d2c5d4fcSChristoph Hellwig
2499d2c5d4fcSChristoph Hellwig if (!(cdi->ops->capability & CDC_MCN))
25001da177e4SLinus Torvalds return -ENOSYS;
2501d2c5d4fcSChristoph Hellwig ret = cdi->ops->get_mcn(cdi, &mcn);
2502d2c5d4fcSChristoph Hellwig if (ret)
25031da177e4SLinus Torvalds return ret;
2504d2c5d4fcSChristoph Hellwig
2505d2c5d4fcSChristoph Hellwig if (copy_to_user(argp, &mcn, sizeof(mcn)))
2506d2c5d4fcSChristoph Hellwig return -EFAULT;
25075944b2ceSJoe Perches cd_dbg(CD_DO_IOCTL, "CDROM_GET_MCN successful\n");
25081da177e4SLinus Torvalds return 0;
25091da177e4SLinus Torvalds }
25101da177e4SLinus Torvalds
cdrom_ioctl_drive_status(struct cdrom_device_info * cdi,unsigned long arg)2511d2c5d4fcSChristoph Hellwig static int cdrom_ioctl_drive_status(struct cdrom_device_info *cdi,
2512d2c5d4fcSChristoph Hellwig unsigned long arg)
2513d2c5d4fcSChristoph Hellwig {
25145944b2ceSJoe Perches cd_dbg(CD_DO_IOCTL, "entering CDROM_DRIVE_STATUS\n");
2515d2c5d4fcSChristoph Hellwig
2516d2c5d4fcSChristoph Hellwig if (!(cdi->ops->capability & CDC_DRIVE_STATUS))
25171da177e4SLinus Torvalds return -ENOSYS;
2518d2c5d4fcSChristoph Hellwig if (!CDROM_CAN(CDC_SELECT_DISC) ||
2519d2c5d4fcSChristoph Hellwig (arg == CDSL_CURRENT || arg == CDSL_NONE))
2520d2c5d4fcSChristoph Hellwig return cdi->ops->drive_status(cdi, CDSL_CURRENT);
25218f3fafc9SScott Bauer if (arg >= cdi->capacity)
25221da177e4SLinus Torvalds return -EINVAL;
25231da177e4SLinus Torvalds return cdrom_slot_status(cdi, arg);
25241da177e4SLinus Torvalds }
25251da177e4SLinus Torvalds
2526d2c5d4fcSChristoph Hellwig /*
2527d2c5d4fcSChristoph Hellwig * Ok, this is where problems start. The current interface for the
2528d2c5d4fcSChristoph Hellwig * CDROM_DISC_STATUS ioctl is flawed. It makes the false assumption that
252925985edcSLucas De Marchi * CDs are all CDS_DATA_1 or all CDS_AUDIO, etc. Unfortunately, while this
2530d2c5d4fcSChristoph Hellwig * is often the case, it is also very common for CDs to have some tracks
2531d2c5d4fcSChristoph Hellwig * with data, and some tracks with audio. Just because I feel like it,
2532d2c5d4fcSChristoph Hellwig * I declare the following to be the best way to cope. If the CD has ANY
2533d2c5d4fcSChristoph Hellwig * data tracks on it, it will be returned as a data CD. If it has any XA
2534d2c5d4fcSChristoph Hellwig * tracks, I will return it as that. Now I could simplify this interface
2535d2c5d4fcSChristoph Hellwig * by combining these returns with the above, but this more clearly
2536d2c5d4fcSChristoph Hellwig * demonstrates the problem with the current interface. Too bad this
2537d2c5d4fcSChristoph Hellwig * wasn't designed to use bitmasks... -Erik
2538d2c5d4fcSChristoph Hellwig *
2539d2c5d4fcSChristoph Hellwig * Well, now we have the option CDS_MIXED: a mixed-type CD.
2540d2c5d4fcSChristoph Hellwig * User level programmers might feel the ioctl is not very useful.
2541d2c5d4fcSChristoph Hellwig * ---david
25421da177e4SLinus Torvalds */
cdrom_ioctl_disc_status(struct cdrom_device_info * cdi)2543d2c5d4fcSChristoph Hellwig static int cdrom_ioctl_disc_status(struct cdrom_device_info *cdi)
2544d2c5d4fcSChristoph Hellwig {
25451da177e4SLinus Torvalds tracktype tracks;
2546d2c5d4fcSChristoph Hellwig
25475944b2ceSJoe Perches cd_dbg(CD_DO_IOCTL, "entering CDROM_DISC_STATUS\n");
2548d2c5d4fcSChristoph Hellwig
25491da177e4SLinus Torvalds cdrom_count_tracks(cdi, &tracks);
25501da177e4SLinus Torvalds if (tracks.error)
2551d2c5d4fcSChristoph Hellwig return tracks.error;
25521da177e4SLinus Torvalds
25531da177e4SLinus Torvalds /* Policy mode on */
25541da177e4SLinus Torvalds if (tracks.audio > 0) {
2555d2c5d4fcSChristoph Hellwig if (!tracks.data && !tracks.cdi && !tracks.xa)
25561da177e4SLinus Torvalds return CDS_AUDIO;
25571da177e4SLinus Torvalds else
25581da177e4SLinus Torvalds return CDS_MIXED;
25591da177e4SLinus Torvalds }
2560d2c5d4fcSChristoph Hellwig
2561d2c5d4fcSChristoph Hellwig if (tracks.cdi > 0)
2562d2c5d4fcSChristoph Hellwig return CDS_XA_2_2;
2563d2c5d4fcSChristoph Hellwig if (tracks.xa > 0)
2564d2c5d4fcSChristoph Hellwig return CDS_XA_2_1;
2565d2c5d4fcSChristoph Hellwig if (tracks.data > 0)
2566d2c5d4fcSChristoph Hellwig return CDS_DATA_1;
25671da177e4SLinus Torvalds /* Policy mode off */
25681da177e4SLinus Torvalds
25695944b2ceSJoe Perches cd_dbg(CD_WARNING, "This disc doesn't have any tracks I recognize!\n");
25701da177e4SLinus Torvalds return CDS_NO_INFO;
25711da177e4SLinus Torvalds }
25721da177e4SLinus Torvalds
cdrom_ioctl_changer_nslots(struct cdrom_device_info * cdi)2573d2c5d4fcSChristoph Hellwig static int cdrom_ioctl_changer_nslots(struct cdrom_device_info *cdi)
2574d2c5d4fcSChristoph Hellwig {
25755944b2ceSJoe Perches cd_dbg(CD_DO_IOCTL, "entering CDROM_CHANGER_NSLOTS\n");
25761da177e4SLinus Torvalds return cdi->capacity;
25771da177e4SLinus Torvalds }
25781da177e4SLinus Torvalds
cdrom_ioctl_get_subchnl(struct cdrom_device_info * cdi,void __user * argp)2579d2c5d4fcSChristoph Hellwig static int cdrom_ioctl_get_subchnl(struct cdrom_device_info *cdi,
2580d2c5d4fcSChristoph Hellwig void __user *argp)
2581d2c5d4fcSChristoph Hellwig {
25821da177e4SLinus Torvalds struct cdrom_subchnl q;
2583d2c5d4fcSChristoph Hellwig u8 requested, back;
2584d2c5d4fcSChristoph Hellwig int ret;
2585d2c5d4fcSChristoph Hellwig
25865944b2ceSJoe Perches /* cd_dbg(CD_DO_IOCTL,"entering CDROMSUBCHNL\n");*/
2587d2c5d4fcSChristoph Hellwig
2588d2c5d4fcSChristoph Hellwig if (copy_from_user(&q, argp, sizeof(q)))
2589d2c5d4fcSChristoph Hellwig return -EFAULT;
2590d2c5d4fcSChristoph Hellwig
25911da177e4SLinus Torvalds requested = q.cdsc_format;
2592d2c5d4fcSChristoph Hellwig if (requested != CDROM_MSF && requested != CDROM_LBA)
25931da177e4SLinus Torvalds return -EINVAL;
25941da177e4SLinus Torvalds q.cdsc_format = CDROM_MSF;
2595d2c5d4fcSChristoph Hellwig
2596d2c5d4fcSChristoph Hellwig ret = cdi->ops->audio_ioctl(cdi, CDROMSUBCHNL, &q);
2597d2c5d4fcSChristoph Hellwig if (ret)
25981da177e4SLinus Torvalds return ret;
2599d2c5d4fcSChristoph Hellwig
26001da177e4SLinus Torvalds back = q.cdsc_format; /* local copy */
26011da177e4SLinus Torvalds sanitize_format(&q.cdsc_absaddr, &back, requested);
26021da177e4SLinus Torvalds sanitize_format(&q.cdsc_reladdr, &q.cdsc_format, requested);
2603d2c5d4fcSChristoph Hellwig
2604d2c5d4fcSChristoph Hellwig if (copy_to_user(argp, &q, sizeof(q)))
2605d2c5d4fcSChristoph Hellwig return -EFAULT;
26065944b2ceSJoe Perches /* cd_dbg(CD_DO_IOCTL, "CDROMSUBCHNL successful\n"); */
26071da177e4SLinus Torvalds return 0;
26081da177e4SLinus Torvalds }
2609d2c5d4fcSChristoph Hellwig
cdrom_ioctl_read_tochdr(struct cdrom_device_info * cdi,void __user * argp)2610d2c5d4fcSChristoph Hellwig static int cdrom_ioctl_read_tochdr(struct cdrom_device_info *cdi,
2611d2c5d4fcSChristoph Hellwig void __user *argp)
2612d2c5d4fcSChristoph Hellwig {
26131da177e4SLinus Torvalds struct cdrom_tochdr header;
2614d2c5d4fcSChristoph Hellwig int ret;
2615d2c5d4fcSChristoph Hellwig
26165944b2ceSJoe Perches /* cd_dbg(CD_DO_IOCTL, "entering CDROMREADTOCHDR\n"); */
2617d2c5d4fcSChristoph Hellwig
2618d2c5d4fcSChristoph Hellwig if (copy_from_user(&header, argp, sizeof(header)))
2619d2c5d4fcSChristoph Hellwig return -EFAULT;
2620d2c5d4fcSChristoph Hellwig
2621d2c5d4fcSChristoph Hellwig ret = cdi->ops->audio_ioctl(cdi, CDROMREADTOCHDR, &header);
2622d2c5d4fcSChristoph Hellwig if (ret)
26231da177e4SLinus Torvalds return ret;
2624d2c5d4fcSChristoph Hellwig
2625d2c5d4fcSChristoph Hellwig if (copy_to_user(argp, &header, sizeof(header)))
2626d2c5d4fcSChristoph Hellwig return -EFAULT;
26275944b2ceSJoe Perches /* cd_dbg(CD_DO_IOCTL, "CDROMREADTOCHDR successful\n"); */
26281da177e4SLinus Torvalds return 0;
26291da177e4SLinus Torvalds }
2630d2c5d4fcSChristoph Hellwig
cdrom_read_tocentry(struct cdrom_device_info * cdi,struct cdrom_tocentry * entry)26314c3cfcceSChristoph Hellwig int cdrom_read_tocentry(struct cdrom_device_info *cdi,
26324c3cfcceSChristoph Hellwig struct cdrom_tocentry *entry)
26334c3cfcceSChristoph Hellwig {
26344c3cfcceSChristoph Hellwig u8 requested_format = entry->cdte_format;
26354c3cfcceSChristoph Hellwig int ret;
26364c3cfcceSChristoph Hellwig
26374c3cfcceSChristoph Hellwig if (requested_format != CDROM_MSF && requested_format != CDROM_LBA)
26384c3cfcceSChristoph Hellwig return -EINVAL;
26394c3cfcceSChristoph Hellwig
26404c3cfcceSChristoph Hellwig /* make interface to low-level uniform */
26414c3cfcceSChristoph Hellwig entry->cdte_format = CDROM_MSF;
26424c3cfcceSChristoph Hellwig ret = cdi->ops->audio_ioctl(cdi, CDROMREADTOCENTRY, entry);
26434c3cfcceSChristoph Hellwig if (!ret)
26444c3cfcceSChristoph Hellwig sanitize_format(&entry->cdte_addr, &entry->cdte_format,
26454c3cfcceSChristoph Hellwig requested_format);
26464c3cfcceSChristoph Hellwig return ret;
26474c3cfcceSChristoph Hellwig }
26484c3cfcceSChristoph Hellwig EXPORT_SYMBOL_GPL(cdrom_read_tocentry);
26494c3cfcceSChristoph Hellwig
cdrom_ioctl_read_tocentry(struct cdrom_device_info * cdi,void __user * argp)2650d2c5d4fcSChristoph Hellwig static int cdrom_ioctl_read_tocentry(struct cdrom_device_info *cdi,
2651d2c5d4fcSChristoph Hellwig void __user *argp)
2652d2c5d4fcSChristoph Hellwig {
26531da177e4SLinus Torvalds struct cdrom_tocentry entry;
2654d2c5d4fcSChristoph Hellwig int ret;
2655d2c5d4fcSChristoph Hellwig
2656d2c5d4fcSChristoph Hellwig if (copy_from_user(&entry, argp, sizeof(entry)))
2657d2c5d4fcSChristoph Hellwig return -EFAULT;
26584c3cfcceSChristoph Hellwig ret = cdrom_read_tocentry(cdi, &entry);
26594c3cfcceSChristoph Hellwig if (!ret && copy_to_user(argp, &entry, sizeof(entry)))
2660d2c5d4fcSChristoph Hellwig return -EFAULT;
26614c3cfcceSChristoph Hellwig return ret;
26621da177e4SLinus Torvalds }
2663d2c5d4fcSChristoph Hellwig
cdrom_ioctl_play_msf(struct cdrom_device_info * cdi,void __user * argp)2664d2c5d4fcSChristoph Hellwig static int cdrom_ioctl_play_msf(struct cdrom_device_info *cdi,
2665d2c5d4fcSChristoph Hellwig void __user *argp)
2666d2c5d4fcSChristoph Hellwig {
26671da177e4SLinus Torvalds struct cdrom_msf msf;
2668d2c5d4fcSChristoph Hellwig
26695944b2ceSJoe Perches cd_dbg(CD_DO_IOCTL, "entering CDROMPLAYMSF\n");
2670d2c5d4fcSChristoph Hellwig
2671d2c5d4fcSChristoph Hellwig if (!CDROM_CAN(CDC_PLAY_AUDIO))
2672d2c5d4fcSChristoph Hellwig return -ENOSYS;
2673d2c5d4fcSChristoph Hellwig if (copy_from_user(&msf, argp, sizeof(msf)))
2674d2c5d4fcSChristoph Hellwig return -EFAULT;
2675d2c5d4fcSChristoph Hellwig return cdi->ops->audio_ioctl(cdi, CDROMPLAYMSF, &msf);
26761da177e4SLinus Torvalds }
2677d2c5d4fcSChristoph Hellwig
cdrom_ioctl_play_trkind(struct cdrom_device_info * cdi,void __user * argp)2678d2c5d4fcSChristoph Hellwig static int cdrom_ioctl_play_trkind(struct cdrom_device_info *cdi,
2679d2c5d4fcSChristoph Hellwig void __user *argp)
2680d2c5d4fcSChristoph Hellwig {
26811da177e4SLinus Torvalds struct cdrom_ti ti;
2682d2c5d4fcSChristoph Hellwig int ret;
2683d2c5d4fcSChristoph Hellwig
26845944b2ceSJoe Perches cd_dbg(CD_DO_IOCTL, "entering CDROMPLAYTRKIND\n");
2685d2c5d4fcSChristoph Hellwig
26861da177e4SLinus Torvalds if (!CDROM_CAN(CDC_PLAY_AUDIO))
26871da177e4SLinus Torvalds return -ENOSYS;
2688d2c5d4fcSChristoph Hellwig if (copy_from_user(&ti, argp, sizeof(ti)))
2689d2c5d4fcSChristoph Hellwig return -EFAULT;
2690d2c5d4fcSChristoph Hellwig
2691d2c5d4fcSChristoph Hellwig ret = check_for_audio_disc(cdi, cdi->ops);
2692d2c5d4fcSChristoph Hellwig if (ret)
26931da177e4SLinus Torvalds return ret;
2694d2c5d4fcSChristoph Hellwig return cdi->ops->audio_ioctl(cdi, CDROMPLAYTRKIND, &ti);
2695d2c5d4fcSChristoph Hellwig }
cdrom_ioctl_volctrl(struct cdrom_device_info * cdi,void __user * argp)2696d2c5d4fcSChristoph Hellwig static int cdrom_ioctl_volctrl(struct cdrom_device_info *cdi,
2697d2c5d4fcSChristoph Hellwig void __user *argp)
2698d2c5d4fcSChristoph Hellwig {
2699d2c5d4fcSChristoph Hellwig struct cdrom_volctrl volume;
2700d2c5d4fcSChristoph Hellwig
27015944b2ceSJoe Perches cd_dbg(CD_DO_IOCTL, "entering CDROMVOLCTRL\n");
2702d2c5d4fcSChristoph Hellwig
2703d2c5d4fcSChristoph Hellwig if (!CDROM_CAN(CDC_PLAY_AUDIO))
2704d2c5d4fcSChristoph Hellwig return -ENOSYS;
2705d2c5d4fcSChristoph Hellwig if (copy_from_user(&volume, argp, sizeof(volume)))
2706d2c5d4fcSChristoph Hellwig return -EFAULT;
2707d2c5d4fcSChristoph Hellwig return cdi->ops->audio_ioctl(cdi, CDROMVOLCTRL, &volume);
2708d2c5d4fcSChristoph Hellwig }
2709d2c5d4fcSChristoph Hellwig
cdrom_ioctl_volread(struct cdrom_device_info * cdi,void __user * argp)2710d2c5d4fcSChristoph Hellwig static int cdrom_ioctl_volread(struct cdrom_device_info *cdi,
2711d2c5d4fcSChristoph Hellwig void __user *argp)
2712d2c5d4fcSChristoph Hellwig {
2713d2c5d4fcSChristoph Hellwig struct cdrom_volctrl volume;
2714d2c5d4fcSChristoph Hellwig int ret;
2715d2c5d4fcSChristoph Hellwig
27165944b2ceSJoe Perches cd_dbg(CD_DO_IOCTL, "entering CDROMVOLREAD\n");
2717d2c5d4fcSChristoph Hellwig
2718d2c5d4fcSChristoph Hellwig if (!CDROM_CAN(CDC_PLAY_AUDIO))
2719d2c5d4fcSChristoph Hellwig return -ENOSYS;
2720d2c5d4fcSChristoph Hellwig
2721d2c5d4fcSChristoph Hellwig ret = cdi->ops->audio_ioctl(cdi, CDROMVOLREAD, &volume);
2722d2c5d4fcSChristoph Hellwig if (ret)
2723d2c5d4fcSChristoph Hellwig return ret;
2724d2c5d4fcSChristoph Hellwig
2725d2c5d4fcSChristoph Hellwig if (copy_to_user(argp, &volume, sizeof(volume)))
2726d2c5d4fcSChristoph Hellwig return -EFAULT;
27271da177e4SLinus Torvalds return 0;
27281da177e4SLinus Torvalds }
2729d2c5d4fcSChristoph Hellwig
cdrom_ioctl_audioctl(struct cdrom_device_info * cdi,unsigned int cmd)2730d2c5d4fcSChristoph Hellwig static int cdrom_ioctl_audioctl(struct cdrom_device_info *cdi,
2731d2c5d4fcSChristoph Hellwig unsigned int cmd)
2732d2c5d4fcSChristoph Hellwig {
2733d2c5d4fcSChristoph Hellwig int ret;
2734d2c5d4fcSChristoph Hellwig
27355944b2ceSJoe Perches cd_dbg(CD_DO_IOCTL, "doing audio ioctl (start/stop/pause/resume)\n");
2736d2c5d4fcSChristoph Hellwig
2737d2c5d4fcSChristoph Hellwig if (!CDROM_CAN(CDC_PLAY_AUDIO))
2738d2c5d4fcSChristoph Hellwig return -ENOSYS;
2739d2c5d4fcSChristoph Hellwig ret = check_for_audio_disc(cdi, cdi->ops);
2740d2c5d4fcSChristoph Hellwig if (ret)
2741d2c5d4fcSChristoph Hellwig return ret;
2742d2c5d4fcSChristoph Hellwig return cdi->ops->audio_ioctl(cdi, cmd, NULL);
2743d2c5d4fcSChristoph Hellwig }
2744d2c5d4fcSChristoph Hellwig
2745d2c5d4fcSChristoph Hellwig /*
27461da177e4SLinus Torvalds * Required when we need to use READ_10 to issue other than 2048 block
27471da177e4SLinus Torvalds * reads
27481da177e4SLinus Torvalds */
cdrom_switch_blocksize(struct cdrom_device_info * cdi,int size)27491da177e4SLinus Torvalds static int cdrom_switch_blocksize(struct cdrom_device_info *cdi, int size)
27501da177e4SLinus Torvalds {
2751853fe1bfSKees Cook const struct cdrom_device_ops *cdo = cdi->ops;
27521da177e4SLinus Torvalds struct packet_command cgc;
27531da177e4SLinus Torvalds struct modesel_head mh;
27541da177e4SLinus Torvalds
27551da177e4SLinus Torvalds memset(&mh, 0, sizeof(mh));
27561da177e4SLinus Torvalds mh.block_desc_length = 0x08;
27571da177e4SLinus Torvalds mh.block_length_med = (size >> 8) & 0xff;
27581da177e4SLinus Torvalds mh.block_length_lo = size & 0xff;
27591da177e4SLinus Torvalds
27601da177e4SLinus Torvalds memset(&cgc, 0, sizeof(cgc));
27611da177e4SLinus Torvalds cgc.cmd[0] = 0x15;
27621da177e4SLinus Torvalds cgc.cmd[1] = 1 << 4;
27631da177e4SLinus Torvalds cgc.cmd[4] = 12;
27641da177e4SLinus Torvalds cgc.buflen = sizeof(mh);
27651da177e4SLinus Torvalds cgc.buffer = (char *) &mh;
27661da177e4SLinus Torvalds cgc.data_direction = CGC_DATA_WRITE;
27671da177e4SLinus Torvalds mh.block_desc_length = 0x08;
27681da177e4SLinus Torvalds mh.block_length_med = (size >> 8) & 0xff;
27691da177e4SLinus Torvalds mh.block_length_lo = size & 0xff;
27701da177e4SLinus Torvalds
27711da177e4SLinus Torvalds return cdo->generic_packet(cdi, &cgc);
27721da177e4SLinus Torvalds }
27731da177e4SLinus Torvalds
cdrom_get_track_info(struct cdrom_device_info * cdi,__u16 track,__u8 type,track_information * ti)2774e1e60fdaSJoe Perches static int cdrom_get_track_info(struct cdrom_device_info *cdi,
2775e1e60fdaSJoe Perches __u16 track, __u8 type, track_information *ti)
2776e1e60fdaSJoe Perches {
2777853fe1bfSKees Cook const struct cdrom_device_ops *cdo = cdi->ops;
2778e1e60fdaSJoe Perches struct packet_command cgc;
2779e1e60fdaSJoe Perches int ret, buflen;
2780e1e60fdaSJoe Perches
2781e1e60fdaSJoe Perches init_cdrom_command(&cgc, ti, 8, CGC_DATA_READ);
2782e1e60fdaSJoe Perches cgc.cmd[0] = GPCMD_READ_TRACK_RZONE_INFO;
2783e1e60fdaSJoe Perches cgc.cmd[1] = type & 3;
2784e1e60fdaSJoe Perches cgc.cmd[4] = (track & 0xff00) >> 8;
2785e1e60fdaSJoe Perches cgc.cmd[5] = track & 0xff;
2786e1e60fdaSJoe Perches cgc.cmd[8] = 8;
2787e1e60fdaSJoe Perches cgc.quiet = 1;
2788e1e60fdaSJoe Perches
2789e1e60fdaSJoe Perches ret = cdo->generic_packet(cdi, &cgc);
2790e1e60fdaSJoe Perches if (ret)
2791e1e60fdaSJoe Perches return ret;
2792e1e60fdaSJoe Perches
2793e1e60fdaSJoe Perches buflen = be16_to_cpu(ti->track_information_length) +
2794e1e60fdaSJoe Perches sizeof(ti->track_information_length);
2795e1e60fdaSJoe Perches
2796e1e60fdaSJoe Perches if (buflen > sizeof(track_information))
2797e1e60fdaSJoe Perches buflen = sizeof(track_information);
2798e1e60fdaSJoe Perches
2799e1e60fdaSJoe Perches cgc.cmd[8] = cgc.buflen = buflen;
2800e1e60fdaSJoe Perches ret = cdo->generic_packet(cdi, &cgc);
2801e1e60fdaSJoe Perches if (ret)
2802e1e60fdaSJoe Perches return ret;
2803e1e60fdaSJoe Perches
2804e1e60fdaSJoe Perches /* return actual fill size */
2805e1e60fdaSJoe Perches return buflen;
2806e1e60fdaSJoe Perches }
2807e1e60fdaSJoe Perches
2808e1e60fdaSJoe Perches /* return the last written block on the CD-R media. this is for the udf
2809e1e60fdaSJoe Perches file system. */
cdrom_get_last_written(struct cdrom_device_info * cdi,long * last_written)2810e1e60fdaSJoe Perches int cdrom_get_last_written(struct cdrom_device_info *cdi, long *last_written)
2811e1e60fdaSJoe Perches {
2812e1e60fdaSJoe Perches struct cdrom_tocentry toc;
2813e1e60fdaSJoe Perches disc_information di;
2814e1e60fdaSJoe Perches track_information ti;
2815e1e60fdaSJoe Perches __u32 last_track;
2816e1e60fdaSJoe Perches int ret = -1, ti_size;
2817e1e60fdaSJoe Perches
2818e1e60fdaSJoe Perches if (!CDROM_CAN(CDC_GENERIC_PACKET))
2819e1e60fdaSJoe Perches goto use_toc;
2820e1e60fdaSJoe Perches
2821e1e60fdaSJoe Perches ret = cdrom_get_disc_info(cdi, &di);
2822e1e60fdaSJoe Perches if (ret < (int)(offsetof(typeof(di), last_track_lsb)
2823e1e60fdaSJoe Perches + sizeof(di.last_track_lsb)))
2824e1e60fdaSJoe Perches goto use_toc;
2825e1e60fdaSJoe Perches
2826e1e60fdaSJoe Perches /* if unit didn't return msb, it's zeroed by cdrom_get_disc_info */
2827e1e60fdaSJoe Perches last_track = (di.last_track_msb << 8) | di.last_track_lsb;
2828e1e60fdaSJoe Perches ti_size = cdrom_get_track_info(cdi, last_track, 1, &ti);
2829e1e60fdaSJoe Perches if (ti_size < (int)offsetof(typeof(ti), track_start))
2830e1e60fdaSJoe Perches goto use_toc;
2831e1e60fdaSJoe Perches
2832e1e60fdaSJoe Perches /* if this track is blank, try the previous. */
2833e1e60fdaSJoe Perches if (ti.blank) {
2834e1e60fdaSJoe Perches if (last_track == 1)
2835e1e60fdaSJoe Perches goto use_toc;
2836e1e60fdaSJoe Perches last_track--;
2837e1e60fdaSJoe Perches ti_size = cdrom_get_track_info(cdi, last_track, 1, &ti);
2838e1e60fdaSJoe Perches }
2839e1e60fdaSJoe Perches
2840e1e60fdaSJoe Perches if (ti_size < (int)(offsetof(typeof(ti), track_size)
2841e1e60fdaSJoe Perches + sizeof(ti.track_size)))
2842e1e60fdaSJoe Perches goto use_toc;
2843e1e60fdaSJoe Perches
2844e1e60fdaSJoe Perches /* if last recorded field is valid, return it. */
2845e1e60fdaSJoe Perches if (ti.lra_v && ti_size >= (int)(offsetof(typeof(ti), last_rec_address)
2846e1e60fdaSJoe Perches + sizeof(ti.last_rec_address))) {
2847e1e60fdaSJoe Perches *last_written = be32_to_cpu(ti.last_rec_address);
2848e1e60fdaSJoe Perches } else {
2849e1e60fdaSJoe Perches /* make it up instead */
2850e1e60fdaSJoe Perches *last_written = be32_to_cpu(ti.track_start) +
2851e1e60fdaSJoe Perches be32_to_cpu(ti.track_size);
2852e1e60fdaSJoe Perches if (ti.free_blocks)
2853e1e60fdaSJoe Perches *last_written -= (be32_to_cpu(ti.free_blocks) + 7);
2854e1e60fdaSJoe Perches }
2855e1e60fdaSJoe Perches return 0;
2856e1e60fdaSJoe Perches
2857e1e60fdaSJoe Perches /* this is where we end up if the drive either can't do a
2858e1e60fdaSJoe Perches GPCMD_READ_DISC_INFO or GPCMD_READ_TRACK_RZONE_INFO or if
2859e1e60fdaSJoe Perches it doesn't give enough information or fails. then we return
2860e1e60fdaSJoe Perches the toc contents. */
2861e1e60fdaSJoe Perches use_toc:
2862366ba7c7SDiego Elio Pettenò if (!CDROM_CAN(CDC_PLAY_AUDIO))
2863366ba7c7SDiego Elio Pettenò return -ENOSYS;
2864366ba7c7SDiego Elio Pettenò
2865e1e60fdaSJoe Perches toc.cdte_format = CDROM_MSF;
2866e1e60fdaSJoe Perches toc.cdte_track = CDROM_LEADOUT;
2867e1e60fdaSJoe Perches if ((ret = cdi->ops->audio_ioctl(cdi, CDROMREADTOCENTRY, &toc)))
2868e1e60fdaSJoe Perches return ret;
2869e1e60fdaSJoe Perches sanitize_format(&toc.cdte_addr, &toc.cdte_format, CDROM_LBA);
2870e1e60fdaSJoe Perches *last_written = toc.cdte_addr.lba;
2871e1e60fdaSJoe Perches return 0;
2872e1e60fdaSJoe Perches }
2873eeef7565SEnze Li EXPORT_SYMBOL(cdrom_get_last_written);
2874e1e60fdaSJoe Perches
2875dac1c5cfSJoe Perches /* return the next writable block. also for udf file system. */
cdrom_get_next_writable(struct cdrom_device_info * cdi,long * next_writable)2876dac1c5cfSJoe Perches static int cdrom_get_next_writable(struct cdrom_device_info *cdi,
2877dac1c5cfSJoe Perches long *next_writable)
2878dac1c5cfSJoe Perches {
2879dac1c5cfSJoe Perches disc_information di;
2880dac1c5cfSJoe Perches track_information ti;
2881dac1c5cfSJoe Perches __u16 last_track;
2882dac1c5cfSJoe Perches int ret, ti_size;
2883dac1c5cfSJoe Perches
2884dac1c5cfSJoe Perches if (!CDROM_CAN(CDC_GENERIC_PACKET))
2885dac1c5cfSJoe Perches goto use_last_written;
2886dac1c5cfSJoe Perches
2887dac1c5cfSJoe Perches ret = cdrom_get_disc_info(cdi, &di);
2888dac1c5cfSJoe Perches if (ret < 0 || ret < offsetof(typeof(di), last_track_lsb)
2889dac1c5cfSJoe Perches + sizeof(di.last_track_lsb))
2890dac1c5cfSJoe Perches goto use_last_written;
2891dac1c5cfSJoe Perches
2892dac1c5cfSJoe Perches /* if unit didn't return msb, it's zeroed by cdrom_get_disc_info */
2893dac1c5cfSJoe Perches last_track = (di.last_track_msb << 8) | di.last_track_lsb;
2894dac1c5cfSJoe Perches ti_size = cdrom_get_track_info(cdi, last_track, 1, &ti);
2895dac1c5cfSJoe Perches if (ti_size < 0 || ti_size < offsetof(typeof(ti), track_start))
2896dac1c5cfSJoe Perches goto use_last_written;
2897dac1c5cfSJoe Perches
2898dac1c5cfSJoe Perches /* if this track is blank, try the previous. */
2899dac1c5cfSJoe Perches if (ti.blank) {
2900dac1c5cfSJoe Perches if (last_track == 1)
2901dac1c5cfSJoe Perches goto use_last_written;
2902dac1c5cfSJoe Perches last_track--;
2903dac1c5cfSJoe Perches ti_size = cdrom_get_track_info(cdi, last_track, 1, &ti);
2904dac1c5cfSJoe Perches if (ti_size < 0)
2905dac1c5cfSJoe Perches goto use_last_written;
2906dac1c5cfSJoe Perches }
2907dac1c5cfSJoe Perches
2908dac1c5cfSJoe Perches /* if next recordable address field is valid, use it. */
2909dac1c5cfSJoe Perches if (ti.nwa_v && ti_size >= offsetof(typeof(ti), next_writable)
2910dac1c5cfSJoe Perches + sizeof(ti.next_writable)) {
2911dac1c5cfSJoe Perches *next_writable = be32_to_cpu(ti.next_writable);
2912dac1c5cfSJoe Perches return 0;
2913dac1c5cfSJoe Perches }
2914dac1c5cfSJoe Perches
2915dac1c5cfSJoe Perches use_last_written:
2916dac1c5cfSJoe Perches ret = cdrom_get_last_written(cdi, next_writable);
2917dac1c5cfSJoe Perches if (ret) {
2918dac1c5cfSJoe Perches *next_writable = 0;
2919dac1c5cfSJoe Perches return ret;
2920dac1c5cfSJoe Perches } else {
2921dac1c5cfSJoe Perches *next_writable += 7;
2922dac1c5cfSJoe Perches return 0;
2923dac1c5cfSJoe Perches }
2924dac1c5cfSJoe Perches }
2925dac1c5cfSJoe Perches
mmc_ioctl_cdrom_read_data(struct cdrom_device_info * cdi,void __user * arg,struct packet_command * cgc,int cmd)29263147c531SMarcin Slusarz static noinline int mmc_ioctl_cdrom_read_data(struct cdrom_device_info *cdi,
29273147c531SMarcin Slusarz void __user *arg,
29283147c531SMarcin Slusarz struct packet_command *cgc,
29293147c531SMarcin Slusarz int cmd)
29301da177e4SLinus Torvalds {
2931e7d0748dSKees Cook struct scsi_sense_hdr sshdr;
29321da177e4SLinus Torvalds struct cdrom_msf msf;
29331da177e4SLinus Torvalds int blocksize = 0, format = 0, lba;
29343147c531SMarcin Slusarz int ret;
29351da177e4SLinus Torvalds
29361da177e4SLinus Torvalds switch (cmd) {
29371da177e4SLinus Torvalds case CDROMREADRAW:
29381da177e4SLinus Torvalds blocksize = CD_FRAMESIZE_RAW;
29391da177e4SLinus Torvalds break;
29401da177e4SLinus Torvalds case CDROMREADMODE1:
29411da177e4SLinus Torvalds blocksize = CD_FRAMESIZE;
29421da177e4SLinus Torvalds format = 2;
29431da177e4SLinus Torvalds break;
29441da177e4SLinus Torvalds case CDROMREADMODE2:
29451da177e4SLinus Torvalds blocksize = CD_FRAMESIZE_RAW0;
29461da177e4SLinus Torvalds break;
29471da177e4SLinus Torvalds }
2948a09c391dSJoe Perches if (copy_from_user(&msf, (struct cdrom_msf __user *)arg, sizeof(msf)))
2949a09c391dSJoe Perches return -EFAULT;
29501da177e4SLinus Torvalds lba = msf_to_lba(msf.cdmsf_min0, msf.cdmsf_sec0, msf.cdmsf_frame0);
29511da177e4SLinus Torvalds /* FIXME: we need upper bound checking, too!! */
29521da177e4SLinus Torvalds if (lba < 0)
29531da177e4SLinus Torvalds return -EINVAL;
29543147c531SMarcin Slusarz
2955542db015SJonathan Salwan cgc->buffer = kzalloc(blocksize, GFP_KERNEL);
29563147c531SMarcin Slusarz if (cgc->buffer == NULL)
29571da177e4SLinus Torvalds return -ENOMEM;
29583147c531SMarcin Slusarz
2959e7d0748dSKees Cook memset(&sshdr, 0, sizeof(sshdr));
2960e7d0748dSKees Cook cgc->sshdr = &sshdr;
29613147c531SMarcin Slusarz cgc->data_direction = CGC_DATA_READ;
29623147c531SMarcin Slusarz ret = cdrom_read_block(cdi, cgc, lba, 1, format, blocksize);
2963e7d0748dSKees Cook if (ret && sshdr.sense_key == 0x05 &&
2964e7d0748dSKees Cook sshdr.asc == 0x20 &&
2965e7d0748dSKees Cook sshdr.ascq == 0x00) {
29661da177e4SLinus Torvalds /*
29671da177e4SLinus Torvalds * SCSI-II devices are not required to support
29681da177e4SLinus Torvalds * READ_CD, so let's try switching block size
29691da177e4SLinus Torvalds */
2970b5f32555SSebastian Andrzej Siewior if (blocksize != CD_FRAMESIZE) {
29713147c531SMarcin Slusarz ret = cdrom_switch_blocksize(cdi, blocksize);
29723147c531SMarcin Slusarz if (ret)
29733147c531SMarcin Slusarz goto out;
2974b5f32555SSebastian Andrzej Siewior }
2975e7d0748dSKees Cook cgc->sshdr = NULL;
29763147c531SMarcin Slusarz ret = cdrom_read_cd(cdi, cgc, lba, blocksize, 1);
2977b5f32555SSebastian Andrzej Siewior if (blocksize != CD_FRAMESIZE)
2978b5f32555SSebastian Andrzej Siewior ret |= cdrom_switch_blocksize(cdi, CD_FRAMESIZE);
29791da177e4SLinus Torvalds }
29803147c531SMarcin Slusarz if (!ret && copy_to_user(arg, cgc->buffer, blocksize))
29811da177e4SLinus Torvalds ret = -EFAULT;
29823147c531SMarcin Slusarz out:
29833147c531SMarcin Slusarz kfree(cgc->buffer);
29841da177e4SLinus Torvalds return ret;
29851da177e4SLinus Torvalds }
29863147c531SMarcin Slusarz
mmc_ioctl_cdrom_read_audio(struct cdrom_device_info * cdi,void __user * arg)29873147c531SMarcin Slusarz static noinline int mmc_ioctl_cdrom_read_audio(struct cdrom_device_info *cdi,
29883147c531SMarcin Slusarz void __user *arg)
29893147c531SMarcin Slusarz {
29901da177e4SLinus Torvalds struct cdrom_read_audio ra;
29911da177e4SLinus Torvalds int lba;
29921da177e4SLinus Torvalds
29938f8f5620SArnd Bergmann #ifdef CONFIG_COMPAT
29948f8f5620SArnd Bergmann if (in_compat_syscall()) {
29958f8f5620SArnd Bergmann struct compat_cdrom_read_audio {
29968f8f5620SArnd Bergmann union cdrom_addr addr;
29978f8f5620SArnd Bergmann u8 addr_format;
29988f8f5620SArnd Bergmann compat_int_t nframes;
29998f8f5620SArnd Bergmann compat_caddr_t buf;
30008f8f5620SArnd Bergmann } ra32;
30018f8f5620SArnd Bergmann
30028f8f5620SArnd Bergmann if (copy_from_user(&ra32, arg, sizeof(ra32)))
30038f8f5620SArnd Bergmann return -EFAULT;
30048f8f5620SArnd Bergmann
30058f8f5620SArnd Bergmann ra = (struct cdrom_read_audio) {
30068f8f5620SArnd Bergmann .addr = ra32.addr,
30078f8f5620SArnd Bergmann .addr_format = ra32.addr_format,
30088f8f5620SArnd Bergmann .nframes = ra32.nframes,
30098f8f5620SArnd Bergmann .buf = compat_ptr(ra32.buf),
30108f8f5620SArnd Bergmann };
30118f8f5620SArnd Bergmann } else
30128f8f5620SArnd Bergmann #endif
30138f8f5620SArnd Bergmann {
3014a09c391dSJoe Perches if (copy_from_user(&ra, (struct cdrom_read_audio __user *)arg,
3015a09c391dSJoe Perches sizeof(ra)))
3016a09c391dSJoe Perches return -EFAULT;
30178f8f5620SArnd Bergmann }
30181da177e4SLinus Torvalds
30191da177e4SLinus Torvalds if (ra.addr_format == CDROM_MSF)
30201da177e4SLinus Torvalds lba = msf_to_lba(ra.addr.msf.minute,
30211da177e4SLinus Torvalds ra.addr.msf.second,
30221da177e4SLinus Torvalds ra.addr.msf.frame);
30231da177e4SLinus Torvalds else if (ra.addr_format == CDROM_LBA)
30241da177e4SLinus Torvalds lba = ra.addr.lba;
30251da177e4SLinus Torvalds else
30261da177e4SLinus Torvalds return -EINVAL;
30271da177e4SLinus Torvalds
30281da177e4SLinus Torvalds /* FIXME: we need upper bound checking, too!! */
30291da177e4SLinus Torvalds if (lba < 0 || ra.nframes <= 0 || ra.nframes > CD_FRAMES)
30301da177e4SLinus Torvalds return -EINVAL;
30311da177e4SLinus Torvalds
30321da177e4SLinus Torvalds return cdrom_read_cdda(cdi, ra.buf, lba, ra.nframes);
30331da177e4SLinus Torvalds }
30343147c531SMarcin Slusarz
mmc_ioctl_cdrom_subchannel(struct cdrom_device_info * cdi,void __user * arg)30353147c531SMarcin Slusarz static noinline int mmc_ioctl_cdrom_subchannel(struct cdrom_device_info *cdi,
30363147c531SMarcin Slusarz void __user *arg)
30373147c531SMarcin Slusarz {
30383147c531SMarcin Slusarz int ret;
30391da177e4SLinus Torvalds struct cdrom_subchnl q;
30401da177e4SLinus Torvalds u_char requested, back;
3041a09c391dSJoe Perches if (copy_from_user(&q, (struct cdrom_subchnl __user *)arg, sizeof(q)))
3042a09c391dSJoe Perches return -EFAULT;
30431da177e4SLinus Torvalds requested = q.cdsc_format;
30441da177e4SLinus Torvalds if (!((requested == CDROM_MSF) ||
30451da177e4SLinus Torvalds (requested == CDROM_LBA)))
30461da177e4SLinus Torvalds return -EINVAL;
3047bf294e83Svchannaiah
30483147c531SMarcin Slusarz ret = cdrom_read_subchannel(cdi, &q, 0);
30493147c531SMarcin Slusarz if (ret)
30501da177e4SLinus Torvalds return ret;
30511da177e4SLinus Torvalds back = q.cdsc_format; /* local copy */
30521da177e4SLinus Torvalds sanitize_format(&q.cdsc_absaddr, &back, requested);
30531da177e4SLinus Torvalds sanitize_format(&q.cdsc_reladdr, &q.cdsc_format, requested);
3054a09c391dSJoe Perches if (copy_to_user((struct cdrom_subchnl __user *)arg, &q, sizeof(q)))
3055a09c391dSJoe Perches return -EFAULT;
30565944b2ceSJoe Perches /* cd_dbg(CD_DO_IOCTL, "CDROMSUBCHNL successful\n"); */
30571da177e4SLinus Torvalds return 0;
30581da177e4SLinus Torvalds }
30593147c531SMarcin Slusarz
mmc_ioctl_cdrom_play_msf(struct cdrom_device_info * cdi,void __user * arg,struct packet_command * cgc)30603147c531SMarcin Slusarz static noinline int mmc_ioctl_cdrom_play_msf(struct cdrom_device_info *cdi,
30613147c531SMarcin Slusarz void __user *arg,
30623147c531SMarcin Slusarz struct packet_command *cgc)
30633147c531SMarcin Slusarz {
3064853fe1bfSKees Cook const struct cdrom_device_ops *cdo = cdi->ops;
30651da177e4SLinus Torvalds struct cdrom_msf msf;
30665944b2ceSJoe Perches cd_dbg(CD_DO_IOCTL, "entering CDROMPLAYMSF\n");
3067a09c391dSJoe Perches if (copy_from_user(&msf, (struct cdrom_msf __user *)arg, sizeof(msf)))
3068a09c391dSJoe Perches return -EFAULT;
30693147c531SMarcin Slusarz cgc->cmd[0] = GPCMD_PLAY_AUDIO_MSF;
30703147c531SMarcin Slusarz cgc->cmd[3] = msf.cdmsf_min0;
30713147c531SMarcin Slusarz cgc->cmd[4] = msf.cdmsf_sec0;
30723147c531SMarcin Slusarz cgc->cmd[5] = msf.cdmsf_frame0;
30733147c531SMarcin Slusarz cgc->cmd[6] = msf.cdmsf_min1;
30743147c531SMarcin Slusarz cgc->cmd[7] = msf.cdmsf_sec1;
30753147c531SMarcin Slusarz cgc->cmd[8] = msf.cdmsf_frame1;
30763147c531SMarcin Slusarz cgc->data_direction = CGC_DATA_NONE;
30773147c531SMarcin Slusarz return cdo->generic_packet(cdi, cgc);
30781da177e4SLinus Torvalds }
30793147c531SMarcin Slusarz
mmc_ioctl_cdrom_play_blk(struct cdrom_device_info * cdi,void __user * arg,struct packet_command * cgc)30803147c531SMarcin Slusarz static noinline int mmc_ioctl_cdrom_play_blk(struct cdrom_device_info *cdi,
30813147c531SMarcin Slusarz void __user *arg,
30823147c531SMarcin Slusarz struct packet_command *cgc)
30833147c531SMarcin Slusarz {
3084853fe1bfSKees Cook const struct cdrom_device_ops *cdo = cdi->ops;
30851da177e4SLinus Torvalds struct cdrom_blk blk;
30865944b2ceSJoe Perches cd_dbg(CD_DO_IOCTL, "entering CDROMPLAYBLK\n");
3087a09c391dSJoe Perches if (copy_from_user(&blk, (struct cdrom_blk __user *)arg, sizeof(blk)))
3088a09c391dSJoe Perches return -EFAULT;
30893147c531SMarcin Slusarz cgc->cmd[0] = GPCMD_PLAY_AUDIO_10;
30903147c531SMarcin Slusarz cgc->cmd[2] = (blk.from >> 24) & 0xff;
30913147c531SMarcin Slusarz cgc->cmd[3] = (blk.from >> 16) & 0xff;
30923147c531SMarcin Slusarz cgc->cmd[4] = (blk.from >> 8) & 0xff;
30933147c531SMarcin Slusarz cgc->cmd[5] = blk.from & 0xff;
30943147c531SMarcin Slusarz cgc->cmd[7] = (blk.len >> 8) & 0xff;
30953147c531SMarcin Slusarz cgc->cmd[8] = blk.len & 0xff;
30963147c531SMarcin Slusarz cgc->data_direction = CGC_DATA_NONE;
30973147c531SMarcin Slusarz return cdo->generic_packet(cdi, cgc);
30981da177e4SLinus Torvalds }
30993147c531SMarcin Slusarz
mmc_ioctl_cdrom_volume(struct cdrom_device_info * cdi,void __user * arg,struct packet_command * cgc,unsigned int cmd)31003147c531SMarcin Slusarz static noinline int mmc_ioctl_cdrom_volume(struct cdrom_device_info *cdi,
31013147c531SMarcin Slusarz void __user *arg,
31023147c531SMarcin Slusarz struct packet_command *cgc,
31033147c531SMarcin Slusarz unsigned int cmd)
31043147c531SMarcin Slusarz {
31051da177e4SLinus Torvalds struct cdrom_volctrl volctrl;
31063147c531SMarcin Slusarz unsigned char buffer[32];
31071da177e4SLinus Torvalds char mask[sizeof(buffer)];
31081da177e4SLinus Torvalds unsigned short offset;
31093147c531SMarcin Slusarz int ret;
31101da177e4SLinus Torvalds
31115944b2ceSJoe Perches cd_dbg(CD_DO_IOCTL, "entering CDROMVOLUME\n");
31121da177e4SLinus Torvalds
3113a09c391dSJoe Perches if (copy_from_user(&volctrl, (struct cdrom_volctrl __user *)arg,
3114a09c391dSJoe Perches sizeof(volctrl)))
3115a09c391dSJoe Perches return -EFAULT;
31161da177e4SLinus Torvalds
31173147c531SMarcin Slusarz cgc->buffer = buffer;
31183147c531SMarcin Slusarz cgc->buflen = 24;
31193147c531SMarcin Slusarz ret = cdrom_mode_sense(cdi, cgc, GPMODE_AUDIO_CTL_PAGE, 0);
31203147c531SMarcin Slusarz if (ret)
31211da177e4SLinus Torvalds return ret;
31221da177e4SLinus Torvalds
31231da177e4SLinus Torvalds /* originally the code depended on buffer[1] to determine
31241da177e4SLinus Torvalds how much data is available for transfer. buffer[1] is
31251da177e4SLinus Torvalds unfortunately ambigious and the only reliable way seem
31261da177e4SLinus Torvalds to be to simply skip over the block descriptor... */
312756052d52SAlexey Dobriyan offset = 8 + be16_to_cpu(*(__be16 *)(buffer + 6));
31281da177e4SLinus Torvalds
31291da177e4SLinus Torvalds if (offset + 16 > sizeof(buffer))
31301da177e4SLinus Torvalds return -E2BIG;
31311da177e4SLinus Torvalds
31323147c531SMarcin Slusarz if (offset + 16 > cgc->buflen) {
31333147c531SMarcin Slusarz cgc->buflen = offset + 16;
31343147c531SMarcin Slusarz ret = cdrom_mode_sense(cdi, cgc,
31351da177e4SLinus Torvalds GPMODE_AUDIO_CTL_PAGE, 0);
31361da177e4SLinus Torvalds if (ret)
31371da177e4SLinus Torvalds return ret;
31381da177e4SLinus Torvalds }
31391da177e4SLinus Torvalds
31401da177e4SLinus Torvalds /* sanity check */
31411da177e4SLinus Torvalds if ((buffer[offset] & 0x3f) != GPMODE_AUDIO_CTL_PAGE ||
31421da177e4SLinus Torvalds buffer[offset + 1] < 14)
31431da177e4SLinus Torvalds return -EINVAL;
31441da177e4SLinus Torvalds
31451da177e4SLinus Torvalds /* now we have the current volume settings. if it was only
31461da177e4SLinus Torvalds a CDROMVOLREAD, return these values */
31471da177e4SLinus Torvalds if (cmd == CDROMVOLREAD) {
31481da177e4SLinus Torvalds volctrl.channel0 = buffer[offset+9];
31491da177e4SLinus Torvalds volctrl.channel1 = buffer[offset+11];
31501da177e4SLinus Torvalds volctrl.channel2 = buffer[offset+13];
31511da177e4SLinus Torvalds volctrl.channel3 = buffer[offset+15];
3152a09c391dSJoe Perches if (copy_to_user((struct cdrom_volctrl __user *)arg, &volctrl,
3153a09c391dSJoe Perches sizeof(volctrl)))
3154a09c391dSJoe Perches return -EFAULT;
31551da177e4SLinus Torvalds return 0;
31561da177e4SLinus Torvalds }
31571da177e4SLinus Torvalds
31581da177e4SLinus Torvalds /* get the volume mask */
31593147c531SMarcin Slusarz cgc->buffer = mask;
31603147c531SMarcin Slusarz ret = cdrom_mode_sense(cdi, cgc, GPMODE_AUDIO_CTL_PAGE, 1);
31613147c531SMarcin Slusarz if (ret)
31621da177e4SLinus Torvalds return ret;
31631da177e4SLinus Torvalds
31641da177e4SLinus Torvalds buffer[offset + 9] = volctrl.channel0 & mask[offset + 9];
31651da177e4SLinus Torvalds buffer[offset + 11] = volctrl.channel1 & mask[offset + 11];
31661da177e4SLinus Torvalds buffer[offset + 13] = volctrl.channel2 & mask[offset + 13];
31671da177e4SLinus Torvalds buffer[offset + 15] = volctrl.channel3 & mask[offset + 15];
31681da177e4SLinus Torvalds
31691da177e4SLinus Torvalds /* set volume */
31703147c531SMarcin Slusarz cgc->buffer = buffer + offset - 8;
31713147c531SMarcin Slusarz memset(cgc->buffer, 0, 8);
31723147c531SMarcin Slusarz return cdrom_mode_select(cdi, cgc);
31731da177e4SLinus Torvalds }
31741da177e4SLinus Torvalds
mmc_ioctl_cdrom_start_stop(struct cdrom_device_info * cdi,struct packet_command * cgc,int cmd)31753147c531SMarcin Slusarz static noinline int mmc_ioctl_cdrom_start_stop(struct cdrom_device_info *cdi,
31763147c531SMarcin Slusarz struct packet_command *cgc,
31773147c531SMarcin Slusarz int cmd)
31783147c531SMarcin Slusarz {
3179853fe1bfSKees Cook const struct cdrom_device_ops *cdo = cdi->ops;
31805944b2ceSJoe Perches cd_dbg(CD_DO_IOCTL, "entering CDROMSTART/CDROMSTOP\n");
31813147c531SMarcin Slusarz cgc->cmd[0] = GPCMD_START_STOP_UNIT;
31823147c531SMarcin Slusarz cgc->cmd[1] = 1;
31833147c531SMarcin Slusarz cgc->cmd[4] = (cmd == CDROMSTART) ? 1 : 0;
31843147c531SMarcin Slusarz cgc->data_direction = CGC_DATA_NONE;
31853147c531SMarcin Slusarz return cdo->generic_packet(cdi, cgc);
31861da177e4SLinus Torvalds }
31871da177e4SLinus Torvalds
mmc_ioctl_cdrom_pause_resume(struct cdrom_device_info * cdi,struct packet_command * cgc,int cmd)31883147c531SMarcin Slusarz static noinline int mmc_ioctl_cdrom_pause_resume(struct cdrom_device_info *cdi,
31893147c531SMarcin Slusarz struct packet_command *cgc,
31903147c531SMarcin Slusarz int cmd)
31913147c531SMarcin Slusarz {
3192853fe1bfSKees Cook const struct cdrom_device_ops *cdo = cdi->ops;
31935944b2ceSJoe Perches cd_dbg(CD_DO_IOCTL, "entering CDROMPAUSE/CDROMRESUME\n");
31943147c531SMarcin Slusarz cgc->cmd[0] = GPCMD_PAUSE_RESUME;
31953147c531SMarcin Slusarz cgc->cmd[8] = (cmd == CDROMRESUME) ? 1 : 0;
31963147c531SMarcin Slusarz cgc->data_direction = CGC_DATA_NONE;
31973147c531SMarcin Slusarz return cdo->generic_packet(cdi, cgc);
31981da177e4SLinus Torvalds }
31991da177e4SLinus Torvalds
mmc_ioctl_dvd_read_struct(struct cdrom_device_info * cdi,void __user * arg,struct packet_command * cgc)32003147c531SMarcin Slusarz static noinline int mmc_ioctl_dvd_read_struct(struct cdrom_device_info *cdi,
3201d194139cSMarcin Slusarz void __user *arg,
3202d194139cSMarcin Slusarz struct packet_command *cgc)
32033147c531SMarcin Slusarz {
32043147c531SMarcin Slusarz int ret;
32051da177e4SLinus Torvalds dvd_struct *s;
32061da177e4SLinus Torvalds int size = sizeof(dvd_struct);
32073147c531SMarcin Slusarz
32081da177e4SLinus Torvalds if (!CDROM_CAN(CDC_DVD))
32091da177e4SLinus Torvalds return -ENOSYS;
32103147c531SMarcin Slusarz
3211abb0f6a7SAl Viro s = memdup_user(arg, size);
3212abb0f6a7SAl Viro if (IS_ERR(s))
3213abb0f6a7SAl Viro return PTR_ERR(s);
32143147c531SMarcin Slusarz
32155944b2ceSJoe Perches cd_dbg(CD_DO_IOCTL, "entering DVD_READ_STRUCT\n");
32163147c531SMarcin Slusarz
3217d194139cSMarcin Slusarz ret = dvd_read_struct(cdi, s, cgc);
32183147c531SMarcin Slusarz if (ret)
32193147c531SMarcin Slusarz goto out;
32203147c531SMarcin Slusarz
32213147c531SMarcin Slusarz if (copy_to_user(arg, s, size))
32221da177e4SLinus Torvalds ret = -EFAULT;
32233147c531SMarcin Slusarz out:
32241da177e4SLinus Torvalds kfree(s);
32251da177e4SLinus Torvalds return ret;
32261da177e4SLinus Torvalds }
32271da177e4SLinus Torvalds
mmc_ioctl_dvd_auth(struct cdrom_device_info * cdi,void __user * arg)32283147c531SMarcin Slusarz static noinline int mmc_ioctl_dvd_auth(struct cdrom_device_info *cdi,
32293147c531SMarcin Slusarz void __user *arg)
32303147c531SMarcin Slusarz {
32313147c531SMarcin Slusarz int ret;
32321da177e4SLinus Torvalds dvd_authinfo ai;
32331da177e4SLinus Torvalds if (!CDROM_CAN(CDC_DVD))
32341da177e4SLinus Torvalds return -ENOSYS;
32355944b2ceSJoe Perches cd_dbg(CD_DO_IOCTL, "entering DVD_AUTH\n");
3236a09c391dSJoe Perches if (copy_from_user(&ai, (dvd_authinfo __user *)arg, sizeof(ai)))
3237a09c391dSJoe Perches return -EFAULT;
32383147c531SMarcin Slusarz ret = dvd_do_auth(cdi, &ai);
32393147c531SMarcin Slusarz if (ret)
32401da177e4SLinus Torvalds return ret;
3241a09c391dSJoe Perches if (copy_to_user((dvd_authinfo __user *)arg, &ai, sizeof(ai)))
3242a09c391dSJoe Perches return -EFAULT;
32431da177e4SLinus Torvalds return 0;
32441da177e4SLinus Torvalds }
32451da177e4SLinus Torvalds
mmc_ioctl_cdrom_next_writable(struct cdrom_device_info * cdi,void __user * arg)32463147c531SMarcin Slusarz static noinline int mmc_ioctl_cdrom_next_writable(struct cdrom_device_info *cdi,
32473147c531SMarcin Slusarz void __user *arg)
32483147c531SMarcin Slusarz {
32493147c531SMarcin Slusarz int ret;
32501da177e4SLinus Torvalds long next = 0;
32515944b2ceSJoe Perches cd_dbg(CD_DO_IOCTL, "entering CDROM_NEXT_WRITABLE\n");
32523147c531SMarcin Slusarz ret = cdrom_get_next_writable(cdi, &next);
32533147c531SMarcin Slusarz if (ret)
32541da177e4SLinus Torvalds return ret;
3255a09c391dSJoe Perches if (copy_to_user((long __user *)arg, &next, sizeof(next)))
3256a09c391dSJoe Perches return -EFAULT;
32571da177e4SLinus Torvalds return 0;
32581da177e4SLinus Torvalds }
32593147c531SMarcin Slusarz
mmc_ioctl_cdrom_last_written(struct cdrom_device_info * cdi,void __user * arg)32603147c531SMarcin Slusarz static noinline int mmc_ioctl_cdrom_last_written(struct cdrom_device_info *cdi,
32613147c531SMarcin Slusarz void __user *arg)
32623147c531SMarcin Slusarz {
32633147c531SMarcin Slusarz int ret;
32641da177e4SLinus Torvalds long last = 0;
32655944b2ceSJoe Perches cd_dbg(CD_DO_IOCTL, "entering CDROM_LAST_WRITTEN\n");
32663147c531SMarcin Slusarz ret = cdrom_get_last_written(cdi, &last);
32673147c531SMarcin Slusarz if (ret)
32681da177e4SLinus Torvalds return ret;
3269ab8bc541SArnd Bergmann if (in_compat_syscall())
3270ab8bc541SArnd Bergmann return put_user(last, (__s32 __user *)arg);
3271ab8bc541SArnd Bergmann
3272ab8bc541SArnd Bergmann return put_user(last, (long __user *)arg);
32731da177e4SLinus Torvalds }
32743147c531SMarcin Slusarz
mmc_ioctl(struct cdrom_device_info * cdi,unsigned int cmd,unsigned long arg)32753147c531SMarcin Slusarz static int mmc_ioctl(struct cdrom_device_info *cdi, unsigned int cmd,
32763147c531SMarcin Slusarz unsigned long arg)
32773147c531SMarcin Slusarz {
32783147c531SMarcin Slusarz struct packet_command cgc;
32793147c531SMarcin Slusarz void __user *userptr = (void __user *)arg;
32803147c531SMarcin Slusarz
32813147c531SMarcin Slusarz memset(&cgc, 0, sizeof(cgc));
32823147c531SMarcin Slusarz
32833147c531SMarcin Slusarz /* build a unified command and queue it through
32843147c531SMarcin Slusarz cdo->generic_packet() */
32853147c531SMarcin Slusarz switch (cmd) {
32863147c531SMarcin Slusarz case CDROMREADRAW:
32873147c531SMarcin Slusarz case CDROMREADMODE1:
32883147c531SMarcin Slusarz case CDROMREADMODE2:
32893147c531SMarcin Slusarz return mmc_ioctl_cdrom_read_data(cdi, userptr, &cgc, cmd);
32903147c531SMarcin Slusarz case CDROMREADAUDIO:
32913147c531SMarcin Slusarz return mmc_ioctl_cdrom_read_audio(cdi, userptr);
32923147c531SMarcin Slusarz case CDROMSUBCHNL:
32933147c531SMarcin Slusarz return mmc_ioctl_cdrom_subchannel(cdi, userptr);
32943147c531SMarcin Slusarz case CDROMPLAYMSF:
32953147c531SMarcin Slusarz return mmc_ioctl_cdrom_play_msf(cdi, userptr, &cgc);
32963147c531SMarcin Slusarz case CDROMPLAYBLK:
32973147c531SMarcin Slusarz return mmc_ioctl_cdrom_play_blk(cdi, userptr, &cgc);
32983147c531SMarcin Slusarz case CDROMVOLCTRL:
32993147c531SMarcin Slusarz case CDROMVOLREAD:
33003147c531SMarcin Slusarz return mmc_ioctl_cdrom_volume(cdi, userptr, &cgc, cmd);
33013147c531SMarcin Slusarz case CDROMSTART:
33023147c531SMarcin Slusarz case CDROMSTOP:
33033147c531SMarcin Slusarz return mmc_ioctl_cdrom_start_stop(cdi, &cgc, cmd);
33043147c531SMarcin Slusarz case CDROMPAUSE:
33053147c531SMarcin Slusarz case CDROMRESUME:
33063147c531SMarcin Slusarz return mmc_ioctl_cdrom_pause_resume(cdi, &cgc, cmd);
33073147c531SMarcin Slusarz case DVD_READ_STRUCT:
3308d194139cSMarcin Slusarz return mmc_ioctl_dvd_read_struct(cdi, userptr, &cgc);
33093147c531SMarcin Slusarz case DVD_AUTH:
33103147c531SMarcin Slusarz return mmc_ioctl_dvd_auth(cdi, userptr);
33113147c531SMarcin Slusarz case CDROM_NEXT_WRITABLE:
33123147c531SMarcin Slusarz return mmc_ioctl_cdrom_next_writable(cdi, userptr);
33133147c531SMarcin Slusarz case CDROM_LAST_WRITTEN:
33143147c531SMarcin Slusarz return mmc_ioctl_cdrom_last_written(cdi, userptr);
33153147c531SMarcin Slusarz }
33161da177e4SLinus Torvalds
33171da177e4SLinus Torvalds return -ENOTTY;
33181da177e4SLinus Torvalds }
33191da177e4SLinus Torvalds
33202e9aa08aSJoe Perches /*
33212e9aa08aSJoe Perches * Just about every imaginable ioctl is supported in the Uniform layer
33222e9aa08aSJoe Perches * these days.
33232e9aa08aSJoe Perches * ATAPI / SCSI specific code now mainly resides in mmc_ioctl().
33242e9aa08aSJoe Perches */
cdrom_ioctl(struct cdrom_device_info * cdi,struct block_device * bdev,unsigned int cmd,unsigned long arg)33252e9aa08aSJoe Perches int cdrom_ioctl(struct cdrom_device_info *cdi, struct block_device *bdev,
3326473399b5SChristoph Hellwig unsigned int cmd, unsigned long arg)
33272e9aa08aSJoe Perches {
33282e9aa08aSJoe Perches void __user *argp = (void __user *)arg;
33292e9aa08aSJoe Perches int ret;
33302e9aa08aSJoe Perches
33312e9aa08aSJoe Perches switch (cmd) {
33322e9aa08aSJoe Perches case CDROMMULTISESSION:
33332e9aa08aSJoe Perches return cdrom_ioctl_multisession(cdi, argp);
33342e9aa08aSJoe Perches case CDROMEJECT:
33352e9aa08aSJoe Perches return cdrom_ioctl_eject(cdi);
33362e9aa08aSJoe Perches case CDROMCLOSETRAY:
33372e9aa08aSJoe Perches return cdrom_ioctl_closetray(cdi);
33382e9aa08aSJoe Perches case CDROMEJECT_SW:
33392e9aa08aSJoe Perches return cdrom_ioctl_eject_sw(cdi, arg);
33402e9aa08aSJoe Perches case CDROM_MEDIA_CHANGED:
33412e9aa08aSJoe Perches return cdrom_ioctl_media_changed(cdi, arg);
334267f1e027SLukas Prediger case CDROM_TIMED_MEDIA_CHANGE:
334367f1e027SLukas Prediger return cdrom_ioctl_timed_media_change(cdi, arg);
33442e9aa08aSJoe Perches case CDROM_SET_OPTIONS:
33452e9aa08aSJoe Perches return cdrom_ioctl_set_options(cdi, arg);
33462e9aa08aSJoe Perches case CDROM_CLEAR_OPTIONS:
33472e9aa08aSJoe Perches return cdrom_ioctl_clear_options(cdi, arg);
33482e9aa08aSJoe Perches case CDROM_SELECT_SPEED:
33492e9aa08aSJoe Perches return cdrom_ioctl_select_speed(cdi, arg);
33502e9aa08aSJoe Perches case CDROM_SELECT_DISC:
33512e9aa08aSJoe Perches return cdrom_ioctl_select_disc(cdi, arg);
33522e9aa08aSJoe Perches case CDROMRESET:
33532e9aa08aSJoe Perches return cdrom_ioctl_reset(cdi, bdev);
33542e9aa08aSJoe Perches case CDROM_LOCKDOOR:
33552e9aa08aSJoe Perches return cdrom_ioctl_lock_door(cdi, arg);
33562e9aa08aSJoe Perches case CDROM_DEBUG:
33572e9aa08aSJoe Perches return cdrom_ioctl_debug(cdi, arg);
33582e9aa08aSJoe Perches case CDROM_GET_CAPABILITY:
33592e9aa08aSJoe Perches return cdrom_ioctl_get_capability(cdi);
33602e9aa08aSJoe Perches case CDROM_GET_MCN:
33612e9aa08aSJoe Perches return cdrom_ioctl_get_mcn(cdi, argp);
33622e9aa08aSJoe Perches case CDROM_DRIVE_STATUS:
33632e9aa08aSJoe Perches return cdrom_ioctl_drive_status(cdi, arg);
33642e9aa08aSJoe Perches case CDROM_DISC_STATUS:
33652e9aa08aSJoe Perches return cdrom_ioctl_disc_status(cdi);
33662e9aa08aSJoe Perches case CDROM_CHANGER_NSLOTS:
33672e9aa08aSJoe Perches return cdrom_ioctl_changer_nslots(cdi);
33682e9aa08aSJoe Perches }
33692e9aa08aSJoe Perches
33702e9aa08aSJoe Perches /*
33712e9aa08aSJoe Perches * Use the ioctls that are implemented through the generic_packet()
33722e9aa08aSJoe Perches * interface. this may look at bit funny, but if -ENOTTY is
33732e9aa08aSJoe Perches * returned that particular ioctl is not implemented and we
33742e9aa08aSJoe Perches * let it go through the device specific ones.
33752e9aa08aSJoe Perches */
33762e9aa08aSJoe Perches if (CDROM_CAN(CDC_GENERIC_PACKET)) {
33772e9aa08aSJoe Perches ret = mmc_ioctl(cdi, cmd, arg);
33782e9aa08aSJoe Perches if (ret != -ENOTTY)
33792e9aa08aSJoe Perches return ret;
33802e9aa08aSJoe Perches }
33812e9aa08aSJoe Perches
33822e9aa08aSJoe Perches /*
33832e9aa08aSJoe Perches * Note: most of the cd_dbg() calls are commented out here,
33842e9aa08aSJoe Perches * because they fill up the sys log when CD players poll
33852e9aa08aSJoe Perches * the drive.
33862e9aa08aSJoe Perches */
33872e9aa08aSJoe Perches switch (cmd) {
33882e9aa08aSJoe Perches case CDROMSUBCHNL:
33892e9aa08aSJoe Perches return cdrom_ioctl_get_subchnl(cdi, argp);
33902e9aa08aSJoe Perches case CDROMREADTOCHDR:
33912e9aa08aSJoe Perches return cdrom_ioctl_read_tochdr(cdi, argp);
33922e9aa08aSJoe Perches case CDROMREADTOCENTRY:
33932e9aa08aSJoe Perches return cdrom_ioctl_read_tocentry(cdi, argp);
33942e9aa08aSJoe Perches case CDROMPLAYMSF:
33952e9aa08aSJoe Perches return cdrom_ioctl_play_msf(cdi, argp);
33962e9aa08aSJoe Perches case CDROMPLAYTRKIND:
33972e9aa08aSJoe Perches return cdrom_ioctl_play_trkind(cdi, argp);
33982e9aa08aSJoe Perches case CDROMVOLCTRL:
33992e9aa08aSJoe Perches return cdrom_ioctl_volctrl(cdi, argp);
34002e9aa08aSJoe Perches case CDROMVOLREAD:
34012e9aa08aSJoe Perches return cdrom_ioctl_volread(cdi, argp);
34022e9aa08aSJoe Perches case CDROMSTART:
34032e9aa08aSJoe Perches case CDROMSTOP:
34042e9aa08aSJoe Perches case CDROMPAUSE:
34052e9aa08aSJoe Perches case CDROMRESUME:
34062e9aa08aSJoe Perches return cdrom_ioctl_audioctl(cdi, cmd);
34072e9aa08aSJoe Perches }
34082e9aa08aSJoe Perches
34092e9aa08aSJoe Perches return -ENOSYS;
34102e9aa08aSJoe Perches }
34111da177e4SLinus Torvalds EXPORT_SYMBOL(cdrom_ioctl);
34121da177e4SLinus Torvalds
34131da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
34141da177e4SLinus Torvalds
34151da177e4SLinus Torvalds #define CDROM_STR_SIZE 1000
34161da177e4SLinus Torvalds
34171da177e4SLinus Torvalds static struct cdrom_sysctl_settings {
34181da177e4SLinus Torvalds char info[CDROM_STR_SIZE]; /* general info */
34191da177e4SLinus Torvalds int autoclose; /* close tray upon mount, etc */
34201da177e4SLinus Torvalds int autoeject; /* eject on umount */
34211da177e4SLinus Torvalds int debug; /* turn on debugging messages */
34221da177e4SLinus Torvalds int lock; /* lock the door on device open */
34231da177e4SLinus Torvalds int check; /* check media type */
34241da177e4SLinus Torvalds } cdrom_sysctl_settings;
34251da177e4SLinus Torvalds
3426554988d6SDave Young enum cdrom_print_option {
3427554988d6SDave Young CTL_NAME,
3428554988d6SDave Young CTL_SPEED,
3429554988d6SDave Young CTL_SLOTS,
3430554988d6SDave Young CTL_CAPABILITY
3431554988d6SDave Young };
3432554988d6SDave Young
cdrom_print_info(const char * header,int val,char * info,int * pos,enum cdrom_print_option option)3433554988d6SDave Young static int cdrom_print_info(const char *header, int val, char *info,
3434554988d6SDave Young int *pos, enum cdrom_print_option option)
3435554988d6SDave Young {
3436554988d6SDave Young const int max_size = sizeof(cdrom_sysctl_settings.info);
3437554988d6SDave Young struct cdrom_device_info *cdi;
3438554988d6SDave Young int ret;
3439554988d6SDave Young
3440554988d6SDave Young ret = scnprintf(info + *pos, max_size - *pos, header);
3441554988d6SDave Young if (!ret)
3442554988d6SDave Young return 1;
3443554988d6SDave Young
3444554988d6SDave Young *pos += ret;
3445554988d6SDave Young
34467fd097d4SAkinobu Mita list_for_each_entry(cdi, &cdrom_list, list) {
3447554988d6SDave Young switch (option) {
3448554988d6SDave Young case CTL_NAME:
3449554988d6SDave Young ret = scnprintf(info + *pos, max_size - *pos,
3450554988d6SDave Young "\t%s", cdi->name);
3451554988d6SDave Young break;
3452554988d6SDave Young case CTL_SPEED:
3453554988d6SDave Young ret = scnprintf(info + *pos, max_size - *pos,
3454554988d6SDave Young "\t%d", cdi->speed);
3455554988d6SDave Young break;
3456554988d6SDave Young case CTL_SLOTS:
3457554988d6SDave Young ret = scnprintf(info + *pos, max_size - *pos,
3458554988d6SDave Young "\t%d", cdi->capacity);
3459554988d6SDave Young break;
3460554988d6SDave Young case CTL_CAPABILITY:
3461554988d6SDave Young ret = scnprintf(info + *pos, max_size - *pos,
3462554988d6SDave Young "\t%d", CDROM_CAN(val) != 0);
3463554988d6SDave Young break;
3464554988d6SDave Young default:
3465e597cd09SJoe Perches pr_info("invalid option%d\n", option);
3466554988d6SDave Young return 1;
3467554988d6SDave Young }
3468554988d6SDave Young if (!ret)
3469554988d6SDave Young return 1;
3470554988d6SDave Young *pos += ret;
3471554988d6SDave Young }
3472554988d6SDave Young
3473554988d6SDave Young return 0;
3474554988d6SDave Young }
3475554988d6SDave Young
cdrom_sysctl_info(struct ctl_table * ctl,int write,void * buffer,size_t * lenp,loff_t * ppos)347690a3b89eSJoe Perches static int cdrom_sysctl_info(struct ctl_table *ctl, int write,
34778c46fa96SChristoph Hellwig void *buffer, size_t *lenp, loff_t *ppos)
34781da177e4SLinus Torvalds {
34791da177e4SLinus Torvalds int pos;
34801da177e4SLinus Torvalds char *info = cdrom_sysctl_settings.info;
3481554988d6SDave Young const int max_size = sizeof(cdrom_sysctl_settings.info);
34821da177e4SLinus Torvalds
34831da177e4SLinus Torvalds if (!*lenp || (*ppos && !write)) {
34841da177e4SLinus Torvalds *lenp = 0;
34851da177e4SLinus Torvalds return 0;
34861da177e4SLinus Torvalds }
34871da177e4SLinus Torvalds
3488554988d6SDave Young mutex_lock(&cdrom_mutex);
3489554988d6SDave Young
34901da177e4SLinus Torvalds pos = sprintf(info, "CD-ROM information, " VERSION "\n");
34911da177e4SLinus Torvalds
3492554988d6SDave Young if (cdrom_print_info("\ndrive name:\t", 0, info, &pos, CTL_NAME))
3493554988d6SDave Young goto done;
3494554988d6SDave Young if (cdrom_print_info("\ndrive speed:\t", 0, info, &pos, CTL_SPEED))
3495554988d6SDave Young goto done;
3496554988d6SDave Young if (cdrom_print_info("\ndrive # of slots:", 0, info, &pos, CTL_SLOTS))
3497554988d6SDave Young goto done;
3498554988d6SDave Young if (cdrom_print_info("\nCan close tray:\t",
3499554988d6SDave Young CDC_CLOSE_TRAY, info, &pos, CTL_CAPABILITY))
3500554988d6SDave Young goto done;
3501554988d6SDave Young if (cdrom_print_info("\nCan open tray:\t",
3502554988d6SDave Young CDC_OPEN_TRAY, info, &pos, CTL_CAPABILITY))
3503554988d6SDave Young goto done;
3504554988d6SDave Young if (cdrom_print_info("\nCan lock tray:\t",
3505554988d6SDave Young CDC_LOCK, info, &pos, CTL_CAPABILITY))
3506554988d6SDave Young goto done;
3507554988d6SDave Young if (cdrom_print_info("\nCan change speed:",
3508554988d6SDave Young CDC_SELECT_SPEED, info, &pos, CTL_CAPABILITY))
3509554988d6SDave Young goto done;
3510554988d6SDave Young if (cdrom_print_info("\nCan select disk:",
3511554988d6SDave Young CDC_SELECT_DISC, info, &pos, CTL_CAPABILITY))
3512554988d6SDave Young goto done;
3513554988d6SDave Young if (cdrom_print_info("\nCan read multisession:",
3514554988d6SDave Young CDC_MULTI_SESSION, info, &pos, CTL_CAPABILITY))
3515554988d6SDave Young goto done;
3516554988d6SDave Young if (cdrom_print_info("\nCan read MCN:\t",
3517554988d6SDave Young CDC_MCN, info, &pos, CTL_CAPABILITY))
3518554988d6SDave Young goto done;
3519554988d6SDave Young if (cdrom_print_info("\nReports media changed:",
3520554988d6SDave Young CDC_MEDIA_CHANGED, info, &pos, CTL_CAPABILITY))
3521554988d6SDave Young goto done;
3522554988d6SDave Young if (cdrom_print_info("\nCan play audio:\t",
3523554988d6SDave Young CDC_PLAY_AUDIO, info, &pos, CTL_CAPABILITY))
3524554988d6SDave Young goto done;
3525554988d6SDave Young if (cdrom_print_info("\nCan write CD-R:\t",
3526554988d6SDave Young CDC_CD_R, info, &pos, CTL_CAPABILITY))
3527554988d6SDave Young goto done;
3528554988d6SDave Young if (cdrom_print_info("\nCan write CD-RW:",
3529554988d6SDave Young CDC_CD_RW, info, &pos, CTL_CAPABILITY))
3530554988d6SDave Young goto done;
3531554988d6SDave Young if (cdrom_print_info("\nCan read DVD:\t",
3532554988d6SDave Young CDC_DVD, info, &pos, CTL_CAPABILITY))
3533554988d6SDave Young goto done;
3534554988d6SDave Young if (cdrom_print_info("\nCan write DVD-R:",
3535554988d6SDave Young CDC_DVD_R, info, &pos, CTL_CAPABILITY))
3536554988d6SDave Young goto done;
3537554988d6SDave Young if (cdrom_print_info("\nCan write DVD-RAM:",
3538554988d6SDave Young CDC_DVD_RAM, info, &pos, CTL_CAPABILITY))
3539554988d6SDave Young goto done;
3540554988d6SDave Young if (cdrom_print_info("\nCan read MRW:\t",
3541554988d6SDave Young CDC_MRW, info, &pos, CTL_CAPABILITY))
3542554988d6SDave Young goto done;
3543554988d6SDave Young if (cdrom_print_info("\nCan write MRW:\t",
3544554988d6SDave Young CDC_MRW_W, info, &pos, CTL_CAPABILITY))
3545554988d6SDave Young goto done;
3546554988d6SDave Young if (cdrom_print_info("\nCan write RAM:\t",
3547554988d6SDave Young CDC_RAM, info, &pos, CTL_CAPABILITY))
3548554988d6SDave Young goto done;
3549554988d6SDave Young if (!scnprintf(info + pos, max_size - pos, "\n\n"))
3550554988d6SDave Young goto done;
3551554988d6SDave Young doit:
3552554988d6SDave Young mutex_unlock(&cdrom_mutex);
35538d65af78SAlexey Dobriyan return proc_dostring(ctl, write, buffer, lenp, ppos);
3554554988d6SDave Young done:
3555e597cd09SJoe Perches pr_info("info buffer too small\n");
3556554988d6SDave Young goto doit;
35571da177e4SLinus Torvalds }
35581da177e4SLinus Torvalds
35591da177e4SLinus Torvalds /* Unfortunately, per device settings are not implemented through
35601da177e4SLinus Torvalds procfs/sysctl yet. When they are, this will naturally disappear. For now
35611da177e4SLinus Torvalds just update all drives. Later this will become the template on which
35621da177e4SLinus Torvalds new registered drives will be based. */
cdrom_update_settings(void)35631da177e4SLinus Torvalds static void cdrom_update_settings(void)
35641da177e4SLinus Torvalds {
35651da177e4SLinus Torvalds struct cdrom_device_info *cdi;
35661da177e4SLinus Torvalds
3567032d8d90SAkinobu Mita mutex_lock(&cdrom_mutex);
35687fd097d4SAkinobu Mita list_for_each_entry(cdi, &cdrom_list, list) {
35691da177e4SLinus Torvalds if (autoclose && CDROM_CAN(CDC_CLOSE_TRAY))
35701da177e4SLinus Torvalds cdi->options |= CDO_AUTO_CLOSE;
35711da177e4SLinus Torvalds else if (!autoclose)
35721da177e4SLinus Torvalds cdi->options &= ~CDO_AUTO_CLOSE;
35731da177e4SLinus Torvalds if (autoeject && CDROM_CAN(CDC_OPEN_TRAY))
35741da177e4SLinus Torvalds cdi->options |= CDO_AUTO_EJECT;
35751da177e4SLinus Torvalds else if (!autoeject)
35761da177e4SLinus Torvalds cdi->options &= ~CDO_AUTO_EJECT;
35771da177e4SLinus Torvalds if (lockdoor && CDROM_CAN(CDC_LOCK))
35781da177e4SLinus Torvalds cdi->options |= CDO_LOCK;
35791da177e4SLinus Torvalds else if (!lockdoor)
35801da177e4SLinus Torvalds cdi->options &= ~CDO_LOCK;
35811da177e4SLinus Torvalds if (check_media_type)
35821da177e4SLinus Torvalds cdi->options |= CDO_CHECK_TYPE;
35831da177e4SLinus Torvalds else
35841da177e4SLinus Torvalds cdi->options &= ~CDO_CHECK_TYPE;
35851da177e4SLinus Torvalds }
3586032d8d90SAkinobu Mita mutex_unlock(&cdrom_mutex);
35871da177e4SLinus Torvalds }
35881da177e4SLinus Torvalds
cdrom_sysctl_handler(struct ctl_table * ctl,int write,void * buffer,size_t * lenp,loff_t * ppos)358990a3b89eSJoe Perches static int cdrom_sysctl_handler(struct ctl_table *ctl, int write,
359032927393SChristoph Hellwig void *buffer, size_t *lenp, loff_t *ppos)
35911da177e4SLinus Torvalds {
35921da177e4SLinus Torvalds int ret;
35931da177e4SLinus Torvalds
35948d65af78SAlexey Dobriyan ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
35951da177e4SLinus Torvalds
359606489b4eSEric W. Biederman if (write) {
35971da177e4SLinus Torvalds
35981da177e4SLinus Torvalds /* we only care for 1 or 0. */
359906489b4eSEric W. Biederman autoclose = !!cdrom_sysctl_settings.autoclose;
360006489b4eSEric W. Biederman autoeject = !!cdrom_sysctl_settings.autoeject;
360106489b4eSEric W. Biederman debug = !!cdrom_sysctl_settings.debug;
360206489b4eSEric W. Biederman lockdoor = !!cdrom_sysctl_settings.lock;
360306489b4eSEric W. Biederman check_media_type = !!cdrom_sysctl_settings.check;
36041da177e4SLinus Torvalds
36051da177e4SLinus Torvalds /* update the option flags according to the changes. we
36061da177e4SLinus Torvalds don't have per device options through sysctl yet,
36071da177e4SLinus Torvalds but we will have and then this will disappear. */
36081da177e4SLinus Torvalds cdrom_update_settings();
36091da177e4SLinus Torvalds }
36101da177e4SLinus Torvalds
36111da177e4SLinus Torvalds return ret;
36121da177e4SLinus Torvalds }
36131da177e4SLinus Torvalds
36141da177e4SLinus Torvalds /* Place files in /proc/sys/dev/cdrom */
361590a3b89eSJoe Perches static struct ctl_table cdrom_table[] = {
36161da177e4SLinus Torvalds {
36171da177e4SLinus Torvalds .procname = "info",
36181da177e4SLinus Torvalds .data = &cdrom_sysctl_settings.info,
36191da177e4SLinus Torvalds .maxlen = CDROM_STR_SIZE,
36201da177e4SLinus Torvalds .mode = 0444,
36216d456111SEric W. Biederman .proc_handler = cdrom_sysctl_info,
36221da177e4SLinus Torvalds },
36231da177e4SLinus Torvalds {
36241da177e4SLinus Torvalds .procname = "autoclose",
36251da177e4SLinus Torvalds .data = &cdrom_sysctl_settings.autoclose,
36261da177e4SLinus Torvalds .maxlen = sizeof(int),
36271da177e4SLinus Torvalds .mode = 0644,
36286d456111SEric W. Biederman .proc_handler = cdrom_sysctl_handler,
36291da177e4SLinus Torvalds },
36301da177e4SLinus Torvalds {
36311da177e4SLinus Torvalds .procname = "autoeject",
36321da177e4SLinus Torvalds .data = &cdrom_sysctl_settings.autoeject,
36331da177e4SLinus Torvalds .maxlen = sizeof(int),
36341da177e4SLinus Torvalds .mode = 0644,
36356d456111SEric W. Biederman .proc_handler = cdrom_sysctl_handler,
36361da177e4SLinus Torvalds },
36371da177e4SLinus Torvalds {
36381da177e4SLinus Torvalds .procname = "debug",
36391da177e4SLinus Torvalds .data = &cdrom_sysctl_settings.debug,
36401da177e4SLinus Torvalds .maxlen = sizeof(int),
36411da177e4SLinus Torvalds .mode = 0644,
36426d456111SEric W. Biederman .proc_handler = cdrom_sysctl_handler,
36431da177e4SLinus Torvalds },
36441da177e4SLinus Torvalds {
36451da177e4SLinus Torvalds .procname = "lock",
36461da177e4SLinus Torvalds .data = &cdrom_sysctl_settings.lock,
36471da177e4SLinus Torvalds .maxlen = sizeof(int),
36481da177e4SLinus Torvalds .mode = 0644,
36496d456111SEric W. Biederman .proc_handler = cdrom_sysctl_handler,
36501da177e4SLinus Torvalds },
36511da177e4SLinus Torvalds {
36521da177e4SLinus Torvalds .procname = "check_media",
36531da177e4SLinus Torvalds .data = &cdrom_sysctl_settings.check,
36541da177e4SLinus Torvalds .maxlen = sizeof(int),
36551da177e4SLinus Torvalds .mode = 0644,
36566d456111SEric W. Biederman .proc_handler = cdrom_sysctl_handler
36571da177e4SLinus Torvalds },
3658894d2491SEric W. Biederman { }
36591da177e4SLinus Torvalds };
36601da177e4SLinus Torvalds static struct ctl_table_header *cdrom_sysctl_header;
36611da177e4SLinus Torvalds
cdrom_sysctl_register(void)36621da177e4SLinus Torvalds static void cdrom_sysctl_register(void)
36631da177e4SLinus Torvalds {
3664f25191bbSGuenter Roeck static atomic_t initialized = ATOMIC_INIT(0);
36651da177e4SLinus Torvalds
3666f25191bbSGuenter Roeck if (!atomic_add_unless(&initialized, 1, 1))
36671da177e4SLinus Torvalds return;
36681da177e4SLinus Torvalds
3669ad8f7431SLuis Chamberlain cdrom_sysctl_header = register_sysctl("dev/cdrom", cdrom_table);
36701da177e4SLinus Torvalds
36711da177e4SLinus Torvalds /* set the defaults */
36721da177e4SLinus Torvalds cdrom_sysctl_settings.autoclose = autoclose;
36731da177e4SLinus Torvalds cdrom_sysctl_settings.autoeject = autoeject;
36741da177e4SLinus Torvalds cdrom_sysctl_settings.debug = debug;
36751da177e4SLinus Torvalds cdrom_sysctl_settings.lock = lockdoor;
36761da177e4SLinus Torvalds cdrom_sysctl_settings.check = check_media_type;
36771da177e4SLinus Torvalds }
36781da177e4SLinus Torvalds
cdrom_sysctl_unregister(void)36791da177e4SLinus Torvalds static void cdrom_sysctl_unregister(void)
36801da177e4SLinus Torvalds {
36811da177e4SLinus Torvalds if (cdrom_sysctl_header)
36821da177e4SLinus Torvalds unregister_sysctl_table(cdrom_sysctl_header);
36831da177e4SLinus Torvalds }
36841da177e4SLinus Torvalds
368517672cf0SAkinobu Mita #else /* CONFIG_SYSCTL */
368617672cf0SAkinobu Mita
cdrom_sysctl_register(void)368717672cf0SAkinobu Mita static void cdrom_sysctl_register(void)
368817672cf0SAkinobu Mita {
368917672cf0SAkinobu Mita }
369017672cf0SAkinobu Mita
cdrom_sysctl_unregister(void)369117672cf0SAkinobu Mita static void cdrom_sysctl_unregister(void)
369217672cf0SAkinobu Mita {
369317672cf0SAkinobu Mita }
369417672cf0SAkinobu Mita
36951da177e4SLinus Torvalds #endif /* CONFIG_SYSCTL */
36961da177e4SLinus Torvalds
cdrom_init(void)36971da177e4SLinus Torvalds static int __init cdrom_init(void)
36981da177e4SLinus Torvalds {
36991da177e4SLinus Torvalds cdrom_sysctl_register();
370017672cf0SAkinobu Mita
37011da177e4SLinus Torvalds return 0;
37021da177e4SLinus Torvalds }
37031da177e4SLinus Torvalds
cdrom_exit(void)37041da177e4SLinus Torvalds static void __exit cdrom_exit(void)
37051da177e4SLinus Torvalds {
3706e597cd09SJoe Perches pr_info("Uniform CD-ROM driver unloaded\n");
37071da177e4SLinus Torvalds cdrom_sysctl_unregister();
37081da177e4SLinus Torvalds }
37091da177e4SLinus Torvalds
37101da177e4SLinus Torvalds module_init(cdrom_init);
37111da177e4SLinus Torvalds module_exit(cdrom_exit);
37121da177e4SLinus Torvalds MODULE_LICENSE("GPL");
3713