1f30c2269544bffc7bf1b0d7c0abe5be1be83b8cbUwe Zeisberger/* linux/drivers/cdrom/cdrom.c
21da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds   Copyright (c) 1996, 1997 David A. van Leeuwen.
31da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds   Copyright (c) 1997, 1998 Erik Andersen <andersee@debian.org>
41da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds   Copyright (c) 1998, 1999 Jens Axboe <axboe@image.dk>
51da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
61da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds   May be copied or modified under the terms of the GNU General Public
71da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds   License.  See linux/COPYING for more information.
81da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
91da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds   Uniform CD-ROM driver for Linux.
101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds   See Documentation/cdrom/cdrom-standard.tex for usage information.
111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds   The routines in the file provide a uniform interface between the
131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds   software that uses CD-ROMs and the various low-level drivers that
141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds   actually talk to the hardware. Suggestions are welcome.
151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds   Patches that work are more welcome though.  ;-)
161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds To Do List:
181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds ----------------------------------
191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds -- Modify sysctl/proc interface. I plan on having one directory per
211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds drive, with entries for outputing general drive information, and sysctl
221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds based tunable parameters such as whether the tray should auto-close for
231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds that drive. Suggestions (or patches) for this welcome!
241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds Revision History
271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds ----------------------------------
281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 1.00  Date Unknown -- David van Leeuwen <david@tm.tno.nl>
291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds -- Initial version by David A. van Leeuwen. I don't have a detailed
301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  changelog for the 1.x series, David?
311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds2.00  Dec  2, 1997 -- Erik Andersen <andersee@debian.org>
3325985edcedea6396277003854657b5f3cb31a628Lucas De Marchi  -- New maintainer! As David A. van Leeuwen has been too busy to actively
341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  maintain and improve this driver, I am now carrying on the torch. If
351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  you have a problem with this driver, please feel free to contact me.
361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- Added (rudimentary) sysctl interface. I realize this is really weak
381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  right now, and is _very_ badly implemented. It will be improved...
391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- Modified CDROM_DISC_STATUS so that it is now incorporated into
411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  the Uniform CD-ROM driver via the cdrom_count_tracks function.
421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  The cdrom_count_tracks function helps resolve some of the false
431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  assumptions of the CDROM_DISC_STATUS ioctl, and is also used to check
441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  for the correct media type when mounting or playing audio from a CD.
451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- Remove the calls to verify_area and only use the copy_from_user and
471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  copy_to_user stuff, since these calls now provide their own memory
481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  checking with the 2.1.x kernels.
491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- Major update to return codes so that errors from low-level drivers
511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  are passed on through (thanks to Gerd Knorr for pointing out this
521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  problem).
531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- Made it so if a function isn't implemented in a low-level driver,
551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  ENOSYS is now returned instead of EINVAL.
561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- Simplified some complex logic so that the source code is easier to read.
581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- Other stuff I probably forgot to mention (lots of changes).
601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds2.01 to 2.11 Dec 1997-Jan 1998
621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- TO-DO!  Write changelogs for 2.01 to 2.12.
631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds2.12  Jan  24, 1998 -- Erik Andersen <andersee@debian.org>
651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- Fixed a bug in the IOCTL_IN and IOCTL_OUT macros.  It turns out that
661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  copy_*_user does not return EFAULT on error, but instead returns the number
671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  of bytes not copied.  I was returning whatever non-zero stuff came back from
681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  the copy_*_user functions directly, which would result in strange errors.
691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds2.13  July 17, 1998 -- Erik Andersen <andersee@debian.org>
711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- Fixed a bug in CDROM_SELECT_SPEED where you couldn't lower the speed
721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  of the drive.  Thanks to Tobias Ringstr|m <tori@prosolvia.se> for pointing
731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  this out and providing a simple fix.
741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- Fixed the procfs-unload-module bug with the fill_inode procfs callback.
751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  thanks to Andrea Arcangeli
761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- Fixed it so that the /proc entry now also shows up when cdrom is
771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  compiled into the kernel.  Before it only worked when loaded as a module.
781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  2.14 August 17, 1998 -- Erik Andersen <andersee@debian.org>
801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- Fixed a bug in cdrom_media_changed and handling of reporting that
811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  the media had changed for devices that _don't_ implement media_changed.
821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  Thanks to Grant R. Guenther <grant@torque.net> for spotting this bug.
831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- Made a few things more pedanticly correct.
841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds2.50 Oct 19, 1998 - Jens Axboe <axboe@image.dk>
861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- New maintainers! Erik was too busy to continue the work on the driver,
871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  so now Chris Zwilling <chris@cloudnet.com> and Jens Axboe <axboe@image.dk>
881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  will do their best to follow in his footsteps
891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  2.51 Dec 20, 1998 - Jens Axboe <axboe@image.dk>
911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- Check if drive is capable of doing what we ask before blindly changing
921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  cdi->options in various ioctl.
931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- Added version to proc entry.
941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  2.52 Jan 16, 1999 - Jens Axboe <axboe@image.dk>
961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- Fixed an error in open_for_data where we would sometimes not return
971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  the correct error value. Thanks Huba Gaspar <huba@softcell.hu>.
981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- Fixed module usage count - usage was based on /proc/sys/dev
991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  instead of /proc/sys/dev/cdrom. This could lead to an oops when other
1001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  modules had entries in dev. Feb 02 - real bug was in sysctl.c where
1011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  dev would be removed even though it was used. cdrom.c just illuminated
1021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  that bug.
1031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  2.53 Feb 22, 1999 - Jens Axboe <axboe@image.dk>
1051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- Fixup of several ioctl calls, in particular CDROM_SET_OPTIONS has
1061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  been "rewritten" because capabilities and options aren't in sync. They
1071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  should be...
1081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- Added CDROM_LOCKDOOR ioctl. Locks the door and keeps it that way.
1091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- Added CDROM_RESET ioctl.
1101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- Added CDROM_DEBUG ioctl. Enable debug messages on-the-fly.
1111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- Added CDROM_GET_CAPABILITY ioctl. This relieves userspace programs
1121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  from parsing /proc/sys/dev/cdrom/info.
1131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  2.54 Mar 15, 1999 - Jens Axboe <axboe@image.dk>
1151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- Check capability mask from low level driver when counting tracks as
1161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  per suggestion from Corey J. Scotts <cstotts@blue.weeg.uiowa.edu>.
1171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  2.55 Apr 25, 1999 - Jens Axboe <axboe@image.dk>
1191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- autoclose was mistakenly checked against CDC_OPEN_TRAY instead of
1201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  CDC_CLOSE_TRAY.
1211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- proc info didn't mask against capabilities mask.
1221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  3.00 Aug 5, 1999 - Jens Axboe <axboe@image.dk>
1241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- Unified audio ioctl handling across CD-ROM drivers. A lot of the
1251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  code was duplicated before. Drives that support the generic packet
1261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  interface are now being fed packets from here instead.
1271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- First attempt at adding support for MMC2 commands - for DVD and
1281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  CD-R(W) drives. Only the DVD parts are in now - the interface used is
1291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  the same as for the audio ioctls.
1301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- ioctl cleanups. if a drive couldn't play audio, it didn't get
1311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  a change to perform device specific ioctls as well.
1321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- Defined CDROM_CAN(CDC_XXX) for checking the capabilities.
1331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- Put in sysctl files for autoclose, autoeject, check_media, debug,
1341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  and lock.
1351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- /proc/sys/dev/cdrom/info has been updated to also contain info about
1361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  CD-Rx and DVD capabilities.
1371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- Now default to checking media type.
1381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- CDROM_SEND_PACKET ioctl added. The infrastructure was in place for
1391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  doing this anyway, with the generic_packet addition.
1401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  3.01 Aug 6, 1999 - Jens Axboe <axboe@image.dk>
1421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- Fix up the sysctl handling so that the option flags get set
1431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  correctly.
1441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- Fix up ioctl handling so the device specific ones actually get
1451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  called :).
1461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  3.02 Aug 8, 1999 - Jens Axboe <axboe@image.dk>
1481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- Fixed volume control on SCSI drives (or others with longer audio
1491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  page).
1501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- Fixed a couple of DVD minors. Thanks to Andrew T. Veliath
1511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  <andrewtv@usa.net> for telling me and for having defined the various
1521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  DVD structures and ioctls in the first place! He designed the original
1531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  DVD patches for ide-cd and while I rearranged and unified them, the
1541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  interface is still the same.
1551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  3.03 Sep 1, 1999 - Jens Axboe <axboe@image.dk>
1571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- Moved the rest of the audio ioctls from the CD-ROM drivers here. Only
1581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  CDROMREADTOCENTRY and CDROMREADTOCHDR are left.
1591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- Moved the CDROMREADxxx ioctls in here.
1601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- Defined the cdrom_get_last_written and cdrom_get_next_block as ioctls
1611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  and exported functions.
1621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- Erik Andersen <andersen@xmission.com> modified all SCMD_ commands
1631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  to now read GPCMD_ for the new generic packet interface. All low level
1641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  drivers are updated as well.
1651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- Various other cleanups.
1661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  3.04 Sep 12, 1999 - Jens Axboe <axboe@image.dk>
1681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- Fixed a couple of possible memory leaks (if an operation failed and
1691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  we didn't free the buffer before returning the error).
1701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- Integrated Uniform CD Changer handling from Richard Sharman
1711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  <rsharman@pobox.com>.
1721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- Defined CD_DVD and CD_CHANGER log levels.
1731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- Fixed the CDROMREADxxx ioctls.
1741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- CDROMPLAYTRKIND uses the GPCMD_PLAY_AUDIO_MSF command - too few
1751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  drives supported it. We lose the index part, however.
1761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- Small modifications to accommodate opens of /dev/hdc1, required
1771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  for ide-cd to handle multisession discs.
1781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- Export cdrom_mode_sense and cdrom_mode_select.
1791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- init_cdrom_command() for setting up a cgc command.
1801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  3.05 Oct 24, 1999 - Jens Axboe <axboe@image.dk>
1821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- Changed the interface for CDROM_SEND_PACKET. Before it was virtually
1831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  impossible to send the drive data in a sensible way.
1841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- Lowered stack usage in mmc_ioctl(), dvd_read_disckey(), and
1851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  dvd_read_manufact.
1861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- Added setup of write mode for packet writing.
1871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- Fixed CDDA ripping with cdda2wav - accept much larger requests of
1881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  number of frames and split the reads in blocks of 8.
1891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  3.06 Dec 13, 1999 - Jens Axboe <axboe@image.dk>
1911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- Added support for changing the region of DVD drives.
1921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- Added sense data to generic command.
1931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  3.07 Feb 2, 2000 - Jens Axboe <axboe@suse.de>
1951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- Do same "read header length" trick in cdrom_get_disc_info() as
1961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  we do in cdrom_get_track_info() -- some drive don't obey specs and
1971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  fail if they can't supply the full Mt Fuji size table.
1981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- Deleted stuff related to setting up write modes. It has a different
1991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  home now.
2001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- Clear header length in mode_select unconditionally.
2011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- Removed the register_disk() that was added, not needed here.
2021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  3.08 May 1, 2000 - Jens Axboe <axboe@suse.de>
2041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- Fix direction flag in setup_send_key and setup_report_key. This
2051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  gave some SCSI adapters problems.
2061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- Always return -EROFS for write opens
2071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- Convert to module_init/module_exit style init and remove some
2081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  of the #ifdef MODULE stuff
2091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- Fix several dvd errors - DVD_LU_SEND_ASF should pass agid,
2101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  DVD_HOST_SEND_RPC_STATE did not set buffer size in cdb, and
2111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  dvd_do_auth passed uninitialized data to drive because init_cdrom_command
2121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  did not clear a 0 sized buffer.
2131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  3.09 May 12, 2000 - Jens Axboe <axboe@suse.de>
2151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- Fix Video-CD on SCSI drives that don't support READ_CD command. In
2161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  that case switch block size and issue plain READ_10 again, then switch
2171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  back.
2181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  3.10 Jun 10, 2000 - Jens Axboe <axboe@suse.de>
2201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- Fix volume control on CD's - old SCSI-II drives now use their own
2211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  code, as doing MODE6 stuff in here is really not my intention.
2221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- Use READ_DISC_INFO for more reliable end-of-disc.
2231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  3.11 Jun 12, 2000 - Jens Axboe <axboe@suse.de>
2251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- Fix bug in getting rpc phase 2 region info.
2261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- Reinstate "correct" CDROMPLAYTRKIND
2271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds   3.12 Oct 18, 2000 - Jens Axboe <axboe@suse.de>
2291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- Use quiet bit on packet commands not known to work
2301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds   3.20 Dec 17, 2003 - Jens Axboe <axboe@suse.de>
2321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- Various fixes and lots of cleanups not listed :-)
2331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- Locking fixes
2341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- Mt Rainier support
2351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  -- DVD-RAM write open fixes
2361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  Nov 5 2001, Aug 8 2002. Modified by Andy Polyakov
2381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  <appro@fy.chalmers.se> to support MMC-3 compliant DVD+RW units.
2391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  Modified by Nigel Kukard <nkukard@lbsd.net> - support DVD+RW
2411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  2.4.x patch by Andy Polyakov <appro@fy.chalmers.se>
2421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds-------------------------------------------------------------------------*/
2441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
245e597cd09f711b28b8466ebdc2f12e55b44fa81e4Joe Perches#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
246e597cd09f711b28b8466ebdc2f12e55b44fa81e4Joe Perches
2471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define REVISION "Revision: 3.20"
2481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define VERSION "Id: cdrom.c 3.20 2003/12/17"
2491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* I use an error-log mask to give fine grain control over the type of
2511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds   messages dumped to the system logs.  The available masks include: */
2521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define CD_NOTHING      0x0
2531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define CD_WARNING	0x1
2541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define CD_REG_UNREG	0x2
2551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define CD_DO_IOCTL	0x4
2561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define CD_OPEN		0x8
2571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define CD_CLOSE	0x10
2581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define CD_COUNT_TRACKS 0x20
2591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define CD_CHANGER	0x40
2601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define CD_DVD		0x80
2611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Define this to remove _all_ the debugging messages */
2631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* #define ERRLOGMASK CD_NOTHING */
2641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define ERRLOGMASK CD_WARNING
2651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* #define ERRLOGMASK (CD_WARNING|CD_OPEN|CD_COUNT_TRACKS|CD_CLOSE) */
2661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* #define ERRLOGMASK (CD_WARNING|CD_REG_UNREG|CD_DO_IOCTL|CD_OPEN|CD_CLOSE|CD_COUNT_TRACKS) */
2671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/module.h>
2691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/fs.h>
2701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/major.h>
2711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/types.h>
2721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/errno.h>
2731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/kernel.h>
2741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/mm.h>
2751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/slab.h>
2761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/cdrom.h>
2771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/sysctl.h>
2781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/proc_fs.h>
2791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/blkpg.h>
2801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/init.h>
2811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/fcntl.h>
2821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/blkdev.h>
2831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/times.h>
2841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <asm/uaccess.h>
2861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* used to tell the module to turn on full debugging messages */
28890ab5ee94171b3e28de6bb42ee30b527014e0be7Rusty Russellstatic bool debug;
2891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* default compatibility mode */
29090ab5ee94171b3e28de6bb42ee30b527014e0be7Rusty Russellstatic bool autoclose=1;
29190ab5ee94171b3e28de6bb42ee30b527014e0be7Rusty Russellstatic bool autoeject;
29290ab5ee94171b3e28de6bb42ee30b527014e0be7Rusty Russellstatic bool lockdoor = 1;
2931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* will we ever get to use this... sigh. */
29490ab5ee94171b3e28de6bb42ee30b527014e0be7Rusty Russellstatic bool check_media_type;
2951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* automatically restart mrw format */
29690ab5ee94171b3e28de6bb42ee30b527014e0be7Rusty Russellstatic bool mrw_format_restart = 1;
2971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsmodule_param(debug, bool, 0);
2981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsmodule_param(autoclose, bool, 0);
2991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsmodule_param(autoeject, bool, 0);
3001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsmodule_param(lockdoor, bool, 0);
3011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsmodule_param(check_media_type, bool, 0);
3021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsmodule_param(mrw_format_restart, bool, 0);
3031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
304554988d6fe369719ae5b41255c577569ecf47c30Dave Youngstatic DEFINE_MUTEX(cdrom_mutex);
3051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic const char *mrw_format_status[] = {
3071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	"not mrw",
3081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	"bgformat inactive",
3091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	"bgformat active",
3101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	"mrw complete",
3111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
3121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic const char *mrw_address_space[] = { "DMA", "GAA" };
3141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#if (ERRLOGMASK!=CD_NOTHING)
316e597cd09f711b28b8466ebdc2f12e55b44fa81e4Joe Perches#define cdinfo(type, fmt, args...)			\
317e597cd09f711b28b8466ebdc2f12e55b44fa81e4Joe Perchesdo {							\
318e597cd09f711b28b8466ebdc2f12e55b44fa81e4Joe Perches	if ((ERRLOGMASK & type) || debug == 1)		\
319e597cd09f711b28b8466ebdc2f12e55b44fa81e4Joe Perches		pr_info(fmt, ##args);			\
320e597cd09f711b28b8466ebdc2f12e55b44fa81e4Joe Perches} while (0)
3211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#else
322e597cd09f711b28b8466ebdc2f12e55b44fa81e4Joe Perches#define cdinfo(type, fmt, args...)			\
323e597cd09f711b28b8466ebdc2f12e55b44fa81e4Joe Perchesdo {							\
324e597cd09f711b28b8466ebdc2f12e55b44fa81e4Joe Perches	if (0 && (ERRLOGMASK & type) || debug == 1)	\
325e597cd09f711b28b8466ebdc2f12e55b44fa81e4Joe Perches		pr_info(fmt, ##args);			\
326e597cd09f711b28b8466ebdc2f12e55b44fa81e4Joe Perches} while (0)
3271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#endif
3281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* These are used to simplify getting data in from and back to user land */
3301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define IOCTL_IN(arg, type, in)					\
3311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (copy_from_user(&(in), (type __user *) (arg), sizeof (in)))	\
3321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return -EFAULT;
3331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define IOCTL_OUT(arg, type, out) \
3351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (copy_to_user((type __user *) (arg), &(out), sizeof (out)))	\
3361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return -EFAULT;
3371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* The (cdo->capability & ~cdi->mask & CDC_XXX) construct was used in
3391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds   a lot of places. This macro makes the code more clear. */
3401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define CDROM_CAN(type) (cdi->ops->capability & ~cdi->mask & (type))
3411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* used in the audio ioctls */
3431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define CHECKAUDIO if ((ret=check_for_audio_disc(cdi, cdo))) return ret
3441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3452e11c207b029cfaf57159cabac4b002204445258Jens Axboe/*
3462e11c207b029cfaf57159cabac4b002204445258Jens Axboe * Another popular OS uses 7 seconds as the hard timeout for default
3472e11c207b029cfaf57159cabac4b002204445258Jens Axboe * commands, so it is a good choice for us as well.
3482e11c207b029cfaf57159cabac4b002204445258Jens Axboe */
3492e11c207b029cfaf57159cabac4b002204445258Jens Axboe#define CDROM_DEF_TIMEOUT	(7 * HZ)
3502e11c207b029cfaf57159cabac4b002204445258Jens Axboe
3511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Not-exported routines. */
3521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int open_for_data(struct cdrom_device_info * cdi);
3531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int check_for_audio_disc(struct cdrom_device_info * cdi,
3541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			 struct cdrom_device_ops * cdo);
3551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void sanitize_format(union cdrom_addr *addr,
3561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		u_char * curr, u_char requested);
3571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int mmc_ioctl(struct cdrom_device_info *cdi, unsigned int cmd,
3581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		     unsigned long arg);
3591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsint cdrom_get_last_written(struct cdrom_device_info *, long *);
3611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int cdrom_get_next_writable(struct cdrom_device_info *, long *);
3621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void cdrom_count_tracks(struct cdrom_device_info *, tracktype*);
3631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int cdrom_mrw_exit(struct cdrom_device_info *cdi);
3651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int cdrom_get_disc_info(struct cdrom_device_info *cdi, disc_information *di);
3671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void cdrom_sysctl_register(void);
36917672cf0a7cec57dea4c2a6705bd73114d5e9265Akinobu Mita
3707fd097d42b90afadae4867db5d580bcd7b3b596dAkinobu Mitastatic LIST_HEAD(cdrom_list);
3711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int cdrom_dummy_generic_packet(struct cdrom_device_info *cdi,
3731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				      struct packet_command *cgc)
3741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
3751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (cgc->sense) {
3761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cgc->sense->sense_key = 0x05;
3771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cgc->sense->asc = 0x20;
3781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cgc->sense->ascq = 0x00;
3791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
3801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc->stat = -EIO;
3821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return -EIO;
3831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
3841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* This macro makes sure we don't have to check on cdrom_device_ops
3861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * existence in the run-time routines below. Change_capability is a
3871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * hack to have the capability flags defined const, while we can still
3881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * change it here without gcc complaining at every line.
3891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
3901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define ENSURE(call, bits) if (cdo->call == NULL) *change_capability &= ~(bits)
3911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsint register_cdrom(struct cdrom_device_info *cdi)
3931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
3941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	static char banner_printed;
3951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds        struct cdrom_device_ops *cdo = cdi->ops;
3961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds        int *change_capability = (int *)&cdo->capability; /* hack */
3971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cdinfo(CD_OPEN, "entering register_cdrom\n");
3991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (cdo->open == NULL || cdo->release == NULL)
4013c3f4e012ab3471e8bd19220ed897ae9700e5fddAkinobu Mita		return -EINVAL;
4021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!banner_printed) {
403e597cd09f711b28b8466ebdc2f12e55b44fa81e4Joe Perches		pr_info("Uniform CD-ROM driver " REVISION "\n");
4041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		banner_printed = 1;
4051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cdrom_sysctl_register();
4061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
4071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	ENSURE(drive_status, CDC_DRIVE_STATUS );
409b8cf0e0e552ca48e9a00f518aeb4f5e03984022bSimon Arlott	if (cdo->check_events == NULL && cdo->media_changed == NULL)
410b8cf0e0e552ca48e9a00f518aeb4f5e03984022bSimon Arlott		*change_capability = ~(CDC_MEDIA_CHANGED | CDC_SELECT_DISC);
4111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	ENSURE(tray_move, CDC_CLOSE_TRAY | CDC_OPEN_TRAY);
4121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	ENSURE(lock_door, CDC_LOCK);
4131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	ENSURE(select_speed, CDC_SELECT_SPEED);
4141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	ENSURE(get_last_session, CDC_MULTI_SESSION);
4151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	ENSURE(get_mcn, CDC_MCN);
4161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	ENSURE(reset, CDC_RESET);
4171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	ENSURE(generic_packet, CDC_GENERIC_PACKET);
4181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cdi->mc_flags = 0;
4191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cdo->n_minors = 0;
4201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds        cdi->options = CDO_USE_FFLAGS;
4211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (autoclose==1 && CDROM_CAN(CDC_CLOSE_TRAY))
4231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cdi->options |= (int) CDO_AUTO_CLOSE;
4241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (autoeject==1 && CDROM_CAN(CDC_OPEN_TRAY))
4251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cdi->options |= (int) CDO_AUTO_EJECT;
4261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (lockdoor==1)
4271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cdi->options |= (int) CDO_LOCK;
4281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (check_media_type==1)
4291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cdi->options |= (int) CDO_CHECK_TYPE;
4301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (CDROM_CAN(CDC_MRW_W))
4321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cdi->exit = cdrom_mrw_exit;
4331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (cdi->disk)
4351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cdi->cdda_method = CDDA_BPC_FULL;
4361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else
4371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cdi->cdda_method = CDDA_OLD;
4381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!cdo->generic_packet)
4401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cdo->generic_packet = cdrom_dummy_generic_packet;
4411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cdinfo(CD_REG_UNREG, "drive \"/dev/%s\" registered\n", cdi->name);
443554988d6fe369719ae5b41255c577569ecf47c30Dave Young	mutex_lock(&cdrom_mutex);
4447fd097d42b90afadae4867db5d580bcd7b3b596dAkinobu Mita	list_add(&cdi->list, &cdrom_list);
445554988d6fe369719ae5b41255c577569ecf47c30Dave Young	mutex_unlock(&cdrom_mutex);
4461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
4471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
4481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#undef ENSURE
4491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4500a0c4114df4a6903bccb65b06cabb6ddc968f877Akinobu Mitavoid unregister_cdrom(struct cdrom_device_info *cdi)
4511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
4521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cdinfo(CD_OPEN, "entering unregister_cdrom\n");
4531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
454554988d6fe369719ae5b41255c577569ecf47c30Dave Young	mutex_lock(&cdrom_mutex);
4557fd097d42b90afadae4867db5d580bcd7b3b596dAkinobu Mita	list_del(&cdi->list);
456554988d6fe369719ae5b41255c577569ecf47c30Dave Young	mutex_unlock(&cdrom_mutex);
4571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (cdi->exit)
4591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cdi->exit(cdi);
4601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cdi->ops->n_minors--;
4621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cdinfo(CD_REG_UNREG, "drive \"/dev/%s\" unregistered\n", cdi->name);
4631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
4641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsint cdrom_get_media_event(struct cdrom_device_info *cdi,
4661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			  struct media_event_desc *med)
4671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
4681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct packet_command cgc;
469fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori	unsigned char buffer[8];
470fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori	struct event_header *eh = (struct event_header *) buffer;
4711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
472fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori	init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_READ);
4731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc.cmd[0] = GPCMD_GET_EVENT_STATUS_NOTIFICATION;
4741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc.cmd[1] = 1;		/* IMMED */
4751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc.cmd[4] = 1 << 4;	/* media event */
476fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori	cgc.cmd[8] = sizeof(buffer);
4771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc.quiet = 1;
4781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (cdi->ops->generic_packet(cdi, &cgc))
480fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori		return 1;
4811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (be16_to_cpu(eh->data_len) < sizeof(*med))
483fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori		return 1;
4841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (eh->nea || eh->notification_class != 0x4)
486fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori		return 1;
4871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
488fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori	memcpy(med, &buffer[sizeof(*eh)], sizeof(*med));
489fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori	return 0;
4901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
4911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
4931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * the first prototypes used 0x2c as the page code for the mrw mode page,
4941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * subsequently this was changed to 0x03. probe the one used by this drive
4951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
4961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int cdrom_mrw_probe_pc(struct cdrom_device_info *cdi)
4971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
4981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct packet_command cgc;
499fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori	char buffer[16];
5001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
501fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori	init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_READ);
5021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc.timeout = HZ;
5041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc.quiet = 1;
5051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!cdrom_mode_sense(cdi, &cgc, MRW_MODE_PC, 0)) {
5071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cdi->mrw_mode_page = MRW_MODE_PC;
508fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori		return 0;
5091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	} else if (!cdrom_mode_sense(cdi, &cgc, MRW_MODE_PC_PRE1, 0)) {
5101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cdi->mrw_mode_page = MRW_MODE_PC_PRE1;
511fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori		return 0;
5121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
513fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori
514fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori	return 1;
5151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
5161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int cdrom_is_mrw(struct cdrom_device_info *cdi, int *write)
5181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
5191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct packet_command cgc;
5201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct mrw_feature_desc *mfd;
521fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori	unsigned char buffer[16];
5221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int ret;
5231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	*write = 0;
5251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
526fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori	init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_READ);
5271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc.cmd[0] = GPCMD_GET_CONFIGURATION;
5291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc.cmd[3] = CDF_MRW;
530fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori	cgc.cmd[8] = sizeof(buffer);
5311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc.quiet = 1;
5321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if ((ret = cdi->ops->generic_packet(cdi, &cgc)))
534fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori		return ret;
5351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	mfd = (struct mrw_feature_desc *)&buffer[sizeof(struct feature_header)];
537fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori	if (be16_to_cpu(mfd->feature_code) != CDF_MRW)
538fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori		return 1;
5391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	*write = mfd->write;
5401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if ((ret = cdrom_mrw_probe_pc(cdi))) {
5421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		*write = 0;
543fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori		return ret;
5441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
545fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori
546fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori	return 0;
5471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
5481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int cdrom_mrw_bgformat(struct cdrom_device_info *cdi, int cont)
5501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
5511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct packet_command cgc;
552fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori	unsigned char buffer[12];
5531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int ret;
5541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
555e597cd09f711b28b8466ebdc2f12e55b44fa81e4Joe Perches	pr_info("%sstarting format\n", cont ? "Re" : "");
5561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
5581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * FmtData bit set (bit 4), format type is 1
5591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
560fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori	init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_WRITE);
5611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc.cmd[0] = GPCMD_FORMAT_UNIT;
5621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc.cmd[1] = (1 << 4) | 1;
5631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc.timeout = 5 * 60 * HZ;
5651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
5671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * 4 byte format list header, 8 byte format list descriptor
5681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
5691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	buffer[1] = 1 << 1;
5701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	buffer[3] = 8;
5711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
5731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * nr_blocks field
5741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
5751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	buffer[4] = 0xff;
5761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	buffer[5] = 0xff;
5771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	buffer[6] = 0xff;
5781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	buffer[7] = 0xff;
5791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	buffer[8] = 0x24 << 2;
5811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	buffer[11] = cont;
5821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	ret = cdi->ops->generic_packet(cdi, &cgc);
5841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (ret)
585e597cd09f711b28b8466ebdc2f12e55b44fa81e4Joe Perches		pr_info("bgformat failed\n");
5861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return ret;
5881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
5891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int cdrom_mrw_bgformat_susp(struct cdrom_device_info *cdi, int immed)
5911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
5921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct packet_command cgc;
5931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
5951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc.cmd[0] = GPCMD_CLOSE_TRACK;
5961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
5981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Session = 1, Track = 0
5991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
6001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc.cmd[1] = !!immed;
6011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc.cmd[2] = 1 << 1;
6021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc.timeout = 5 * 60 * HZ;
6041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return cdi->ops->generic_packet(cdi, &cgc);
6061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
6071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int cdrom_flush_cache(struct cdrom_device_info *cdi)
6091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
6101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct packet_command cgc;
6111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
6131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc.cmd[0] = GPCMD_FLUSH_CACHE;
6141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc.timeout = 5 * 60 * HZ;
6161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return cdi->ops->generic_packet(cdi, &cgc);
6181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
6191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int cdrom_mrw_exit(struct cdrom_device_info *cdi)
6211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
6221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	disc_information di;
6231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int ret;
6241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	ret = cdrom_get_disc_info(cdi, &di);
6261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (ret < 0 || ret < (int)offsetof(typeof(di),disc_type))
6271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return 1;
6281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	ret = 0;
6301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (di.mrw_status == CDM_MRW_BGFORMAT_ACTIVE) {
631e597cd09f711b28b8466ebdc2f12e55b44fa81e4Joe Perches		pr_info("issuing MRW background format suspend\n");
6321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ret = cdrom_mrw_bgformat_susp(cdi, 0);
6331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
6341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6357da21a02b3587157bd43910ea6d4c76661228ebbJens Axboe	if (!ret && cdi->media_written)
6361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ret = cdrom_flush_cache(cdi);
6371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return ret;
6391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
6401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int cdrom_mrw_set_lba_space(struct cdrom_device_info *cdi, int space)
6421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
6431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct packet_command cgc;
6441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct mode_page_header *mph;
645fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori	char buffer[16];
6461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int ret, offset, size;
6471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
648fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori	init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_READ);
6491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
650fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori	cgc.buffer = buffer;
651fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori	cgc.buflen = sizeof(buffer);
6521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if ((ret = cdrom_mode_sense(cdi, &cgc, cdi->mrw_mode_page, 0)))
654fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori		return ret;
6551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	mph = (struct mode_page_header *) buffer;
6571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	offset = be16_to_cpu(mph->desc_length);
6581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	size = be16_to_cpu(mph->mode_data_length) + 2;
6591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	buffer[offset + 3] = space;
6611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc.buflen = size;
6621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if ((ret = cdrom_mode_select(cdi, &cgc)))
664fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori		return ret;
6651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
666e597cd09f711b28b8466ebdc2f12e55b44fa81e4Joe Perches	pr_info("%s: mrw address space %s selected\n",
667e597cd09f711b28b8466ebdc2f12e55b44fa81e4Joe Perches		cdi->name, mrw_address_space[space]);
668fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori	return 0;
6691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
6701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int cdrom_get_random_writable(struct cdrom_device_info *cdi,
6721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			      struct rwrt_feature_desc *rfd)
6731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
6741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct packet_command cgc;
675fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori	char buffer[24];
6761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int ret;
6771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
678fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori	init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_READ);
6791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc.cmd[0] = GPCMD_GET_CONFIGURATION;	/* often 0x46 */
6811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc.cmd[3] = CDF_RWRT;			/* often 0x0020 */
682fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori	cgc.cmd[8] = sizeof(buffer);		/* often 0x18 */
6831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc.quiet = 1;
6841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if ((ret = cdi->ops->generic_packet(cdi, &cgc)))
686fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori		return ret;
6871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	memcpy(rfd, &buffer[sizeof(struct feature_header)], sizeof (*rfd));
689fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori	return 0;
6901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
6911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int cdrom_has_defect_mgt(struct cdrom_device_info *cdi)
6931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
6941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct packet_command cgc;
695fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori	char buffer[16];
69656052d525a05ba9e53d4f11be2d5deee64924514Alexey Dobriyan	__be16 *feature_code;
6971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int ret;
6981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
699fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori	init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_READ);
7001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc.cmd[0] = GPCMD_GET_CONFIGURATION;
7021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc.cmd[3] = CDF_HWDM;
703fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori	cgc.cmd[8] = sizeof(buffer);
7041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc.quiet = 1;
7051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if ((ret = cdi->ops->generic_packet(cdi, &cgc)))
707fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori		return ret;
7081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
70956052d525a05ba9e53d4f11be2d5deee64924514Alexey Dobriyan	feature_code = (__be16 *) &buffer[sizeof(struct feature_header)];
7101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (be16_to_cpu(*feature_code) == CDF_HWDM)
711fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori		return 0;
712fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori
713fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori	return 1;
7141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
7151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int cdrom_is_random_writable(struct cdrom_device_info *cdi, int *write)
7181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
7191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct rwrt_feature_desc rfd;
7201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int ret;
7211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	*write = 0;
7231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if ((ret = cdrom_get_random_writable(cdi, &rfd)))
7251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return ret;
7261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (CDF_RWRT == be16_to_cpu(rfd.feature_code))
7281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		*write = 1;
7291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
7311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
7321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int cdrom_media_erasable(struct cdrom_device_info *cdi)
7341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
7351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	disc_information di;
7361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int ret;
7371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	ret = cdrom_get_disc_info(cdi, &di);
7391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (ret < 0 || ret < offsetof(typeof(di), n_first_track))
7401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return -1;
7411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return di.erasable;
7431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
7441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
7461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * FIXME: check RO bit
7471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
7481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int cdrom_dvdram_open_write(struct cdrom_device_info *cdi)
7491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
7501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int ret = cdrom_media_erasable(cdi);
7511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
7531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * allow writable open if media info read worked and media is
7541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * erasable, _or_ if it fails since not all drives support it
7551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
7561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!ret)
7571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return 1;
7581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
7601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
7611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int cdrom_mrw_open_write(struct cdrom_device_info *cdi)
7631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
7641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	disc_information di;
7651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int ret;
7661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
7681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * always reset to DMA lba space on open
7691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
7701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (cdrom_mrw_set_lba_space(cdi, MRW_LBA_DMA)) {
771e597cd09f711b28b8466ebdc2f12e55b44fa81e4Joe Perches		pr_err("failed setting lba address space\n");
7721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return 1;
7731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
7741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	ret = cdrom_get_disc_info(cdi, &di);
7761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (ret < 0 || ret < offsetof(typeof(di),disc_type))
7771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return 1;
7781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!di.erasable)
7801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return 1;
7811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
7831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * mrw_status
7841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * 0	-	not MRW formatted
7851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * 1	-	MRW bgformat started, but not running or complete
7861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * 2	-	MRW bgformat in progress
7871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * 3	-	MRW formatting complete
7881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
7891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	ret = 0;
790e597cd09f711b28b8466ebdc2f12e55b44fa81e4Joe Perches	pr_info("open: mrw_status '%s'\n", mrw_format_status[di.mrw_status]);
7911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!di.mrw_status)
7921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ret = 1;
7931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else if (di.mrw_status == CDM_MRW_BGFORMAT_INACTIVE &&
7941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			mrw_format_restart)
7951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ret = cdrom_mrw_bgformat(cdi, 1);
7961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return ret;
7981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
7991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int mo_open_write(struct cdrom_device_info *cdi)
8011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
8021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct packet_command cgc;
803fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori	char buffer[255];
8041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int ret;
8051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
806fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori	init_cdrom_command(&cgc, &buffer, 4, CGC_DATA_READ);
8071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc.quiet = 1;
8081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
8101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * obtain write protect information as per
8111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * drivers/scsi/sd.c:sd_read_write_protect_flag
8121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
8131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	ret = cdrom_mode_sense(cdi, &cgc, GPMODE_ALL_PAGES, 0);
8151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (ret)
8161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ret = cdrom_mode_sense(cdi, &cgc, GPMODE_VENDOR_PAGE, 0);
8171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (ret) {
8181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cgc.buflen = 255;
8191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ret = cdrom_mode_sense(cdi, &cgc, GPMODE_ALL_PAGES, 0);
8201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
8211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* drive gave us no info, let the user go ahead */
823fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori	if (ret)
824fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori		return 0;
8251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
826fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori	return buffer[3] & 0x80;
8271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
8281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int cdrom_ram_open_write(struct cdrom_device_info *cdi)
8301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
8311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct rwrt_feature_desc rfd;
8321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int ret;
8331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if ((ret = cdrom_has_defect_mgt(cdi)))
8351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return ret;
8361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if ((ret = cdrom_get_random_writable(cdi, &rfd)))
8381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return ret;
8391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else if (CDF_RWRT == be16_to_cpu(rfd.feature_code))
8401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ret = !rfd.curr;
8411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cdinfo(CD_OPEN, "can open for random write\n");
8431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return ret;
8441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
8451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void cdrom_mmc3_profile(struct cdrom_device_info *cdi)
8471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
8481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct packet_command cgc;
849fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori	char buffer[32];
8501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int ret, mmc3_profile;
8511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
852fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori	init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_READ);
8531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc.cmd[0] = GPCMD_GET_CONFIGURATION;
8551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc.cmd[1] = 0;
8561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc.cmd[2] = cgc.cmd[3] = 0;		/* Starting Feature Number */
857fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori	cgc.cmd[8] = sizeof(buffer);		/* Allocation Length */
8581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc.quiet = 1;
8591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if ((ret = cdi->ops->generic_packet(cdi, &cgc)))
8611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		mmc3_profile = 0xffff;
8621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else
8631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		mmc3_profile = (buffer[6] << 8) | buffer[7];
8641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cdi->mmc3_profile = mmc3_profile;
8661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
8671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int cdrom_is_dvd_rw(struct cdrom_device_info *cdi)
8691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
8701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	switch (cdi->mmc3_profile) {
8711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case 0x12:	/* DVD-RAM	*/
8721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case 0x1A:	/* DVD+RW	*/
8731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return 0;
8741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	default:
8751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return 1;
8761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
8771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
8781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
8801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * returns 0 for ok to open write, non-0 to disallow
8811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
8821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int cdrom_open_write(struct cdrom_device_info *cdi)
8831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
8841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int mrw, mrw_write, ram_write;
8851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int ret = 1;
8861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	mrw = 0;
8881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!cdrom_is_mrw(cdi, &mrw_write))
8891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		mrw = 1;
8901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (CDROM_CAN(CDC_MO_DRIVE))
8921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ram_write = 1;
8931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else
8941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		(void) cdrom_is_random_writable(cdi, &ram_write);
8951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (mrw)
8971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cdi->mask &= ~CDC_MRW;
8981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else
8991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cdi->mask |= CDC_MRW;
9001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (mrw_write)
9021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cdi->mask &= ~CDC_MRW_W;
9031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else
9041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cdi->mask |= CDC_MRW_W;
9051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (ram_write)
9071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cdi->mask &= ~CDC_RAM;
9081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else
9091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cdi->mask |= CDC_RAM;
9101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (CDROM_CAN(CDC_MRW_W))
9121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ret = cdrom_mrw_open_write(cdi);
9131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else if (CDROM_CAN(CDC_DVD_RAM))
9141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ret = cdrom_dvdram_open_write(cdi);
9151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 	else if (CDROM_CAN(CDC_RAM) &&
9161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 		 !CDROM_CAN(CDC_CD_R|CDC_CD_RW|CDC_DVD|CDC_DVD_R|CDC_MRW|CDC_MO_DRIVE))
9171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds 		ret = cdrom_ram_open_write(cdi);
9181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else if (CDROM_CAN(CDC_MO_DRIVE))
9191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ret = mo_open_write(cdi);
9201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else if (!cdrom_is_dvd_rw(cdi))
9211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ret = 0;
9221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return ret;
9241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
9251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void cdrom_dvd_rw_close_write(struct cdrom_device_info *cdi)
9271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
9281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct packet_command cgc;
9291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (cdi->mmc3_profile != 0x1a) {
9311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cdinfo(CD_CLOSE, "%s: No DVD+RW\n", cdi->name);
9321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return;
9331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
9341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!cdi->media_written) {
9361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cdinfo(CD_CLOSE, "%s: DVD+RW media clean\n", cdi->name);
9371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return;
9381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
9391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
940e597cd09f711b28b8466ebdc2f12e55b44fa81e4Joe Perches	pr_info("%s: dirty DVD+RW media, \"finalizing\"\n", cdi->name);
9411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
9431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc.cmd[0] = GPCMD_FLUSH_CACHE;
9441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc.timeout = 30*HZ;
9451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cdi->ops->generic_packet(cdi, &cgc);
9461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
9481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc.cmd[0] = GPCMD_CLOSE_TRACK;
9491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc.timeout = 3000*HZ;
9501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc.quiet = 1;
9511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cdi->ops->generic_packet(cdi, &cgc);
9521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
9541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc.cmd[0] = GPCMD_CLOSE_TRACK;
9551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc.cmd[2] = 2;	 /* Close session */
9561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc.quiet = 1;
9571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc.timeout = 3000*HZ;
9581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cdi->ops->generic_packet(cdi, &cgc);
9591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cdi->media_written = 0;
9611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
9621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int cdrom_close_write(struct cdrom_device_info *cdi)
9641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
9651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#if 0
9661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return cdrom_flush_cache(cdi);
9671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#else
9681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
9691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#endif
9701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
9711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* We use the open-option O_NONBLOCK to indicate that the
9731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * purpose of opening is only for subsequent ioctl() calls; no device
9741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * integrity checks are performed.
9751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
9761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * We hope that all cd-player programs will adopt this convention. It
9771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * is in their own interest: device control becomes a lot easier
9781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * this way.
9791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
980bbc1cc978404105da23d505163ce9fd5598ed5b1Al Viroint cdrom_open(struct cdrom_device_info *cdi, struct block_device *bdev, fmode_t mode)
9811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
9821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int ret;
9831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cdinfo(CD_OPEN, "entering cdrom_open\n");
9851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
986bf2253a6f00e8fea5b026e471e9f0d0a1b3621f2Tejun Heo	/* open is event synchronization point, check events first */
987bf2253a6f00e8fea5b026e471e9f0d0a1b3621f2Tejun Heo	check_disk_change(bdev);
988bf2253a6f00e8fea5b026e471e9f0d0a1b3621f2Tejun Heo
9891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* if this was a O_NONBLOCK open and we should honor the flags,
9901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * do a quick open without drive/disc integrity checks. */
9911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cdi->use_count++;
992bbc1cc978404105da23d505163ce9fd5598ed5b1Al Viro	if ((mode & FMODE_NDELAY) && (cdi->options & CDO_USE_FFLAGS)) {
9931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ret = cdi->ops->open(cdi, 1);
9941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	} else {
9951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ret = open_for_data(cdi);
9961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (ret)
9971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			goto err;
9981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cdrom_mmc3_profile(cdi);
999bbc1cc978404105da23d505163ce9fd5598ed5b1Al Viro		if (mode & FMODE_WRITE) {
10001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			ret = -EROFS;
10011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			if (cdrom_open_write(cdi))
100216070428d389ff47aa3476b0911179ad90c640a2Jens Axboe				goto err_release;
10031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			if (!CDROM_CAN(CDC_RAM))
100416070428d389ff47aa3476b0911179ad90c640a2Jens Axboe				goto err_release;
10051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			ret = 0;
10061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			cdi->media_written = 0;
10071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
10081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
10091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (ret)
10111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto err;
10121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cdinfo(CD_OPEN, "Use count for \"/dev/%s\" now %d\n",
10141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			cdi->name, cdi->use_count);
10151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
101616070428d389ff47aa3476b0911179ad90c640a2Jens Axboeerr_release:
1017ae3ba4fd966e38d8d766d19f7a58b551df8ecc97Al Viro	if (CDROM_CAN(CDC_LOCK) && cdi->options & CDO_LOCK) {
1018ae3ba4fd966e38d8d766d19f7a58b551df8ecc97Al Viro		cdi->ops->lock_door(cdi, 0);
1019ae3ba4fd966e38d8d766d19f7a58b551df8ecc97Al Viro		cdinfo(CD_OPEN, "door unlocked.\n");
1020ae3ba4fd966e38d8d766d19f7a58b551df8ecc97Al Viro	}
102116070428d389ff47aa3476b0911179ad90c640a2Jens Axboe	cdi->ops->release(cdi);
10221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldserr:
10231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cdi->use_count--;
10241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return ret;
10251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
10261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic
10281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsint open_for_data(struct cdrom_device_info * cdi)
10291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
10301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int ret;
10311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct cdrom_device_ops *cdo = cdi->ops;
10321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	tracktype tracks;
10331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cdinfo(CD_OPEN, "entering open_for_data\n");
10341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* Check if the driver can report drive status.  If it can, we
10351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	   can do clever things.  If it can't, well, we at least tried! */
10361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (cdo->drive_status != NULL) {
10371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ret = cdo->drive_status(cdi, CDSL_CURRENT);
10381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cdinfo(CD_OPEN, "drive_status=%d\n", ret);
10391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (ret == CDS_TRAY_OPEN) {
10401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			cdinfo(CD_OPEN, "the tray is open...\n");
10411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			/* can/may i close it? */
10421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			if (CDROM_CAN(CDC_CLOSE_TRAY) &&
10431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			    cdi->options & CDO_AUTO_CLOSE) {
10441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				cdinfo(CD_OPEN, "trying to close the tray.\n");
10451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				ret=cdo->tray_move(cdi,0);
10461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				if (ret) {
10471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					cdinfo(CD_OPEN, "bummer. tried to close the tray but failed.\n");
10481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					/* Ignore the error from the low
10491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					level driver.  We don't care why it
10501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					couldn't close the tray.  We only care
10511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					that there is no disc in the drive,
10521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					since that is the _REAL_ problem here.*/
10531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					ret=-ENOMEDIUM;
10541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					goto clean_up_and_return;
10551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				}
10561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			} else {
10571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				cdinfo(CD_OPEN, "bummer. this drive can't close the tray.\n");
10581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				ret=-ENOMEDIUM;
10591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				goto clean_up_and_return;
10601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			}
10611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			/* Ok, the door should be closed now.. Check again */
10621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			ret = cdo->drive_status(cdi, CDSL_CURRENT);
10631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			if ((ret == CDS_NO_DISC) || (ret==CDS_TRAY_OPEN)) {
10641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				cdinfo(CD_OPEN, "bummer. the tray is still not closed.\n");
10651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				cdinfo(CD_OPEN, "tray might not contain a medium.\n");
10661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				ret=-ENOMEDIUM;
10671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				goto clean_up_and_return;
10681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			}
10691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			cdinfo(CD_OPEN, "the tray is now closed.\n");
10701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
10711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* the door should be closed now, check for the disc */
10721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ret = cdo->drive_status(cdi, CDSL_CURRENT);
10731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (ret!=CDS_DISC_OK) {
10741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			ret = -ENOMEDIUM;
10751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			goto clean_up_and_return;
10761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
10771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
10781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cdrom_count_tracks(cdi, &tracks);
10791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (tracks.error == CDS_NO_DISC) {
10801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cdinfo(CD_OPEN, "bummer. no disc.\n");
10811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ret=-ENOMEDIUM;
10821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto clean_up_and_return;
10831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
10841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* CD-Players which don't use O_NONBLOCK, workman
10851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * for example, need bit CDO_CHECK_TYPE cleared! */
10861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (tracks.data==0) {
10871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (cdi->options & CDO_CHECK_TYPE) {
10881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		    /* give people a warning shot, now that CDO_CHECK_TYPE
10891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		       is the default case! */
10901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		    cdinfo(CD_OPEN, "bummer. wrong media type.\n");
10911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		    cdinfo(CD_WARNING, "pid %d must open device O_NONBLOCK!\n",
1092ba25f9dcc4ea6e30839fcab5a5516f2176d5bfedPavel Emelyanov					(unsigned int)task_pid_nr(current));
10931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		    ret=-EMEDIUMTYPE;
10941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		    goto clean_up_and_return;
10951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
10961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		else {
10971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		    cdinfo(CD_OPEN, "wrong media type, but CDO_CHECK_TYPE not set.\n");
10981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
10991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
11001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
11011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cdinfo(CD_OPEN, "all seems well, opening the device.\n");
11021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
11031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* all seems well, we can open the device */
11041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	ret = cdo->open(cdi, 0); /* open for data */
11051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cdinfo(CD_OPEN, "opening the device gave me %d.\n", ret);
11061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* After all this careful checking, we shouldn't have problems
11071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	   opening the device, but we don't want the device locked if
11081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	   this somehow fails... */
11091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (ret) {
11101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cdinfo(CD_OPEN, "open device failed.\n");
11111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto clean_up_and_return;
11121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
11131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (CDROM_CAN(CDC_LOCK) && (cdi->options & CDO_LOCK)) {
11141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			cdo->lock_door(cdi, 1);
11151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			cdinfo(CD_OPEN, "door locked.\n");
11161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
11171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cdinfo(CD_OPEN, "device opened successfully.\n");
11181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return ret;
11191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
11201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* Something failed.  Try to unlock the drive, because some drivers
11211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	(notably ide-cd) lock the drive after every command.  This produced
11221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	a nasty bug where after mount failed, the drive would remain locked!
11231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	This ensures that the drive gets unlocked after a mount fails.  This
11241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	is a goto to avoid bloating the driver with redundant code. */
11251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsclean_up_and_return:
112600d6da9b4d6707b808481372537adb0fb38f99b3Jens Axboe	cdinfo(CD_OPEN, "open failed.\n");
11271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (CDROM_CAN(CDC_LOCK) && cdi->options & CDO_LOCK) {
11281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			cdo->lock_door(cdi, 0);
11291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			cdinfo(CD_OPEN, "door unlocked.\n");
11301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
11311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return ret;
11321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
11331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
11341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* This code is similar to that in open_for_data. The routine is called
11351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds   whenever an audio play operation is requested.
11361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*/
1137ecb80c6a490863af569853eea2a925f97e9e856aAdrian Bunkstatic int check_for_audio_disc(struct cdrom_device_info * cdi,
1138ecb80c6a490863af569853eea2a925f97e9e856aAdrian Bunk				struct cdrom_device_ops * cdo)
11391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
11401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds        int ret;
11411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	tracktype tracks;
11421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cdinfo(CD_OPEN, "entering check_for_audio_disc\n");
11431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!(cdi->options & CDO_CHECK_TYPE))
11441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return 0;
11451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (cdo->drive_status != NULL) {
11461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ret = cdo->drive_status(cdi, CDSL_CURRENT);
11471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cdinfo(CD_OPEN, "drive_status=%d\n", ret);
11481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (ret == CDS_TRAY_OPEN) {
11491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			cdinfo(CD_OPEN, "the tray is open...\n");
11501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			/* can/may i close it? */
11511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			if (CDROM_CAN(CDC_CLOSE_TRAY) &&
11521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			    cdi->options & CDO_AUTO_CLOSE) {
11531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				cdinfo(CD_OPEN, "trying to close the tray.\n");
11541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				ret=cdo->tray_move(cdi,0);
11551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				if (ret) {
11561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					cdinfo(CD_OPEN, "bummer. tried to close tray but failed.\n");
11571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					/* Ignore the error from the low
11581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					level driver.  We don't care why it
11591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					couldn't close the tray.  We only care
11601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					that there is no disc in the drive,
11611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					since that is the _REAL_ problem here.*/
11621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					return -ENOMEDIUM;
11631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				}
11641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			} else {
11651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				cdinfo(CD_OPEN, "bummer. this driver can't close the tray.\n");
11661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				return -ENOMEDIUM;
11671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			}
11681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			/* Ok, the door should be closed now.. Check again */
11691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			ret = cdo->drive_status(cdi, CDSL_CURRENT);
11701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			if ((ret == CDS_NO_DISC) || (ret==CDS_TRAY_OPEN)) {
11711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				cdinfo(CD_OPEN, "bummer. the tray is still not closed.\n");
11721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				return -ENOMEDIUM;
11731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			}
11741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			if (ret!=CDS_DISC_OK) {
11751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				cdinfo(CD_OPEN, "bummer. disc isn't ready.\n");
11761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				return -EIO;
11771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			}
11781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			cdinfo(CD_OPEN, "the tray is now closed.\n");
11791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
11801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
11811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cdrom_count_tracks(cdi, &tracks);
11821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (tracks.error)
11831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return(tracks.error);
11841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
11851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (tracks.audio==0)
11861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return -EMEDIUMTYPE;
11871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
11881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
11891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
11901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1191bbc1cc978404105da23d505163ce9fd5598ed5b1Al Virovoid cdrom_release(struct cdrom_device_info *cdi, fmode_t mode)
11921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
11931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct cdrom_device_ops *cdo = cdi->ops;
11941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int opened_for_data;
11951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
119635841f708070bd295a96f6c98e566f8b251c1b48Borislav Petkov	cdinfo(CD_CLOSE, "entering cdrom_release\n");
11971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
11981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (cdi->use_count > 0)
11991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cdi->use_count--;
120035841f708070bd295a96f6c98e566f8b251c1b48Borislav Petkov
120135841f708070bd295a96f6c98e566f8b251c1b48Borislav Petkov	if (cdi->use_count == 0) {
12021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cdinfo(CD_CLOSE, "Use count for \"/dev/%s\" now zero\n", cdi->name);
12031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cdrom_dvd_rw_close_write(cdi);
120435841f708070bd295a96f6c98e566f8b251c1b48Borislav Petkov
1205cdccaa9467b982d57b139818d15e1e994feca372Paolo Bonzini		if ((cdo->capability & CDC_LOCK) && !cdi->keeplocked) {
120635841f708070bd295a96f6c98e566f8b251c1b48Borislav Petkov			cdinfo(CD_CLOSE, "Unlocking door!\n");
120735841f708070bd295a96f6c98e566f8b251c1b48Borislav Petkov			cdo->lock_door(cdi, 0);
120835841f708070bd295a96f6c98e566f8b251c1b48Borislav Petkov		}
12091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
121035841f708070bd295a96f6c98e566f8b251c1b48Borislav Petkov
12111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	opened_for_data = !(cdi->options & CDO_USE_FFLAGS) ||
1212bbc1cc978404105da23d505163ce9fd5598ed5b1Al Viro		!(mode & FMODE_NDELAY);
12131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
12141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
12151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * flush cache on last write release
12161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
12171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (CDROM_CAN(CDC_RAM) && !cdi->use_count && cdi->for_data)
12181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cdrom_close_write(cdi);
12191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
12201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cdo->release(cdi);
12211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (cdi->use_count == 0) {      /* last process that closes dev*/
12221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (opened_for_data &&
12231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		    cdi->options & CDO_AUTO_EJECT && CDROM_CAN(CDC_OPEN_TRAY))
12241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			cdo->tray_move(cdi, 1);
12251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
12261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
12271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
12281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int cdrom_read_mech_status(struct cdrom_device_info *cdi,
12291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				  struct cdrom_changer_info *buf)
12301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
12311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct packet_command cgc;
12321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct cdrom_device_ops *cdo = cdi->ops;
12331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int length;
12341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
12351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
12361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Sanyo changer isn't spec compliant (doesn't use regular change
12371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * LOAD_UNLOAD command, and it doesn't implement the mech status
12381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * command below
12391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
12401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (cdi->sanyo_slot) {
12411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		buf->hdr.nslots = 3;
12421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		buf->hdr.curslot = cdi->sanyo_slot == 3 ? 0 : cdi->sanyo_slot;
12431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		for (length = 0; length < 3; length++) {
12441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			buf->slots[length].disc_present = 1;
12451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			buf->slots[length].change = 0;
12461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
12471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return 0;
12481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
12491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
12501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	length = sizeof(struct cdrom_mechstat_header) +
12511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 cdi->capacity * sizeof(struct cdrom_slot);
12521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
12531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	init_cdrom_command(&cgc, buf, length, CGC_DATA_READ);
12541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc.cmd[0] = GPCMD_MECHANISM_STATUS;
12551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc.cmd[8] = (length >> 8) & 0xff;
12561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc.cmd[9] = length & 0xff;
12571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return cdo->generic_packet(cdi, &cgc);
12581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
12591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
12601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int cdrom_slot_status(struct cdrom_device_info *cdi, int slot)
12611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
12621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct cdrom_changer_info *info;
12631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int ret;
12641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
12651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cdinfo(CD_CHANGER, "entering cdrom_slot_status()\n");
12661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (cdi->sanyo_slot)
12671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return CDS_NO_INFO;
12681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
12691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	info = kmalloc(sizeof(*info), GFP_KERNEL);
12701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!info)
12711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return -ENOMEM;
12721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
12731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if ((ret = cdrom_read_mech_status(cdi, info)))
12741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto out_free;
12751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
12761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (info->slots[slot].disc_present)
12771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ret = CDS_DISC_OK;
12781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else
12791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ret = CDS_NO_DISC;
12801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
12811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsout_free:
12821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	kfree(info);
12831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return ret;
12841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
12851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
12861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Return the number of slots for an ATAPI/SCSI cdrom,
12871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * return 1 if not a changer.
12881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
12891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsint cdrom_number_of_slots(struct cdrom_device_info *cdi)
12901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
12911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int status;
12921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int nslots = 1;
12931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct cdrom_changer_info *info;
12941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
12951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cdinfo(CD_CHANGER, "entering cdrom_number_of_slots()\n");
12961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* cdrom_read_mech_status requires a valid value for capacity: */
12971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cdi->capacity = 0;
12981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
12991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	info = kmalloc(sizeof(*info), GFP_KERNEL);
13001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!info)
13011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return -ENOMEM;
13021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if ((status = cdrom_read_mech_status(cdi, info)) == 0)
13041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		nslots = info->hdr.nslots;
13051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	kfree(info);
13071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return nslots;
13081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
13091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* If SLOT < 0, unload the current slot.  Otherwise, try to load SLOT. */
13121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int cdrom_load_unload(struct cdrom_device_info *cdi, int slot)
13131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
13141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct packet_command cgc;
13151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cdinfo(CD_CHANGER, "entering cdrom_load_unload()\n");
13171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (cdi->sanyo_slot && slot < 0)
13181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return 0;
13191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
13211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc.cmd[0] = GPCMD_LOAD_UNLOAD;
13221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc.cmd[4] = 2 + (slot >= 0);
13231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc.cmd[8] = slot;
13241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc.timeout = 60 * HZ;
13251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* The Sanyo 3 CD changer uses byte 7 of the
13271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	GPCMD_TEST_UNIT_READY to command to switch CDs instead of
13281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	using the GPCMD_LOAD_UNLOAD opcode. */
13291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (cdi->sanyo_slot && -1 < slot) {
13301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cgc.cmd[0] = GPCMD_TEST_UNIT_READY;
13311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cgc.cmd[7] = slot;
13321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cgc.cmd[4] = cgc.cmd[8] = 0;
13331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cdi->sanyo_slot = slot ? slot : 3;
13341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
13351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return cdi->ops->generic_packet(cdi, &cgc);
13371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
13381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int cdrom_select_disc(struct cdrom_device_info *cdi, int slot)
13401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
13411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct cdrom_changer_info *info;
13421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int curslot;
13431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int ret;
13441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cdinfo(CD_CHANGER, "entering cdrom_select_disc()\n");
13461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!CDROM_CAN(CDC_SELECT_DISC))
13471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return -EDRIVE_CANT_DO_THIS;
13481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13492d9217296bfa6fdc0d3707264076e5296faffdbdTejun Heo	if (cdi->ops->check_events)
13502d9217296bfa6fdc0d3707264076e5296faffdbdTejun Heo		cdi->ops->check_events(cdi, 0, slot);
13512d9217296bfa6fdc0d3707264076e5296faffdbdTejun Heo	else
13522d9217296bfa6fdc0d3707264076e5296faffdbdTejun Heo		cdi->ops->media_changed(cdi, slot);
13531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (slot == CDSL_NONE) {
13551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* set media changed bits, on both queues */
13561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cdi->mc_flags = 0x3;
13571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return cdrom_load_unload(cdi, -1);
13581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
13591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	info = kmalloc(sizeof(*info), GFP_KERNEL);
13611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!info)
13621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return -ENOMEM;
13631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if ((ret = cdrom_read_mech_status(cdi, info))) {
13651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		kfree(info);
13661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return ret;
13671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
13681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	curslot = info->hdr.curslot;
13701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	kfree(info);
13711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1372cdccaa9467b982d57b139818d15e1e994feca372Paolo Bonzini	if (cdi->use_count > 1 || cdi->keeplocked) {
13731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (slot == CDSL_CURRENT) {
13741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	    		return curslot;
13751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		} else {
13761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return -EBUSY;
13771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
13781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
13791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* Specifying CDSL_CURRENT will attempt to load the currnet slot,
13811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	which is useful if it had been previously unloaded.
13821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	Whether it can or not, it returns the current slot.
13831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	Similarly,  if slot happens to be the current one, we still
13841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	try and load it. */
13851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (slot == CDSL_CURRENT)
13861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		slot = curslot;
13871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* set media changed bits on both queues */
13891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cdi->mc_flags = 0x3;
13901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if ((ret = cdrom_load_unload(cdi, slot)))
13911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return ret;
13921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return slot;
13941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
13951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13962d9217296bfa6fdc0d3707264076e5296faffdbdTejun Heo/*
13972d9217296bfa6fdc0d3707264076e5296faffdbdTejun Heo * As cdrom implements an extra ioctl consumer for media changed
13982d9217296bfa6fdc0d3707264076e5296faffdbdTejun Heo * event, it needs to buffer ->check_events() output, such that event
13992d9217296bfa6fdc0d3707264076e5296faffdbdTejun Heo * is not lost for both the usual VFS and ioctl paths.
14002d9217296bfa6fdc0d3707264076e5296faffdbdTejun Heo * cdi->{vfs|ioctl}_events are used to buffer pending events for each
14012d9217296bfa6fdc0d3707264076e5296faffdbdTejun Heo * path.
14022d9217296bfa6fdc0d3707264076e5296faffdbdTejun Heo *
14032d9217296bfa6fdc0d3707264076e5296faffdbdTejun Heo * XXX: Locking is non-existent.  cdi->ops->check_events() can be
14042d9217296bfa6fdc0d3707264076e5296faffdbdTejun Heo * called in parallel and buffering fields are accessed without any
14052d9217296bfa6fdc0d3707264076e5296faffdbdTejun Heo * exclusion.  The original media_changed code had the same problem.
14062d9217296bfa6fdc0d3707264076e5296faffdbdTejun Heo * It might be better to simply deprecate CDROM_MEDIA_CHANGED ioctl
14072d9217296bfa6fdc0d3707264076e5296faffdbdTejun Heo * and remove this cruft altogether.  It doesn't have much usefulness
14082d9217296bfa6fdc0d3707264076e5296faffdbdTejun Heo * at this point.
14092d9217296bfa6fdc0d3707264076e5296faffdbdTejun Heo */
14102d9217296bfa6fdc0d3707264076e5296faffdbdTejun Heostatic void cdrom_update_events(struct cdrom_device_info *cdi,
14112d9217296bfa6fdc0d3707264076e5296faffdbdTejun Heo				unsigned int clearing)
14122d9217296bfa6fdc0d3707264076e5296faffdbdTejun Heo{
14132d9217296bfa6fdc0d3707264076e5296faffdbdTejun Heo	unsigned int events;
14142d9217296bfa6fdc0d3707264076e5296faffdbdTejun Heo
14152d9217296bfa6fdc0d3707264076e5296faffdbdTejun Heo	events = cdi->ops->check_events(cdi, clearing, CDSL_CURRENT);
14162d9217296bfa6fdc0d3707264076e5296faffdbdTejun Heo	cdi->vfs_events |= events;
14172d9217296bfa6fdc0d3707264076e5296faffdbdTejun Heo	cdi->ioctl_events |= events;
14182d9217296bfa6fdc0d3707264076e5296faffdbdTejun Heo}
14192d9217296bfa6fdc0d3707264076e5296faffdbdTejun Heo
14202d9217296bfa6fdc0d3707264076e5296faffdbdTejun Heounsigned int cdrom_check_events(struct cdrom_device_info *cdi,
14212d9217296bfa6fdc0d3707264076e5296faffdbdTejun Heo				unsigned int clearing)
14222d9217296bfa6fdc0d3707264076e5296faffdbdTejun Heo{
14232d9217296bfa6fdc0d3707264076e5296faffdbdTejun Heo	unsigned int events;
14242d9217296bfa6fdc0d3707264076e5296faffdbdTejun Heo
14252d9217296bfa6fdc0d3707264076e5296faffdbdTejun Heo	cdrom_update_events(cdi, clearing);
14262d9217296bfa6fdc0d3707264076e5296faffdbdTejun Heo	events = cdi->vfs_events;
14272d9217296bfa6fdc0d3707264076e5296faffdbdTejun Heo	cdi->vfs_events = 0;
14282d9217296bfa6fdc0d3707264076e5296faffdbdTejun Heo	return events;
14292d9217296bfa6fdc0d3707264076e5296faffdbdTejun Heo}
143030a7caabbb8e8702b9cf453fee83f773d8b3430dJens AxboeEXPORT_SYMBOL(cdrom_check_events);
14312d9217296bfa6fdc0d3707264076e5296faffdbdTejun Heo
14321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* We want to make media_changed accessible to the user through an
14331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * ioctl. The main problem now is that we must double-buffer the
14341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * low-level implementation, to assure that the VFS and the user both
14351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * see a medium change once.
14361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
14371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic
14391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsint media_changed(struct cdrom_device_info *cdi, int queue)
14401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
14411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned int mask = (1 << (queue & 1));
14421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int ret = !!(cdi->mc_flags & mask);
14432d9217296bfa6fdc0d3707264076e5296faffdbdTejun Heo	bool changed;
14441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!CDROM_CAN(CDC_MEDIA_CHANGED))
14462d9217296bfa6fdc0d3707264076e5296faffdbdTejun Heo		return ret;
14472d9217296bfa6fdc0d3707264076e5296faffdbdTejun Heo
14481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* changed since last call? */
14492d9217296bfa6fdc0d3707264076e5296faffdbdTejun Heo	if (cdi->ops->check_events) {
14502d9217296bfa6fdc0d3707264076e5296faffdbdTejun Heo		BUG_ON(!queue);	/* shouldn't be called from VFS path */
14512d9217296bfa6fdc0d3707264076e5296faffdbdTejun Heo		cdrom_update_events(cdi, DISK_EVENT_MEDIA_CHANGE);
14522d9217296bfa6fdc0d3707264076e5296faffdbdTejun Heo		changed = cdi->ioctl_events & DISK_EVENT_MEDIA_CHANGE;
14532d9217296bfa6fdc0d3707264076e5296faffdbdTejun Heo		cdi->ioctl_events = 0;
14542d9217296bfa6fdc0d3707264076e5296faffdbdTejun Heo	} else
14552d9217296bfa6fdc0d3707264076e5296faffdbdTejun Heo		changed = cdi->ops->media_changed(cdi, CDSL_CURRENT);
14562d9217296bfa6fdc0d3707264076e5296faffdbdTejun Heo
14572d9217296bfa6fdc0d3707264076e5296faffdbdTejun Heo	if (changed) {
14581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cdi->mc_flags = 0x3;    /* set bit on both queues */
14591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ret |= 1;
14601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cdi->media_written = 0;
14611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
14622d9217296bfa6fdc0d3707264076e5296faffdbdTejun Heo
14631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cdi->mc_flags &= ~mask;         /* clear bit */
14641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return ret;
14651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
14661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsint cdrom_media_changed(struct cdrom_device_info *cdi)
14681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
14691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* This talks to the VFS, which doesn't like errors - just 1 or 0.
14701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Returning "0" is always safe (media hasn't been changed). Do that
14711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * if the low-level cdrom driver dosn't support media changed. */
14721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (cdi == NULL || cdi->ops->media_changed == NULL)
14731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return 0;
14741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!CDROM_CAN(CDC_MEDIA_CHANGED))
14751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return 0;
14761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return media_changed(cdi, 0);
14771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
14781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* badly broken, I know. Is due for a fixup anytime. */
14801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void cdrom_count_tracks(struct cdrom_device_info *cdi, tracktype* tracks)
14811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
14821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct cdrom_tochdr header;
14831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct cdrom_tocentry entry;
14841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int ret, i;
14851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	tracks->data=0;
14861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	tracks->audio=0;
14871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	tracks->cdi=0;
14881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	tracks->xa=0;
14891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	tracks->error=0;
14901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cdinfo(CD_COUNT_TRACKS, "entering cdrom_count_tracks\n");
14911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* Grab the TOC header so we can see how many tracks there are */
14921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if ((ret = cdi->ops->audio_ioctl(cdi, CDROMREADTOCHDR, &header))) {
14931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (ret == -ENOMEDIUM)
14941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			tracks->error = CDS_NO_DISC;
14951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		else
14961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			tracks->error = CDS_NO_INFO;
14971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return;
14981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
14991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* check what type of tracks are on this disc */
15001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	entry.cdte_format = CDROM_MSF;
15011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	for (i = header.cdth_trk0; i <= header.cdth_trk1; i++) {
15021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		entry.cdte_track  = i;
15031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (cdi->ops->audio_ioctl(cdi, CDROMREADTOCENTRY, &entry)) {
15041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			tracks->error=CDS_NO_INFO;
15051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return;
15061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
15071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (entry.cdte_ctrl & CDROM_DATA_TRACK) {
15081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		    if (entry.cdte_format == 0x10)
15091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			tracks->cdi++;
15101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		    else if (entry.cdte_format == 0x20)
15111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			tracks->xa++;
15121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		    else
15131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			tracks->data++;
15141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		} else
15151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		    tracks->audio++;
15161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cdinfo(CD_COUNT_TRACKS, "track %d: format=%d, ctrl=%d\n",
15171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		       i, entry.cdte_format, entry.cdte_ctrl);
15181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
15191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cdinfo(CD_COUNT_TRACKS, "disc has %d tracks: %d=audio %d=data %d=Cd-I %d=XA\n",
15201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		header.cdth_trk1, tracks->audio, tracks->data,
15211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		tracks->cdi, tracks->xa);
15221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
15231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Requests to the low-level drivers will /always/ be done in the
15251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds   following format convention:
15261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds   CDROM_LBA: all data-related requests.
15281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds   CDROM_MSF: all audio-related requests.
15291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds   However, a low-level implementation is allowed to refuse this
15311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds   request, and return information in its own favorite format.
15321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds   It doesn't make sense /at all/ to ask for a play_audio in LBA
15341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds   format, or ask for multi-session info in MSF format. However, for
15351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds   backward compatibility these format requests will be satisfied, but
15361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds   the requests to the low-level drivers will be sanitized in the more
15371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds   meaningful format indicated above.
15381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
15391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic
15411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsvoid sanitize_format(union cdrom_addr *addr,
15421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		     u_char * curr, u_char requested)
15431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
15441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (*curr == requested)
15451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return;                 /* nothing to be done! */
15461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (requested == CDROM_LBA) {
15471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		addr->lba = (int) addr->msf.frame +
15481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			75 * (addr->msf.second - 2 + 60 * addr->msf.minute);
15491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	} else {                        /* CDROM_MSF */
15501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		int lba = addr->lba;
15511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		addr->msf.frame = lba % 75;
15521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		lba /= 75;
15531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		lba += 2;
15541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		addr->msf.second = lba % 60;
15551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		addr->msf.minute = lba / 60;
15561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
15571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	*curr = requested;
15581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
15591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsvoid init_cdrom_command(struct packet_command *cgc, void *buf, int len,
15611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			int type)
15621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
15631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	memset(cgc, 0, sizeof(struct packet_command));
15641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (buf)
15651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		memset(buf, 0, len);
15661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc->buffer = (char *) buf;
15671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc->buflen = len;
15681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc->data_direction = type;
15692e11c207b029cfaf57159cabac4b002204445258Jens Axboe	cgc->timeout = CDROM_DEF_TIMEOUT;
15701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
15711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* DVD handling */
15731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define copy_key(dest,src)	memcpy((dest), (src), sizeof(dvd_key))
15751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define copy_chal(dest,src)	memcpy((dest), (src), sizeof(dvd_challenge))
15761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void setup_report_key(struct packet_command *cgc, unsigned agid, unsigned type)
15781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
15791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc->cmd[0] = GPCMD_REPORT_KEY;
15801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc->cmd[10] = type | (agid << 6);
15811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	switch (type) {
15821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case 0: case 8: case 5: {
15831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			cgc->buflen = 8;
15841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
15851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
15861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case 1: {
15871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			cgc->buflen = 16;
15881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
15891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
15901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case 2: case 4: {
15911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			cgc->buflen = 12;
15921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
15931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
15941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
15951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc->cmd[9] = cgc->buflen;
15961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc->data_direction = CGC_DATA_READ;
15971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
15981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void setup_send_key(struct packet_command *cgc, unsigned agid, unsigned type)
16001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
16011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc->cmd[0] = GPCMD_SEND_KEY;
16021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc->cmd[10] = type | (agid << 6);
16031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	switch (type) {
16041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case 1: {
16051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			cgc->buflen = 16;
16061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
16071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
16081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case 3: {
16091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			cgc->buflen = 12;
16101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
16111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
16121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case 6: {
16131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			cgc->buflen = 8;
16141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
16151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
16161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
16171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc->cmd[9] = cgc->buflen;
16181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc->data_direction = CGC_DATA_WRITE;
16191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
16201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int dvd_do_auth(struct cdrom_device_info *cdi, dvd_authinfo *ai)
16221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
16231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int ret;
1624fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori	u_char buf[20];
16251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct packet_command cgc;
16261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct cdrom_device_ops *cdo = cdi->ops;
1627fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori	rpc_state_t rpc_state;
16281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1629fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori	memset(buf, 0, sizeof(buf));
16301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	init_cdrom_command(&cgc, buf, 0, CGC_DATA_READ);
16311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	switch (ai->type) {
16331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* LU data send */
16341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case DVD_LU_SEND_AGID:
16351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cdinfo(CD_DVD, "entering DVD_LU_SEND_AGID\n");
16361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cgc.quiet = 1;
16371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		setup_report_key(&cgc, ai->lsa.agid, 0);
16381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if ((ret = cdo->generic_packet(cdi, &cgc)))
1640fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori			return ret;
16411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ai->lsa.agid = buf[7] >> 6;
16431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* Returning data, let host change state */
16441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		break;
16451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case DVD_LU_SEND_KEY1:
16471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cdinfo(CD_DVD, "entering DVD_LU_SEND_KEY1\n");
16481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		setup_report_key(&cgc, ai->lsk.agid, 2);
16491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if ((ret = cdo->generic_packet(cdi, &cgc)))
1651fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori			return ret;
16521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		copy_key(ai->lsk.key, &buf[4]);
16541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* Returning data, let host change state */
16551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		break;
16561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case DVD_LU_SEND_CHALLENGE:
16581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cdinfo(CD_DVD, "entering DVD_LU_SEND_CHALLENGE\n");
16591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		setup_report_key(&cgc, ai->lsc.agid, 1);
16601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if ((ret = cdo->generic_packet(cdi, &cgc)))
1662fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori			return ret;
16631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		copy_chal(ai->lsc.chal, &buf[4]);
16651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* Returning data, let host change state */
16661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		break;
16671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* Post-auth key */
16691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case DVD_LU_SEND_TITLE_KEY:
16701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cdinfo(CD_DVD, "entering DVD_LU_SEND_TITLE_KEY\n");
16711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cgc.quiet = 1;
16721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		setup_report_key(&cgc, ai->lstk.agid, 4);
16731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cgc.cmd[5] = ai->lstk.lba;
16741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cgc.cmd[4] = ai->lstk.lba >> 8;
16751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cgc.cmd[3] = ai->lstk.lba >> 16;
16761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cgc.cmd[2] = ai->lstk.lba >> 24;
16771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if ((ret = cdo->generic_packet(cdi, &cgc)))
1679fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori			return ret;
16801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ai->lstk.cpm = (buf[4] >> 7) & 1;
16821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ai->lstk.cp_sec = (buf[4] >> 6) & 1;
16831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ai->lstk.cgms = (buf[4] >> 4) & 3;
16841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		copy_key(ai->lstk.title_key, &buf[5]);
16851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* Returning data, let host change state */
16861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		break;
16871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case DVD_LU_SEND_ASF:
16891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cdinfo(CD_DVD, "entering DVD_LU_SEND_ASF\n");
16901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		setup_report_key(&cgc, ai->lsasf.agid, 5);
16911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if ((ret = cdo->generic_packet(cdi, &cgc)))
1693fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori			return ret;
16941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ai->lsasf.asf = buf[7] & 1;
16961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		break;
16971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* LU data receive (LU changes state) */
16991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case DVD_HOST_SEND_CHALLENGE:
17001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cdinfo(CD_DVD, "entering DVD_HOST_SEND_CHALLENGE\n");
17011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		setup_send_key(&cgc, ai->hsc.agid, 1);
17021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		buf[1] = 0xe;
17031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		copy_chal(&buf[4], ai->hsc.chal);
17041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
17051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if ((ret = cdo->generic_packet(cdi, &cgc)))
1706fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori			return ret;
17071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
17081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ai->type = DVD_LU_SEND_KEY1;
17091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		break;
17101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
17111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case DVD_HOST_SEND_KEY2:
17121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cdinfo(CD_DVD, "entering DVD_HOST_SEND_KEY2\n");
17131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		setup_send_key(&cgc, ai->hsk.agid, 3);
17141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		buf[1] = 0xa;
17151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		copy_key(&buf[4], ai->hsk.key);
17161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
17171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if ((ret = cdo->generic_packet(cdi, &cgc))) {
17181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			ai->type = DVD_AUTH_FAILURE;
1719fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori			return ret;
17201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
17211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ai->type = DVD_AUTH_ESTABLISHED;
17221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		break;
17231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
17241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* Misc */
17251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case DVD_INVALIDATE_AGID:
17261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cgc.quiet = 1;
17271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cdinfo(CD_DVD, "entering DVD_INVALIDATE_AGID\n");
17281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		setup_report_key(&cgc, ai->lsa.agid, 0x3f);
17291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if ((ret = cdo->generic_packet(cdi, &cgc)))
1730fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori			return ret;
17311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		break;
17321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
17331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* Get region settings */
17341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case DVD_LU_SEND_RPC_STATE:
17351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cdinfo(CD_DVD, "entering DVD_LU_SEND_RPC_STATE\n");
17361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		setup_report_key(&cgc, 0, 8);
1737fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori		memset(&rpc_state, 0, sizeof(rpc_state_t));
1738fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori		cgc.buffer = (char *) &rpc_state;
17391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
17401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if ((ret = cdo->generic_packet(cdi, &cgc)))
1741fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori			return ret;
17421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1743fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori		ai->lrpcs.type = rpc_state.type_code;
1744fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori		ai->lrpcs.vra = rpc_state.vra;
1745fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori		ai->lrpcs.ucca = rpc_state.ucca;
1746fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori		ai->lrpcs.region_mask = rpc_state.region_mask;
1747fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori		ai->lrpcs.rpc_scheme = rpc_state.rpc_scheme;
17481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		break;
17491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
17501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* Set region settings */
17511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case DVD_HOST_SEND_RPC_STATE:
17521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cdinfo(CD_DVD, "entering DVD_HOST_SEND_RPC_STATE\n");
17531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		setup_send_key(&cgc, 0, 6);
17541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		buf[1] = 6;
17551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		buf[4] = ai->hrpcs.pdrc;
17561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
17571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if ((ret = cdo->generic_packet(cdi, &cgc)))
1758fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori			return ret;
17591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		break;
17601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
17611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	default:
17621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cdinfo(CD_WARNING, "Invalid DVD key ioctl (%d)\n", ai->type);
1763fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori		return -ENOTTY;
17641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
1765fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori
1766fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori	return 0;
17671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
17681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1769d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarzstatic int dvd_read_physical(struct cdrom_device_info *cdi, dvd_struct *s,
1770d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarz				struct packet_command *cgc)
17711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1772fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori	unsigned char buf[21], *base;
17731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct dvd_layer *layer;
17741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct cdrom_device_ops *cdo = cdi->ops;
17751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int ret, layer_num = s->physical.layer_num;
17761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
17771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (layer_num >= DVD_LAYERS)
17781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return -EINVAL;
17791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1780d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarz	init_cdrom_command(cgc, buf, sizeof(buf), CGC_DATA_READ);
1781d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarz	cgc->cmd[0] = GPCMD_READ_DVD_STRUCTURE;
1782d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarz	cgc->cmd[6] = layer_num;
1783d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarz	cgc->cmd[7] = s->type;
1784d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarz	cgc->cmd[9] = cgc->buflen & 0xff;
17851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
17861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
17871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * refrain from reporting errors on non-existing layers (mainly)
17881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
1789d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarz	cgc->quiet = 1;
17901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1791d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarz	ret = cdo->generic_packet(cdi, cgc);
1792d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarz	if (ret)
1793fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori		return ret;
17941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
17951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	base = &buf[4];
17961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	layer = &s->physical.layer[layer_num];
17971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
17981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
17991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * place the data... really ugly, but at least we won't have to
18001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * worry about endianess in userspace.
18011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
18021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	memset(layer, 0, sizeof(*layer));
18031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	layer->book_version = base[0] & 0xf;
18041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	layer->book_type = base[0] >> 4;
18051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	layer->min_rate = base[1] & 0xf;
18061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	layer->disc_size = base[1] >> 4;
18071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	layer->layer_type = base[2] & 0xf;
18081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	layer->track_path = (base[2] >> 4) & 1;
18091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	layer->nlayers = (base[2] >> 5) & 3;
18101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	layer->track_density = base[3] & 0xf;
18111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	layer->linear_density = base[3] >> 4;
18121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	layer->start_sector = base[5] << 16 | base[6] << 8 | base[7];
18131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	layer->end_sector = base[9] << 16 | base[10] << 8 | base[11];
18141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	layer->end_sector_l0 = base[13] << 16 | base[14] << 8 | base[15];
18151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	layer->bca = base[16] >> 7;
18161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1817fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori	return 0;
18181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
18191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1820d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarzstatic int dvd_read_copyright(struct cdrom_device_info *cdi, dvd_struct *s,
1821d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarz				struct packet_command *cgc)
18221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
18231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int ret;
1824fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori	u_char buf[8];
18251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct cdrom_device_ops *cdo = cdi->ops;
18261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1827d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarz	init_cdrom_command(cgc, buf, sizeof(buf), CGC_DATA_READ);
1828d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarz	cgc->cmd[0] = GPCMD_READ_DVD_STRUCTURE;
1829d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarz	cgc->cmd[6] = s->copyright.layer_num;
1830d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarz	cgc->cmd[7] = s->type;
1831d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarz	cgc->cmd[8] = cgc->buflen >> 8;
1832d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarz	cgc->cmd[9] = cgc->buflen & 0xff;
18331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1834d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarz	ret = cdo->generic_packet(cdi, cgc);
1835d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarz	if (ret)
1836fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori		return ret;
18371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	s->copyright.cpst = buf[4];
18391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	s->copyright.rmi = buf[5];
18401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1841fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori	return 0;
18421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
18431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1844d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarzstatic int dvd_read_disckey(struct cdrom_device_info *cdi, dvd_struct *s,
1845d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarz				struct packet_command *cgc)
18461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
18471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int ret, size;
18481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	u_char *buf;
18491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct cdrom_device_ops *cdo = cdi->ops;
18501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	size = sizeof(s->disckey.value) + 4;
18521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1853d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarz	buf = kmalloc(size, GFP_KERNEL);
1854d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarz	if (!buf)
18551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return -ENOMEM;
18561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1857d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarz	init_cdrom_command(cgc, buf, size, CGC_DATA_READ);
1858d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarz	cgc->cmd[0] = GPCMD_READ_DVD_STRUCTURE;
1859d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarz	cgc->cmd[7] = s->type;
1860d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarz	cgc->cmd[8] = size >> 8;
1861d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarz	cgc->cmd[9] = size & 0xff;
1862d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarz	cgc->cmd[10] = s->disckey.agid << 6;
18631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1864d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarz	ret = cdo->generic_packet(cdi, cgc);
1865d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarz	if (!ret)
18661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		memcpy(s->disckey.value, &buf[4], sizeof(s->disckey.value));
18671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	kfree(buf);
18691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return ret;
18701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
18711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1872d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarzstatic int dvd_read_bca(struct cdrom_device_info *cdi, dvd_struct *s,
1873d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarz			struct packet_command *cgc)
18741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1875d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarz	int ret, size = 4 + 188;
1876d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarz	u_char *buf;
18771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct cdrom_device_ops *cdo = cdi->ops;
18781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1879d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarz	buf = kmalloc(size, GFP_KERNEL);
1880d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarz	if (!buf)
1881d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarz		return -ENOMEM;
18821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1883d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarz	init_cdrom_command(cgc, buf, size, CGC_DATA_READ);
1884d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarz	cgc->cmd[0] = GPCMD_READ_DVD_STRUCTURE;
1885d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarz	cgc->cmd[7] = s->type;
1886d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarz	cgc->cmd[9] = cgc->buflen & 0xff;
1887d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarz
1888d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarz	ret = cdo->generic_packet(cdi, cgc);
1889d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarz	if (ret)
1890d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarz		goto out;
18911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	s->bca.len = buf[0] << 8 | buf[1];
18931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (s->bca.len < 12 || s->bca.len > 188) {
18941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cdinfo(CD_WARNING, "Received invalid BCA length (%d)\n", s->bca.len);
1895d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarz		ret = -EIO;
1896d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarz		goto out;
18971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
18981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	memcpy(s->bca.value, &buf[4], s->bca.len);
1899d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarz	ret = 0;
1900d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarzout:
1901d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarz	kfree(buf);
1902d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarz	return ret;
19031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
19041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1905d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarzstatic int dvd_read_manufact(struct cdrom_device_info *cdi, dvd_struct *s,
1906d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarz				struct packet_command *cgc)
19071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
19081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int ret = 0, size;
19091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	u_char *buf;
19101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct cdrom_device_ops *cdo = cdi->ops;
19111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
19121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	size = sizeof(s->manufact.value) + 4;
19131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1914d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarz	buf = kmalloc(size, GFP_KERNEL);
1915d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarz	if (!buf)
19161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return -ENOMEM;
19171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1918d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarz	init_cdrom_command(cgc, buf, size, CGC_DATA_READ);
1919d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarz	cgc->cmd[0] = GPCMD_READ_DVD_STRUCTURE;
1920d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarz	cgc->cmd[7] = s->type;
1921d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarz	cgc->cmd[8] = size >> 8;
1922d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarz	cgc->cmd[9] = size & 0xff;
19231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1924d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarz	ret = cdo->generic_packet(cdi, cgc);
1925d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarz	if (ret)
1926d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarz		goto out;
19271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
19281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	s->manufact.len = buf[0] << 8 | buf[1];
1929aec9f377e4f235c47e27fd8a429555dfa2dda342Andrew Morton	if (s->manufact.len < 0) {
19301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cdinfo(CD_WARNING, "Received invalid manufacture info length"
19311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				   " (%d)\n", s->manufact.len);
19321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ret = -EIO;
19331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	} else {
1934aec9f377e4f235c47e27fd8a429555dfa2dda342Andrew Morton		if (s->manufact.len > 2048) {
1935aec9f377e4f235c47e27fd8a429555dfa2dda342Andrew Morton			cdinfo(CD_WARNING, "Received invalid manufacture info "
1936aec9f377e4f235c47e27fd8a429555dfa2dda342Andrew Morton					"length (%d): truncating to 2048\n",
1937aec9f377e4f235c47e27fd8a429555dfa2dda342Andrew Morton					s->manufact.len);
1938aec9f377e4f235c47e27fd8a429555dfa2dda342Andrew Morton			s->manufact.len = 2048;
1939aec9f377e4f235c47e27fd8a429555dfa2dda342Andrew Morton		}
19401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		memcpy(s->manufact.value, &buf[4], s->manufact.len);
19411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
19421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1943d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarzout:
19441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	kfree(buf);
19451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return ret;
19461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
19471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1948d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarzstatic int dvd_read_struct(struct cdrom_device_info *cdi, dvd_struct *s,
1949d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarz				struct packet_command *cgc)
19501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
19511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	switch (s->type) {
19521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case DVD_STRUCT_PHYSICAL:
1953d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarz		return dvd_read_physical(cdi, s, cgc);
19541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
19551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case DVD_STRUCT_COPYRIGHT:
1956d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarz		return dvd_read_copyright(cdi, s, cgc);
19571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
19581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case DVD_STRUCT_DISCKEY:
1959d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarz		return dvd_read_disckey(cdi, s, cgc);
19601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
19611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case DVD_STRUCT_BCA:
1962d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarz		return dvd_read_bca(cdi, s, cgc);
19631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
19641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case DVD_STRUCT_MANUFACT:
1965d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarz		return dvd_read_manufact(cdi, s, cgc);
19661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
19671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	default:
19681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cdinfo(CD_WARNING, ": Invalid DVD structure read requested (%d)\n",
19691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					s->type);
19701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return -EINVAL;
19711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
19721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
19731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
19741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsint cdrom_mode_sense(struct cdrom_device_info *cdi,
19751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		     struct packet_command *cgc,
19761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		     int page_code, int page_control)
19771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
19781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct cdrom_device_ops *cdo = cdi->ops;
19791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
19801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	memset(cgc->cmd, 0, sizeof(cgc->cmd));
19811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
19821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc->cmd[0] = GPCMD_MODE_SENSE_10;
19831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc->cmd[2] = page_code | (page_control << 6);
19841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc->cmd[7] = cgc->buflen >> 8;
19851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc->cmd[8] = cgc->buflen & 0xff;
19861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc->data_direction = CGC_DATA_READ;
19871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return cdo->generic_packet(cdi, cgc);
19881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
19891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
19901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsint cdrom_mode_select(struct cdrom_device_info *cdi,
19911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		      struct packet_command *cgc)
19921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
19931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct cdrom_device_ops *cdo = cdi->ops;
19941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
19951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	memset(cgc->cmd, 0, sizeof(cgc->cmd));
19961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	memset(cgc->buffer, 0, 2);
19971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc->cmd[0] = GPCMD_MODE_SELECT_10;
19981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc->cmd[1] = 0x10;		/* PF */
19991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc->cmd[7] = cgc->buflen >> 8;
20001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc->cmd[8] = cgc->buflen & 0xff;
20011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc->data_direction = CGC_DATA_WRITE;
20021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return cdo->generic_packet(cdi, cgc);
20031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
20041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
20051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int cdrom_read_subchannel(struct cdrom_device_info *cdi,
20061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				 struct cdrom_subchnl *subchnl, int mcn)
20071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
20081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct cdrom_device_ops *cdo = cdi->ops;
20091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct packet_command cgc;
2010fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori	char buffer[32];
20111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int ret;
20121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
20131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	init_cdrom_command(&cgc, buffer, 16, CGC_DATA_READ);
20141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc.cmd[0] = GPCMD_READ_SUBCHANNEL;
20151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc.cmd[1] = 2;     /* MSF addressing */
20161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc.cmd[2] = 0x40;  /* request subQ data */
20171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc.cmd[3] = mcn ? 2 : 1;
20181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc.cmd[8] = 16;
20191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
20201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if ((ret = cdo->generic_packet(cdi, &cgc)))
2021fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori		return ret;
20221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
20231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	subchnl->cdsc_audiostatus = cgc.buffer[1];
20241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	subchnl->cdsc_format = CDROM_MSF;
20251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	subchnl->cdsc_ctrl = cgc.buffer[5] & 0xf;
20261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	subchnl->cdsc_trk = cgc.buffer[6];
20271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	subchnl->cdsc_ind = cgc.buffer[7];
20281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
20291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	subchnl->cdsc_reladdr.msf.minute = cgc.buffer[13];
20301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	subchnl->cdsc_reladdr.msf.second = cgc.buffer[14];
20311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	subchnl->cdsc_reladdr.msf.frame = cgc.buffer[15];
20321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	subchnl->cdsc_absaddr.msf.minute = cgc.buffer[9];
20331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	subchnl->cdsc_absaddr.msf.second = cgc.buffer[10];
20341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	subchnl->cdsc_absaddr.msf.frame = cgc.buffer[11];
20351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2036fce5384755e8e0e56c5609d2972db4702990d592FUJITA Tomonori	return 0;
20371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
20381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
20391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
20401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Specific READ_10 interface
20411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
20421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int cdrom_read_cd(struct cdrom_device_info *cdi,
20431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			 struct packet_command *cgc, int lba,
20441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			 int blocksize, int nblocks)
20451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
20461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct cdrom_device_ops *cdo = cdi->ops;
20471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
20481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	memset(&cgc->cmd, 0, sizeof(cgc->cmd));
20491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc->cmd[0] = GPCMD_READ_10;
20501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc->cmd[2] = (lba >> 24) & 0xff;
20511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc->cmd[3] = (lba >> 16) & 0xff;
20521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc->cmd[4] = (lba >>  8) & 0xff;
20531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc->cmd[5] = lba & 0xff;
20541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc->cmd[6] = (nblocks >> 16) & 0xff;
20551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc->cmd[7] = (nblocks >>  8) & 0xff;
20561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc->cmd[8] = nblocks & 0xff;
20571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc->buflen = blocksize * nblocks;
20581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return cdo->generic_packet(cdi, cgc);
20591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
20601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
20611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* very generic interface for reading the various types of blocks */
20621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int cdrom_read_block(struct cdrom_device_info *cdi,
20631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			    struct packet_command *cgc,
20641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			    int lba, int nblocks, int format, int blksize)
20651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
20661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct cdrom_device_ops *cdo = cdi->ops;
20671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
20681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	memset(&cgc->cmd, 0, sizeof(cgc->cmd));
20691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc->cmd[0] = GPCMD_READ_CD;
20701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* expected sector size - cdda,mode1,etc. */
20711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc->cmd[1] = format << 2;
20721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* starting address */
20731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc->cmd[2] = (lba >> 24) & 0xff;
20741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc->cmd[3] = (lba >> 16) & 0xff;
20751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc->cmd[4] = (lba >>  8) & 0xff;
20761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc->cmd[5] = lba & 0xff;
20771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* number of blocks */
20781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc->cmd[6] = (nblocks >> 16) & 0xff;
20791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc->cmd[7] = (nblocks >>  8) & 0xff;
20801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc->cmd[8] = nblocks & 0xff;
20811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc->buflen = blksize * nblocks;
20821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
20831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* set the header info returned */
20841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	switch (blksize) {
20851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case CD_FRAMESIZE_RAW0	: cgc->cmd[9] = 0x58; break;
20861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case CD_FRAMESIZE_RAW1	: cgc->cmd[9] = 0x78; break;
20871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case CD_FRAMESIZE_RAW	: cgc->cmd[9] = 0xf8; break;
20881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	default			: cgc->cmd[9] = 0x10;
20891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
20901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
20911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return cdo->generic_packet(cdi, cgc);
20921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
20931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
20941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int cdrom_read_cdda_old(struct cdrom_device_info *cdi, __u8 __user *ubuf,
20951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			       int lba, int nframes)
20961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
20971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct packet_command cgc;
20981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int ret = 0;
20991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int nr;
21001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
21011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cdi->last_sense = 0;
21021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
21031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	memset(&cgc, 0, sizeof(cgc));
21041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
21051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
21061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * start with will ra.nframes size, back down if alloc fails
21071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
21081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	nr = nframes;
21091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	do {
21101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cgc.buffer = kmalloc(CD_FRAMESIZE_RAW * nr, GFP_KERNEL);
21111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (cgc.buffer)
21121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
21131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
21141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		nr >>= 1;
21151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	} while (nr);
21161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
21171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!nr)
21181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return -ENOMEM;
21191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
21201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc.data_direction = CGC_DATA_READ;
21211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	while (nframes > 0) {
21221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (nr > nframes)
21231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			nr = nframes;
21241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
21251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ret = cdrom_read_block(cdi, &cgc, lba, nr, 1, CD_FRAMESIZE_RAW);
21261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (ret)
21271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
2128822bfa51ce44f2c63c300fdb76dc99c4d5a5ca9fDan Carpenter		if (copy_to_user(ubuf, cgc.buffer, CD_FRAMESIZE_RAW * nr)) {
21291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			ret = -EFAULT;
21301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
21311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
21321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ubuf += CD_FRAMESIZE_RAW * nr;
21331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		nframes -= nr;
21341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		lba += nr;
21351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
21361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	kfree(cgc.buffer);
21371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return ret;
21381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
21391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
21401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int cdrom_read_cdda_bpc(struct cdrom_device_info *cdi, __u8 __user *ubuf,
21411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			       int lba, int nframes)
21421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
2143165125e1e480f9510a5ffcfbfee4e3ee38c05f23Jens Axboe	struct request_queue *q = cdi->disk->queue;
21441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct request *rq;
21451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct bio *bio;
21461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned int len;
21471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int nr, ret = 0;
21481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
21491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!q)
21501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return -ENXIO;
21511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
21521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cdi->last_sense = 0;
21531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
21541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	while (nframes) {
21551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		nr = nframes;
21561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (cdi->cdda_method == CDDA_BPC_SINGLE)
21571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			nr = 1;
2158ae03bf639a5027d27270123f5f6e3ee6a412781dMartin K. Petersen		if (nr * CD_FRAMESIZE_RAW > (queue_max_sectors(q) << 9))
2159ae03bf639a5027d27270123f5f6e3ee6a412781dMartin K. Petersen			nr = (queue_max_sectors(q) << 9) / CD_FRAMESIZE_RAW;
21601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
21611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		len = nr * CD_FRAMESIZE_RAW;
21621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2163febd7a5c131433ea128b54dd5712e260c79eb3abAlan D. Brunelle		rq = blk_get_request(q, READ, GFP_KERNEL);
2164febd7a5c131433ea128b54dd5712e260c79eb3abAlan D. Brunelle		if (!rq) {
2165febd7a5c131433ea128b54dd5712e260c79eb3abAlan D. Brunelle			ret = -ENOMEM;
2166febd7a5c131433ea128b54dd5712e260c79eb3abAlan D. Brunelle			break;
2167febd7a5c131433ea128b54dd5712e260c79eb3abAlan D. Brunelle		}
2168febd7a5c131433ea128b54dd5712e260c79eb3abAlan D. Brunelle
2169152e283fdfea0cd11e297d982378b55937842ddeFUJITA Tomonori		ret = blk_rq_map_user(q, rq, NULL, ubuf, len, GFP_KERNEL);
2170febd7a5c131433ea128b54dd5712e260c79eb3abAlan D. Brunelle		if (ret) {
2171febd7a5c131433ea128b54dd5712e260c79eb3abAlan D. Brunelle			blk_put_request(rq);
2172dd1cab95f356f1395278633565f198463cf6bd24Jens Axboe			break;
2173febd7a5c131433ea128b54dd5712e260c79eb3abAlan D. Brunelle		}
21741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
21751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		rq->cmd[0] = GPCMD_READ_CD;
21761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		rq->cmd[1] = 1 << 2;
21771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		rq->cmd[2] = (lba >> 24) & 0xff;
21781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		rq->cmd[3] = (lba >> 16) & 0xff;
21791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		rq->cmd[4] = (lba >>  8) & 0xff;
21801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		rq->cmd[5] = lba & 0xff;
21811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		rq->cmd[6] = (nr >> 16) & 0xff;
21821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		rq->cmd[7] = (nr >>  8) & 0xff;
21831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		rq->cmd[8] = nr & 0xff;
21841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		rq->cmd[9] = 0xf8;
21851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
21861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		rq->cmd_len = 12;
21874aff5e2333c9a1609662f2091f55c3f6fffdad36Jens Axboe		rq->cmd_type = REQ_TYPE_BLOCK_PC;
21881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		rq->timeout = 60 * HZ;
21891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		bio = rq->bio;
21901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2191994ca9a19616f0d4161a9e825f0835925d522426James Bottomley		if (blk_execute_rq(q, cdi->disk, rq, 0)) {
21921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			struct request_sense *s = rq->sense;
21931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			ret = -EIO;
21941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			cdi->last_sense = s->sense_key;
21951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
21961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
21978e5cfc45e7527eb5c8a9a22d56a7b9227e7c0913Jens Axboe		if (blk_rq_unmap_user(bio))
21981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			ret = -EFAULT;
2199febd7a5c131433ea128b54dd5712e260c79eb3abAlan D. Brunelle		blk_put_request(rq);
22001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
22011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (ret)
22021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
22031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
22041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		nframes -= nr;
22051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		lba += nr;
22061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ubuf += len;
22071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
22081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
22091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return ret;
22101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
22111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
22121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int cdrom_read_cdda(struct cdrom_device_info *cdi, __u8 __user *ubuf,
22131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			   int lba, int nframes)
22141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
22151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int ret;
22161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
22171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (cdi->cdda_method == CDDA_OLD)
22181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return cdrom_read_cdda_old(cdi, ubuf, lba, nframes);
22191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
22201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsretry:
22211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
22221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * for anything else than success and io error, we need to retry
22231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
22241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	ret = cdrom_read_cdda_bpc(cdi, ubuf, lba, nframes);
22251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!ret || ret != -EIO)
22261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return ret;
22271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
22281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
22291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * I've seen drives get sense 4/8/3 udma crc errors on multi
22301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * frame dma, so drop to single frame dma if we need to
22311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
22321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (cdi->cdda_method == CDDA_BPC_FULL && nframes > 1) {
2233e597cd09f711b28b8466ebdc2f12e55b44fa81e4Joe Perches		pr_info("dropping to single frame dma\n");
22341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cdi->cdda_method = CDDA_BPC_SINGLE;
22351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto retry;
22361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
22371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
22381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
22391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * so we have an io error of some sort with multi frame dma. if the
22401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * condition wasn't a hardware error
22411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * problems, not for any error
22421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
22431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (cdi->last_sense != 0x04 && cdi->last_sense != 0x0b)
22441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return ret;
22451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2246e597cd09f711b28b8466ebdc2f12e55b44fa81e4Joe Perches	pr_info("dropping to old style cdda (sense=%x)\n", cdi->last_sense);
22471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cdi->cdda_method = CDDA_OLD;
22481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return cdrom_read_cdda_old(cdi, ubuf, lba, nframes);
22491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
22501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2251d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwigstatic int cdrom_ioctl_multisession(struct cdrom_device_info *cdi,
2252d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		void __user *argp)
22531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
2254d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	struct cdrom_multisession ms_info;
2255d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	u8 requested_format;
22561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int ret;
22571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2258d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	cdinfo(CD_DO_IOCTL, "entering CDROMMULTISESSION\n");
2259d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig
2260d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	if (!(cdi->ops->capability & CDC_MULTI_SESSION))
2261d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return -ENOSYS;
2262d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig
2263d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	if (copy_from_user(&ms_info, argp, sizeof(ms_info)))
2264d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return -EFAULT;
2265d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig
2266d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	requested_format = ms_info.addr_format;
2267d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	if (requested_format != CDROM_MSF && requested_format != CDROM_LBA)
2268d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return -EINVAL;
2269d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	ms_info.addr_format = CDROM_LBA;
2270d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig
2271d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	ret = cdi->ops->get_last_session(cdi, &ms_info);
2272d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	if (ret)
22731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return ret;
22741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2275d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	sanitize_format(&ms_info.addr, &ms_info.addr_format, requested_format);
2276d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig
2277d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	if (copy_to_user(argp, &ms_info, sizeof(ms_info)))
2278d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return -EFAULT;
2279d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig
2280d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	cdinfo(CD_DO_IOCTL, "CDROMMULTISESSION successful\n");
2281d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	return 0;
2282d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig}
2283d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig
2284d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwigstatic int cdrom_ioctl_eject(struct cdrom_device_info *cdi)
2285d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig{
2286d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	cdinfo(CD_DO_IOCTL, "entering CDROMEJECT\n");
2287d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig
2288d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	if (!CDROM_CAN(CDC_OPEN_TRAY))
2289d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return -ENOSYS;
2290cdccaa9467b982d57b139818d15e1e994feca372Paolo Bonzini	if (cdi->use_count != 1 || cdi->keeplocked)
2291d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return -EBUSY;
2292d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	if (CDROM_CAN(CDC_LOCK)) {
2293d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		int ret = cdi->ops->lock_door(cdi, 0);
2294d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		if (ret)
22951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return ret;
2296d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	}
22971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2298d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	return cdi->ops->tray_move(cdi, 1);
2299d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig}
23001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2301d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwigstatic int cdrom_ioctl_closetray(struct cdrom_device_info *cdi)
2302d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig{
2303d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	cdinfo(CD_DO_IOCTL, "entering CDROMCLOSETRAY\n");
23041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2305d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	if (!CDROM_CAN(CDC_CLOSE_TRAY))
2306d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return -ENOSYS;
2307d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	return cdi->ops->tray_move(cdi, 0);
2308d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig}
23091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2310d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwigstatic int cdrom_ioctl_eject_sw(struct cdrom_device_info *cdi,
2311d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		unsigned long arg)
2312d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig{
2313d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	cdinfo(CD_DO_IOCTL, "entering CDROMEJECT_SW\n");
23141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2315d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	if (!CDROM_CAN(CDC_OPEN_TRAY))
2316d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return -ENOSYS;
2317cdccaa9467b982d57b139818d15e1e994feca372Paolo Bonzini	if (cdi->keeplocked)
2318d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return -EBUSY;
23191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2320d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	cdi->options &= ~(CDO_AUTO_CLOSE | CDO_AUTO_EJECT);
2321d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	if (arg)
2322d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		cdi->options |= CDO_AUTO_CLOSE | CDO_AUTO_EJECT;
2323d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	return 0;
2324d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig}
23251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2326d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwigstatic int cdrom_ioctl_media_changed(struct cdrom_device_info *cdi,
2327d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		unsigned long arg)
2328d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig{
2329d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	struct cdrom_changer_info *info;
2330d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	int ret;
23311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2332d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	cdinfo(CD_DO_IOCTL, "entering CDROM_MEDIA_CHANGED\n");
23331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2334d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	if (!CDROM_CAN(CDC_MEDIA_CHANGED))
2335d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return -ENOSYS;
23361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2337d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	/* cannot select disc or select current disc */
2338d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	if (!CDROM_CAN(CDC_SELECT_DISC) || arg == CDSL_CURRENT)
2339d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return media_changed(cdi, 1);
23401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2341d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	if ((unsigned int)arg >= cdi->capacity)
2342d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return -EINVAL;
23431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2344d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	info = kmalloc(sizeof(*info), GFP_KERNEL);
2345d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	if (!info)
2346d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return -ENOMEM;
23471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2348d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	ret = cdrom_read_mech_status(cdi, info);
2349d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	if (!ret)
2350d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		ret = info->slots[arg].change;
2351d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	kfree(info);
2352d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	return ret;
2353d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig}
23541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2355d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwigstatic int cdrom_ioctl_set_options(struct cdrom_device_info *cdi,
2356d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		unsigned long arg)
2357d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig{
2358d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	cdinfo(CD_DO_IOCTL, "entering CDROM_SET_OPTIONS\n");
23591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2360d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	/*
2361d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	 * Options need to be in sync with capability.
2362d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	 * Too late for that, so we have to check each one separately.
2363d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	 */
2364d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	switch (arg) {
2365d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	case CDO_USE_FFLAGS:
2366d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	case CDO_CHECK_TYPE:
2367d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		break;
2368d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	case CDO_LOCK:
2369d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		if (!CDROM_CAN(CDC_LOCK))
2370d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig			return -ENOSYS;
2371d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		break;
2372d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	case 0:
2373d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return cdi->options;
2374d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	/* default is basically CDO_[AUTO_CLOSE|AUTO_EJECT] */
2375d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	default:
2376d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		if (!CDROM_CAN(arg))
23771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return -ENOSYS;
2378d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	}
2379d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	cdi->options |= (int) arg;
2380d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	return cdi->options;
2381d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig}
23821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2383d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwigstatic int cdrom_ioctl_clear_options(struct cdrom_device_info *cdi,
2384d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		unsigned long arg)
2385d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig{
2386d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	cdinfo(CD_DO_IOCTL, "entering CDROM_CLEAR_OPTIONS\n");
23871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2388d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	cdi->options &= ~(int) arg;
2389d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	return cdi->options;
2390d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig}
23911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2392d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwigstatic int cdrom_ioctl_select_speed(struct cdrom_device_info *cdi,
2393d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		unsigned long arg)
2394d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig{
2395d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	cdinfo(CD_DO_IOCTL, "entering CDROM_SELECT_SPEED\n");
23961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2397d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	if (!CDROM_CAN(CDC_SELECT_SPEED))
2398d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return -ENOSYS;
2399d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	return cdi->ops->select_speed(cdi, arg);
2400d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig}
24011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2402d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwigstatic int cdrom_ioctl_select_disc(struct cdrom_device_info *cdi,
2403d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		unsigned long arg)
2404d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig{
2405d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	cdinfo(CD_DO_IOCTL, "entering CDROM_SELECT_DISC\n");
2406d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig
2407d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	if (!CDROM_CAN(CDC_SELECT_DISC))
2408d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return -ENOSYS;
2409d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig
2410d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	if (arg != CDSL_CURRENT && arg != CDSL_NONE) {
2411d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		if ((int)arg >= cdi->capacity)
2412d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig			return -EINVAL;
2413d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	}
2414d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig
2415d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	/*
2416d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	 * ->select_disc is a hook to allow a driver-specific way of
2417d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	 * seleting disc.  However, since there is no equivalent hook for
2418d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	 * cdrom_slot_status this may not actually be useful...
2419d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	 */
2420d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	if (cdi->ops->select_disc)
2421d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return cdi->ops->select_disc(cdi, arg);
2422d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig
2423d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	cdinfo(CD_CHANGER, "Using generic cdrom_select_disc()\n");
2424d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	return cdrom_select_disc(cdi, arg);
2425d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig}
2426d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig
2427d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwigstatic int cdrom_ioctl_reset(struct cdrom_device_info *cdi,
2428d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		struct block_device *bdev)
2429d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig{
2430d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	cdinfo(CD_DO_IOCTL, "entering CDROM_RESET\n");
2431d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig
2432d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	if (!capable(CAP_SYS_ADMIN))
2433d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return -EACCES;
2434d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	if (!CDROM_CAN(CDC_RESET))
2435d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return -ENOSYS;
2436f98393a64ca1392130724c3acb4e3f325801d2b6Peter Zijlstra	invalidate_bdev(bdev);
2437d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	return cdi->ops->reset(cdi);
2438d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig}
2439d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig
2440d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwigstatic int cdrom_ioctl_lock_door(struct cdrom_device_info *cdi,
2441d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		unsigned long arg)
2442d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig{
2443d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	cdinfo(CD_DO_IOCTL, "%socking door.\n", arg ? "L" : "Unl");
2444d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig
2445d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	if (!CDROM_CAN(CDC_LOCK))
2446d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return -EDRIVE_CANT_DO_THIS;
2447d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig
2448cdccaa9467b982d57b139818d15e1e994feca372Paolo Bonzini	cdi->keeplocked = arg ? 1 : 0;
2449d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig
2450d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	/*
2451d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	 * Don't unlock the door on multiple opens by default, but allow
2452d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	 * root to do so.
2453d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	 */
2454d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	if (cdi->use_count != 1 && !arg && !capable(CAP_SYS_ADMIN))
2455d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return -EBUSY;
2456d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	return cdi->ops->lock_door(cdi, arg);
2457d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig}
2458d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig
2459d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwigstatic int cdrom_ioctl_debug(struct cdrom_device_info *cdi,
2460d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		unsigned long arg)
2461d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig{
2462d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	cdinfo(CD_DO_IOCTL, "%sabling debug.\n", arg ? "En" : "Dis");
2463d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig
2464d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	if (!capable(CAP_SYS_ADMIN))
2465d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return -EACCES;
2466d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	debug = arg ? 1 : 0;
2467d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	return debug;
2468d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig}
24691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2470d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwigstatic int cdrom_ioctl_get_capability(struct cdrom_device_info *cdi)
2471d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig{
2472d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	cdinfo(CD_DO_IOCTL, "entering CDROM_GET_CAPABILITY\n");
2473d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	return (cdi->ops->capability & ~cdi->mask);
2474d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig}
2475d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig
2476d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig/*
2477d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig * The following function is implemented, although very few audio
24781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * discs give Universal Product Code information, which should just be
24791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * the Medium Catalog Number on the box.  Note, that the way the code
24801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * is written on the CD is /not/ uniform across all discs!
24811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
2482d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwigstatic int cdrom_ioctl_get_mcn(struct cdrom_device_info *cdi,
2483d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		void __user *argp)
2484d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig{
2485d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	struct cdrom_mcn mcn;
2486d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	int ret;
24871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2488d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	cdinfo(CD_DO_IOCTL, "entering CDROM_GET_MCN\n");
24891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2490d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	if (!(cdi->ops->capability & CDC_MCN))
2491d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return -ENOSYS;
2492d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	ret = cdi->ops->get_mcn(cdi, &mcn);
2493d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	if (ret)
2494d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return ret;
24951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2496d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	if (copy_to_user(argp, &mcn, sizeof(mcn)))
2497d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return -EFAULT;
2498d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	cdinfo(CD_DO_IOCTL, "CDROM_GET_MCN successful\n");
2499d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	return 0;
2500d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig}
25011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2502d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwigstatic int cdrom_ioctl_drive_status(struct cdrom_device_info *cdi,
2503d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		unsigned long arg)
2504d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig{
2505d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	cdinfo(CD_DO_IOCTL, "entering CDROM_DRIVE_STATUS\n");
2506d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig
2507d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	if (!(cdi->ops->capability & CDC_DRIVE_STATUS))
2508d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return -ENOSYS;
2509d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	if (!CDROM_CAN(CDC_SELECT_DISC) ||
2510d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	    (arg == CDSL_CURRENT || arg == CDSL_NONE))
2511d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return cdi->ops->drive_status(cdi, CDSL_CURRENT);
2512d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	if (((int)arg >= cdi->capacity))
2513d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return -EINVAL;
2514d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	return cdrom_slot_status(cdi, arg);
2515d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig}
2516d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig
2517d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig/*
2518d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig * Ok, this is where problems start.  The current interface for the
2519d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig * CDROM_DISC_STATUS ioctl is flawed.  It makes the false assumption that
252025985edcedea6396277003854657b5f3cb31a628Lucas De Marchi * CDs are all CDS_DATA_1 or all CDS_AUDIO, etc.  Unfortunately, while this
2521d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig * is often the case, it is also very common for CDs to have some tracks
2522d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig * with data, and some tracks with audio.  Just because I feel like it,
2523d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig * I declare the following to be the best way to cope.  If the CD has ANY
2524d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig * data tracks on it, it will be returned as a data CD.  If it has any XA
2525d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig * tracks, I will return it as that.  Now I could simplify this interface
2526d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig * by combining these  returns with the above, but this more clearly
2527d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig * demonstrates the problem with the current interface.  Too bad this
2528d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig * wasn't designed to use bitmasks...         -Erik
2529d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig *
2530d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig * Well, now we have the option CDS_MIXED: a mixed-type CD.
2531d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig * User level programmers might feel the ioctl is not very useful.
2532d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig *					---david
2533d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig */
2534d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwigstatic int cdrom_ioctl_disc_status(struct cdrom_device_info *cdi)
2535d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig{
2536d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	tracktype tracks;
2537d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig
2538d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	cdinfo(CD_DO_IOCTL, "entering CDROM_DISC_STATUS\n");
2539d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig
2540d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	cdrom_count_tracks(cdi, &tracks);
2541d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	if (tracks.error)
2542d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return tracks.error;
2543d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig
2544d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	/* Policy mode on */
2545d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	if (tracks.audio > 0) {
2546d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		if (!tracks.data && !tracks.cdi && !tracks.xa)
2547d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig			return CDS_AUDIO;
2548d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		else
2549d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig			return CDS_MIXED;
25501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
25511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2552d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	if (tracks.cdi > 0)
2553d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return CDS_XA_2_2;
2554d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	if (tracks.xa > 0)
2555d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return CDS_XA_2_1;
2556d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	if (tracks.data > 0)
2557d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return CDS_DATA_1;
2558d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	/* Policy mode off */
2559d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig
2560d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	cdinfo(CD_WARNING,"This disc doesn't have any tracks I recognize!\n");
2561d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	return CDS_NO_INFO;
2562d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig}
2563d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig
2564d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwigstatic int cdrom_ioctl_changer_nslots(struct cdrom_device_info *cdi)
2565d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig{
2566d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	cdinfo(CD_DO_IOCTL, "entering CDROM_CHANGER_NSLOTS\n");
2567d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	return cdi->capacity;
2568d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig}
2569d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig
2570d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwigstatic int cdrom_ioctl_get_subchnl(struct cdrom_device_info *cdi,
2571d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		void __user *argp)
2572d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig{
2573d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	struct cdrom_subchnl q;
2574d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	u8 requested, back;
2575d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	int ret;
2576d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig
2577d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	/* cdinfo(CD_DO_IOCTL,"entering CDROMSUBCHNL\n");*/
2578d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig
2579d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	if (copy_from_user(&q, argp, sizeof(q)))
2580d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return -EFAULT;
2581d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig
2582d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	requested = q.cdsc_format;
2583d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	if (requested != CDROM_MSF && requested != CDROM_LBA)
2584d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return -EINVAL;
2585d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	q.cdsc_format = CDROM_MSF;
2586d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig
2587d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	ret = cdi->ops->audio_ioctl(cdi, CDROMSUBCHNL, &q);
2588d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	if (ret)
2589d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return ret;
2590d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig
2591d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	back = q.cdsc_format; /* local copy */
2592d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	sanitize_format(&q.cdsc_absaddr, &back, requested);
2593d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	sanitize_format(&q.cdsc_reladdr, &q.cdsc_format, requested);
2594d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig
2595d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	if (copy_to_user(argp, &q, sizeof(q)))
2596d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return -EFAULT;
2597d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	/* cdinfo(CD_DO_IOCTL, "CDROMSUBCHNL successful\n"); */
2598d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	return 0;
2599d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig}
2600d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig
2601d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwigstatic int cdrom_ioctl_read_tochdr(struct cdrom_device_info *cdi,
2602d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		void __user *argp)
2603d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig{
2604d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	struct cdrom_tochdr header;
2605d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	int ret;
2606d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig
2607d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	/* cdinfo(CD_DO_IOCTL, "entering CDROMREADTOCHDR\n"); */
2608d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig
2609d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	if (copy_from_user(&header, argp, sizeof(header)))
2610d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return -EFAULT;
2611d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig
2612d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	ret = cdi->ops->audio_ioctl(cdi, CDROMREADTOCHDR, &header);
2613d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	if (ret)
2614d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return ret;
2615d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig
2616d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	if (copy_to_user(argp, &header, sizeof(header)))
2617d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return -EFAULT;
2618d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	/* cdinfo(CD_DO_IOCTL, "CDROMREADTOCHDR successful\n"); */
2619d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	return 0;
2620d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig}
2621d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig
2622d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwigstatic int cdrom_ioctl_read_tocentry(struct cdrom_device_info *cdi,
2623d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		void __user *argp)
2624d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig{
2625d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	struct cdrom_tocentry entry;
2626d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	u8 requested_format;
2627d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	int ret;
2628d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig
2629d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	/* cdinfo(CD_DO_IOCTL, "entering CDROMREADTOCENTRY\n"); */
2630d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig
2631d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	if (copy_from_user(&entry, argp, sizeof(entry)))
2632d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return -EFAULT;
2633d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig
2634d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	requested_format = entry.cdte_format;
2635d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	if (requested_format != CDROM_MSF && requested_format != CDROM_LBA)
2636d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return -EINVAL;
2637d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	/* make interface to low-level uniform */
2638d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	entry.cdte_format = CDROM_MSF;
2639d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	ret = cdi->ops->audio_ioctl(cdi, CDROMREADTOCENTRY, &entry);
2640d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	if (ret)
2641d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return ret;
2642d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	sanitize_format(&entry.cdte_addr, &entry.cdte_format, requested_format);
2643d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig
2644d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	if (copy_to_user(argp, &entry, sizeof(entry)))
2645d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return -EFAULT;
2646d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	/* cdinfo(CD_DO_IOCTL, "CDROMREADTOCENTRY successful\n"); */
2647d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	return 0;
2648d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig}
2649d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig
2650d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwigstatic int cdrom_ioctl_play_msf(struct cdrom_device_info *cdi,
2651d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		void __user *argp)
2652d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig{
2653d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	struct cdrom_msf msf;
2654d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig
2655d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	cdinfo(CD_DO_IOCTL, "entering CDROMPLAYMSF\n");
2656d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig
2657d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	if (!CDROM_CAN(CDC_PLAY_AUDIO))
2658d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return -ENOSYS;
2659d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	if (copy_from_user(&msf, argp, sizeof(msf)))
2660d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return -EFAULT;
2661d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	return cdi->ops->audio_ioctl(cdi, CDROMPLAYMSF, &msf);
2662d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig}
2663d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig
2664d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwigstatic int cdrom_ioctl_play_trkind(struct cdrom_device_info *cdi,
2665d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		void __user *argp)
2666d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig{
2667d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	struct cdrom_ti ti;
2668d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	int ret;
2669d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig
2670d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	cdinfo(CD_DO_IOCTL, "entering CDROMPLAYTRKIND\n");
2671d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig
2672d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	if (!CDROM_CAN(CDC_PLAY_AUDIO))
2673d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return -ENOSYS;
2674d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	if (copy_from_user(&ti, argp, sizeof(ti)))
2675d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return -EFAULT;
2676d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig
2677d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	ret = check_for_audio_disc(cdi, cdi->ops);
2678d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	if (ret)
2679d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return ret;
2680d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	return cdi->ops->audio_ioctl(cdi, CDROMPLAYTRKIND, &ti);
2681d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig}
2682d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwigstatic int cdrom_ioctl_volctrl(struct cdrom_device_info *cdi,
2683d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		void __user *argp)
2684d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig{
2685d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	struct cdrom_volctrl volume;
2686d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig
2687d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	cdinfo(CD_DO_IOCTL, "entering CDROMVOLCTRL\n");
2688d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig
2689d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	if (!CDROM_CAN(CDC_PLAY_AUDIO))
2690d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return -ENOSYS;
2691d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	if (copy_from_user(&volume, argp, sizeof(volume)))
2692d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return -EFAULT;
2693d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	return cdi->ops->audio_ioctl(cdi, CDROMVOLCTRL, &volume);
2694d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig}
2695d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig
2696d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwigstatic int cdrom_ioctl_volread(struct cdrom_device_info *cdi,
2697d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		void __user *argp)
2698d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig{
2699d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	struct cdrom_volctrl volume;
2700d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	int ret;
2701d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig
2702d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	cdinfo(CD_DO_IOCTL, "entering CDROMVOLREAD\n");
2703d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig
2704d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	if (!CDROM_CAN(CDC_PLAY_AUDIO))
2705d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return -ENOSYS;
2706d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig
2707d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	ret = cdi->ops->audio_ioctl(cdi, CDROMVOLREAD, &volume);
2708d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	if (ret)
2709d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return ret;
2710d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig
2711d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	if (copy_to_user(argp, &volume, sizeof(volume)))
2712d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return -EFAULT;
2713d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	return 0;
2714d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig}
2715d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig
2716d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwigstatic int cdrom_ioctl_audioctl(struct cdrom_device_info *cdi,
2717d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		unsigned int cmd)
2718d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig{
2719d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	int ret;
2720d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig
2721d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	cdinfo(CD_DO_IOCTL, "doing audio ioctl (start/stop/pause/resume)\n");
2722d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig
2723d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	if (!CDROM_CAN(CDC_PLAY_AUDIO))
2724d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return -ENOSYS;
2725d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	ret = check_for_audio_disc(cdi, cdi->ops);
2726d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	if (ret)
2727d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return ret;
2728d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	return cdi->ops->audio_ioctl(cdi, cmd, NULL);
2729d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig}
2730d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig
2731d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig/*
2732d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig * Just about every imaginable ioctl is supported in the Uniform layer
2733d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig * these days.
2734d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig * ATAPI / SCSI specific code now mainly resides in mmc_ioctl().
2735d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig */
2736bbc1cc978404105da23d505163ce9fd5598ed5b1Al Viroint cdrom_ioctl(struct cdrom_device_info *cdi, struct block_device *bdev,
2737bbc1cc978404105da23d505163ce9fd5598ed5b1Al Viro		fmode_t mode, unsigned int cmd, unsigned long arg)
2738d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig{
2739d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	void __user *argp = (void __user *)arg;
2740d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	int ret;
2741d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig
2742d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	/*
2743d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	 * Try the generic SCSI command ioctl's first.
2744d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	 */
2745577ebb374c78314ac4617242f509e2f5e7156649Paolo Bonzini	ret = scsi_cmd_blk_ioctl(bdev, mode, cmd, argp);
2746d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	if (ret != -ENOTTY)
2747d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return ret;
2748d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig
2749d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	switch (cmd) {
2750d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	case CDROMMULTISESSION:
2751d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return cdrom_ioctl_multisession(cdi, argp);
2752d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	case CDROMEJECT:
2753d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return cdrom_ioctl_eject(cdi);
2754d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	case CDROMCLOSETRAY:
2755d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return cdrom_ioctl_closetray(cdi);
2756d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	case CDROMEJECT_SW:
2757d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return cdrom_ioctl_eject_sw(cdi, arg);
2758d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	case CDROM_MEDIA_CHANGED:
2759d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return cdrom_ioctl_media_changed(cdi, arg);
2760d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	case CDROM_SET_OPTIONS:
2761d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return cdrom_ioctl_set_options(cdi, arg);
2762d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	case CDROM_CLEAR_OPTIONS:
2763d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return cdrom_ioctl_clear_options(cdi, arg);
2764d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	case CDROM_SELECT_SPEED:
2765d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return cdrom_ioctl_select_speed(cdi, arg);
2766d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	case CDROM_SELECT_DISC:
2767d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return cdrom_ioctl_select_disc(cdi, arg);
2768d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	case CDROMRESET:
2769bbc1cc978404105da23d505163ce9fd5598ed5b1Al Viro		return cdrom_ioctl_reset(cdi, bdev);
2770d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	case CDROM_LOCKDOOR:
2771d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return cdrom_ioctl_lock_door(cdi, arg);
2772d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	case CDROM_DEBUG:
2773d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return cdrom_ioctl_debug(cdi, arg);
2774d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	case CDROM_GET_CAPABILITY:
2775d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return cdrom_ioctl_get_capability(cdi);
2776d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	case CDROM_GET_MCN:
2777d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return cdrom_ioctl_get_mcn(cdi, argp);
2778d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	case CDROM_DRIVE_STATUS:
2779d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return cdrom_ioctl_drive_status(cdi, arg);
2780d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	case CDROM_DISC_STATUS:
2781d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return cdrom_ioctl_disc_status(cdi);
2782d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	case CDROM_CHANGER_NSLOTS:
2783d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return cdrom_ioctl_changer_nslots(cdi);
2784d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	}
2785d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig
2786d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	/*
2787d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	 * Use the ioctls that are implemented through the generic_packet()
2788d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	 * interface. this may look at bit funny, but if -ENOTTY is
2789d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	 * returned that particular ioctl is not implemented and we
2790d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	 * let it go through the device specific ones.
2791d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	 */
27921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (CDROM_CAN(CDC_GENERIC_PACKET)) {
27931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ret = mmc_ioctl(cdi, cmd, arg);
2794d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		if (ret != -ENOTTY)
27951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return ret;
27961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
27971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2798d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	/*
2799d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	 * Note: most of the cdinfo() calls are commented out here,
2800d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	 * because they fill up the sys log when CD players poll
2801d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	 * the drive.
2802d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	 */
28031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	switch (cmd) {
2804d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	case CDROMSUBCHNL:
2805d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return cdrom_ioctl_get_subchnl(cdi, argp);
2806d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	case CDROMREADTOCHDR:
2807d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return cdrom_ioctl_read_tochdr(cdi, argp);
2808d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	case CDROMREADTOCENTRY:
2809d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return cdrom_ioctl_read_tocentry(cdi, argp);
2810d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	case CDROMPLAYMSF:
2811d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return cdrom_ioctl_play_msf(cdi, argp);
2812d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	case CDROMPLAYTRKIND:
2813d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return cdrom_ioctl_play_trkind(cdi, argp);
2814d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	case CDROMVOLCTRL:
2815d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return cdrom_ioctl_volctrl(cdi, argp);
2816d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	case CDROMVOLREAD:
2817d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return cdrom_ioctl_volread(cdi, argp);
28181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case CDROMSTART:
28191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case CDROMSTOP:
28201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case CDROMPAUSE:
2821d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	case CDROMRESUME:
2822d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig		return cdrom_ioctl_audioctl(cdi, cmd);
2823d2c5d4fc07a41a6048b01d3b2930aeff1235fda5Christoph Hellwig	}
28241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
28251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return -ENOSYS;
28261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
28271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
28281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
28291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Required when we need to use READ_10 to issue other than 2048 block
28301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * reads
28311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
28321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int cdrom_switch_blocksize(struct cdrom_device_info *cdi, int size)
28331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
28341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct cdrom_device_ops *cdo = cdi->ops;
28351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct packet_command cgc;
28361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct modesel_head mh;
28371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
28381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	memset(&mh, 0, sizeof(mh));
28391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	mh.block_desc_length = 0x08;
28401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	mh.block_length_med = (size >> 8) & 0xff;
28411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	mh.block_length_lo = size & 0xff;
28421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
28431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	memset(&cgc, 0, sizeof(cgc));
28441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc.cmd[0] = 0x15;
28451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc.cmd[1] = 1 << 4;
28461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc.cmd[4] = 12;
28471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc.buflen = sizeof(mh);
28481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc.buffer = (char *) &mh;
28491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc.data_direction = CGC_DATA_WRITE;
28501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	mh.block_desc_length = 0x08;
28511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	mh.block_length_med = (size >> 8) & 0xff;
28521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	mh.block_length_lo = size & 0xff;
28531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
28541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return cdo->generic_packet(cdi, &cgc);
28551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
28561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
28573147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarzstatic noinline int mmc_ioctl_cdrom_read_data(struct cdrom_device_info *cdi,
28583147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz					void __user *arg,
28593147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz					struct packet_command *cgc,
28603147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz					int cmd)
28613147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz{
28621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct request_sense sense;
28633147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	struct cdrom_msf msf;
28643147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	int blocksize = 0, format = 0, lba;
28653147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	int ret;
28661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
28671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	switch (cmd) {
28681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case CDROMREADRAW:
28693147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz		blocksize = CD_FRAMESIZE_RAW;
28703147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz		break;
28711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case CDROMREADMODE1:
28723147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz		blocksize = CD_FRAMESIZE;
28733147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz		format = 2;
28743147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz		break;
28753147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	case CDROMREADMODE2:
28763147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz		blocksize = CD_FRAMESIZE_RAW0;
28773147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz		break;
28783147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	}
28793147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	IOCTL_IN(arg, struct cdrom_msf, msf);
28803147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	lba = msf_to_lba(msf.cdmsf_min0, msf.cdmsf_sec0, msf.cdmsf_frame0);
28813147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	/* FIXME: we need upper bound checking, too!! */
28823147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	if (lba < 0)
28833147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz		return -EINVAL;
28843147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz
28853147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	cgc->buffer = kmalloc(blocksize, GFP_KERNEL);
28863147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	if (cgc->buffer == NULL)
28873147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz		return -ENOMEM;
28883147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz
28893147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	memset(&sense, 0, sizeof(sense));
28903147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	cgc->sense = &sense;
28913147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	cgc->data_direction = CGC_DATA_READ;
28923147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	ret = cdrom_read_block(cdi, cgc, lba, 1, format, blocksize);
28933147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	if (ret && sense.sense_key == 0x05 &&
28943147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz		   sense.asc == 0x20 &&
28953147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz		   sense.ascq == 0x00) {
28963147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz		/*
28973147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz		 * SCSI-II devices are not required to support
28983147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz		 * READ_CD, so let's try switching block size
28993147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz		 */
29003147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz		/* FIXME: switch back again... */
29013147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz		ret = cdrom_switch_blocksize(cdi, blocksize);
29023147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz		if (ret)
29033147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz			goto out;
29043147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz		cgc->sense = NULL;
29053147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz		ret = cdrom_read_cd(cdi, cgc, lba, blocksize, 1);
29063147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz		ret |= cdrom_switch_blocksize(cdi, blocksize);
29073147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	}
29083147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	if (!ret && copy_to_user(arg, cgc->buffer, blocksize))
29093147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz		ret = -EFAULT;
29103147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarzout:
29113147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	kfree(cgc->buffer);
29123147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	return ret;
29133147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz}
29143147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz
29153147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarzstatic noinline int mmc_ioctl_cdrom_read_audio(struct cdrom_device_info *cdi,
29163147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz					void __user *arg)
29173147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz{
29183147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	struct cdrom_read_audio ra;
29193147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	int lba;
29203147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz
29213147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	IOCTL_IN(arg, struct cdrom_read_audio, ra);
29223147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz
29233147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	if (ra.addr_format == CDROM_MSF)
29243147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz		lba = msf_to_lba(ra.addr.msf.minute,
29253147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz				 ra.addr.msf.second,
29263147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz				 ra.addr.msf.frame);
29273147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	else if (ra.addr_format == CDROM_LBA)
29283147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz		lba = ra.addr.lba;
29293147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	else
29303147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz		return -EINVAL;
29313147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz
29323147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	/* FIXME: we need upper bound checking, too!! */
29333147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	if (lba < 0 || ra.nframes <= 0 || ra.nframes > CD_FRAMES)
29343147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz		return -EINVAL;
29353147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz
29363147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	return cdrom_read_cdda(cdi, ra.buf, lba, ra.nframes);
29373147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz}
29383147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz
29393147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarzstatic noinline int mmc_ioctl_cdrom_subchannel(struct cdrom_device_info *cdi,
29403147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz					void __user *arg)
29413147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz{
29423147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	int ret;
29433147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	struct cdrom_subchnl q;
29443147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	u_char requested, back;
29453147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	IOCTL_IN(arg, struct cdrom_subchnl, q);
29463147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	requested = q.cdsc_format;
29473147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	if (!((requested == CDROM_MSF) ||
29483147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	      (requested == CDROM_LBA)))
29493147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz		return -EINVAL;
29503147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	q.cdsc_format = CDROM_MSF;
29513147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	ret = cdrom_read_subchannel(cdi, &q, 0);
29523147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	if (ret)
29531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return ret;
29543147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	back = q.cdsc_format; /* local copy */
29553147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	sanitize_format(&q.cdsc_absaddr, &back, requested);
29563147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	sanitize_format(&q.cdsc_reladdr, &q.cdsc_format, requested);
29573147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	IOCTL_OUT(arg, struct cdrom_subchnl, q);
29583147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	/* cdinfo(CD_DO_IOCTL, "CDROMSUBCHNL successful\n"); */
29593147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	return 0;
29603147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz}
29611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
29623147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarzstatic noinline int mmc_ioctl_cdrom_play_msf(struct cdrom_device_info *cdi,
29633147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz					void __user *arg,
29643147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz					struct packet_command *cgc)
29653147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz{
29663147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	struct cdrom_device_ops *cdo = cdi->ops;
29673147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	struct cdrom_msf msf;
29683147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	cdinfo(CD_DO_IOCTL, "entering CDROMPLAYMSF\n");
29693147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	IOCTL_IN(arg, struct cdrom_msf, msf);
29703147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	cgc->cmd[0] = GPCMD_PLAY_AUDIO_MSF;
29713147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	cgc->cmd[3] = msf.cdmsf_min0;
29723147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	cgc->cmd[4] = msf.cdmsf_sec0;
29733147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	cgc->cmd[5] = msf.cdmsf_frame0;
29743147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	cgc->cmd[6] = msf.cdmsf_min1;
29753147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	cgc->cmd[7] = msf.cdmsf_sec1;
29763147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	cgc->cmd[8] = msf.cdmsf_frame1;
29773147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	cgc->data_direction = CGC_DATA_NONE;
29783147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	return cdo->generic_packet(cdi, cgc);
29793147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz}
29801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
29813147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarzstatic noinline int mmc_ioctl_cdrom_play_blk(struct cdrom_device_info *cdi,
29823147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz					void __user *arg,
29833147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz					struct packet_command *cgc)
29843147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz{
29853147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	struct cdrom_device_ops *cdo = cdi->ops;
29863147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	struct cdrom_blk blk;
29873147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	cdinfo(CD_DO_IOCTL, "entering CDROMPLAYBLK\n");
29883147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	IOCTL_IN(arg, struct cdrom_blk, blk);
29893147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	cgc->cmd[0] = GPCMD_PLAY_AUDIO_10;
29903147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	cgc->cmd[2] = (blk.from >> 24) & 0xff;
29913147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	cgc->cmd[3] = (blk.from >> 16) & 0xff;
29923147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	cgc->cmd[4] = (blk.from >>  8) & 0xff;
29933147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	cgc->cmd[5] = blk.from & 0xff;
29943147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	cgc->cmd[7] = (blk.len >> 8) & 0xff;
29953147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	cgc->cmd[8] = blk.len & 0xff;
29963147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	cgc->data_direction = CGC_DATA_NONE;
29973147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	return cdo->generic_packet(cdi, cgc);
29983147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz}
29991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
30003147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarzstatic noinline int mmc_ioctl_cdrom_volume(struct cdrom_device_info *cdi,
30013147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz					void __user *arg,
30023147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz					struct packet_command *cgc,
30033147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz					unsigned int cmd)
30043147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz{
30053147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	struct cdrom_volctrl volctrl;
30063147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	unsigned char buffer[32];
30073147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	char mask[sizeof(buffer)];
30083147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	unsigned short offset;
30093147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	int ret;
30103147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz
30113147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	cdinfo(CD_DO_IOCTL, "entering CDROMVOLUME\n");
30121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
30133147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	IOCTL_IN(arg, struct cdrom_volctrl, volctrl);
30141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
30153147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	cgc->buffer = buffer;
30163147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	cgc->buflen = 24;
30173147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	ret = cdrom_mode_sense(cdi, cgc, GPMODE_AUDIO_CTL_PAGE, 0);
30183147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	if (ret)
30193147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz		return ret;
30201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
30213147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	/* originally the code depended on buffer[1] to determine
30223147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	   how much data is available for transfer. buffer[1] is
30233147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	   unfortunately ambigious and the only reliable way seem
30243147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	   to be to simply skip over the block descriptor... */
30253147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	offset = 8 + be16_to_cpu(*(__be16 *)(buffer + 6));
30263147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz
30273147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	if (offset + 16 > sizeof(buffer))
30283147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz		return -E2BIG;
30293147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz
30303147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	if (offset + 16 > cgc->buflen) {
30313147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz		cgc->buflen = offset + 16;
30323147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz		ret = cdrom_mode_sense(cdi, cgc,
30333147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz					GPMODE_AUDIO_CTL_PAGE, 0);
30343147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz		if (ret)
30353147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz			return ret;
30363147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	}
30371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
30383147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	/* sanity check */
30393147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	if ((buffer[offset] & 0x3f) != GPMODE_AUDIO_CTL_PAGE ||
30403147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz			buffer[offset + 1] < 14)
30413147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz		return -EINVAL;
30421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
30433147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	/* now we have the current volume settings. if it was only
30443147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	   a CDROMVOLREAD, return these values */
30453147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	if (cmd == CDROMVOLREAD) {
30463147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz		volctrl.channel0 = buffer[offset+9];
30473147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz		volctrl.channel1 = buffer[offset+11];
30483147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz		volctrl.channel2 = buffer[offset+13];
30493147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz		volctrl.channel3 = buffer[offset+15];
30503147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz		IOCTL_OUT(arg, struct cdrom_volctrl, volctrl);
30513147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz		return 0;
30523147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	}
30531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
30543147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	/* get the volume mask */
30553147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	cgc->buffer = mask;
30563147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	ret = cdrom_mode_sense(cdi, cgc, GPMODE_AUDIO_CTL_PAGE, 1);
30573147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	if (ret)
30583147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz		return ret;
30591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
30603147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	buffer[offset + 9]  = volctrl.channel0 & mask[offset + 9];
30613147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	buffer[offset + 11] = volctrl.channel1 & mask[offset + 11];
30623147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	buffer[offset + 13] = volctrl.channel2 & mask[offset + 13];
30633147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	buffer[offset + 15] = volctrl.channel3 & mask[offset + 15];
30641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
30653147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	/* set volume */
30663147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	cgc->buffer = buffer + offset - 8;
30673147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	memset(cgc->buffer, 0, 8);
30683147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	return cdrom_mode_select(cdi, cgc);
30693147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz}
30701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
30713147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarzstatic noinline int mmc_ioctl_cdrom_start_stop(struct cdrom_device_info *cdi,
30723147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz					struct packet_command *cgc,
30733147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz					int cmd)
30743147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz{
30753147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	struct cdrom_device_ops *cdo = cdi->ops;
30763147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	cdinfo(CD_DO_IOCTL, "entering CDROMSTART/CDROMSTOP\n");
30773147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	cgc->cmd[0] = GPCMD_START_STOP_UNIT;
30783147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	cgc->cmd[1] = 1;
30793147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	cgc->cmd[4] = (cmd == CDROMSTART) ? 1 : 0;
30803147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	cgc->data_direction = CGC_DATA_NONE;
30813147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	return cdo->generic_packet(cdi, cgc);
30823147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz}
30831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
30843147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarzstatic noinline int mmc_ioctl_cdrom_pause_resume(struct cdrom_device_info *cdi,
30853147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz					struct packet_command *cgc,
30863147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz					int cmd)
30873147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz{
30883147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	struct cdrom_device_ops *cdo = cdi->ops;
30893147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	cdinfo(CD_DO_IOCTL, "entering CDROMPAUSE/CDROMRESUME\n");
30903147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	cgc->cmd[0] = GPCMD_PAUSE_RESUME;
30913147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	cgc->cmd[8] = (cmd == CDROMRESUME) ? 1 : 0;
30923147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	cgc->data_direction = CGC_DATA_NONE;
30933147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	return cdo->generic_packet(cdi, cgc);
30943147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz}
30951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
30963147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarzstatic noinline int mmc_ioctl_dvd_read_struct(struct cdrom_device_info *cdi,
3097d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarz						void __user *arg,
3098d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarz						struct packet_command *cgc)
30993147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz{
31003147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	int ret;
31013147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	dvd_struct *s;
31023147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	int size = sizeof(dvd_struct);
31033147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz
31043147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	if (!CDROM_CAN(CDC_DVD))
31053147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz		return -ENOSYS;
31063147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz
31073147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	s = kmalloc(size, GFP_KERNEL);
31083147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	if (!s)
31093147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz		return -ENOMEM;
31103147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz
31113147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	cdinfo(CD_DO_IOCTL, "entering DVD_READ_STRUCT\n");
31123147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	if (copy_from_user(s, arg, size)) {
31131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		kfree(s);
31143147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz		return -EFAULT;
31153147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	}
31163147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz
3117d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarz	ret = dvd_read_struct(cdi, s, cgc);
31183147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	if (ret)
31193147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz		goto out;
31203147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz
31213147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	if (copy_to_user(arg, s, size))
31223147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz		ret = -EFAULT;
31233147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarzout:
31243147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	kfree(s);
31253147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	return ret;
31263147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz}
31273147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz
31283147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarzstatic noinline int mmc_ioctl_dvd_auth(struct cdrom_device_info *cdi,
31293147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz					void __user *arg)
31303147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz{
31313147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	int ret;
31323147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	dvd_authinfo ai;
31333147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	if (!CDROM_CAN(CDC_DVD))
31343147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz		return -ENOSYS;
31353147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	cdinfo(CD_DO_IOCTL, "entering DVD_AUTH\n");
31363147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	IOCTL_IN(arg, dvd_authinfo, ai);
31373147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	ret = dvd_do_auth(cdi, &ai);
31383147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	if (ret)
31391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return ret;
31403147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	IOCTL_OUT(arg, dvd_authinfo, ai);
31413147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	return 0;
31423147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz}
31431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
31443147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarzstatic noinline int mmc_ioctl_cdrom_next_writable(struct cdrom_device_info *cdi,
31453147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz						void __user *arg)
31463147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz{
31473147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	int ret;
31483147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	long next = 0;
31493147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	cdinfo(CD_DO_IOCTL, "entering CDROM_NEXT_WRITABLE\n");
31503147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	ret = cdrom_get_next_writable(cdi, &next);
31513147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	if (ret)
31523147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz		return ret;
31533147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	IOCTL_OUT(arg, long, next);
31543147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	return 0;
31553147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz}
31561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
31573147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarzstatic noinline int mmc_ioctl_cdrom_last_written(struct cdrom_device_info *cdi,
31583147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz						void __user *arg)
31593147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz{
31603147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	int ret;
31613147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	long last = 0;
31623147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	cdinfo(CD_DO_IOCTL, "entering CDROM_LAST_WRITTEN\n");
31633147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	ret = cdrom_get_last_written(cdi, &last);
31643147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	if (ret)
31653147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz		return ret;
31663147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	IOCTL_OUT(arg, long, last);
31673147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	return 0;
31683147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz}
31693147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz
31703147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarzstatic int mmc_ioctl(struct cdrom_device_info *cdi, unsigned int cmd,
31713147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz		     unsigned long arg)
31723147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz{
31733147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	struct packet_command cgc;
31743147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	void __user *userptr = (void __user *)arg;
31753147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz
31763147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	memset(&cgc, 0, sizeof(cgc));
31773147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz
31783147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	/* build a unified command and queue it through
31793147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	   cdo->generic_packet() */
31803147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	switch (cmd) {
31813147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	case CDROMREADRAW:
31823147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	case CDROMREADMODE1:
31833147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	case CDROMREADMODE2:
31843147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz		return mmc_ioctl_cdrom_read_data(cdi, userptr, &cgc, cmd);
31853147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	case CDROMREADAUDIO:
31863147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz		return mmc_ioctl_cdrom_read_audio(cdi, userptr);
31873147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	case CDROMSUBCHNL:
31883147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz		return mmc_ioctl_cdrom_subchannel(cdi, userptr);
31893147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	case CDROMPLAYMSF:
31903147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz		return mmc_ioctl_cdrom_play_msf(cdi, userptr, &cgc);
31913147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	case CDROMPLAYBLK:
31923147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz		return mmc_ioctl_cdrom_play_blk(cdi, userptr, &cgc);
31933147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	case CDROMVOLCTRL:
31943147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	case CDROMVOLREAD:
31953147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz		return mmc_ioctl_cdrom_volume(cdi, userptr, &cgc, cmd);
31963147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	case CDROMSTART:
31973147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	case CDROMSTOP:
31983147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz		return mmc_ioctl_cdrom_start_stop(cdi, &cgc, cmd);
31993147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	case CDROMPAUSE:
32003147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	case CDROMRESUME:
32013147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz		return mmc_ioctl_cdrom_pause_resume(cdi, &cgc, cmd);
32023147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	case DVD_READ_STRUCT:
3203d194139c18edb7dae45e159a24f2aa72091033b8Marcin Slusarz		return mmc_ioctl_dvd_read_struct(cdi, userptr, &cgc);
32043147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	case DVD_AUTH:
32053147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz		return mmc_ioctl_dvd_auth(cdi, userptr);
32063147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	case CDROM_NEXT_WRITABLE:
32073147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz		return mmc_ioctl_cdrom_next_writable(cdi, userptr);
32083147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	case CDROM_LAST_WRITTEN:
32093147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz		return mmc_ioctl_cdrom_last_written(cdi, userptr);
32103147c531b6b53fbbf8942689c4aa113c519f5e4eMarcin Slusarz	}
32111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
32121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return -ENOTTY;
32131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
32141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
32151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int cdrom_get_track_info(struct cdrom_device_info *cdi, __u16 track, __u8 type,
32161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			 track_information *ti)
32171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
32181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct cdrom_device_ops *cdo = cdi->ops;
32191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct packet_command cgc;
32201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int ret, buflen;
32211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
32221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	init_cdrom_command(&cgc, ti, 8, CGC_DATA_READ);
32231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc.cmd[0] = GPCMD_READ_TRACK_RZONE_INFO;
32241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc.cmd[1] = type & 3;
32251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc.cmd[4] = (track & 0xff00) >> 8;
32261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc.cmd[5] = track & 0xff;
32271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc.cmd[8] = 8;
32281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc.quiet = 1;
32291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
32301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if ((ret = cdo->generic_packet(cdi, &cgc)))
32311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return ret;
32321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
32331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	buflen = be16_to_cpu(ti->track_information_length) +
32341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		     sizeof(ti->track_information_length);
32351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
32361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (buflen > sizeof(track_information))
32371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		buflen = sizeof(track_information);
32381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
32391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc.cmd[8] = cgc.buflen = buflen;
32401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if ((ret = cdo->generic_packet(cdi, &cgc)))
32411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return ret;
32421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
32431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* return actual fill size */
32441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return buflen;
32451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
32461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
32471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* requires CD R/RW */
32481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int cdrom_get_disc_info(struct cdrom_device_info *cdi, disc_information *di)
32491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
32501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct cdrom_device_ops *cdo = cdi->ops;
32511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct packet_command cgc;
32521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int ret, buflen;
32531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
32541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* set up command and get the disc info */
32551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	init_cdrom_command(&cgc, di, sizeof(*di), CGC_DATA_READ);
32561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc.cmd[0] = GPCMD_READ_DISC_INFO;
32571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc.cmd[8] = cgc.buflen = 2;
32581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc.quiet = 1;
32591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
32601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if ((ret = cdo->generic_packet(cdi, &cgc)))
32611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return ret;
32621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
32631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* not all drives have the same disc_info length, so requeue
32641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * packet with the length the drive tells us it can supply
32651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
32661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	buflen = be16_to_cpu(di->disc_information_length) +
32671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		     sizeof(di->disc_information_length);
32681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
32691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (buflen > sizeof(disc_information))
32701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		buflen = sizeof(disc_information);
32711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
32721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cgc.cmd[8] = cgc.buflen = buflen;
32731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if ((ret = cdo->generic_packet(cdi, &cgc)))
32741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return ret;
32751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
32761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* return actual fill size */
32771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return buflen;
32781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
32791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
32801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* return the last written block on the CD-R media. this is for the udf
32811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds   file system. */
32821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsint cdrom_get_last_written(struct cdrom_device_info *cdi, long *last_written)
32831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
32841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct cdrom_tocentry toc;
32851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	disc_information di;
32861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	track_information ti;
32871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	__u32 last_track;
32881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int ret = -1, ti_size;
32891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
32901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!CDROM_CAN(CDC_GENERIC_PACKET))
32911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto use_toc;
32921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
32931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	ret = cdrom_get_disc_info(cdi, &di);
32941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (ret < (int)(offsetof(typeof(di), last_track_lsb)
32951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			+ sizeof(di.last_track_lsb)))
32961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto use_toc;
32971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
32981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* if unit didn't return msb, it's zeroed by cdrom_get_disc_info */
32991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	last_track = (di.last_track_msb << 8) | di.last_track_lsb;
33001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	ti_size = cdrom_get_track_info(cdi, last_track, 1, &ti);
33011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (ti_size < (int)offsetof(typeof(ti), track_start))
33021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto use_toc;
33031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
33041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* if this track is blank, try the previous. */
33051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (ti.blank) {
33061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (last_track==1)
33071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			goto use_toc;
33081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		last_track--;
33091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ti_size = cdrom_get_track_info(cdi, last_track, 1, &ti);
33101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
33111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
33121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (ti_size < (int)(offsetof(typeof(ti), track_size)
33131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				+ sizeof(ti.track_size)))
33141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto use_toc;
33151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
33161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* if last recorded field is valid, return it. */
33171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (ti.lra_v && ti_size >= (int)(offsetof(typeof(ti), last_rec_address)
33181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				+ sizeof(ti.last_rec_address))) {
33191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		*last_written = be32_to_cpu(ti.last_rec_address);
33201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	} else {
33211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* make it up instead */
33221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		*last_written = be32_to_cpu(ti.track_start) +
33231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				be32_to_cpu(ti.track_size);
33241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (ti.free_blocks)
33251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			*last_written -= (be32_to_cpu(ti.free_blocks) + 7);
33261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
33271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
33281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
33291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* this is where we end up if the drive either can't do a
33301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	   GPCMD_READ_DISC_INFO or GPCMD_READ_TRACK_RZONE_INFO or if
33311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	   it doesn't give enough information or fails. then we return
33321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	   the toc contents. */
33331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsuse_toc:
33341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	toc.cdte_format = CDROM_MSF;
33351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	toc.cdte_track = CDROM_LEADOUT;
33361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if ((ret = cdi->ops->audio_ioctl(cdi, CDROMREADTOCENTRY, &toc)))
33371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return ret;
33381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	sanitize_format(&toc.cdte_addr, &toc.cdte_format, CDROM_LBA);
33391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	*last_written = toc.cdte_addr.lba;
33401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
33411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
33421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
33431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* return the next writable block. also for udf file system. */
33441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int cdrom_get_next_writable(struct cdrom_device_info *cdi, long *next_writable)
33451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
33461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	disc_information di;
33471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	track_information ti;
33481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	__u16 last_track;
33491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int ret, ti_size;
33501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
33511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!CDROM_CAN(CDC_GENERIC_PACKET))
33521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto use_last_written;
33531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
33541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	ret = cdrom_get_disc_info(cdi, &di);
33551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (ret < 0 || ret < offsetof(typeof(di), last_track_lsb)
33561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				+ sizeof(di.last_track_lsb))
33571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto use_last_written;
33581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
33591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* if unit didn't return msb, it's zeroed by cdrom_get_disc_info */
33601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	last_track = (di.last_track_msb << 8) | di.last_track_lsb;
33611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	ti_size = cdrom_get_track_info(cdi, last_track, 1, &ti);
33621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (ti_size < 0 || ti_size < offsetof(typeof(ti), track_start))
33631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto use_last_written;
33641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
33651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds        /* if this track is blank, try the previous. */
33661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (ti.blank) {
33671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (last_track == 1)
33681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			goto use_last_written;
33691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		last_track--;
33701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ti_size = cdrom_get_track_info(cdi, last_track, 1, &ti);
33711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (ti_size < 0)
33721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			goto use_last_written;
33731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
33741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
33751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* if next recordable address field is valid, use it. */
33761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (ti.nwa_v && ti_size >= offsetof(typeof(ti), next_writable)
33771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				+ sizeof(ti.next_writable)) {
33781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		*next_writable = be32_to_cpu(ti.next_writable);
33791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return 0;
33801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
33811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
33821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsuse_last_written:
33831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if ((ret = cdrom_get_last_written(cdi, next_writable))) {
33841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		*next_writable = 0;
33851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return ret;
33861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	} else {
33871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		*next_writable += 7;
33881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return 0;
33891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
33901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
33911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
33921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsEXPORT_SYMBOL(cdrom_get_last_written);
33931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsEXPORT_SYMBOL(register_cdrom);
33941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsEXPORT_SYMBOL(unregister_cdrom);
33951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsEXPORT_SYMBOL(cdrom_open);
33961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsEXPORT_SYMBOL(cdrom_release);
33971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsEXPORT_SYMBOL(cdrom_ioctl);
33981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsEXPORT_SYMBOL(cdrom_media_changed);
33991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsEXPORT_SYMBOL(cdrom_number_of_slots);
34001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsEXPORT_SYMBOL(cdrom_mode_select);
34011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsEXPORT_SYMBOL(cdrom_mode_sense);
34021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsEXPORT_SYMBOL(init_cdrom_command);
34031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsEXPORT_SYMBOL(cdrom_get_media_event);
34041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
34051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#ifdef CONFIG_SYSCTL
34061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
34071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define CDROM_STR_SIZE 1000
34081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
34091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct cdrom_sysctl_settings {
34101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	char	info[CDROM_STR_SIZE];	/* general info */
34111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int	autoclose;		/* close tray upon mount, etc */
34121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int	autoeject;		/* eject on umount */
34131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int	debug;			/* turn on debugging messages */
34141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int	lock;			/* lock the door on device open */
34151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int	check;			/* check media type */
34161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} cdrom_sysctl_settings;
34171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3418554988d6fe369719ae5b41255c577569ecf47c30Dave Youngenum cdrom_print_option {
3419554988d6fe369719ae5b41255c577569ecf47c30Dave Young	CTL_NAME,
3420554988d6fe369719ae5b41255c577569ecf47c30Dave Young	CTL_SPEED,
3421554988d6fe369719ae5b41255c577569ecf47c30Dave Young	CTL_SLOTS,
3422554988d6fe369719ae5b41255c577569ecf47c30Dave Young	CTL_CAPABILITY
3423554988d6fe369719ae5b41255c577569ecf47c30Dave Young};
3424554988d6fe369719ae5b41255c577569ecf47c30Dave Young
3425554988d6fe369719ae5b41255c577569ecf47c30Dave Youngstatic int cdrom_print_info(const char *header, int val, char *info,
3426554988d6fe369719ae5b41255c577569ecf47c30Dave Young				int *pos, enum cdrom_print_option option)
3427554988d6fe369719ae5b41255c577569ecf47c30Dave Young{
3428554988d6fe369719ae5b41255c577569ecf47c30Dave Young	const int max_size = sizeof(cdrom_sysctl_settings.info);
3429554988d6fe369719ae5b41255c577569ecf47c30Dave Young	struct cdrom_device_info *cdi;
3430554988d6fe369719ae5b41255c577569ecf47c30Dave Young	int ret;
3431554988d6fe369719ae5b41255c577569ecf47c30Dave Young
3432554988d6fe369719ae5b41255c577569ecf47c30Dave Young	ret = scnprintf(info + *pos, max_size - *pos, header);
3433554988d6fe369719ae5b41255c577569ecf47c30Dave Young	if (!ret)
3434554988d6fe369719ae5b41255c577569ecf47c30Dave Young		return 1;
3435554988d6fe369719ae5b41255c577569ecf47c30Dave Young
3436554988d6fe369719ae5b41255c577569ecf47c30Dave Young	*pos += ret;
3437554988d6fe369719ae5b41255c577569ecf47c30Dave Young
34387fd097d42b90afadae4867db5d580bcd7b3b596dAkinobu Mita	list_for_each_entry(cdi, &cdrom_list, list) {
3439554988d6fe369719ae5b41255c577569ecf47c30Dave Young		switch (option) {
3440554988d6fe369719ae5b41255c577569ecf47c30Dave Young		case CTL_NAME:
3441554988d6fe369719ae5b41255c577569ecf47c30Dave Young			ret = scnprintf(info + *pos, max_size - *pos,
3442554988d6fe369719ae5b41255c577569ecf47c30Dave Young					"\t%s", cdi->name);
3443554988d6fe369719ae5b41255c577569ecf47c30Dave Young			break;
3444554988d6fe369719ae5b41255c577569ecf47c30Dave Young		case CTL_SPEED:
3445554988d6fe369719ae5b41255c577569ecf47c30Dave Young			ret = scnprintf(info + *pos, max_size - *pos,
3446554988d6fe369719ae5b41255c577569ecf47c30Dave Young					"\t%d", cdi->speed);
3447554988d6fe369719ae5b41255c577569ecf47c30Dave Young			break;
3448554988d6fe369719ae5b41255c577569ecf47c30Dave Young		case CTL_SLOTS:
3449554988d6fe369719ae5b41255c577569ecf47c30Dave Young			ret = scnprintf(info + *pos, max_size - *pos,
3450554988d6fe369719ae5b41255c577569ecf47c30Dave Young					"\t%d", cdi->capacity);
3451554988d6fe369719ae5b41255c577569ecf47c30Dave Young			break;
3452554988d6fe369719ae5b41255c577569ecf47c30Dave Young		case CTL_CAPABILITY:
3453554988d6fe369719ae5b41255c577569ecf47c30Dave Young			ret = scnprintf(info + *pos, max_size - *pos,
3454554988d6fe369719ae5b41255c577569ecf47c30Dave Young					"\t%d", CDROM_CAN(val) != 0);
3455554988d6fe369719ae5b41255c577569ecf47c30Dave Young			break;
3456554988d6fe369719ae5b41255c577569ecf47c30Dave Young		default:
3457e597cd09f711b28b8466ebdc2f12e55b44fa81e4Joe Perches			pr_info("invalid option%d\n", option);
3458554988d6fe369719ae5b41255c577569ecf47c30Dave Young			return 1;
3459554988d6fe369719ae5b41255c577569ecf47c30Dave Young		}
3460554988d6fe369719ae5b41255c577569ecf47c30Dave Young		if (!ret)
3461554988d6fe369719ae5b41255c577569ecf47c30Dave Young			return 1;
3462554988d6fe369719ae5b41255c577569ecf47c30Dave Young		*pos += ret;
3463554988d6fe369719ae5b41255c577569ecf47c30Dave Young	}
3464554988d6fe369719ae5b41255c577569ecf47c30Dave Young
3465554988d6fe369719ae5b41255c577569ecf47c30Dave Young	return 0;
3466554988d6fe369719ae5b41255c577569ecf47c30Dave Young}
3467554988d6fe369719ae5b41255c577569ecf47c30Dave Young
34688d65af789f3e2cf4cfbdbf71a0f7a61ebcd41d38Alexey Dobriyanstatic int cdrom_sysctl_info(ctl_table *ctl, int write,
34691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds                           void __user *buffer, size_t *lenp, loff_t *ppos)
34701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
3471554988d6fe369719ae5b41255c577569ecf47c30Dave Young	int pos;
34721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	char *info = cdrom_sysctl_settings.info;
3473554988d6fe369719ae5b41255c577569ecf47c30Dave Young	const int max_size = sizeof(cdrom_sysctl_settings.info);
34741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
34751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!*lenp || (*ppos && !write)) {
34761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		*lenp = 0;
34771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return 0;
34781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
34791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3480554988d6fe369719ae5b41255c577569ecf47c30Dave Young	mutex_lock(&cdrom_mutex);
3481554988d6fe369719ae5b41255c577569ecf47c30Dave Young
34821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	pos = sprintf(info, "CD-ROM information, " VERSION "\n");
34831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3484554988d6fe369719ae5b41255c577569ecf47c30Dave Young	if (cdrom_print_info("\ndrive name:\t", 0, info, &pos, CTL_NAME))
3485554988d6fe369719ae5b41255c577569ecf47c30Dave Young		goto done;
3486554988d6fe369719ae5b41255c577569ecf47c30Dave Young	if (cdrom_print_info("\ndrive speed:\t", 0, info, &pos, CTL_SPEED))
3487554988d6fe369719ae5b41255c577569ecf47c30Dave Young		goto done;
3488554988d6fe369719ae5b41255c577569ecf47c30Dave Young	if (cdrom_print_info("\ndrive # of slots:", 0, info, &pos, CTL_SLOTS))
3489554988d6fe369719ae5b41255c577569ecf47c30Dave Young		goto done;
3490554988d6fe369719ae5b41255c577569ecf47c30Dave Young	if (cdrom_print_info("\nCan close tray:\t",
3491554988d6fe369719ae5b41255c577569ecf47c30Dave Young				CDC_CLOSE_TRAY, info, &pos, CTL_CAPABILITY))
3492554988d6fe369719ae5b41255c577569ecf47c30Dave Young		goto done;
3493554988d6fe369719ae5b41255c577569ecf47c30Dave Young	if (cdrom_print_info("\nCan open tray:\t",
3494554988d6fe369719ae5b41255c577569ecf47c30Dave Young				CDC_OPEN_TRAY, info, &pos, CTL_CAPABILITY))
3495554988d6fe369719ae5b41255c577569ecf47c30Dave Young		goto done;
3496554988d6fe369719ae5b41255c577569ecf47c30Dave Young	if (cdrom_print_info("\nCan lock tray:\t",
3497554988d6fe369719ae5b41255c577569ecf47c30Dave Young				CDC_LOCK, info, &pos, CTL_CAPABILITY))
3498554988d6fe369719ae5b41255c577569ecf47c30Dave Young		goto done;
3499554988d6fe369719ae5b41255c577569ecf47c30Dave Young	if (cdrom_print_info("\nCan change speed:",
3500554988d6fe369719ae5b41255c577569ecf47c30Dave Young				CDC_SELECT_SPEED, info, &pos, CTL_CAPABILITY))
3501554988d6fe369719ae5b41255c577569ecf47c30Dave Young		goto done;
3502554988d6fe369719ae5b41255c577569ecf47c30Dave Young	if (cdrom_print_info("\nCan select disk:",
3503554988d6fe369719ae5b41255c577569ecf47c30Dave Young				CDC_SELECT_DISC, info, &pos, CTL_CAPABILITY))
3504554988d6fe369719ae5b41255c577569ecf47c30Dave Young		goto done;
3505554988d6fe369719ae5b41255c577569ecf47c30Dave Young	if (cdrom_print_info("\nCan read multisession:",
3506554988d6fe369719ae5b41255c577569ecf47c30Dave Young				CDC_MULTI_SESSION, info, &pos, CTL_CAPABILITY))
3507554988d6fe369719ae5b41255c577569ecf47c30Dave Young		goto done;
3508554988d6fe369719ae5b41255c577569ecf47c30Dave Young	if (cdrom_print_info("\nCan read MCN:\t",
3509554988d6fe369719ae5b41255c577569ecf47c30Dave Young				CDC_MCN, info, &pos, CTL_CAPABILITY))
3510554988d6fe369719ae5b41255c577569ecf47c30Dave Young		goto done;
3511554988d6fe369719ae5b41255c577569ecf47c30Dave Young	if (cdrom_print_info("\nReports media changed:",
3512554988d6fe369719ae5b41255c577569ecf47c30Dave Young				CDC_MEDIA_CHANGED, info, &pos, CTL_CAPABILITY))
3513554988d6fe369719ae5b41255c577569ecf47c30Dave Young		goto done;
3514554988d6fe369719ae5b41255c577569ecf47c30Dave Young	if (cdrom_print_info("\nCan play audio:\t",
3515554988d6fe369719ae5b41255c577569ecf47c30Dave Young				CDC_PLAY_AUDIO, info, &pos, CTL_CAPABILITY))
3516554988d6fe369719ae5b41255c577569ecf47c30Dave Young		goto done;
3517554988d6fe369719ae5b41255c577569ecf47c30Dave Young	if (cdrom_print_info("\nCan write CD-R:\t",
3518554988d6fe369719ae5b41255c577569ecf47c30Dave Young				CDC_CD_R, info, &pos, CTL_CAPABILITY))
3519554988d6fe369719ae5b41255c577569ecf47c30Dave Young		goto done;
3520554988d6fe369719ae5b41255c577569ecf47c30Dave Young	if (cdrom_print_info("\nCan write CD-RW:",
3521554988d6fe369719ae5b41255c577569ecf47c30Dave Young				CDC_CD_RW, info, &pos, CTL_CAPABILITY))
3522554988d6fe369719ae5b41255c577569ecf47c30Dave Young		goto done;
3523554988d6fe369719ae5b41255c577569ecf47c30Dave Young	if (cdrom_print_info("\nCan read DVD:\t",
3524554988d6fe369719ae5b41255c577569ecf47c30Dave Young				CDC_DVD, info, &pos, CTL_CAPABILITY))
3525554988d6fe369719ae5b41255c577569ecf47c30Dave Young		goto done;
3526554988d6fe369719ae5b41255c577569ecf47c30Dave Young	if (cdrom_print_info("\nCan write DVD-R:",
3527554988d6fe369719ae5b41255c577569ecf47c30Dave Young				CDC_DVD_R, info, &pos, CTL_CAPABILITY))
3528554988d6fe369719ae5b41255c577569ecf47c30Dave Young		goto done;
3529554988d6fe369719ae5b41255c577569ecf47c30Dave Young	if (cdrom_print_info("\nCan write DVD-RAM:",
3530554988d6fe369719ae5b41255c577569ecf47c30Dave Young				CDC_DVD_RAM, info, &pos, CTL_CAPABILITY))
3531554988d6fe369719ae5b41255c577569ecf47c30Dave Young		goto done;
3532554988d6fe369719ae5b41255c577569ecf47c30Dave Young	if (cdrom_print_info("\nCan read MRW:\t",
3533554988d6fe369719ae5b41255c577569ecf47c30Dave Young				CDC_MRW, info, &pos, CTL_CAPABILITY))
3534554988d6fe369719ae5b41255c577569ecf47c30Dave Young		goto done;
3535554988d6fe369719ae5b41255c577569ecf47c30Dave Young	if (cdrom_print_info("\nCan write MRW:\t",
3536554988d6fe369719ae5b41255c577569ecf47c30Dave Young				CDC_MRW_W, info, &pos, CTL_CAPABILITY))
3537554988d6fe369719ae5b41255c577569ecf47c30Dave Young		goto done;
3538554988d6fe369719ae5b41255c577569ecf47c30Dave Young	if (cdrom_print_info("\nCan write RAM:\t",
3539554988d6fe369719ae5b41255c577569ecf47c30Dave Young				CDC_RAM, info, &pos, CTL_CAPABILITY))
3540554988d6fe369719ae5b41255c577569ecf47c30Dave Young		goto done;
3541554988d6fe369719ae5b41255c577569ecf47c30Dave Young	if (!scnprintf(info + pos, max_size - pos, "\n\n"))
3542554988d6fe369719ae5b41255c577569ecf47c30Dave Young		goto done;
3543554988d6fe369719ae5b41255c577569ecf47c30Dave Youngdoit:
3544554988d6fe369719ae5b41255c577569ecf47c30Dave Young	mutex_unlock(&cdrom_mutex);
35458d65af789f3e2cf4cfbdbf71a0f7a61ebcd41d38Alexey Dobriyan	return proc_dostring(ctl, write, buffer, lenp, ppos);
3546554988d6fe369719ae5b41255c577569ecf47c30Dave Youngdone:
3547e597cd09f711b28b8466ebdc2f12e55b44fa81e4Joe Perches	pr_info("info buffer too small\n");
3548554988d6fe369719ae5b41255c577569ecf47c30Dave Young	goto doit;
35491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
35501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
35511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Unfortunately, per device settings are not implemented through
35521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds   procfs/sysctl yet. When they are, this will naturally disappear. For now
35531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds   just update all drives. Later this will become the template on which
35541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds   new registered drives will be based. */
35551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void cdrom_update_settings(void)
35561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
35571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct cdrom_device_info *cdi;
35581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3559032d8d90ba23e1770604bbb1102ec77f5aa8ae44Akinobu Mita	mutex_lock(&cdrom_mutex);
35607fd097d42b90afadae4867db5d580bcd7b3b596dAkinobu Mita	list_for_each_entry(cdi, &cdrom_list, list) {
35611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (autoclose && CDROM_CAN(CDC_CLOSE_TRAY))
35621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			cdi->options |= CDO_AUTO_CLOSE;
35631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		else if (!autoclose)
35641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			cdi->options &= ~CDO_AUTO_CLOSE;
35651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (autoeject && CDROM_CAN(CDC_OPEN_TRAY))
35661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			cdi->options |= CDO_AUTO_EJECT;
35671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		else if (!autoeject)
35681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			cdi->options &= ~CDO_AUTO_EJECT;
35691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (lockdoor && CDROM_CAN(CDC_LOCK))
35701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			cdi->options |= CDO_LOCK;
35711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		else if (!lockdoor)
35721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			cdi->options &= ~CDO_LOCK;
35731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (check_media_type)
35741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			cdi->options |= CDO_CHECK_TYPE;
35751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		else
35761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			cdi->options &= ~CDO_CHECK_TYPE;
35771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
3578032d8d90ba23e1770604bbb1102ec77f5aa8ae44Akinobu Mita	mutex_unlock(&cdrom_mutex);
35791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
35801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
35818d65af789f3e2cf4cfbdbf71a0f7a61ebcd41d38Alexey Dobriyanstatic int cdrom_sysctl_handler(ctl_table *ctl, int write,
35821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				void __user *buffer, size_t *lenp, loff_t *ppos)
35831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
35841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int ret;
35851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
35868d65af789f3e2cf4cfbdbf71a0f7a61ebcd41d38Alexey Dobriyan	ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
35871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
358806489b4eec832227b6f30b4b43ab26a2c50d9225Eric W. Biederman	if (write) {
35891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
35901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* we only care for 1 or 0. */
359106489b4eec832227b6f30b4b43ab26a2c50d9225Eric W. Biederman		autoclose        = !!cdrom_sysctl_settings.autoclose;
359206489b4eec832227b6f30b4b43ab26a2c50d9225Eric W. Biederman		autoeject        = !!cdrom_sysctl_settings.autoeject;
359306489b4eec832227b6f30b4b43ab26a2c50d9225Eric W. Biederman		debug	         = !!cdrom_sysctl_settings.debug;
359406489b4eec832227b6f30b4b43ab26a2c50d9225Eric W. Biederman		lockdoor         = !!cdrom_sysctl_settings.lock;
359506489b4eec832227b6f30b4b43ab26a2c50d9225Eric W. Biederman		check_media_type = !!cdrom_sysctl_settings.check;
35961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
35971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* update the option flags according to the changes. we
35981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		   don't have per device options through sysctl yet,
35991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		   but we will have and then this will disappear. */
36001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cdrom_update_settings();
36011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
36021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
36031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds        return ret;
36041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
36051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
36061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Place files in /proc/sys/dev/cdrom */
36071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ctl_table cdrom_table[] = {
36081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{
36091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		.procname	= "info",
36101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		.data		= &cdrom_sysctl_settings.info,
36111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		.maxlen		= CDROM_STR_SIZE,
36121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		.mode		= 0444,
36136d4561110a3e9fa742aeec6717248a491dfb1878Eric W. Biederman		.proc_handler	= cdrom_sysctl_info,
36141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	},
36151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{
36161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		.procname	= "autoclose",
36171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		.data		= &cdrom_sysctl_settings.autoclose,
36181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		.maxlen		= sizeof(int),
36191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		.mode		= 0644,
36206d4561110a3e9fa742aeec6717248a491dfb1878Eric W. Biederman		.proc_handler	= cdrom_sysctl_handler,
36211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	},
36221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{
36231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		.procname	= "autoeject",
36241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		.data		= &cdrom_sysctl_settings.autoeject,
36251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		.maxlen		= sizeof(int),
36261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		.mode		= 0644,
36276d4561110a3e9fa742aeec6717248a491dfb1878Eric W. Biederman		.proc_handler	= cdrom_sysctl_handler,
36281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	},
36291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{
36301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		.procname	= "debug",
36311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		.data		= &cdrom_sysctl_settings.debug,
36321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		.maxlen		= sizeof(int),
36331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		.mode		= 0644,
36346d4561110a3e9fa742aeec6717248a491dfb1878Eric W. Biederman		.proc_handler	= cdrom_sysctl_handler,
36351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	},
36361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{
36371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		.procname	= "lock",
36381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		.data		= &cdrom_sysctl_settings.lock,
36391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		.maxlen		= sizeof(int),
36401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		.mode		= 0644,
36416d4561110a3e9fa742aeec6717248a491dfb1878Eric W. Biederman		.proc_handler	= cdrom_sysctl_handler,
36421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	},
36431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{
36441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		.procname	= "check_media",
36451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		.data		= &cdrom_sysctl_settings.check,
36461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		.maxlen		= sizeof(int),
36471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		.mode		= 0644,
36486d4561110a3e9fa742aeec6717248a491dfb1878Eric W. Biederman		.proc_handler	= cdrom_sysctl_handler
36491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	},
3650894d2491153a9f8270dbed21175d06fde4eba6c7Eric W. Biederman	{ }
36511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
36521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
36531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ctl_table cdrom_cdrom_table[] = {
36541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{
36551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		.procname	= "cdrom",
36561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		.maxlen		= 0,
36571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		.mode		= 0555,
36581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		.child		= cdrom_table,
36591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	},
3660894d2491153a9f8270dbed21175d06fde4eba6c7Eric W. Biederman	{ }
36611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
36621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
36631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Make sure that /proc/sys/dev is there */
36641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ctl_table cdrom_root_table[] = {
36651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{
36661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		.procname	= "dev",
36671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		.maxlen		= 0,
36681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		.mode		= 0555,
36691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		.child		= cdrom_cdrom_table,
36701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	},
3671894d2491153a9f8270dbed21175d06fde4eba6c7Eric W. Biederman	{ }
36721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
36731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct ctl_table_header *cdrom_sysctl_header;
36741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
36751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void cdrom_sysctl_register(void)
36761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
36771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	static int initialized;
36781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
36791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (initialized == 1)
36801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return;
36811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
36820b4d414714f0d2f922d39424b0c5c82ad900a381Eric W. Biederman	cdrom_sysctl_header = register_sysctl_table(cdrom_root_table);
36831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
36841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* set the defaults */
36851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cdrom_sysctl_settings.autoclose = autoclose;
36861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cdrom_sysctl_settings.autoeject = autoeject;
36871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cdrom_sysctl_settings.debug = debug;
36881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cdrom_sysctl_settings.lock = lockdoor;
36891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cdrom_sysctl_settings.check = check_media_type;
36901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
36911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	initialized = 1;
36921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
36931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
36941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void cdrom_sysctl_unregister(void)
36951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
36961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (cdrom_sysctl_header)
36971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		unregister_sysctl_table(cdrom_sysctl_header);
36981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
36991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
370017672cf0a7cec57dea4c2a6705bd73114d5e9265Akinobu Mita#else /* CONFIG_SYSCTL */
370117672cf0a7cec57dea4c2a6705bd73114d5e9265Akinobu Mita
370217672cf0a7cec57dea4c2a6705bd73114d5e9265Akinobu Mitastatic void cdrom_sysctl_register(void)
370317672cf0a7cec57dea4c2a6705bd73114d5e9265Akinobu Mita{
370417672cf0a7cec57dea4c2a6705bd73114d5e9265Akinobu Mita}
370517672cf0a7cec57dea4c2a6705bd73114d5e9265Akinobu Mita
370617672cf0a7cec57dea4c2a6705bd73114d5e9265Akinobu Mitastatic void cdrom_sysctl_unregister(void)
370717672cf0a7cec57dea4c2a6705bd73114d5e9265Akinobu Mita{
370817672cf0a7cec57dea4c2a6705bd73114d5e9265Akinobu Mita}
370917672cf0a7cec57dea4c2a6705bd73114d5e9265Akinobu Mita
37101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#endif /* CONFIG_SYSCTL */
37111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
37121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int __init cdrom_init(void)
37131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
37141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cdrom_sysctl_register();
371517672cf0a7cec57dea4c2a6705bd73114d5e9265Akinobu Mita
37161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
37171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
37181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
37191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void __exit cdrom_exit(void)
37201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
3721e597cd09f711b28b8466ebdc2f12e55b44fa81e4Joe Perches	pr_info("Uniform CD-ROM driver unloaded\n");
37221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cdrom_sysctl_unregister();
37231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
37241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
37251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsmodule_init(cdrom_init);
37261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsmodule_exit(cdrom_exit);
37271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsMODULE_LICENSE("GPL");
3728