]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/cdrom/cdrom.c
Merge tag '9p-for-5.7-2' of git://github.com/martinetd/linux
[mirror_ubuntu-jammy-kernel.git] / drivers / cdrom / cdrom.c
CommitLineData
f30c2269 1/* linux/drivers/cdrom/cdrom.c
1da177e4
LT
2 Copyright (c) 1996, 1997 David A. van Leeuwen.
3 Copyright (c) 1997, 1998 Erik Andersen <andersee@debian.org>
4 Copyright (c) 1998, 1999 Jens Axboe <axboe@image.dk>
5
6 May be copied or modified under the terms of the GNU General Public
7 License. See linux/COPYING for more information.
8
9 Uniform CD-ROM driver for Linux.
8ea61889 10 See Documentation/cdrom/cdrom-standard.rst for usage information.
1da177e4
LT
11
12 The routines in the file provide a uniform interface between the
13 software that uses CD-ROMs and the various low-level drivers that
14 actually talk to the hardware. Suggestions are welcome.
15 Patches that work are more welcome though. ;-)
16
17 To Do List:
18 ----------------------------------
19
20 -- Modify sysctl/proc interface. I plan on having one directory per
21 drive, with entries for outputing general drive information, and sysctl
22 based tunable parameters such as whether the tray should auto-close for
23 that drive. Suggestions (or patches) for this welcome!
24
25
26 Revision History
27 ----------------------------------
28 1.00 Date Unknown -- David van Leeuwen <david@tm.tno.nl>
29 -- Initial version by David A. van Leeuwen. I don't have a detailed
30 changelog for the 1.x series, David?
31
322.00 Dec 2, 1997 -- Erik Andersen <andersee@debian.org>
25985edc 33 -- New maintainer! As David A. van Leeuwen has been too busy to actively
1da177e4
LT
34 maintain and improve this driver, I am now carrying on the torch. If
35 you have a problem with this driver, please feel free to contact me.
36
37 -- Added (rudimentary) sysctl interface. I realize this is really weak
38 right now, and is _very_ badly implemented. It will be improved...
39
40 -- Modified CDROM_DISC_STATUS so that it is now incorporated into
41 the Uniform CD-ROM driver via the cdrom_count_tracks function.
42 The cdrom_count_tracks function helps resolve some of the false
43 assumptions of the CDROM_DISC_STATUS ioctl, and is also used to check
44 for the correct media type when mounting or playing audio from a CD.
45
46 -- Remove the calls to verify_area and only use the copy_from_user and
47 copy_to_user stuff, since these calls now provide their own memory
48 checking with the 2.1.x kernels.
49
50 -- Major update to return codes so that errors from low-level drivers
51 are passed on through (thanks to Gerd Knorr for pointing out this
52 problem).
53
54 -- Made it so if a function isn't implemented in a low-level driver,
55 ENOSYS is now returned instead of EINVAL.
56
57 -- Simplified some complex logic so that the source code is easier to read.
58
59 -- Other stuff I probably forgot to mention (lots of changes).
60
612.01 to 2.11 Dec 1997-Jan 1998
62 -- TO-DO! Write changelogs for 2.01 to 2.12.
63
642.12 Jan 24, 1998 -- Erik Andersen <andersee@debian.org>
65 -- Fixed a bug in the IOCTL_IN and IOCTL_OUT macros. It turns out that
66 copy_*_user does not return EFAULT on error, but instead returns the number
67 of bytes not copied. I was returning whatever non-zero stuff came back from
68 the copy_*_user functions directly, which would result in strange errors.
69
702.13 July 17, 1998 -- Erik Andersen <andersee@debian.org>
71 -- Fixed a bug in CDROM_SELECT_SPEED where you couldn't lower the speed
72 of the drive. Thanks to Tobias Ringstr|m <tori@prosolvia.se> for pointing
73 this out and providing a simple fix.
74 -- Fixed the procfs-unload-module bug with the fill_inode procfs callback.
75 thanks to Andrea Arcangeli
76 -- Fixed it so that the /proc entry now also shows up when cdrom is
77 compiled into the kernel. Before it only worked when loaded as a module.
78
79 2.14 August 17, 1998 -- Erik Andersen <andersee@debian.org>
80 -- Fixed a bug in cdrom_media_changed and handling of reporting that
81 the media had changed for devices that _don't_ implement media_changed.
82 Thanks to Grant R. Guenther <grant@torque.net> for spotting this bug.
83 -- Made a few things more pedanticly correct.
84
852.50 Oct 19, 1998 - Jens Axboe <axboe@image.dk>
86 -- New maintainers! Erik was too busy to continue the work on the driver,
87 so now Chris Zwilling <chris@cloudnet.com> and Jens Axboe <axboe@image.dk>
88 will do their best to follow in his footsteps
89
90 2.51 Dec 20, 1998 - Jens Axboe <axboe@image.dk>
91 -- Check if drive is capable of doing what we ask before blindly changing
92 cdi->options in various ioctl.
93 -- Added version to proc entry.
94
95 2.52 Jan 16, 1999 - Jens Axboe <axboe@image.dk>
96 -- Fixed an error in open_for_data where we would sometimes not return
97 the correct error value. Thanks Huba Gaspar <huba@softcell.hu>.
98 -- Fixed module usage count - usage was based on /proc/sys/dev
99 instead of /proc/sys/dev/cdrom. This could lead to an oops when other
100 modules had entries in dev. Feb 02 - real bug was in sysctl.c where
101 dev would be removed even though it was used. cdrom.c just illuminated
102 that bug.
103
104 2.53 Feb 22, 1999 - Jens Axboe <axboe@image.dk>
105 -- Fixup of several ioctl calls, in particular CDROM_SET_OPTIONS has
106 been "rewritten" because capabilities and options aren't in sync. They
107 should be...
108 -- Added CDROM_LOCKDOOR ioctl. Locks the door and keeps it that way.
109 -- Added CDROM_RESET ioctl.
110 -- Added CDROM_DEBUG ioctl. Enable debug messages on-the-fly.
111 -- Added CDROM_GET_CAPABILITY ioctl. This relieves userspace programs
112 from parsing /proc/sys/dev/cdrom/info.
113
114 2.54 Mar 15, 1999 - Jens Axboe <axboe@image.dk>
115 -- Check capability mask from low level driver when counting tracks as
116 per suggestion from Corey J. Scotts <cstotts@blue.weeg.uiowa.edu>.
117
118 2.55 Apr 25, 1999 - Jens Axboe <axboe@image.dk>
119 -- autoclose was mistakenly checked against CDC_OPEN_TRAY instead of
120 CDC_CLOSE_TRAY.
121 -- proc info didn't mask against capabilities mask.
122
123 3.00 Aug 5, 1999 - Jens Axboe <axboe@image.dk>
124 -- Unified audio ioctl handling across CD-ROM drivers. A lot of the
125 code was duplicated before. Drives that support the generic packet
126 interface are now being fed packets from here instead.
127 -- First attempt at adding support for MMC2 commands - for DVD and
128 CD-R(W) drives. Only the DVD parts are in now - the interface used is
129 the same as for the audio ioctls.
130 -- ioctl cleanups. if a drive couldn't play audio, it didn't get
131 a change to perform device specific ioctls as well.
132 -- Defined CDROM_CAN(CDC_XXX) for checking the capabilities.
133 -- Put in sysctl files for autoclose, autoeject, check_media, debug,
134 and lock.
135 -- /proc/sys/dev/cdrom/info has been updated to also contain info about
136 CD-Rx and DVD capabilities.
137 -- Now default to checking media type.
138 -- CDROM_SEND_PACKET ioctl added. The infrastructure was in place for
139 doing this anyway, with the generic_packet addition.
140
141 3.01 Aug 6, 1999 - Jens Axboe <axboe@image.dk>
142 -- Fix up the sysctl handling so that the option flags get set
143 correctly.
144 -- Fix up ioctl handling so the device specific ones actually get
145 called :).
146
147 3.02 Aug 8, 1999 - Jens Axboe <axboe@image.dk>
148 -- Fixed volume control on SCSI drives (or others with longer audio
149 page).
150 -- Fixed a couple of DVD minors. Thanks to Andrew T. Veliath
151 <andrewtv@usa.net> for telling me and for having defined the various
152 DVD structures and ioctls in the first place! He designed the original
153 DVD patches for ide-cd and while I rearranged and unified them, the
154 interface is still the same.
155
156 3.03 Sep 1, 1999 - Jens Axboe <axboe@image.dk>
157 -- Moved the rest of the audio ioctls from the CD-ROM drivers here. Only
158 CDROMREADTOCENTRY and CDROMREADTOCHDR are left.
159 -- Moved the CDROMREADxxx ioctls in here.
160 -- Defined the cdrom_get_last_written and cdrom_get_next_block as ioctls
161 and exported functions.
162 -- Erik Andersen <andersen@xmission.com> modified all SCMD_ commands
163 to now read GPCMD_ for the new generic packet interface. All low level
164 drivers are updated as well.
165 -- Various other cleanups.
166
167 3.04 Sep 12, 1999 - Jens Axboe <axboe@image.dk>
168 -- Fixed a couple of possible memory leaks (if an operation failed and
169 we didn't free the buffer before returning the error).
170 -- Integrated Uniform CD Changer handling from Richard Sharman
171 <rsharman@pobox.com>.
172 -- Defined CD_DVD and CD_CHANGER log levels.
173 -- Fixed the CDROMREADxxx ioctls.
174 -- CDROMPLAYTRKIND uses the GPCMD_PLAY_AUDIO_MSF command - too few
175 drives supported it. We lose the index part, however.
176 -- Small modifications to accommodate opens of /dev/hdc1, required
177 for ide-cd to handle multisession discs.
178 -- Export cdrom_mode_sense and cdrom_mode_select.
179 -- init_cdrom_command() for setting up a cgc command.
180
181 3.05 Oct 24, 1999 - Jens Axboe <axboe@image.dk>
182 -- Changed the interface for CDROM_SEND_PACKET. Before it was virtually
183 impossible to send the drive data in a sensible way.
184 -- Lowered stack usage in mmc_ioctl(), dvd_read_disckey(), and
185 dvd_read_manufact.
186 -- Added setup of write mode for packet writing.
187 -- Fixed CDDA ripping with cdda2wav - accept much larger requests of
188 number of frames and split the reads in blocks of 8.
189
190 3.06 Dec 13, 1999 - Jens Axboe <axboe@image.dk>
191 -- Added support for changing the region of DVD drives.
192 -- Added sense data to generic command.
193
194 3.07 Feb 2, 2000 - Jens Axboe <axboe@suse.de>
195 -- Do same "read header length" trick in cdrom_get_disc_info() as
196 we do in cdrom_get_track_info() -- some drive don't obey specs and
197 fail if they can't supply the full Mt Fuji size table.
198 -- Deleted stuff related to setting up write modes. It has a different
199 home now.
200 -- Clear header length in mode_select unconditionally.
201 -- Removed the register_disk() that was added, not needed here.
202
203 3.08 May 1, 2000 - Jens Axboe <axboe@suse.de>
204 -- Fix direction flag in setup_send_key and setup_report_key. This
205 gave some SCSI adapters problems.
206 -- Always return -EROFS for write opens
207 -- Convert to module_init/module_exit style init and remove some
208 of the #ifdef MODULE stuff
209 -- Fix several dvd errors - DVD_LU_SEND_ASF should pass agid,
210 DVD_HOST_SEND_RPC_STATE did not set buffer size in cdb, and
211 dvd_do_auth passed uninitialized data to drive because init_cdrom_command
212 did not clear a 0 sized buffer.
213
214 3.09 May 12, 2000 - Jens Axboe <axboe@suse.de>
215 -- Fix Video-CD on SCSI drives that don't support READ_CD command. In
216 that case switch block size and issue plain READ_10 again, then switch
217 back.
218
219 3.10 Jun 10, 2000 - Jens Axboe <axboe@suse.de>
220 -- Fix volume control on CD's - old SCSI-II drives now use their own
221 code, as doing MODE6 stuff in here is really not my intention.
222 -- Use READ_DISC_INFO for more reliable end-of-disc.
223
224 3.11 Jun 12, 2000 - Jens Axboe <axboe@suse.de>
225 -- Fix bug in getting rpc phase 2 region info.
226 -- Reinstate "correct" CDROMPLAYTRKIND
227
228 3.12 Oct 18, 2000 - Jens Axboe <axboe@suse.de>
229 -- Use quiet bit on packet commands not known to work
230
231 3.20 Dec 17, 2003 - Jens Axboe <axboe@suse.de>
232 -- Various fixes and lots of cleanups not listed :-)
233 -- Locking fixes
234 -- Mt Rainier support
235 -- DVD-RAM write open fixes
236
237 Nov 5 2001, Aug 8 2002. Modified by Andy Polyakov
238 <appro@fy.chalmers.se> to support MMC-3 compliant DVD+RW units.
239
240 Modified by Nigel Kukard <nkukard@lbsd.net> - support DVD+RW
241 2.4.x patch by Andy Polyakov <appro@fy.chalmers.se>
242
243-------------------------------------------------------------------------*/
244
e597cd09
JP
245#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
246
1da177e4
LT
247#define REVISION "Revision: 3.20"
248#define VERSION "Id: cdrom.c 3.20 2003/12/17"
249
250/* I use an error-log mask to give fine grain control over the type of
251 messages dumped to the system logs. The available masks include: */
252#define CD_NOTHING 0x0
253#define CD_WARNING 0x1
254#define CD_REG_UNREG 0x2
255#define CD_DO_IOCTL 0x4
256#define CD_OPEN 0x8
257#define CD_CLOSE 0x10
258#define CD_COUNT_TRACKS 0x20
259#define CD_CHANGER 0x40
260#define CD_DVD 0x80
261
262/* Define this to remove _all_ the debugging messages */
263/* #define ERRLOGMASK CD_NOTHING */
264#define ERRLOGMASK CD_WARNING
265/* #define ERRLOGMASK (CD_WARNING|CD_OPEN|CD_COUNT_TRACKS|CD_CLOSE) */
266/* #define ERRLOGMASK (CD_WARNING|CD_REG_UNREG|CD_DO_IOCTL|CD_OPEN|CD_CLOSE|CD_COUNT_TRACKS) */
267
f25191bb 268#include <linux/atomic.h>
1da177e4
LT
269#include <linux/module.h>
270#include <linux/fs.h>
1da177e4
LT
271#include <linux/major.h>
272#include <linux/types.h>
273#include <linux/errno.h>
274#include <linux/kernel.h>
275#include <linux/mm.h>
276#include <linux/slab.h>
277#include <linux/cdrom.h>
278#include <linux/sysctl.h>
279#include <linux/proc_fs.h>
280#include <linux/blkpg.h>
281#include <linux/init.h>
282#include <linux/fcntl.h>
283#include <linux/blkdev.h>
284#include <linux/times.h>
7c0f6ba6 285#include <linux/uaccess.h>
4e178c17 286#include <scsi/scsi_common.h>
82ed4db4 287#include <scsi/scsi_request.h>
1da177e4
LT
288
289/* used to tell the module to turn on full debugging messages */
90ab5ee9 290static bool debug;
1da177e4 291/* default compatibility mode */
90ab5ee9
RR
292static bool autoclose=1;
293static bool autoeject;
294static bool lockdoor = 1;
1da177e4 295/* will we ever get to use this... sigh. */
90ab5ee9 296static bool check_media_type;
1da177e4 297/* automatically restart mrw format */
90ab5ee9 298static bool mrw_format_restart = 1;
1da177e4
LT
299module_param(debug, bool, 0);
300module_param(autoclose, bool, 0);
301module_param(autoeject, bool, 0);
302module_param(lockdoor, bool, 0);
303module_param(check_media_type, bool, 0);
304module_param(mrw_format_restart, bool, 0);
305
554988d6 306static DEFINE_MUTEX(cdrom_mutex);
1da177e4
LT
307
308static const char *mrw_format_status[] = {
309 "not mrw",
310 "bgformat inactive",
311 "bgformat active",
312 "mrw complete",
313};
314
315static const char *mrw_address_space[] = { "DMA", "GAA" };
316
5944b2ce
JP
317#if (ERRLOGMASK != CD_NOTHING)
318#define cd_dbg(type, fmt, ...) \
e597cd09
JP
319do { \
320 if ((ERRLOGMASK & type) || debug == 1) \
5944b2ce 321 pr_debug(fmt, ##__VA_ARGS__); \
e597cd09 322} while (0)
1da177e4 323#else
5944b2ce 324#define cd_dbg(type, fmt, ...) \
e597cd09
JP
325do { \
326 if (0 && (ERRLOGMASK & type) || debug == 1) \
5944b2ce 327 pr_debug(fmt, ##__VA_ARGS__); \
e597cd09 328} while (0)
1da177e4
LT
329#endif
330
1da177e4
LT
331/* The (cdo->capability & ~cdi->mask & CDC_XXX) construct was used in
332 a lot of places. This macro makes the code more clear. */
333#define CDROM_CAN(type) (cdi->ops->capability & ~cdi->mask & (type))
334
2e11c207
JA
335/*
336 * Another popular OS uses 7 seconds as the hard timeout for default
337 * commands, so it is a good choice for us as well.
338 */
339#define CDROM_DEF_TIMEOUT (7 * HZ)
340
1da177e4 341/* Not-exported routines. */
1da177e4 342
1da177e4 343static void cdrom_sysctl_register(void);
17672cf0 344
7fd097d4 345static LIST_HEAD(cdrom_list);
1da177e4 346
853fe1bf
KC
347int cdrom_dummy_generic_packet(struct cdrom_device_info *cdi,
348 struct packet_command *cgc)
1da177e4 349{
e7d0748d
KC
350 if (cgc->sshdr) {
351 cgc->sshdr->sense_key = 0x05;
352 cgc->sshdr->asc = 0x20;
353 cgc->sshdr->ascq = 0x00;
1da177e4
LT
354 }
355
356 cgc->stat = -EIO;
357 return -EIO;
358}
853fe1bf 359EXPORT_SYMBOL(cdrom_dummy_generic_packet);
1da177e4 360
40569c61 361static int cdrom_flush_cache(struct cdrom_device_info *cdi)
1da177e4
LT
362{
363 struct packet_command cgc;
1da177e4 364
40569c61
JP
365 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
366 cgc.cmd[0] = GPCMD_FLUSH_CACHE;
1da177e4 367
40569c61 368 cgc.timeout = 5 * 60 * HZ;
1da177e4 369
40569c61 370 return cdi->ops->generic_packet(cdi, &cgc);
1da177e4
LT
371}
372
bd6f0bba
JP
373/* requires CD R/RW */
374static int cdrom_get_disc_info(struct cdrom_device_info *cdi,
375 disc_information *di)
376{
853fe1bf 377 const struct cdrom_device_ops *cdo = cdi->ops;
bd6f0bba
JP
378 struct packet_command cgc;
379 int ret, buflen;
380
381 /* set up command and get the disc info */
382 init_cdrom_command(&cgc, di, sizeof(*di), CGC_DATA_READ);
383 cgc.cmd[0] = GPCMD_READ_DISC_INFO;
384 cgc.cmd[8] = cgc.buflen = 2;
385 cgc.quiet = 1;
386
387 ret = cdo->generic_packet(cdi, &cgc);
388 if (ret)
389 return ret;
390
391 /* not all drives have the same disc_info length, so requeue
392 * packet with the length the drive tells us it can supply
393 */
394 buflen = be16_to_cpu(di->disc_information_length) +
395 sizeof(di->disc_information_length);
396
397 if (buflen > sizeof(disc_information))
398 buflen = sizeof(disc_information);
399
400 cgc.cmd[8] = cgc.buflen = buflen;
401 ret = cdo->generic_packet(cdi, &cgc);
402 if (ret)
403 return ret;
404
405 /* return actual fill size */
406 return buflen;
407}
408
40569c61
JP
409/* This macro makes sure we don't have to check on cdrom_device_ops
410 * existence in the run-time routines below. Change_capability is a
411 * hack to have the capability flags defined const, while we can still
412 * change it here without gcc complaining at every line.
413 */
8f94004e
JA
414#define ENSURE(cdo, call, bits) \
415do { \
416 if (cdo->call == NULL) \
417 WARN_ON_ONCE((cdo)->capability & (bits)); \
40569c61
JP
418} while (0)
419
1da177e4
LT
420/*
421 * the first prototypes used 0x2c as the page code for the mrw mode page,
422 * subsequently this was changed to 0x03. probe the one used by this drive
423 */
424static int cdrom_mrw_probe_pc(struct cdrom_device_info *cdi)
425{
426 struct packet_command cgc;
fce53847 427 char buffer[16];
1da177e4 428
fce53847 429 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_READ);
1da177e4
LT
430
431 cgc.timeout = HZ;
432 cgc.quiet = 1;
433
434 if (!cdrom_mode_sense(cdi, &cgc, MRW_MODE_PC, 0)) {
435 cdi->mrw_mode_page = MRW_MODE_PC;
fce53847 436 return 0;
1da177e4
LT
437 } else if (!cdrom_mode_sense(cdi, &cgc, MRW_MODE_PC_PRE1, 0)) {
438 cdi->mrw_mode_page = MRW_MODE_PC_PRE1;
fce53847 439 return 0;
1da177e4 440 }
fce53847
FT
441
442 return 1;
1da177e4
LT
443}
444
445static int cdrom_is_mrw(struct cdrom_device_info *cdi, int *write)
446{
447 struct packet_command cgc;
448 struct mrw_feature_desc *mfd;
fce53847 449 unsigned char buffer[16];
1da177e4
LT
450 int ret;
451
452 *write = 0;
453
fce53847 454 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_READ);
1da177e4
LT
455
456 cgc.cmd[0] = GPCMD_GET_CONFIGURATION;
457 cgc.cmd[3] = CDF_MRW;
fce53847 458 cgc.cmd[8] = sizeof(buffer);
1da177e4
LT
459 cgc.quiet = 1;
460
461 if ((ret = cdi->ops->generic_packet(cdi, &cgc)))
fce53847 462 return ret;
1da177e4
LT
463
464 mfd = (struct mrw_feature_desc *)&buffer[sizeof(struct feature_header)];
fce53847
FT
465 if (be16_to_cpu(mfd->feature_code) != CDF_MRW)
466 return 1;
1da177e4
LT
467 *write = mfd->write;
468
469 if ((ret = cdrom_mrw_probe_pc(cdi))) {
470 *write = 0;
fce53847 471 return ret;
1da177e4 472 }
fce53847
FT
473
474 return 0;
1da177e4
LT
475}
476
477static int cdrom_mrw_bgformat(struct cdrom_device_info *cdi, int cont)
478{
479 struct packet_command cgc;
fce53847 480 unsigned char buffer[12];
1da177e4
LT
481 int ret;
482
e597cd09 483 pr_info("%sstarting format\n", cont ? "Re" : "");
1da177e4
LT
484
485 /*
486 * FmtData bit set (bit 4), format type is 1
487 */
fce53847 488 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_WRITE);
1da177e4
LT
489 cgc.cmd[0] = GPCMD_FORMAT_UNIT;
490 cgc.cmd[1] = (1 << 4) | 1;
491
492 cgc.timeout = 5 * 60 * HZ;
493
494 /*
495 * 4 byte format list header, 8 byte format list descriptor
496 */
497 buffer[1] = 1 << 1;
498 buffer[3] = 8;
499
500 /*
501 * nr_blocks field
502 */
503 buffer[4] = 0xff;
504 buffer[5] = 0xff;
505 buffer[6] = 0xff;
506 buffer[7] = 0xff;
507
508 buffer[8] = 0x24 << 2;
509 buffer[11] = cont;
510
511 ret = cdi->ops->generic_packet(cdi, &cgc);
512 if (ret)
e597cd09 513 pr_info("bgformat failed\n");
1da177e4
LT
514
515 return ret;
516}
517
518static int cdrom_mrw_bgformat_susp(struct cdrom_device_info *cdi, int immed)
519{
520 struct packet_command cgc;
521
522 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
523 cgc.cmd[0] = GPCMD_CLOSE_TRACK;
524
525 /*
526 * Session = 1, Track = 0
527 */
528 cgc.cmd[1] = !!immed;
529 cgc.cmd[2] = 1 << 1;
530
531 cgc.timeout = 5 * 60 * HZ;
532
533 return cdi->ops->generic_packet(cdi, &cgc);
534}
535
1da177e4
LT
536static int cdrom_mrw_exit(struct cdrom_device_info *cdi)
537{
538 disc_information di;
539 int ret;
540
541 ret = cdrom_get_disc_info(cdi, &di);
542 if (ret < 0 || ret < (int)offsetof(typeof(di),disc_type))
543 return 1;
544
545 ret = 0;
546 if (di.mrw_status == CDM_MRW_BGFORMAT_ACTIVE) {
e597cd09 547 pr_info("issuing MRW background format suspend\n");
1da177e4
LT
548 ret = cdrom_mrw_bgformat_susp(cdi, 0);
549 }
550
7da21a02 551 if (!ret && cdi->media_written)
1da177e4
LT
552 ret = cdrom_flush_cache(cdi);
553
554 return ret;
555}
556
557static int cdrom_mrw_set_lba_space(struct cdrom_device_info *cdi, int space)
558{
559 struct packet_command cgc;
560 struct mode_page_header *mph;
fce53847 561 char buffer[16];
1da177e4
LT
562 int ret, offset, size;
563
fce53847 564 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_READ);
1da177e4 565
fce53847
FT
566 cgc.buffer = buffer;
567 cgc.buflen = sizeof(buffer);
1da177e4 568
40569c61
JP
569 ret = cdrom_mode_sense(cdi, &cgc, cdi->mrw_mode_page, 0);
570 if (ret)
fce53847 571 return ret;
1da177e4 572
40569c61 573 mph = (struct mode_page_header *)buffer;
1da177e4
LT
574 offset = be16_to_cpu(mph->desc_length);
575 size = be16_to_cpu(mph->mode_data_length) + 2;
576
577 buffer[offset + 3] = space;
578 cgc.buflen = size;
579
40569c61
JP
580 ret = cdrom_mode_select(cdi, &cgc);
581 if (ret)
fce53847 582 return ret;
1da177e4 583
e597cd09
JP
584 pr_info("%s: mrw address space %s selected\n",
585 cdi->name, mrw_address_space[space]);
fce53847 586 return 0;
1da177e4
LT
587}
588
40569c61
JP
589int register_cdrom(struct cdrom_device_info *cdi)
590{
591 static char banner_printed;
853fe1bf 592 const struct cdrom_device_ops *cdo = cdi->ops;
40569c61
JP
593
594 cd_dbg(CD_OPEN, "entering register_cdrom\n");
595
596 if (cdo->open == NULL || cdo->release == NULL)
597 return -EINVAL;
598 if (!banner_printed) {
599 pr_info("Uniform CD-ROM driver " REVISION "\n");
600 banner_printed = 1;
601 cdrom_sysctl_register();
602 }
603
8f94004e 604 ENSURE(cdo, drive_status, CDC_DRIVE_STATUS);
40569c61 605 if (cdo->check_events == NULL && cdo->media_changed == NULL)
8f94004e
JA
606 WARN_ON_ONCE(cdo->capability & (CDC_MEDIA_CHANGED | CDC_SELECT_DISC));
607 ENSURE(cdo, tray_move, CDC_CLOSE_TRAY | CDC_OPEN_TRAY);
608 ENSURE(cdo, lock_door, CDC_LOCK);
609 ENSURE(cdo, select_speed, CDC_SELECT_SPEED);
610 ENSURE(cdo, get_last_session, CDC_MULTI_SESSION);
611 ENSURE(cdo, get_mcn, CDC_MCN);
612 ENSURE(cdo, reset, CDC_RESET);
613 ENSURE(cdo, generic_packet, CDC_GENERIC_PACKET);
40569c61 614 cdi->mc_flags = 0;
40569c61
JP
615 cdi->options = CDO_USE_FFLAGS;
616
617 if (autoclose == 1 && CDROM_CAN(CDC_CLOSE_TRAY))
618 cdi->options |= (int) CDO_AUTO_CLOSE;
619 if (autoeject == 1 && CDROM_CAN(CDC_OPEN_TRAY))
620 cdi->options |= (int) CDO_AUTO_EJECT;
621 if (lockdoor == 1)
622 cdi->options |= (int) CDO_LOCK;
623 if (check_media_type == 1)
624 cdi->options |= (int) CDO_CHECK_TYPE;
625
626 if (CDROM_CAN(CDC_MRW_W))
627 cdi->exit = cdrom_mrw_exit;
628
629 if (cdi->disk)
630 cdi->cdda_method = CDDA_BPC_FULL;
631 else
632 cdi->cdda_method = CDDA_OLD;
633
853fe1bf 634 WARN_ON(!cdo->generic_packet);
40569c61
JP
635
636 cd_dbg(CD_REG_UNREG, "drive \"/dev/%s\" registered\n", cdi->name);
637 mutex_lock(&cdrom_mutex);
638 list_add(&cdi->list, &cdrom_list);
639 mutex_unlock(&cdrom_mutex);
640 return 0;
641}
642#undef ENSURE
643
644void unregister_cdrom(struct cdrom_device_info *cdi)
645{
646 cd_dbg(CD_OPEN, "entering unregister_cdrom\n");
647
648 mutex_lock(&cdrom_mutex);
649 list_del(&cdi->list);
650 mutex_unlock(&cdrom_mutex);
651
652 if (cdi->exit)
653 cdi->exit(cdi);
654
40569c61
JP
655 cd_dbg(CD_REG_UNREG, "drive \"/dev/%s\" unregistered\n", cdi->name);
656}
657
658int cdrom_get_media_event(struct cdrom_device_info *cdi,
659 struct media_event_desc *med)
660{
661 struct packet_command cgc;
662 unsigned char buffer[8];
663 struct event_header *eh = (struct event_header *)buffer;
664
665 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_READ);
666 cgc.cmd[0] = GPCMD_GET_EVENT_STATUS_NOTIFICATION;
667 cgc.cmd[1] = 1; /* IMMED */
668 cgc.cmd[4] = 1 << 4; /* media event */
669 cgc.cmd[8] = sizeof(buffer);
670 cgc.quiet = 1;
671
672 if (cdi->ops->generic_packet(cdi, &cgc))
673 return 1;
674
675 if (be16_to_cpu(eh->data_len) < sizeof(*med))
676 return 1;
677
678 if (eh->nea || eh->notification_class != 0x4)
679 return 1;
680
681 memcpy(med, &buffer[sizeof(*eh)], sizeof(*med));
682 return 0;
683}
684
1da177e4
LT
685static int cdrom_get_random_writable(struct cdrom_device_info *cdi,
686 struct rwrt_feature_desc *rfd)
687{
688 struct packet_command cgc;
fce53847 689 char buffer[24];
1da177e4
LT
690 int ret;
691
fce53847 692 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_READ);
1da177e4
LT
693
694 cgc.cmd[0] = GPCMD_GET_CONFIGURATION; /* often 0x46 */
695 cgc.cmd[3] = CDF_RWRT; /* often 0x0020 */
fce53847 696 cgc.cmd[8] = sizeof(buffer); /* often 0x18 */
1da177e4
LT
697 cgc.quiet = 1;
698
699 if ((ret = cdi->ops->generic_packet(cdi, &cgc)))
fce53847 700 return ret;
1da177e4
LT
701
702 memcpy(rfd, &buffer[sizeof(struct feature_header)], sizeof (*rfd));
fce53847 703 return 0;
1da177e4
LT
704}
705
706static int cdrom_has_defect_mgt(struct cdrom_device_info *cdi)
707{
708 struct packet_command cgc;
fce53847 709 char buffer[16];
56052d52 710 __be16 *feature_code;
1da177e4
LT
711 int ret;
712
fce53847 713 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_READ);
1da177e4
LT
714
715 cgc.cmd[0] = GPCMD_GET_CONFIGURATION;
716 cgc.cmd[3] = CDF_HWDM;
fce53847 717 cgc.cmd[8] = sizeof(buffer);
1da177e4
LT
718 cgc.quiet = 1;
719
720 if ((ret = cdi->ops->generic_packet(cdi, &cgc)))
fce53847 721 return ret;
1da177e4 722
56052d52 723 feature_code = (__be16 *) &buffer[sizeof(struct feature_header)];
1da177e4 724 if (be16_to_cpu(*feature_code) == CDF_HWDM)
fce53847
FT
725 return 0;
726
727 return 1;
1da177e4
LT
728}
729
730
731static int cdrom_is_random_writable(struct cdrom_device_info *cdi, int *write)
732{
733 struct rwrt_feature_desc rfd;
734 int ret;
735
736 *write = 0;
737
738 if ((ret = cdrom_get_random_writable(cdi, &rfd)))
739 return ret;
740
741 if (CDF_RWRT == be16_to_cpu(rfd.feature_code))
742 *write = 1;
743
744 return 0;
745}
746
747static int cdrom_media_erasable(struct cdrom_device_info *cdi)
748{
749 disc_information di;
750 int ret;
751
752 ret = cdrom_get_disc_info(cdi, &di);
753 if (ret < 0 || ret < offsetof(typeof(di), n_first_track))
754 return -1;
755
756 return di.erasable;
757}
758
759/*
760 * FIXME: check RO bit
761 */
762static int cdrom_dvdram_open_write(struct cdrom_device_info *cdi)
763{
764 int ret = cdrom_media_erasable(cdi);
765
766 /*
767 * allow writable open if media info read worked and media is
768 * erasable, _or_ if it fails since not all drives support it
769 */
770 if (!ret)
771 return 1;
772
773 return 0;
774}
775
776static int cdrom_mrw_open_write(struct cdrom_device_info *cdi)
777{
778 disc_information di;
779 int ret;
780
781 /*
782 * always reset to DMA lba space on open
783 */
784 if (cdrom_mrw_set_lba_space(cdi, MRW_LBA_DMA)) {
e597cd09 785 pr_err("failed setting lba address space\n");
1da177e4
LT
786 return 1;
787 }
788
789 ret = cdrom_get_disc_info(cdi, &di);
790 if (ret < 0 || ret < offsetof(typeof(di),disc_type))
791 return 1;
792
793 if (!di.erasable)
794 return 1;
795
796 /*
797 * mrw_status
798 * 0 - not MRW formatted
799 * 1 - MRW bgformat started, but not running or complete
800 * 2 - MRW bgformat in progress
801 * 3 - MRW formatting complete
802 */
803 ret = 0;
e597cd09 804 pr_info("open: mrw_status '%s'\n", mrw_format_status[di.mrw_status]);
1da177e4
LT
805 if (!di.mrw_status)
806 ret = 1;
807 else if (di.mrw_status == CDM_MRW_BGFORMAT_INACTIVE &&
808 mrw_format_restart)
809 ret = cdrom_mrw_bgformat(cdi, 1);
810
811 return ret;
812}
813
814static int mo_open_write(struct cdrom_device_info *cdi)
815{
816 struct packet_command cgc;
fce53847 817 char buffer[255];
1da177e4
LT
818 int ret;
819
fce53847 820 init_cdrom_command(&cgc, &buffer, 4, CGC_DATA_READ);
1da177e4
LT
821 cgc.quiet = 1;
822
823 /*
824 * obtain write protect information as per
825 * drivers/scsi/sd.c:sd_read_write_protect_flag
826 */
827
828 ret = cdrom_mode_sense(cdi, &cgc, GPMODE_ALL_PAGES, 0);
829 if (ret)
830 ret = cdrom_mode_sense(cdi, &cgc, GPMODE_VENDOR_PAGE, 0);
831 if (ret) {
832 cgc.buflen = 255;
833 ret = cdrom_mode_sense(cdi, &cgc, GPMODE_ALL_PAGES, 0);
834 }
835
836 /* drive gave us no info, let the user go ahead */
fce53847
FT
837 if (ret)
838 return 0;
1da177e4 839
fce53847 840 return buffer[3] & 0x80;
1da177e4
LT
841}
842
843static int cdrom_ram_open_write(struct cdrom_device_info *cdi)
844{
845 struct rwrt_feature_desc rfd;
846 int ret;
847
848 if ((ret = cdrom_has_defect_mgt(cdi)))
849 return ret;
850
851 if ((ret = cdrom_get_random_writable(cdi, &rfd)))
852 return ret;
853 else if (CDF_RWRT == be16_to_cpu(rfd.feature_code))
854 ret = !rfd.curr;
855
5944b2ce 856 cd_dbg(CD_OPEN, "can open for random write\n");
1da177e4
LT
857 return ret;
858}
859
860static void cdrom_mmc3_profile(struct cdrom_device_info *cdi)
861{
862 struct packet_command cgc;
fce53847 863 char buffer[32];
1da177e4
LT
864 int ret, mmc3_profile;
865
fce53847 866 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_READ);
1da177e4
LT
867
868 cgc.cmd[0] = GPCMD_GET_CONFIGURATION;
869 cgc.cmd[1] = 0;
870 cgc.cmd[2] = cgc.cmd[3] = 0; /* Starting Feature Number */
fce53847 871 cgc.cmd[8] = sizeof(buffer); /* Allocation Length */
1da177e4
LT
872 cgc.quiet = 1;
873
874 if ((ret = cdi->ops->generic_packet(cdi, &cgc)))
875 mmc3_profile = 0xffff;
876 else
877 mmc3_profile = (buffer[6] << 8) | buffer[7];
878
879 cdi->mmc3_profile = mmc3_profile;
880}
881
882static int cdrom_is_dvd_rw(struct cdrom_device_info *cdi)
883{
884 switch (cdi->mmc3_profile) {
885 case 0x12: /* DVD-RAM */
886 case 0x1A: /* DVD+RW */
f7e7868b 887 case 0x43: /* BD-RE */
1da177e4
LT
888 return 0;
889 default:
890 return 1;
891 }
892}
893
894/*
895 * returns 0 for ok to open write, non-0 to disallow
896 */
897static int cdrom_open_write(struct cdrom_device_info *cdi)
898{
899 int mrw, mrw_write, ram_write;
900 int ret = 1;
901
902 mrw = 0;
903 if (!cdrom_is_mrw(cdi, &mrw_write))
904 mrw = 1;
905
906 if (CDROM_CAN(CDC_MO_DRIVE))
907 ram_write = 1;
908 else
909 (void) cdrom_is_random_writable(cdi, &ram_write);
910
911 if (mrw)
912 cdi->mask &= ~CDC_MRW;
913 else
914 cdi->mask |= CDC_MRW;
915
916 if (mrw_write)
917 cdi->mask &= ~CDC_MRW_W;
918 else
919 cdi->mask |= CDC_MRW_W;
920
921 if (ram_write)
922 cdi->mask &= ~CDC_RAM;
923 else
924 cdi->mask |= CDC_RAM;
925
926 if (CDROM_CAN(CDC_MRW_W))
927 ret = cdrom_mrw_open_write(cdi);
928 else if (CDROM_CAN(CDC_DVD_RAM))
929 ret = cdrom_dvdram_open_write(cdi);
930 else if (CDROM_CAN(CDC_RAM) &&
931 !CDROM_CAN(CDC_CD_R|CDC_CD_RW|CDC_DVD|CDC_DVD_R|CDC_MRW|CDC_MO_DRIVE))
932 ret = cdrom_ram_open_write(cdi);
933 else if (CDROM_CAN(CDC_MO_DRIVE))
934 ret = mo_open_write(cdi);
935 else if (!cdrom_is_dvd_rw(cdi))
936 ret = 0;
937
938 return ret;
939}
940
941static void cdrom_dvd_rw_close_write(struct cdrom_device_info *cdi)
942{
943 struct packet_command cgc;
944
945 if (cdi->mmc3_profile != 0x1a) {
5944b2ce 946 cd_dbg(CD_CLOSE, "%s: No DVD+RW\n", cdi->name);
1da177e4
LT
947 return;
948 }
949
950 if (!cdi->media_written) {
5944b2ce 951 cd_dbg(CD_CLOSE, "%s: DVD+RW media clean\n", cdi->name);
1da177e4
LT
952 return;
953 }
954
e597cd09 955 pr_info("%s: dirty DVD+RW media, \"finalizing\"\n", cdi->name);
1da177e4
LT
956
957 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
958 cgc.cmd[0] = GPCMD_FLUSH_CACHE;
959 cgc.timeout = 30*HZ;
960 cdi->ops->generic_packet(cdi, &cgc);
961
962 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
963 cgc.cmd[0] = GPCMD_CLOSE_TRACK;
964 cgc.timeout = 3000*HZ;
965 cgc.quiet = 1;
966 cdi->ops->generic_packet(cdi, &cgc);
967
968 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
969 cgc.cmd[0] = GPCMD_CLOSE_TRACK;
970 cgc.cmd[2] = 2; /* Close session */
971 cgc.quiet = 1;
972 cgc.timeout = 3000*HZ;
973 cdi->ops->generic_packet(cdi, &cgc);
974
975 cdi->media_written = 0;
976}
977
978static int cdrom_close_write(struct cdrom_device_info *cdi)
979{
980#if 0
981 return cdrom_flush_cache(cdi);
982#else
983 return 0;
984#endif
985}
986
a803393b
JP
987/* badly broken, I know. Is due for a fixup anytime. */
988static void cdrom_count_tracks(struct cdrom_device_info *cdi, tracktype *tracks)
989{
990 struct cdrom_tochdr header;
991 struct cdrom_tocentry entry;
992 int ret, i;
993 tracks->data = 0;
994 tracks->audio = 0;
995 tracks->cdi = 0;
996 tracks->xa = 0;
997 tracks->error = 0;
998 cd_dbg(CD_COUNT_TRACKS, "entering cdrom_count_tracks\n");
366ba7c7
DEP
999
1000 if (!CDROM_CAN(CDC_PLAY_AUDIO)) {
1001 tracks->error = CDS_NO_INFO;
1002 return;
1003 }
1004
a803393b
JP
1005 /* Grab the TOC header so we can see how many tracks there are */
1006 ret = cdi->ops->audio_ioctl(cdi, CDROMREADTOCHDR, &header);
1007 if (ret) {
1008 if (ret == -ENOMEDIUM)
1009 tracks->error = CDS_NO_DISC;
1010 else
1011 tracks->error = CDS_NO_INFO;
1012 return;
1013 }
1014 /* check what type of tracks are on this disc */
1015 entry.cdte_format = CDROM_MSF;
1016 for (i = header.cdth_trk0; i <= header.cdth_trk1; i++) {
1017 entry.cdte_track = i;
1018 if (cdi->ops->audio_ioctl(cdi, CDROMREADTOCENTRY, &entry)) {
1019 tracks->error = CDS_NO_INFO;
1020 return;
1021 }
1022 if (entry.cdte_ctrl & CDROM_DATA_TRACK) {
1023 if (entry.cdte_format == 0x10)
1024 tracks->cdi++;
1025 else if (entry.cdte_format == 0x20)
1026 tracks->xa++;
1027 else
1028 tracks->data++;
1029 } else {
1030 tracks->audio++;
1031 }
1032 cd_dbg(CD_COUNT_TRACKS, "track %d: format=%d, ctrl=%d\n",
1033 i, entry.cdte_format, entry.cdte_ctrl);
1034 }
1035 cd_dbg(CD_COUNT_TRACKS, "disc has %d tracks: %d=audio %d=data %d=Cd-I %d=XA\n",
1036 header.cdth_trk1, tracks->audio, tracks->data,
1037 tracks->cdi, tracks->xa);
1038}
1039
1da177e4 1040static
82b91540 1041int open_for_data(struct cdrom_device_info *cdi)
1da177e4
LT
1042{
1043 int ret;
853fe1bf 1044 const struct cdrom_device_ops *cdo = cdi->ops;
1da177e4 1045 tracktype tracks;
5944b2ce 1046 cd_dbg(CD_OPEN, "entering open_for_data\n");
1da177e4
LT
1047 /* Check if the driver can report drive status. If it can, we
1048 can do clever things. If it can't, well, we at least tried! */
1049 if (cdo->drive_status != NULL) {
1050 ret = cdo->drive_status(cdi, CDSL_CURRENT);
5944b2ce 1051 cd_dbg(CD_OPEN, "drive_status=%d\n", ret);
1da177e4 1052 if (ret == CDS_TRAY_OPEN) {
5944b2ce 1053 cd_dbg(CD_OPEN, "the tray is open...\n");
1da177e4
LT
1054 /* can/may i close it? */
1055 if (CDROM_CAN(CDC_CLOSE_TRAY) &&
1056 cdi->options & CDO_AUTO_CLOSE) {
5944b2ce 1057 cd_dbg(CD_OPEN, "trying to close the tray\n");
1da177e4
LT
1058 ret=cdo->tray_move(cdi,0);
1059 if (ret) {
5944b2ce 1060 cd_dbg(CD_OPEN, "bummer. tried to close the tray but failed.\n");
1da177e4
LT
1061 /* Ignore the error from the low
1062 level driver. We don't care why it
1063 couldn't close the tray. We only care
1064 that there is no disc in the drive,
1065 since that is the _REAL_ problem here.*/
1066 ret=-ENOMEDIUM;
1067 goto clean_up_and_return;
1068 }
1069 } else {
5944b2ce 1070 cd_dbg(CD_OPEN, "bummer. this drive can't close the tray.\n");
1da177e4
LT
1071 ret=-ENOMEDIUM;
1072 goto clean_up_and_return;
1073 }
1074 /* Ok, the door should be closed now.. Check again */
1075 ret = cdo->drive_status(cdi, CDSL_CURRENT);
1076 if ((ret == CDS_NO_DISC) || (ret==CDS_TRAY_OPEN)) {
5944b2ce
JP
1077 cd_dbg(CD_OPEN, "bummer. the tray is still not closed.\n");
1078 cd_dbg(CD_OPEN, "tray might not contain a medium\n");
1da177e4
LT
1079 ret=-ENOMEDIUM;
1080 goto clean_up_and_return;
1081 }
5944b2ce 1082 cd_dbg(CD_OPEN, "the tray is now closed\n");
1da177e4
LT
1083 }
1084 /* the door should be closed now, check for the disc */
1085 ret = cdo->drive_status(cdi, CDSL_CURRENT);
1086 if (ret!=CDS_DISC_OK) {
1087 ret = -ENOMEDIUM;
1088 goto clean_up_and_return;
1089 }
1090 }
1091 cdrom_count_tracks(cdi, &tracks);
1092 if (tracks.error == CDS_NO_DISC) {
5944b2ce 1093 cd_dbg(CD_OPEN, "bummer. no disc.\n");
1da177e4
LT
1094 ret=-ENOMEDIUM;
1095 goto clean_up_and_return;
1096 }
1097 /* CD-Players which don't use O_NONBLOCK, workman
1098 * for example, need bit CDO_CHECK_TYPE cleared! */
1099 if (tracks.data==0) {
1100 if (cdi->options & CDO_CHECK_TYPE) {
1101 /* give people a warning shot, now that CDO_CHECK_TYPE
1102 is the default case! */
5944b2ce
JP
1103 cd_dbg(CD_OPEN, "bummer. wrong media type.\n");
1104 cd_dbg(CD_WARNING, "pid %d must open device O_NONBLOCK!\n",
1105 (unsigned int)task_pid_nr(current));
1da177e4
LT
1106 ret=-EMEDIUMTYPE;
1107 goto clean_up_and_return;
1108 }
1109 else {
5944b2ce 1110 cd_dbg(CD_OPEN, "wrong media type, but CDO_CHECK_TYPE not set\n");
1da177e4
LT
1111 }
1112 }
1113
5944b2ce 1114 cd_dbg(CD_OPEN, "all seems well, opening the devicen");
1da177e4
LT
1115
1116 /* all seems well, we can open the device */
1117 ret = cdo->open(cdi, 0); /* open for data */
5944b2ce 1118 cd_dbg(CD_OPEN, "opening the device gave me %d\n", ret);
1da177e4
LT
1119 /* After all this careful checking, we shouldn't have problems
1120 opening the device, but we don't want the device locked if
1121 this somehow fails... */
1122 if (ret) {
5944b2ce 1123 cd_dbg(CD_OPEN, "open device failed\n");
1da177e4
LT
1124 goto clean_up_and_return;
1125 }
1126 if (CDROM_CAN(CDC_LOCK) && (cdi->options & CDO_LOCK)) {
1127 cdo->lock_door(cdi, 1);
5944b2ce 1128 cd_dbg(CD_OPEN, "door locked\n");
1da177e4 1129 }
5944b2ce 1130 cd_dbg(CD_OPEN, "device opened successfully\n");
1da177e4
LT
1131 return ret;
1132
1133 /* Something failed. Try to unlock the drive, because some drivers
1134 (notably ide-cd) lock the drive after every command. This produced
1135 a nasty bug where after mount failed, the drive would remain locked!
1136 This ensures that the drive gets unlocked after a mount fails. This
1137 is a goto to avoid bloating the driver with redundant code. */
1138clean_up_and_return:
5944b2ce 1139 cd_dbg(CD_OPEN, "open failed\n");
1da177e4
LT
1140 if (CDROM_CAN(CDC_LOCK) && cdi->options & CDO_LOCK) {
1141 cdo->lock_door(cdi, 0);
5944b2ce 1142 cd_dbg(CD_OPEN, "door unlocked\n");
1da177e4
LT
1143 }
1144 return ret;
1145}
1146
82b91540
JP
1147/* We use the open-option O_NONBLOCK to indicate that the
1148 * purpose of opening is only for subsequent ioctl() calls; no device
1149 * integrity checks are performed.
1150 *
1151 * We hope that all cd-player programs will adopt this convention. It
1152 * is in their own interest: device control becomes a lot easier
1153 * this way.
1154 */
1155int cdrom_open(struct cdrom_device_info *cdi, struct block_device *bdev,
1156 fmode_t mode)
1157{
1158 int ret;
1159
1160 cd_dbg(CD_OPEN, "entering cdrom_open\n");
1161
82b91540
JP
1162 /* if this was a O_NONBLOCK open and we should honor the flags,
1163 * do a quick open without drive/disc integrity checks. */
1164 cdi->use_count++;
1165 if ((mode & FMODE_NDELAY) && (cdi->options & CDO_USE_FFLAGS)) {
1166 ret = cdi->ops->open(cdi, 1);
1167 } else {
1168 ret = open_for_data(cdi);
1169 if (ret)
1170 goto err;
366ba7c7
DEP
1171 if (CDROM_CAN(CDC_GENERIC_PACKET))
1172 cdrom_mmc3_profile(cdi);
82b91540
JP
1173 if (mode & FMODE_WRITE) {
1174 ret = -EROFS;
1175 if (cdrom_open_write(cdi))
1176 goto err_release;
1177 if (!CDROM_CAN(CDC_RAM))
1178 goto err_release;
1179 ret = 0;
1180 cdi->media_written = 0;
1181 }
1182 }
1183
1184 if (ret)
1185 goto err;
1186
1187 cd_dbg(CD_OPEN, "Use count for \"/dev/%s\" now %d\n",
1188 cdi->name, cdi->use_count);
1189 return 0;
1190err_release:
1191 if (CDROM_CAN(CDC_LOCK) && cdi->options & CDO_LOCK) {
1192 cdi->ops->lock_door(cdi, 0);
1193 cd_dbg(CD_OPEN, "door unlocked\n");
1194 }
1195 cdi->ops->release(cdi);
1196err:
1197 cdi->use_count--;
1198 return ret;
1199}
1200
1da177e4
LT
1201/* This code is similar to that in open_for_data. The routine is called
1202 whenever an audio play operation is requested.
1203*/
853fe1bf
KC
1204static int check_for_audio_disc(struct cdrom_device_info *cdi,
1205 const struct cdrom_device_ops *cdo)
1da177e4
LT
1206{
1207 int ret;
1208 tracktype tracks;
5944b2ce 1209 cd_dbg(CD_OPEN, "entering check_for_audio_disc\n");
1da177e4
LT
1210 if (!(cdi->options & CDO_CHECK_TYPE))
1211 return 0;
1212 if (cdo->drive_status != NULL) {
1213 ret = cdo->drive_status(cdi, CDSL_CURRENT);
5944b2ce 1214 cd_dbg(CD_OPEN, "drive_status=%d\n", ret);
1da177e4 1215 if (ret == CDS_TRAY_OPEN) {
5944b2ce 1216 cd_dbg(CD_OPEN, "the tray is open...\n");
1da177e4
LT
1217 /* can/may i close it? */
1218 if (CDROM_CAN(CDC_CLOSE_TRAY) &&
1219 cdi->options & CDO_AUTO_CLOSE) {
5944b2ce 1220 cd_dbg(CD_OPEN, "trying to close the tray\n");
1da177e4
LT
1221 ret=cdo->tray_move(cdi,0);
1222 if (ret) {
5944b2ce 1223 cd_dbg(CD_OPEN, "bummer. tried to close tray but failed.\n");
1da177e4
LT
1224 /* Ignore the error from the low
1225 level driver. We don't care why it
1226 couldn't close the tray. We only care
1227 that there is no disc in the drive,
1228 since that is the _REAL_ problem here.*/
1229 return -ENOMEDIUM;
1230 }
1231 } else {
5944b2ce 1232 cd_dbg(CD_OPEN, "bummer. this driver can't close the tray.\n");
1da177e4
LT
1233 return -ENOMEDIUM;
1234 }
1235 /* Ok, the door should be closed now.. Check again */
1236 ret = cdo->drive_status(cdi, CDSL_CURRENT);
1237 if ((ret == CDS_NO_DISC) || (ret==CDS_TRAY_OPEN)) {
5944b2ce 1238 cd_dbg(CD_OPEN, "bummer. the tray is still not closed.\n");
1da177e4
LT
1239 return -ENOMEDIUM;
1240 }
1241 if (ret!=CDS_DISC_OK) {
5944b2ce 1242 cd_dbg(CD_OPEN, "bummer. disc isn't ready.\n");
1da177e4
LT
1243 return -EIO;
1244 }
5944b2ce 1245 cd_dbg(CD_OPEN, "the tray is now closed\n");
1da177e4
LT
1246 }
1247 }
1248 cdrom_count_tracks(cdi, &tracks);
1249 if (tracks.error)
1250 return(tracks.error);
1251
1252 if (tracks.audio==0)
1253 return -EMEDIUMTYPE;
1254
1255 return 0;
1256}
1257
bbc1cc97 1258void cdrom_release(struct cdrom_device_info *cdi, fmode_t mode)
1da177e4 1259{
853fe1bf 1260 const struct cdrom_device_ops *cdo = cdi->ops;
1da177e4
LT
1261 int opened_for_data;
1262
5944b2ce 1263 cd_dbg(CD_CLOSE, "entering cdrom_release\n");
1da177e4
LT
1264
1265 if (cdi->use_count > 0)
1266 cdi->use_count--;
35841f70
BP
1267
1268 if (cdi->use_count == 0) {
5944b2ce
JP
1269 cd_dbg(CD_CLOSE, "Use count for \"/dev/%s\" now zero\n",
1270 cdi->name);
1da177e4 1271 cdrom_dvd_rw_close_write(cdi);
35841f70 1272
cdccaa94 1273 if ((cdo->capability & CDC_LOCK) && !cdi->keeplocked) {
5944b2ce 1274 cd_dbg(CD_CLOSE, "Unlocking door!\n");
35841f70
BP
1275 cdo->lock_door(cdi, 0);
1276 }
1da177e4 1277 }
35841f70 1278
1da177e4 1279 opened_for_data = !(cdi->options & CDO_USE_FFLAGS) ||
bbc1cc97 1280 !(mode & FMODE_NDELAY);
1da177e4
LT
1281
1282 /*
1283 * flush cache on last write release
1284 */
1285 if (CDROM_CAN(CDC_RAM) && !cdi->use_count && cdi->for_data)
1286 cdrom_close_write(cdi);
1287
1288 cdo->release(cdi);
1289 if (cdi->use_count == 0) { /* last process that closes dev*/
1290 if (opened_for_data &&
1291 cdi->options & CDO_AUTO_EJECT && CDROM_CAN(CDC_OPEN_TRAY))
1292 cdo->tray_move(cdi, 1);
1293 }
1da177e4
LT
1294}
1295
1296static int cdrom_read_mech_status(struct cdrom_device_info *cdi,
1297 struct cdrom_changer_info *buf)
1298{
1299 struct packet_command cgc;
853fe1bf 1300 const struct cdrom_device_ops *cdo = cdi->ops;
1da177e4
LT
1301 int length;
1302
1303 /*
1304 * Sanyo changer isn't spec compliant (doesn't use regular change
1305 * LOAD_UNLOAD command, and it doesn't implement the mech status
1306 * command below
1307 */
1308 if (cdi->sanyo_slot) {
1309 buf->hdr.nslots = 3;
1310 buf->hdr.curslot = cdi->sanyo_slot == 3 ? 0 : cdi->sanyo_slot;
1311 for (length = 0; length < 3; length++) {
1312 buf->slots[length].disc_present = 1;
1313 buf->slots[length].change = 0;
1314 }
1315 return 0;
1316 }
1317
1318 length = sizeof(struct cdrom_mechstat_header) +
1319 cdi->capacity * sizeof(struct cdrom_slot);
1320
1321 init_cdrom_command(&cgc, buf, length, CGC_DATA_READ);
1322 cgc.cmd[0] = GPCMD_MECHANISM_STATUS;
1323 cgc.cmd[8] = (length >> 8) & 0xff;
1324 cgc.cmd[9] = length & 0xff;
1325 return cdo->generic_packet(cdi, &cgc);
1326}
1327
1328static int cdrom_slot_status(struct cdrom_device_info *cdi, int slot)
1329{
1330 struct cdrom_changer_info *info;
1331 int ret;
1332
5944b2ce 1333 cd_dbg(CD_CHANGER, "entering cdrom_slot_status()\n");
1da177e4
LT
1334 if (cdi->sanyo_slot)
1335 return CDS_NO_INFO;
1336
1337 info = kmalloc(sizeof(*info), GFP_KERNEL);
1338 if (!info)
1339 return -ENOMEM;
1340
1341 if ((ret = cdrom_read_mech_status(cdi, info)))
1342 goto out_free;
1343
1344 if (info->slots[slot].disc_present)
1345 ret = CDS_DISC_OK;
1346 else
1347 ret = CDS_NO_DISC;
1348
1349out_free:
1350 kfree(info);
1351 return ret;
1352}
1353
1354/* Return the number of slots for an ATAPI/SCSI cdrom,
1355 * return 1 if not a changer.
1356 */
1357int cdrom_number_of_slots(struct cdrom_device_info *cdi)
1358{
1359 int status;
1360 int nslots = 1;
1361 struct cdrom_changer_info *info;
1362
5944b2ce 1363 cd_dbg(CD_CHANGER, "entering cdrom_number_of_slots()\n");
1da177e4
LT
1364 /* cdrom_read_mech_status requires a valid value for capacity: */
1365 cdi->capacity = 0;
1366
1367 info = kmalloc(sizeof(*info), GFP_KERNEL);
1368 if (!info)
1369 return -ENOMEM;
1370
1371 if ((status = cdrom_read_mech_status(cdi, info)) == 0)
1372 nslots = info->hdr.nslots;
1373
1374 kfree(info);
1375 return nslots;
1376}
1377
1378
1379/* If SLOT < 0, unload the current slot. Otherwise, try to load SLOT. */
1380static int cdrom_load_unload(struct cdrom_device_info *cdi, int slot)
1381{
1382 struct packet_command cgc;
1383
5944b2ce 1384 cd_dbg(CD_CHANGER, "entering cdrom_load_unload()\n");
1da177e4
LT
1385 if (cdi->sanyo_slot && slot < 0)
1386 return 0;
1387
1388 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
1389 cgc.cmd[0] = GPCMD_LOAD_UNLOAD;
1390 cgc.cmd[4] = 2 + (slot >= 0);
1391 cgc.cmd[8] = slot;
1392 cgc.timeout = 60 * HZ;
1393
1394 /* The Sanyo 3 CD changer uses byte 7 of the
1395 GPCMD_TEST_UNIT_READY to command to switch CDs instead of
1396 using the GPCMD_LOAD_UNLOAD opcode. */
1397 if (cdi->sanyo_slot && -1 < slot) {
1398 cgc.cmd[0] = GPCMD_TEST_UNIT_READY;
1399 cgc.cmd[7] = slot;
1400 cgc.cmd[4] = cgc.cmd[8] = 0;
1401 cdi->sanyo_slot = slot ? slot : 3;
1402 }
1403
1404 return cdi->ops->generic_packet(cdi, &cgc);
1405}
1406
1407static int cdrom_select_disc(struct cdrom_device_info *cdi, int slot)
1408{
1409 struct cdrom_changer_info *info;
1410 int curslot;
1411 int ret;
1412
5944b2ce 1413 cd_dbg(CD_CHANGER, "entering cdrom_select_disc()\n");
1da177e4
LT
1414 if (!CDROM_CAN(CDC_SELECT_DISC))
1415 return -EDRIVE_CANT_DO_THIS;
1416
2d921729
TH
1417 if (cdi->ops->check_events)
1418 cdi->ops->check_events(cdi, 0, slot);
1419 else
1420 cdi->ops->media_changed(cdi, slot);
1da177e4
LT
1421
1422 if (slot == CDSL_NONE) {
1423 /* set media changed bits, on both queues */
1424 cdi->mc_flags = 0x3;
1425 return cdrom_load_unload(cdi, -1);
1426 }
1427
1428 info = kmalloc(sizeof(*info), GFP_KERNEL);
1429 if (!info)
1430 return -ENOMEM;
1431
1432 if ((ret = cdrom_read_mech_status(cdi, info))) {
1433 kfree(info);
1434 return ret;
1435 }
1436
1437 curslot = info->hdr.curslot;
1438 kfree(info);
1439
cdccaa94 1440 if (cdi->use_count > 1 || cdi->keeplocked) {
1da177e4
LT
1441 if (slot == CDSL_CURRENT) {
1442 return curslot;
1443 } else {
1444 return -EBUSY;
1445 }
1446 }
1447
1448 /* Specifying CDSL_CURRENT will attempt to load the currnet slot,
1449 which is useful if it had been previously unloaded.
1450 Whether it can or not, it returns the current slot.
1451 Similarly, if slot happens to be the current one, we still
1452 try and load it. */
1453 if (slot == CDSL_CURRENT)
1454 slot = curslot;
1455
1456 /* set media changed bits on both queues */
1457 cdi->mc_flags = 0x3;
1458 if ((ret = cdrom_load_unload(cdi, slot)))
1459 return ret;
1460
1461 return slot;
1462}
1463
2d921729
TH
1464/*
1465 * As cdrom implements an extra ioctl consumer for media changed
1466 * event, it needs to buffer ->check_events() output, such that event
1467 * is not lost for both the usual VFS and ioctl paths.
1468 * cdi->{vfs|ioctl}_events are used to buffer pending events for each
1469 * path.
1470 *
1471 * XXX: Locking is non-existent. cdi->ops->check_events() can be
1472 * called in parallel and buffering fields are accessed without any
1473 * exclusion. The original media_changed code had the same problem.
1474 * It might be better to simply deprecate CDROM_MEDIA_CHANGED ioctl
1475 * and remove this cruft altogether. It doesn't have much usefulness
1476 * at this point.
1477 */
1478static void cdrom_update_events(struct cdrom_device_info *cdi,
1479 unsigned int clearing)
1480{
1481 unsigned int events;
1482
1483 events = cdi->ops->check_events(cdi, clearing, CDSL_CURRENT);
1484 cdi->vfs_events |= events;
1485 cdi->ioctl_events |= events;
1486}
1487
1488unsigned int cdrom_check_events(struct cdrom_device_info *cdi,
1489 unsigned int clearing)
1490{
1491 unsigned int events;
1492
1493 cdrom_update_events(cdi, clearing);
1494 events = cdi->vfs_events;
1495 cdi->vfs_events = 0;
1496 return events;
1497}
30a7caab 1498EXPORT_SYMBOL(cdrom_check_events);
2d921729 1499
1da177e4
LT
1500/* We want to make media_changed accessible to the user through an
1501 * ioctl. The main problem now is that we must double-buffer the
1502 * low-level implementation, to assure that the VFS and the user both
1503 * see a medium change once.
1504 */
1505
1506static
1507int media_changed(struct cdrom_device_info *cdi, int queue)
1508{
1509 unsigned int mask = (1 << (queue & 1));
1510 int ret = !!(cdi->mc_flags & mask);
2d921729 1511 bool changed;
1da177e4
LT
1512
1513 if (!CDROM_CAN(CDC_MEDIA_CHANGED))
2d921729
TH
1514 return ret;
1515
1da177e4 1516 /* changed since last call? */
2d921729
TH
1517 if (cdi->ops->check_events) {
1518 BUG_ON(!queue); /* shouldn't be called from VFS path */
1519 cdrom_update_events(cdi, DISK_EVENT_MEDIA_CHANGE);
1520 changed = cdi->ioctl_events & DISK_EVENT_MEDIA_CHANGE;
1521 cdi->ioctl_events = 0;
1522 } else
1523 changed = cdi->ops->media_changed(cdi, CDSL_CURRENT);
1524
1525 if (changed) {
1da177e4
LT
1526 cdi->mc_flags = 0x3; /* set bit on both queues */
1527 ret |= 1;
1528 cdi->media_written = 0;
1529 }
2d921729 1530
1da177e4
LT
1531 cdi->mc_flags &= ~mask; /* clear bit */
1532 return ret;
1533}
1534
1535int cdrom_media_changed(struct cdrom_device_info *cdi)
1536{
1537 /* This talks to the VFS, which doesn't like errors - just 1 or 0.
1538 * Returning "0" is always safe (media hasn't been changed). Do that
1539 * if the low-level cdrom driver dosn't support media changed. */
1540 if (cdi == NULL || cdi->ops->media_changed == NULL)
1541 return 0;
1542 if (!CDROM_CAN(CDC_MEDIA_CHANGED))
1543 return 0;
1544 return media_changed(cdi, 0);
1545}
1546
1da177e4
LT
1547/* Requests to the low-level drivers will /always/ be done in the
1548 following format convention:
1549
1550 CDROM_LBA: all data-related requests.
1551 CDROM_MSF: all audio-related requests.
1552
1553 However, a low-level implementation is allowed to refuse this
1554 request, and return information in its own favorite format.
1555
1556 It doesn't make sense /at all/ to ask for a play_audio in LBA
1557 format, or ask for multi-session info in MSF format. However, for
1558 backward compatibility these format requests will be satisfied, but
1559 the requests to the low-level drivers will be sanitized in the more
1560 meaningful format indicated above.
1561 */
1562
1563static
1564void sanitize_format(union cdrom_addr *addr,
1565 u_char * curr, u_char requested)
1566{
1567 if (*curr == requested)
1568 return; /* nothing to be done! */
1569 if (requested == CDROM_LBA) {
1570 addr->lba = (int) addr->msf.frame +
1571 75 * (addr->msf.second - 2 + 60 * addr->msf.minute);
1572 } else { /* CDROM_MSF */
1573 int lba = addr->lba;
1574 addr->msf.frame = lba % 75;
1575 lba /= 75;
1576 lba += 2;
1577 addr->msf.second = lba % 60;
1578 addr->msf.minute = lba / 60;
1579 }
1580 *curr = requested;
1581}
1582
1583void init_cdrom_command(struct packet_command *cgc, void *buf, int len,
1584 int type)
1585{
1586 memset(cgc, 0, sizeof(struct packet_command));
1587 if (buf)
1588 memset(buf, 0, len);
1589 cgc->buffer = (char *) buf;
1590 cgc->buflen = len;
1591 cgc->data_direction = type;
2e11c207 1592 cgc->timeout = CDROM_DEF_TIMEOUT;
1da177e4
LT
1593}
1594
1595/* DVD handling */
1596
1597#define copy_key(dest,src) memcpy((dest), (src), sizeof(dvd_key))
1598#define copy_chal(dest,src) memcpy((dest), (src), sizeof(dvd_challenge))
1599
1600static void setup_report_key(struct packet_command *cgc, unsigned agid, unsigned type)
1601{
1602 cgc->cmd[0] = GPCMD_REPORT_KEY;
1603 cgc->cmd[10] = type | (agid << 6);
1604 switch (type) {
1605 case 0: case 8: case 5: {
1606 cgc->buflen = 8;
1607 break;
1608 }
1609 case 1: {
1610 cgc->buflen = 16;
1611 break;
1612 }
1613 case 2: case 4: {
1614 cgc->buflen = 12;
1615 break;
1616 }
1617 }
1618 cgc->cmd[9] = cgc->buflen;
1619 cgc->data_direction = CGC_DATA_READ;
1620}
1621
1622static void setup_send_key(struct packet_command *cgc, unsigned agid, unsigned type)
1623{
1624 cgc->cmd[0] = GPCMD_SEND_KEY;
1625 cgc->cmd[10] = type | (agid << 6);
1626 switch (type) {
1627 case 1: {
1628 cgc->buflen = 16;
1629 break;
1630 }
1631 case 3: {
1632 cgc->buflen = 12;
1633 break;
1634 }
1635 case 6: {
1636 cgc->buflen = 8;
1637 break;
1638 }
1639 }
1640 cgc->cmd[9] = cgc->buflen;
1641 cgc->data_direction = CGC_DATA_WRITE;
1642}
1643
1644static int dvd_do_auth(struct cdrom_device_info *cdi, dvd_authinfo *ai)
1645{
1646 int ret;
fce53847 1647 u_char buf[20];
1da177e4 1648 struct packet_command cgc;
853fe1bf 1649 const struct cdrom_device_ops *cdo = cdi->ops;
fce53847 1650 rpc_state_t rpc_state;
1da177e4 1651
fce53847 1652 memset(buf, 0, sizeof(buf));
1da177e4
LT
1653 init_cdrom_command(&cgc, buf, 0, CGC_DATA_READ);
1654
1655 switch (ai->type) {
1656 /* LU data send */
1657 case DVD_LU_SEND_AGID:
5944b2ce 1658 cd_dbg(CD_DVD, "entering DVD_LU_SEND_AGID\n");
1da177e4
LT
1659 cgc.quiet = 1;
1660 setup_report_key(&cgc, ai->lsa.agid, 0);
1661
1662 if ((ret = cdo->generic_packet(cdi, &cgc)))
fce53847 1663 return ret;
1da177e4
LT
1664
1665 ai->lsa.agid = buf[7] >> 6;
1666 /* Returning data, let host change state */
1667 break;
1668
1669 case DVD_LU_SEND_KEY1:
5944b2ce 1670 cd_dbg(CD_DVD, "entering DVD_LU_SEND_KEY1\n");
1da177e4
LT
1671 setup_report_key(&cgc, ai->lsk.agid, 2);
1672
1673 if ((ret = cdo->generic_packet(cdi, &cgc)))
fce53847 1674 return ret;
1da177e4
LT
1675
1676 copy_key(ai->lsk.key, &buf[4]);
1677 /* Returning data, let host change state */
1678 break;
1679
1680 case DVD_LU_SEND_CHALLENGE:
5944b2ce 1681 cd_dbg(CD_DVD, "entering DVD_LU_SEND_CHALLENGE\n");
1da177e4
LT
1682 setup_report_key(&cgc, ai->lsc.agid, 1);
1683
1684 if ((ret = cdo->generic_packet(cdi, &cgc)))
fce53847 1685 return ret;
1da177e4
LT
1686
1687 copy_chal(ai->lsc.chal, &buf[4]);
1688 /* Returning data, let host change state */
1689 break;
1690
1691 /* Post-auth key */
1692 case DVD_LU_SEND_TITLE_KEY:
5944b2ce 1693 cd_dbg(CD_DVD, "entering DVD_LU_SEND_TITLE_KEY\n");
1da177e4
LT
1694 cgc.quiet = 1;
1695 setup_report_key(&cgc, ai->lstk.agid, 4);
1696 cgc.cmd[5] = ai->lstk.lba;
1697 cgc.cmd[4] = ai->lstk.lba >> 8;
1698 cgc.cmd[3] = ai->lstk.lba >> 16;
1699 cgc.cmd[2] = ai->lstk.lba >> 24;
1700
1701 if ((ret = cdo->generic_packet(cdi, &cgc)))
fce53847 1702 return ret;
1da177e4
LT
1703
1704 ai->lstk.cpm = (buf[4] >> 7) & 1;
1705 ai->lstk.cp_sec = (buf[4] >> 6) & 1;
1706 ai->lstk.cgms = (buf[4] >> 4) & 3;
1707 copy_key(ai->lstk.title_key, &buf[5]);
1708 /* Returning data, let host change state */
1709 break;
1710
1711 case DVD_LU_SEND_ASF:
5944b2ce 1712 cd_dbg(CD_DVD, "entering DVD_LU_SEND_ASF\n");
1da177e4
LT
1713 setup_report_key(&cgc, ai->lsasf.agid, 5);
1714
1715 if ((ret = cdo->generic_packet(cdi, &cgc)))
fce53847 1716 return ret;
1da177e4
LT
1717
1718 ai->lsasf.asf = buf[7] & 1;
1719 break;
1720
1721 /* LU data receive (LU changes state) */
1722 case DVD_HOST_SEND_CHALLENGE:
5944b2ce 1723 cd_dbg(CD_DVD, "entering DVD_HOST_SEND_CHALLENGE\n");
1da177e4
LT
1724 setup_send_key(&cgc, ai->hsc.agid, 1);
1725 buf[1] = 0xe;
1726 copy_chal(&buf[4], ai->hsc.chal);
1727
1728 if ((ret = cdo->generic_packet(cdi, &cgc)))
fce53847 1729 return ret;
1da177e4
LT
1730
1731 ai->type = DVD_LU_SEND_KEY1;
1732 break;
1733
1734 case DVD_HOST_SEND_KEY2:
5944b2ce 1735 cd_dbg(CD_DVD, "entering DVD_HOST_SEND_KEY2\n");
1da177e4
LT
1736 setup_send_key(&cgc, ai->hsk.agid, 3);
1737 buf[1] = 0xa;
1738 copy_key(&buf[4], ai->hsk.key);
1739
1740 if ((ret = cdo->generic_packet(cdi, &cgc))) {
1741 ai->type = DVD_AUTH_FAILURE;
fce53847 1742 return ret;
1da177e4
LT
1743 }
1744 ai->type = DVD_AUTH_ESTABLISHED;
1745 break;
1746
1747 /* Misc */
1748 case DVD_INVALIDATE_AGID:
1749 cgc.quiet = 1;
5944b2ce 1750 cd_dbg(CD_DVD, "entering DVD_INVALIDATE_AGID\n");
1da177e4
LT
1751 setup_report_key(&cgc, ai->lsa.agid, 0x3f);
1752 if ((ret = cdo->generic_packet(cdi, &cgc)))
fce53847 1753 return ret;
1da177e4
LT
1754 break;
1755
1756 /* Get region settings */
1757 case DVD_LU_SEND_RPC_STATE:
5944b2ce 1758 cd_dbg(CD_DVD, "entering DVD_LU_SEND_RPC_STATE\n");
1da177e4 1759 setup_report_key(&cgc, 0, 8);
fce53847
FT
1760 memset(&rpc_state, 0, sizeof(rpc_state_t));
1761 cgc.buffer = (char *) &rpc_state;
1da177e4
LT
1762
1763 if ((ret = cdo->generic_packet(cdi, &cgc)))
fce53847 1764 return ret;
1da177e4 1765
fce53847
FT
1766 ai->lrpcs.type = rpc_state.type_code;
1767 ai->lrpcs.vra = rpc_state.vra;
1768 ai->lrpcs.ucca = rpc_state.ucca;
1769 ai->lrpcs.region_mask = rpc_state.region_mask;
1770 ai->lrpcs.rpc_scheme = rpc_state.rpc_scheme;
1da177e4
LT
1771 break;
1772
1773 /* Set region settings */
1774 case DVD_HOST_SEND_RPC_STATE:
5944b2ce 1775 cd_dbg(CD_DVD, "entering DVD_HOST_SEND_RPC_STATE\n");
1da177e4
LT
1776 setup_send_key(&cgc, 0, 6);
1777 buf[1] = 6;
1778 buf[4] = ai->hrpcs.pdrc;
1779
1780 if ((ret = cdo->generic_packet(cdi, &cgc)))
fce53847 1781 return ret;
1da177e4
LT
1782 break;
1783
1784 default:
5944b2ce 1785 cd_dbg(CD_WARNING, "Invalid DVD key ioctl (%d)\n", ai->type);
fce53847 1786 return -ENOTTY;
1da177e4 1787 }
fce53847
FT
1788
1789 return 0;
1da177e4
LT
1790}
1791
d194139c
MS
1792static int dvd_read_physical(struct cdrom_device_info *cdi, dvd_struct *s,
1793 struct packet_command *cgc)
1da177e4 1794{
fce53847 1795 unsigned char buf[21], *base;
1da177e4 1796 struct dvd_layer *layer;
853fe1bf 1797 const struct cdrom_device_ops *cdo = cdi->ops;
1da177e4
LT
1798 int ret, layer_num = s->physical.layer_num;
1799
1800 if (layer_num >= DVD_LAYERS)
1801 return -EINVAL;
1802
d194139c
MS
1803 init_cdrom_command(cgc, buf, sizeof(buf), CGC_DATA_READ);
1804 cgc->cmd[0] = GPCMD_READ_DVD_STRUCTURE;
1805 cgc->cmd[6] = layer_num;
1806 cgc->cmd[7] = s->type;
1807 cgc->cmd[9] = cgc->buflen & 0xff;
1da177e4
LT
1808
1809 /*
1810 * refrain from reporting errors on non-existing layers (mainly)
1811 */
d194139c 1812 cgc->quiet = 1;
1da177e4 1813
d194139c
MS
1814 ret = cdo->generic_packet(cdi, cgc);
1815 if (ret)
fce53847 1816 return ret;
1da177e4
LT
1817
1818 base = &buf[4];
1819 layer = &s->physical.layer[layer_num];
1820
1821 /*
1822 * place the data... really ugly, but at least we won't have to
1823 * worry about endianess in userspace.
1824 */
1825 memset(layer, 0, sizeof(*layer));
1826 layer->book_version = base[0] & 0xf;
1827 layer->book_type = base[0] >> 4;
1828 layer->min_rate = base[1] & 0xf;
1829 layer->disc_size = base[1] >> 4;
1830 layer->layer_type = base[2] & 0xf;
1831 layer->track_path = (base[2] >> 4) & 1;
1832 layer->nlayers = (base[2] >> 5) & 3;
1833 layer->track_density = base[3] & 0xf;
1834 layer->linear_density = base[3] >> 4;
1835 layer->start_sector = base[5] << 16 | base[6] << 8 | base[7];
1836 layer->end_sector = base[9] << 16 | base[10] << 8 | base[11];
1837 layer->end_sector_l0 = base[13] << 16 | base[14] << 8 | base[15];
1838 layer->bca = base[16] >> 7;
1839
fce53847 1840 return 0;
1da177e4
LT
1841}
1842
d194139c
MS
1843static int dvd_read_copyright(struct cdrom_device_info *cdi, dvd_struct *s,
1844 struct packet_command *cgc)
1da177e4
LT
1845{
1846 int ret;
fce53847 1847 u_char buf[8];
853fe1bf 1848 const struct cdrom_device_ops *cdo = cdi->ops;
1da177e4 1849
d194139c
MS
1850 init_cdrom_command(cgc, buf, sizeof(buf), CGC_DATA_READ);
1851 cgc->cmd[0] = GPCMD_READ_DVD_STRUCTURE;
1852 cgc->cmd[6] = s->copyright.layer_num;
1853 cgc->cmd[7] = s->type;
1854 cgc->cmd[8] = cgc->buflen >> 8;
1855 cgc->cmd[9] = cgc->buflen & 0xff;
1da177e4 1856
d194139c
MS
1857 ret = cdo->generic_packet(cdi, cgc);
1858 if (ret)
fce53847 1859 return ret;
1da177e4
LT
1860
1861 s->copyright.cpst = buf[4];
1862 s->copyright.rmi = buf[5];
1863
fce53847 1864 return 0;
1da177e4
LT
1865}
1866
d194139c
MS
1867static int dvd_read_disckey(struct cdrom_device_info *cdi, dvd_struct *s,
1868 struct packet_command *cgc)
1da177e4
LT
1869{
1870 int ret, size;
1871 u_char *buf;
853fe1bf 1872 const struct cdrom_device_ops *cdo = cdi->ops;
1da177e4
LT
1873
1874 size = sizeof(s->disckey.value) + 4;
1875
d194139c
MS
1876 buf = kmalloc(size, GFP_KERNEL);
1877 if (!buf)
1da177e4
LT
1878 return -ENOMEM;
1879
d194139c
MS
1880 init_cdrom_command(cgc, buf, size, CGC_DATA_READ);
1881 cgc->cmd[0] = GPCMD_READ_DVD_STRUCTURE;
1882 cgc->cmd[7] = s->type;
1883 cgc->cmd[8] = size >> 8;
1884 cgc->cmd[9] = size & 0xff;
1885 cgc->cmd[10] = s->disckey.agid << 6;
1da177e4 1886
d194139c
MS
1887 ret = cdo->generic_packet(cdi, cgc);
1888 if (!ret)
1da177e4
LT
1889 memcpy(s->disckey.value, &buf[4], sizeof(s->disckey.value));
1890
1891 kfree(buf);
1892 return ret;
1893}
1894
d194139c
MS
1895static int dvd_read_bca(struct cdrom_device_info *cdi, dvd_struct *s,
1896 struct packet_command *cgc)
1da177e4 1897{
d194139c
MS
1898 int ret, size = 4 + 188;
1899 u_char *buf;
853fe1bf 1900 const struct cdrom_device_ops *cdo = cdi->ops;
1da177e4 1901
d194139c
MS
1902 buf = kmalloc(size, GFP_KERNEL);
1903 if (!buf)
1904 return -ENOMEM;
1da177e4 1905
d194139c
MS
1906 init_cdrom_command(cgc, buf, size, CGC_DATA_READ);
1907 cgc->cmd[0] = GPCMD_READ_DVD_STRUCTURE;
1908 cgc->cmd[7] = s->type;
1909 cgc->cmd[9] = cgc->buflen & 0xff;
1910
1911 ret = cdo->generic_packet(cdi, cgc);
1912 if (ret)
1913 goto out;
1da177e4
LT
1914
1915 s->bca.len = buf[0] << 8 | buf[1];
1916 if (s->bca.len < 12 || s->bca.len > 188) {
5944b2ce
JP
1917 cd_dbg(CD_WARNING, "Received invalid BCA length (%d)\n",
1918 s->bca.len);
d194139c
MS
1919 ret = -EIO;
1920 goto out;
1da177e4
LT
1921 }
1922 memcpy(s->bca.value, &buf[4], s->bca.len);
d194139c
MS
1923 ret = 0;
1924out:
1925 kfree(buf);
1926 return ret;
1da177e4
LT
1927}
1928
d194139c
MS
1929static int dvd_read_manufact(struct cdrom_device_info *cdi, dvd_struct *s,
1930 struct packet_command *cgc)
1da177e4
LT
1931{
1932 int ret = 0, size;
1933 u_char *buf;
853fe1bf 1934 const struct cdrom_device_ops *cdo = cdi->ops;
1da177e4
LT
1935
1936 size = sizeof(s->manufact.value) + 4;
1937
d194139c
MS
1938 buf = kmalloc(size, GFP_KERNEL);
1939 if (!buf)
1da177e4
LT
1940 return -ENOMEM;
1941
d194139c
MS
1942 init_cdrom_command(cgc, buf, size, CGC_DATA_READ);
1943 cgc->cmd[0] = GPCMD_READ_DVD_STRUCTURE;
1944 cgc->cmd[7] = s->type;
1945 cgc->cmd[8] = size >> 8;
1946 cgc->cmd[9] = size & 0xff;
1da177e4 1947
d194139c
MS
1948 ret = cdo->generic_packet(cdi, cgc);
1949 if (ret)
1950 goto out;
1da177e4
LT
1951
1952 s->manufact.len = buf[0] << 8 | buf[1];
aec9f377 1953 if (s->manufact.len < 0) {
5944b2ce
JP
1954 cd_dbg(CD_WARNING, "Received invalid manufacture info length (%d)\n",
1955 s->manufact.len);
1da177e4
LT
1956 ret = -EIO;
1957 } else {
aec9f377 1958 if (s->manufact.len > 2048) {
5944b2ce
JP
1959 cd_dbg(CD_WARNING, "Received invalid manufacture info length (%d): truncating to 2048\n",
1960 s->manufact.len);
aec9f377
AM
1961 s->manufact.len = 2048;
1962 }
1da177e4
LT
1963 memcpy(s->manufact.value, &buf[4], s->manufact.len);
1964 }
1965
d194139c 1966out:
1da177e4
LT
1967 kfree(buf);
1968 return ret;
1969}
1970
d194139c
MS
1971static int dvd_read_struct(struct cdrom_device_info *cdi, dvd_struct *s,
1972 struct packet_command *cgc)
1da177e4
LT
1973{
1974 switch (s->type) {
1975 case DVD_STRUCT_PHYSICAL:
d194139c 1976 return dvd_read_physical(cdi, s, cgc);
1da177e4
LT
1977
1978 case DVD_STRUCT_COPYRIGHT:
d194139c 1979 return dvd_read_copyright(cdi, s, cgc);
1da177e4
LT
1980
1981 case DVD_STRUCT_DISCKEY:
d194139c 1982 return dvd_read_disckey(cdi, s, cgc);
1da177e4
LT
1983
1984 case DVD_STRUCT_BCA:
d194139c 1985 return dvd_read_bca(cdi, s, cgc);
1da177e4
LT
1986
1987 case DVD_STRUCT_MANUFACT:
d194139c 1988 return dvd_read_manufact(cdi, s, cgc);
1da177e4
LT
1989
1990 default:
5944b2ce
JP
1991 cd_dbg(CD_WARNING, ": Invalid DVD structure read requested (%d)\n",
1992 s->type);
1da177e4
LT
1993 return -EINVAL;
1994 }
1995}
1996
1997int cdrom_mode_sense(struct cdrom_device_info *cdi,
1998 struct packet_command *cgc,
1999 int page_code, int page_control)
2000{
853fe1bf 2001 const struct cdrom_device_ops *cdo = cdi->ops;
1da177e4
LT
2002
2003 memset(cgc->cmd, 0, sizeof(cgc->cmd));
2004
2005 cgc->cmd[0] = GPCMD_MODE_SENSE_10;
2006 cgc->cmd[2] = page_code | (page_control << 6);
2007 cgc->cmd[7] = cgc->buflen >> 8;
2008 cgc->cmd[8] = cgc->buflen & 0xff;
2009 cgc->data_direction = CGC_DATA_READ;
2010 return cdo->generic_packet(cdi, cgc);
2011}
2012
2013int cdrom_mode_select(struct cdrom_device_info *cdi,
2014 struct packet_command *cgc)
2015{
853fe1bf 2016 const struct cdrom_device_ops *cdo = cdi->ops;
1da177e4
LT
2017
2018 memset(cgc->cmd, 0, sizeof(cgc->cmd));
2019 memset(cgc->buffer, 0, 2);
2020 cgc->cmd[0] = GPCMD_MODE_SELECT_10;
2021 cgc->cmd[1] = 0x10; /* PF */
2022 cgc->cmd[7] = cgc->buflen >> 8;
2023 cgc->cmd[8] = cgc->buflen & 0xff;
2024 cgc->data_direction = CGC_DATA_WRITE;
2025 return cdo->generic_packet(cdi, cgc);
2026}
2027
2028static int cdrom_read_subchannel(struct cdrom_device_info *cdi,
2029 struct cdrom_subchnl *subchnl, int mcn)
2030{
853fe1bf 2031 const struct cdrom_device_ops *cdo = cdi->ops;
1da177e4 2032 struct packet_command cgc;
fce53847 2033 char buffer[32];
1da177e4
LT
2034 int ret;
2035
2036 init_cdrom_command(&cgc, buffer, 16, CGC_DATA_READ);
2037 cgc.cmd[0] = GPCMD_READ_SUBCHANNEL;
bf294e83 2038 cgc.cmd[1] = subchnl->cdsc_format;/* MSF or LBA addressing */
1da177e4
LT
2039 cgc.cmd[2] = 0x40; /* request subQ data */
2040 cgc.cmd[3] = mcn ? 2 : 1;
2041 cgc.cmd[8] = 16;
2042
2043 if ((ret = cdo->generic_packet(cdi, &cgc)))
fce53847 2044 return ret;
1da177e4
LT
2045
2046 subchnl->cdsc_audiostatus = cgc.buffer[1];
1da177e4
LT
2047 subchnl->cdsc_ctrl = cgc.buffer[5] & 0xf;
2048 subchnl->cdsc_trk = cgc.buffer[6];
2049 subchnl->cdsc_ind = cgc.buffer[7];
2050
bf294e83 2051 if (subchnl->cdsc_format == CDROM_LBA) {
2052 subchnl->cdsc_absaddr.lba = ((cgc.buffer[8] << 24) |
2053 (cgc.buffer[9] << 16) |
2054 (cgc.buffer[10] << 8) |
2055 (cgc.buffer[11]));
2056 subchnl->cdsc_reladdr.lba = ((cgc.buffer[12] << 24) |
2057 (cgc.buffer[13] << 16) |
2058 (cgc.buffer[14] << 8) |
2059 (cgc.buffer[15]));
2060 } else {
2061 subchnl->cdsc_reladdr.msf.minute = cgc.buffer[13];
2062 subchnl->cdsc_reladdr.msf.second = cgc.buffer[14];
2063 subchnl->cdsc_reladdr.msf.frame = cgc.buffer[15];
2064 subchnl->cdsc_absaddr.msf.minute = cgc.buffer[9];
2065 subchnl->cdsc_absaddr.msf.second = cgc.buffer[10];
2066 subchnl->cdsc_absaddr.msf.frame = cgc.buffer[11];
2067 }
1da177e4 2068
fce53847 2069 return 0;
1da177e4
LT
2070}
2071
2072/*
2073 * Specific READ_10 interface
2074 */
2075static int cdrom_read_cd(struct cdrom_device_info *cdi,
2076 struct packet_command *cgc, int lba,
2077 int blocksize, int nblocks)
2078{
853fe1bf 2079 const struct cdrom_device_ops *cdo = cdi->ops;
1da177e4
LT
2080
2081 memset(&cgc->cmd, 0, sizeof(cgc->cmd));
2082 cgc->cmd[0] = GPCMD_READ_10;
2083 cgc->cmd[2] = (lba >> 24) & 0xff;
2084 cgc->cmd[3] = (lba >> 16) & 0xff;
2085 cgc->cmd[4] = (lba >> 8) & 0xff;
2086 cgc->cmd[5] = lba & 0xff;
2087 cgc->cmd[6] = (nblocks >> 16) & 0xff;
2088 cgc->cmd[7] = (nblocks >> 8) & 0xff;
2089 cgc->cmd[8] = nblocks & 0xff;
2090 cgc->buflen = blocksize * nblocks;
2091 return cdo->generic_packet(cdi, cgc);
2092}
2093
2094/* very generic interface for reading the various types of blocks */
2095static int cdrom_read_block(struct cdrom_device_info *cdi,
2096 struct packet_command *cgc,
2097 int lba, int nblocks, int format, int blksize)
2098{
853fe1bf 2099 const struct cdrom_device_ops *cdo = cdi->ops;
1da177e4
LT
2100
2101 memset(&cgc->cmd, 0, sizeof(cgc->cmd));
2102 cgc->cmd[0] = GPCMD_READ_CD;
2103 /* expected sector size - cdda,mode1,etc. */
2104 cgc->cmd[1] = format << 2;
2105 /* starting address */
2106 cgc->cmd[2] = (lba >> 24) & 0xff;
2107 cgc->cmd[3] = (lba >> 16) & 0xff;
2108 cgc->cmd[4] = (lba >> 8) & 0xff;
2109 cgc->cmd[5] = lba & 0xff;
2110 /* number of blocks */
2111 cgc->cmd[6] = (nblocks >> 16) & 0xff;
2112 cgc->cmd[7] = (nblocks >> 8) & 0xff;
2113 cgc->cmd[8] = nblocks & 0xff;
2114 cgc->buflen = blksize * nblocks;
2115
2116 /* set the header info returned */
2117 switch (blksize) {
2118 case CD_FRAMESIZE_RAW0 : cgc->cmd[9] = 0x58; break;
2119 case CD_FRAMESIZE_RAW1 : cgc->cmd[9] = 0x78; break;
2120 case CD_FRAMESIZE_RAW : cgc->cmd[9] = 0xf8; break;
2121 default : cgc->cmd[9] = 0x10;
2122 }
2123
2124 return cdo->generic_packet(cdi, cgc);
2125}
2126
2127static int cdrom_read_cdda_old(struct cdrom_device_info *cdi, __u8 __user *ubuf,
2128 int lba, int nframes)
2129{
2130 struct packet_command cgc;
2131 int ret = 0;
2132 int nr;
2133
2134 cdi->last_sense = 0;
2135
2136 memset(&cgc, 0, sizeof(cgc));
2137
2138 /*
2139 * start with will ra.nframes size, back down if alloc fails
2140 */
2141 nr = nframes;
2142 do {
6da2ec56 2143 cgc.buffer = kmalloc_array(nr, CD_FRAMESIZE_RAW, GFP_KERNEL);
1da177e4
LT
2144 if (cgc.buffer)
2145 break;
2146
2147 nr >>= 1;
2148 } while (nr);
2149
2150 if (!nr)
2151 return -ENOMEM;
2152
1da177e4
LT
2153 cgc.data_direction = CGC_DATA_READ;
2154 while (nframes > 0) {
2155 if (nr > nframes)
2156 nr = nframes;
2157
2158 ret = cdrom_read_block(cdi, &cgc, lba, nr, 1, CD_FRAMESIZE_RAW);
2159 if (ret)
2160 break;
822bfa51 2161 if (copy_to_user(ubuf, cgc.buffer, CD_FRAMESIZE_RAW * nr)) {
1da177e4
LT
2162 ret = -EFAULT;
2163 break;
2164 }
2165 ubuf += CD_FRAMESIZE_RAW * nr;
2166 nframes -= nr;
2167 lba += nr;
2168 }
1da177e4
LT
2169 kfree(cgc.buffer);
2170 return ret;
2171}
2172
2173static int cdrom_read_cdda_bpc(struct cdrom_device_info *cdi, __u8 __user *ubuf,
2174 int lba, int nframes)
2175{
165125e1 2176 struct request_queue *q = cdi->disk->queue;
1da177e4 2177 struct request *rq;
82ed4db4 2178 struct scsi_request *req;
1da177e4
LT
2179 struct bio *bio;
2180 unsigned int len;
2181 int nr, ret = 0;
2182
2183 if (!q)
2184 return -ENXIO;
2185
73d17701
BVA
2186 if (!blk_queue_scsi_passthrough(q)) {
2187 WARN_ONCE(true,
2188 "Attempt read CDDA info through a non-SCSI queue\n");
2189 return -EINVAL;
2190 }
2191
1da177e4
LT
2192 cdi->last_sense = 0;
2193
2194 while (nframes) {
2195 nr = nframes;
2196 if (cdi->cdda_method == CDDA_BPC_SINGLE)
2197 nr = 1;
ae03bf63
MP
2198 if (nr * CD_FRAMESIZE_RAW > (queue_max_sectors(q) << 9))
2199 nr = (queue_max_sectors(q) << 9) / CD_FRAMESIZE_RAW;
1da177e4
LT
2200
2201 len = nr * CD_FRAMESIZE_RAW;
2202
ff005a06 2203 rq = blk_get_request(q, REQ_OP_SCSI_IN, 0);
a492f075
JL
2204 if (IS_ERR(rq)) {
2205 ret = PTR_ERR(rq);
febd7a5c
AB
2206 break;
2207 }
82ed4db4 2208 req = scsi_req(rq);
febd7a5c 2209
152e283f 2210 ret = blk_rq_map_user(q, rq, NULL, ubuf, len, GFP_KERNEL);
febd7a5c
AB
2211 if (ret) {
2212 blk_put_request(rq);
dd1cab95 2213 break;
febd7a5c 2214 }
1da177e4 2215
82ed4db4
CH
2216 req->cmd[0] = GPCMD_READ_CD;
2217 req->cmd[1] = 1 << 2;
2218 req->cmd[2] = (lba >> 24) & 0xff;
2219 req->cmd[3] = (lba >> 16) & 0xff;
2220 req->cmd[4] = (lba >> 8) & 0xff;
2221 req->cmd[5] = lba & 0xff;
2222 req->cmd[6] = (nr >> 16) & 0xff;
2223 req->cmd[7] = (nr >> 8) & 0xff;
2224 req->cmd[8] = nr & 0xff;
2225 req->cmd[9] = 0xf8;
2226
2227 req->cmd_len = 12;
1da177e4
LT
2228 rq->timeout = 60 * HZ;
2229 bio = rq->bio;
2230
b7819b92 2231 blk_execute_rq(q, cdi->disk, rq, 0);
17d5363b 2232 if (scsi_req(rq)->result) {
4e178c17
KC
2233 struct scsi_sense_hdr sshdr;
2234
1da177e4 2235 ret = -EIO;
4e178c17
KC
2236 scsi_normalize_sense(req->sense, req->sense_len,
2237 &sshdr);
2238 cdi->last_sense = sshdr.sense_key;
1da177e4
LT
2239 }
2240
8e5cfc45 2241 if (blk_rq_unmap_user(bio))
1da177e4 2242 ret = -EFAULT;
febd7a5c 2243 blk_put_request(rq);
1da177e4
LT
2244
2245 if (ret)
2246 break;
2247
2248 nframes -= nr;
2249 lba += nr;
2250 ubuf += len;
2251 }
2252
2253 return ret;
2254}
2255
2256static int cdrom_read_cdda(struct cdrom_device_info *cdi, __u8 __user *ubuf,
2257 int lba, int nframes)
2258{
2259 int ret;
2260
2261 if (cdi->cdda_method == CDDA_OLD)
2262 return cdrom_read_cdda_old(cdi, ubuf, lba, nframes);
2263
2264retry:
2265 /*
2266 * for anything else than success and io error, we need to retry
2267 */
2268 ret = cdrom_read_cdda_bpc(cdi, ubuf, lba, nframes);
2269 if (!ret || ret != -EIO)
2270 return ret;
2271
2272 /*
2273 * I've seen drives get sense 4/8/3 udma crc errors on multi
2274 * frame dma, so drop to single frame dma if we need to
2275 */
2276 if (cdi->cdda_method == CDDA_BPC_FULL && nframes > 1) {
e597cd09 2277 pr_info("dropping to single frame dma\n");
1da177e4
LT
2278 cdi->cdda_method = CDDA_BPC_SINGLE;
2279 goto retry;
2280 }
2281
2282 /*
2283 * so we have an io error of some sort with multi frame dma. if the
2284 * condition wasn't a hardware error
2285 * problems, not for any error
2286 */
2287 if (cdi->last_sense != 0x04 && cdi->last_sense != 0x0b)
2288 return ret;
2289
e597cd09 2290 pr_info("dropping to old style cdda (sense=%x)\n", cdi->last_sense);
1da177e4
LT
2291 cdi->cdda_method = CDDA_OLD;
2292 return cdrom_read_cdda_old(cdi, ubuf, lba, nframes);
2293}
2294
d2c5d4fc
CH
2295static int cdrom_ioctl_multisession(struct cdrom_device_info *cdi,
2296 void __user *argp)
1da177e4 2297{
d2c5d4fc
CH
2298 struct cdrom_multisession ms_info;
2299 u8 requested_format;
1da177e4
LT
2300 int ret;
2301
5944b2ce 2302 cd_dbg(CD_DO_IOCTL, "entering CDROMMULTISESSION\n");
d2c5d4fc
CH
2303
2304 if (!(cdi->ops->capability & CDC_MULTI_SESSION))
2305 return -ENOSYS;
2306
2307 if (copy_from_user(&ms_info, argp, sizeof(ms_info)))
2308 return -EFAULT;
2309
2310 requested_format = ms_info.addr_format;
2311 if (requested_format != CDROM_MSF && requested_format != CDROM_LBA)
2312 return -EINVAL;
2313 ms_info.addr_format = CDROM_LBA;
2314
2315 ret = cdi->ops->get_last_session(cdi, &ms_info);
2316 if (ret)
1da177e4
LT
2317 return ret;
2318
d2c5d4fc
CH
2319 sanitize_format(&ms_info.addr, &ms_info.addr_format, requested_format);
2320
2321 if (copy_to_user(argp, &ms_info, sizeof(ms_info)))
2322 return -EFAULT;
2323
5944b2ce 2324 cd_dbg(CD_DO_IOCTL, "CDROMMULTISESSION successful\n");
d2c5d4fc
CH
2325 return 0;
2326}
2327
2328static int cdrom_ioctl_eject(struct cdrom_device_info *cdi)
2329{
5944b2ce 2330 cd_dbg(CD_DO_IOCTL, "entering CDROMEJECT\n");
d2c5d4fc
CH
2331
2332 if (!CDROM_CAN(CDC_OPEN_TRAY))
2333 return -ENOSYS;
cdccaa94 2334 if (cdi->use_count != 1 || cdi->keeplocked)
d2c5d4fc
CH
2335 return -EBUSY;
2336 if (CDROM_CAN(CDC_LOCK)) {
2337 int ret = cdi->ops->lock_door(cdi, 0);
2338 if (ret)
1da177e4 2339 return ret;
d2c5d4fc 2340 }
1da177e4 2341
d2c5d4fc
CH
2342 return cdi->ops->tray_move(cdi, 1);
2343}
1da177e4 2344
d2c5d4fc
CH
2345static int cdrom_ioctl_closetray(struct cdrom_device_info *cdi)
2346{
5944b2ce 2347 cd_dbg(CD_DO_IOCTL, "entering CDROMCLOSETRAY\n");
1da177e4 2348
d2c5d4fc
CH
2349 if (!CDROM_CAN(CDC_CLOSE_TRAY))
2350 return -ENOSYS;
2351 return cdi->ops->tray_move(cdi, 0);
2352}
1da177e4 2353
d2c5d4fc
CH
2354static int cdrom_ioctl_eject_sw(struct cdrom_device_info *cdi,
2355 unsigned long arg)
2356{
5944b2ce 2357 cd_dbg(CD_DO_IOCTL, "entering CDROMEJECT_SW\n");
1da177e4 2358
d2c5d4fc
CH
2359 if (!CDROM_CAN(CDC_OPEN_TRAY))
2360 return -ENOSYS;
cdccaa94 2361 if (cdi->keeplocked)
d2c5d4fc 2362 return -EBUSY;
1da177e4 2363
d2c5d4fc
CH
2364 cdi->options &= ~(CDO_AUTO_CLOSE | CDO_AUTO_EJECT);
2365 if (arg)
2366 cdi->options |= CDO_AUTO_CLOSE | CDO_AUTO_EJECT;
2367 return 0;
2368}
1da177e4 2369
d2c5d4fc
CH
2370static int cdrom_ioctl_media_changed(struct cdrom_device_info *cdi,
2371 unsigned long arg)
2372{
2373 struct cdrom_changer_info *info;
2374 int ret;
1da177e4 2375
5944b2ce 2376 cd_dbg(CD_DO_IOCTL, "entering CDROM_MEDIA_CHANGED\n");
1da177e4 2377
d2c5d4fc
CH
2378 if (!CDROM_CAN(CDC_MEDIA_CHANGED))
2379 return -ENOSYS;
1da177e4 2380
d2c5d4fc
CH
2381 /* cannot select disc or select current disc */
2382 if (!CDROM_CAN(CDC_SELECT_DISC) || arg == CDSL_CURRENT)
2383 return media_changed(cdi, 1);
1da177e4 2384
9de4ee40 2385 if (arg >= cdi->capacity)
d2c5d4fc 2386 return -EINVAL;
1da177e4 2387
d2c5d4fc
CH
2388 info = kmalloc(sizeof(*info), GFP_KERNEL);
2389 if (!info)
2390 return -ENOMEM;
1da177e4 2391
d2c5d4fc
CH
2392 ret = cdrom_read_mech_status(cdi, info);
2393 if (!ret)
2394 ret = info->slots[arg].change;
2395 kfree(info);
2396 return ret;
2397}
1da177e4 2398
d2c5d4fc
CH
2399static int cdrom_ioctl_set_options(struct cdrom_device_info *cdi,
2400 unsigned long arg)
2401{
5944b2ce 2402 cd_dbg(CD_DO_IOCTL, "entering CDROM_SET_OPTIONS\n");
1da177e4 2403
d2c5d4fc
CH
2404 /*
2405 * Options need to be in sync with capability.
2406 * Too late for that, so we have to check each one separately.
2407 */
2408 switch (arg) {
2409 case CDO_USE_FFLAGS:
2410 case CDO_CHECK_TYPE:
2411 break;
2412 case CDO_LOCK:
2413 if (!CDROM_CAN(CDC_LOCK))
2414 return -ENOSYS;
2415 break;
2416 case 0:
2417 return cdi->options;
2418 /* default is basically CDO_[AUTO_CLOSE|AUTO_EJECT] */
2419 default:
2420 if (!CDROM_CAN(arg))
1da177e4 2421 return -ENOSYS;
d2c5d4fc
CH
2422 }
2423 cdi->options |= (int) arg;
2424 return cdi->options;
2425}
1da177e4 2426
d2c5d4fc
CH
2427static int cdrom_ioctl_clear_options(struct cdrom_device_info *cdi,
2428 unsigned long arg)
2429{
5944b2ce 2430 cd_dbg(CD_DO_IOCTL, "entering CDROM_CLEAR_OPTIONS\n");
1da177e4 2431
d2c5d4fc
CH
2432 cdi->options &= ~(int) arg;
2433 return cdi->options;
2434}
1da177e4 2435
d2c5d4fc
CH
2436static int cdrom_ioctl_select_speed(struct cdrom_device_info *cdi,
2437 unsigned long arg)
2438{
5944b2ce 2439 cd_dbg(CD_DO_IOCTL, "entering CDROM_SELECT_SPEED\n");
1da177e4 2440
d2c5d4fc
CH
2441 if (!CDROM_CAN(CDC_SELECT_SPEED))
2442 return -ENOSYS;
2443 return cdi->ops->select_speed(cdi, arg);
2444}
1da177e4 2445
d2c5d4fc
CH
2446static int cdrom_ioctl_select_disc(struct cdrom_device_info *cdi,
2447 unsigned long arg)
2448{
5944b2ce 2449 cd_dbg(CD_DO_IOCTL, "entering CDROM_SELECT_DISC\n");
d2c5d4fc
CH
2450
2451 if (!CDROM_CAN(CDC_SELECT_DISC))
2452 return -ENOSYS;
2453
2454 if (arg != CDSL_CURRENT && arg != CDSL_NONE) {
e4f3aa2e 2455 if (arg >= cdi->capacity)
d2c5d4fc
CH
2456 return -EINVAL;
2457 }
2458
2459 /*
2460 * ->select_disc is a hook to allow a driver-specific way of
2461 * seleting disc. However, since there is no equivalent hook for
2462 * cdrom_slot_status this may not actually be useful...
2463 */
2464 if (cdi->ops->select_disc)
2465 return cdi->ops->select_disc(cdi, arg);
2466
5944b2ce 2467 cd_dbg(CD_CHANGER, "Using generic cdrom_select_disc()\n");
d2c5d4fc
CH
2468 return cdrom_select_disc(cdi, arg);
2469}
2470
2471static int cdrom_ioctl_reset(struct cdrom_device_info *cdi,
2472 struct block_device *bdev)
2473{
5944b2ce 2474 cd_dbg(CD_DO_IOCTL, "entering CDROM_RESET\n");
d2c5d4fc
CH
2475
2476 if (!capable(CAP_SYS_ADMIN))
2477 return -EACCES;
2478 if (!CDROM_CAN(CDC_RESET))
2479 return -ENOSYS;
f98393a6 2480 invalidate_bdev(bdev);
d2c5d4fc
CH
2481 return cdi->ops->reset(cdi);
2482}
2483
2484static int cdrom_ioctl_lock_door(struct cdrom_device_info *cdi,
2485 unsigned long arg)
2486{
5944b2ce 2487 cd_dbg(CD_DO_IOCTL, "%socking door\n", arg ? "L" : "Unl");
d2c5d4fc
CH
2488
2489 if (!CDROM_CAN(CDC_LOCK))
2490 return -EDRIVE_CANT_DO_THIS;
2491
cdccaa94 2492 cdi->keeplocked = arg ? 1 : 0;
d2c5d4fc
CH
2493
2494 /*
2495 * Don't unlock the door on multiple opens by default, but allow
2496 * root to do so.
2497 */
2498 if (cdi->use_count != 1 && !arg && !capable(CAP_SYS_ADMIN))
2499 return -EBUSY;
2500 return cdi->ops->lock_door(cdi, arg);
2501}
2502
2503static int cdrom_ioctl_debug(struct cdrom_device_info *cdi,
2504 unsigned long arg)
2505{
5944b2ce 2506 cd_dbg(CD_DO_IOCTL, "%sabling debug\n", arg ? "En" : "Dis");
d2c5d4fc
CH
2507
2508 if (!capable(CAP_SYS_ADMIN))
2509 return -EACCES;
2510 debug = arg ? 1 : 0;
2511 return debug;
2512}
1da177e4 2513
d2c5d4fc
CH
2514static int cdrom_ioctl_get_capability(struct cdrom_device_info *cdi)
2515{
5944b2ce 2516 cd_dbg(CD_DO_IOCTL, "entering CDROM_GET_CAPABILITY\n");
d2c5d4fc
CH
2517 return (cdi->ops->capability & ~cdi->mask);
2518}
2519
2520/*
2521 * The following function is implemented, although very few audio
1da177e4
LT
2522 * discs give Universal Product Code information, which should just be
2523 * the Medium Catalog Number on the box. Note, that the way the code
2524 * is written on the CD is /not/ uniform across all discs!
2525 */
d2c5d4fc
CH
2526static int cdrom_ioctl_get_mcn(struct cdrom_device_info *cdi,
2527 void __user *argp)
2528{
2529 struct cdrom_mcn mcn;
2530 int ret;
1da177e4 2531
5944b2ce 2532 cd_dbg(CD_DO_IOCTL, "entering CDROM_GET_MCN\n");
1da177e4 2533
d2c5d4fc
CH
2534 if (!(cdi->ops->capability & CDC_MCN))
2535 return -ENOSYS;
2536 ret = cdi->ops->get_mcn(cdi, &mcn);
2537 if (ret)
2538 return ret;
1da177e4 2539
d2c5d4fc
CH
2540 if (copy_to_user(argp, &mcn, sizeof(mcn)))
2541 return -EFAULT;
5944b2ce 2542 cd_dbg(CD_DO_IOCTL, "CDROM_GET_MCN successful\n");
d2c5d4fc
CH
2543 return 0;
2544}
1da177e4 2545
d2c5d4fc
CH
2546static int cdrom_ioctl_drive_status(struct cdrom_device_info *cdi,
2547 unsigned long arg)
2548{
5944b2ce 2549 cd_dbg(CD_DO_IOCTL, "entering CDROM_DRIVE_STATUS\n");
d2c5d4fc
CH
2550
2551 if (!(cdi->ops->capability & CDC_DRIVE_STATUS))
2552 return -ENOSYS;
2553 if (!CDROM_CAN(CDC_SELECT_DISC) ||
2554 (arg == CDSL_CURRENT || arg == CDSL_NONE))
2555 return cdi->ops->drive_status(cdi, CDSL_CURRENT);
8f3fafc9 2556 if (arg >= cdi->capacity)
d2c5d4fc
CH
2557 return -EINVAL;
2558 return cdrom_slot_status(cdi, arg);
2559}
2560
2561/*
2562 * Ok, this is where problems start. The current interface for the
2563 * CDROM_DISC_STATUS ioctl is flawed. It makes the false assumption that
25985edc 2564 * CDs are all CDS_DATA_1 or all CDS_AUDIO, etc. Unfortunately, while this
d2c5d4fc
CH
2565 * is often the case, it is also very common for CDs to have some tracks
2566 * with data, and some tracks with audio. Just because I feel like it,
2567 * I declare the following to be the best way to cope. If the CD has ANY
2568 * data tracks on it, it will be returned as a data CD. If it has any XA
2569 * tracks, I will return it as that. Now I could simplify this interface
2570 * by combining these returns with the above, but this more clearly
2571 * demonstrates the problem with the current interface. Too bad this
2572 * wasn't designed to use bitmasks... -Erik
2573 *
2574 * Well, now we have the option CDS_MIXED: a mixed-type CD.
2575 * User level programmers might feel the ioctl is not very useful.
2576 * ---david
2577 */
2578static int cdrom_ioctl_disc_status(struct cdrom_device_info *cdi)
2579{
2580 tracktype tracks;
2581
5944b2ce 2582 cd_dbg(CD_DO_IOCTL, "entering CDROM_DISC_STATUS\n");
d2c5d4fc
CH
2583
2584 cdrom_count_tracks(cdi, &tracks);
2585 if (tracks.error)
2586 return tracks.error;
2587
2588 /* Policy mode on */
2589 if (tracks.audio > 0) {
2590 if (!tracks.data && !tracks.cdi && !tracks.xa)
2591 return CDS_AUDIO;
2592 else
2593 return CDS_MIXED;
1da177e4
LT
2594 }
2595
d2c5d4fc
CH
2596 if (tracks.cdi > 0)
2597 return CDS_XA_2_2;
2598 if (tracks.xa > 0)
2599 return CDS_XA_2_1;
2600 if (tracks.data > 0)
2601 return CDS_DATA_1;
2602 /* Policy mode off */
2603
5944b2ce 2604 cd_dbg(CD_WARNING, "This disc doesn't have any tracks I recognize!\n");
d2c5d4fc
CH
2605 return CDS_NO_INFO;
2606}
2607
2608static int cdrom_ioctl_changer_nslots(struct cdrom_device_info *cdi)
2609{
5944b2ce 2610 cd_dbg(CD_DO_IOCTL, "entering CDROM_CHANGER_NSLOTS\n");
d2c5d4fc
CH
2611 return cdi->capacity;
2612}
2613
2614static int cdrom_ioctl_get_subchnl(struct cdrom_device_info *cdi,
2615 void __user *argp)
2616{
2617 struct cdrom_subchnl q;
2618 u8 requested, back;
2619 int ret;
2620
5944b2ce 2621 /* cd_dbg(CD_DO_IOCTL,"entering CDROMSUBCHNL\n");*/
d2c5d4fc 2622
d2c5d4fc
CH
2623 if (copy_from_user(&q, argp, sizeof(q)))
2624 return -EFAULT;
2625
2626 requested = q.cdsc_format;
2627 if (requested != CDROM_MSF && requested != CDROM_LBA)
2628 return -EINVAL;
2629 q.cdsc_format = CDROM_MSF;
2630
2631 ret = cdi->ops->audio_ioctl(cdi, CDROMSUBCHNL, &q);
2632 if (ret)
2633 return ret;
2634
2635 back = q.cdsc_format; /* local copy */
2636 sanitize_format(&q.cdsc_absaddr, &back, requested);
2637 sanitize_format(&q.cdsc_reladdr, &q.cdsc_format, requested);
2638
2639 if (copy_to_user(argp, &q, sizeof(q)))
2640 return -EFAULT;
5944b2ce 2641 /* cd_dbg(CD_DO_IOCTL, "CDROMSUBCHNL successful\n"); */
d2c5d4fc
CH
2642 return 0;
2643}
2644
2645static int cdrom_ioctl_read_tochdr(struct cdrom_device_info *cdi,
2646 void __user *argp)
2647{
2648 struct cdrom_tochdr header;
2649 int ret;
2650
5944b2ce 2651 /* cd_dbg(CD_DO_IOCTL, "entering CDROMREADTOCHDR\n"); */
d2c5d4fc 2652
d2c5d4fc
CH
2653 if (copy_from_user(&header, argp, sizeof(header)))
2654 return -EFAULT;
2655
2656 ret = cdi->ops->audio_ioctl(cdi, CDROMREADTOCHDR, &header);
2657 if (ret)
2658 return ret;
2659
2660 if (copy_to_user(argp, &header, sizeof(header)))
2661 return -EFAULT;
5944b2ce 2662 /* cd_dbg(CD_DO_IOCTL, "CDROMREADTOCHDR successful\n"); */
d2c5d4fc
CH
2663 return 0;
2664}
2665
2666static int cdrom_ioctl_read_tocentry(struct cdrom_device_info *cdi,
2667 void __user *argp)
2668{
2669 struct cdrom_tocentry entry;
2670 u8 requested_format;
2671 int ret;
2672
5944b2ce 2673 /* cd_dbg(CD_DO_IOCTL, "entering CDROMREADTOCENTRY\n"); */
d2c5d4fc 2674
d2c5d4fc
CH
2675 if (copy_from_user(&entry, argp, sizeof(entry)))
2676 return -EFAULT;
2677
2678 requested_format = entry.cdte_format;
2679 if (requested_format != CDROM_MSF && requested_format != CDROM_LBA)
2680 return -EINVAL;
2681 /* make interface to low-level uniform */
2682 entry.cdte_format = CDROM_MSF;
2683 ret = cdi->ops->audio_ioctl(cdi, CDROMREADTOCENTRY, &entry);
2684 if (ret)
2685 return ret;
2686 sanitize_format(&entry.cdte_addr, &entry.cdte_format, requested_format);
2687
2688 if (copy_to_user(argp, &entry, sizeof(entry)))
2689 return -EFAULT;
5944b2ce 2690 /* cd_dbg(CD_DO_IOCTL, "CDROMREADTOCENTRY successful\n"); */
d2c5d4fc
CH
2691 return 0;
2692}
2693
2694static int cdrom_ioctl_play_msf(struct cdrom_device_info *cdi,
2695 void __user *argp)
2696{
2697 struct cdrom_msf msf;
2698
5944b2ce 2699 cd_dbg(CD_DO_IOCTL, "entering CDROMPLAYMSF\n");
d2c5d4fc
CH
2700
2701 if (!CDROM_CAN(CDC_PLAY_AUDIO))
2702 return -ENOSYS;
2703 if (copy_from_user(&msf, argp, sizeof(msf)))
2704 return -EFAULT;
2705 return cdi->ops->audio_ioctl(cdi, CDROMPLAYMSF, &msf);
2706}
2707
2708static int cdrom_ioctl_play_trkind(struct cdrom_device_info *cdi,
2709 void __user *argp)
2710{
2711 struct cdrom_ti ti;
2712 int ret;
2713
5944b2ce 2714 cd_dbg(CD_DO_IOCTL, "entering CDROMPLAYTRKIND\n");
d2c5d4fc
CH
2715
2716 if (!CDROM_CAN(CDC_PLAY_AUDIO))
2717 return -ENOSYS;
2718 if (copy_from_user(&ti, argp, sizeof(ti)))
2719 return -EFAULT;
2720
2721 ret = check_for_audio_disc(cdi, cdi->ops);
2722 if (ret)
2723 return ret;
2724 return cdi->ops->audio_ioctl(cdi, CDROMPLAYTRKIND, &ti);
2725}
2726static int cdrom_ioctl_volctrl(struct cdrom_device_info *cdi,
2727 void __user *argp)
2728{
2729 struct cdrom_volctrl volume;
2730
5944b2ce 2731 cd_dbg(CD_DO_IOCTL, "entering CDROMVOLCTRL\n");
d2c5d4fc
CH
2732
2733 if (!CDROM_CAN(CDC_PLAY_AUDIO))
2734 return -ENOSYS;
2735 if (copy_from_user(&volume, argp, sizeof(volume)))
2736 return -EFAULT;
2737 return cdi->ops->audio_ioctl(cdi, CDROMVOLCTRL, &volume);
2738}
2739
2740static int cdrom_ioctl_volread(struct cdrom_device_info *cdi,
2741 void __user *argp)
2742{
2743 struct cdrom_volctrl volume;
2744 int ret;
2745
5944b2ce 2746 cd_dbg(CD_DO_IOCTL, "entering CDROMVOLREAD\n");
d2c5d4fc
CH
2747
2748 if (!CDROM_CAN(CDC_PLAY_AUDIO))
2749 return -ENOSYS;
2750
2751 ret = cdi->ops->audio_ioctl(cdi, CDROMVOLREAD, &volume);
2752 if (ret)
2753 return ret;
2754
2755 if (copy_to_user(argp, &volume, sizeof(volume)))
2756 return -EFAULT;
2757 return 0;
2758}
2759
2760static int cdrom_ioctl_audioctl(struct cdrom_device_info *cdi,
2761 unsigned int cmd)
2762{
2763 int ret;
2764
5944b2ce 2765 cd_dbg(CD_DO_IOCTL, "doing audio ioctl (start/stop/pause/resume)\n");
d2c5d4fc
CH
2766
2767 if (!CDROM_CAN(CDC_PLAY_AUDIO))
2768 return -ENOSYS;
2769 ret = check_for_audio_disc(cdi, cdi->ops);
2770 if (ret)
2771 return ret;
2772 return cdi->ops->audio_ioctl(cdi, cmd, NULL);
2773}
2774
1da177e4
LT
2775/*
2776 * Required when we need to use READ_10 to issue other than 2048 block
2777 * reads
2778 */
2779static int cdrom_switch_blocksize(struct cdrom_device_info *cdi, int size)
2780{
853fe1bf 2781 const struct cdrom_device_ops *cdo = cdi->ops;
1da177e4
LT
2782 struct packet_command cgc;
2783 struct modesel_head mh;
2784
2785 memset(&mh, 0, sizeof(mh));
2786 mh.block_desc_length = 0x08;
2787 mh.block_length_med = (size >> 8) & 0xff;
2788 mh.block_length_lo = size & 0xff;
2789
2790 memset(&cgc, 0, sizeof(cgc));
2791 cgc.cmd[0] = 0x15;
2792 cgc.cmd[1] = 1 << 4;
2793 cgc.cmd[4] = 12;
2794 cgc.buflen = sizeof(mh);
2795 cgc.buffer = (char *) &mh;
2796 cgc.data_direction = CGC_DATA_WRITE;
2797 mh.block_desc_length = 0x08;
2798 mh.block_length_med = (size >> 8) & 0xff;
2799 mh.block_length_lo = size & 0xff;
2800
2801 return cdo->generic_packet(cdi, &cgc);
2802}
2803
e1e60fda
JP
2804static int cdrom_get_track_info(struct cdrom_device_info *cdi,
2805 __u16 track, __u8 type, track_information *ti)
2806{
853fe1bf 2807 const struct cdrom_device_ops *cdo = cdi->ops;
e1e60fda
JP
2808 struct packet_command cgc;
2809 int ret, buflen;
2810
2811 init_cdrom_command(&cgc, ti, 8, CGC_DATA_READ);
2812 cgc.cmd[0] = GPCMD_READ_TRACK_RZONE_INFO;
2813 cgc.cmd[1] = type & 3;
2814 cgc.cmd[4] = (track & 0xff00) >> 8;
2815 cgc.cmd[5] = track & 0xff;
2816 cgc.cmd[8] = 8;
2817 cgc.quiet = 1;
2818
2819 ret = cdo->generic_packet(cdi, &cgc);
2820 if (ret)
2821 return ret;
2822
2823 buflen = be16_to_cpu(ti->track_information_length) +
2824 sizeof(ti->track_information_length);
2825
2826 if (buflen > sizeof(track_information))
2827 buflen = sizeof(track_information);
2828
2829 cgc.cmd[8] = cgc.buflen = buflen;
2830 ret = cdo->generic_packet(cdi, &cgc);
2831 if (ret)
2832 return ret;
2833
2834 /* return actual fill size */
2835 return buflen;
2836}
2837
2838/* return the last written block on the CD-R media. this is for the udf
2839 file system. */
2840int cdrom_get_last_written(struct cdrom_device_info *cdi, long *last_written)
2841{
2842 struct cdrom_tocentry toc;
2843 disc_information di;
2844 track_information ti;
2845 __u32 last_track;
2846 int ret = -1, ti_size;
2847
2848 if (!CDROM_CAN(CDC_GENERIC_PACKET))
2849 goto use_toc;
2850
2851 ret = cdrom_get_disc_info(cdi, &di);
2852 if (ret < (int)(offsetof(typeof(di), last_track_lsb)
2853 + sizeof(di.last_track_lsb)))
2854 goto use_toc;
2855
2856 /* if unit didn't return msb, it's zeroed by cdrom_get_disc_info */
2857 last_track = (di.last_track_msb << 8) | di.last_track_lsb;
2858 ti_size = cdrom_get_track_info(cdi, last_track, 1, &ti);
2859 if (ti_size < (int)offsetof(typeof(ti), track_start))
2860 goto use_toc;
2861
2862 /* if this track is blank, try the previous. */
2863 if (ti.blank) {
2864 if (last_track == 1)
2865 goto use_toc;
2866 last_track--;
2867 ti_size = cdrom_get_track_info(cdi, last_track, 1, &ti);
2868 }
2869
2870 if (ti_size < (int)(offsetof(typeof(ti), track_size)
2871 + sizeof(ti.track_size)))
2872 goto use_toc;
2873
2874 /* if last recorded field is valid, return it. */
2875 if (ti.lra_v && ti_size >= (int)(offsetof(typeof(ti), last_rec_address)
2876 + sizeof(ti.last_rec_address))) {
2877 *last_written = be32_to_cpu(ti.last_rec_address);
2878 } else {
2879 /* make it up instead */
2880 *last_written = be32_to_cpu(ti.track_start) +
2881 be32_to_cpu(ti.track_size);
2882 if (ti.free_blocks)
2883 *last_written -= (be32_to_cpu(ti.free_blocks) + 7);
2884 }
2885 return 0;
2886
2887 /* this is where we end up if the drive either can't do a
2888 GPCMD_READ_DISC_INFO or GPCMD_READ_TRACK_RZONE_INFO or if
2889 it doesn't give enough information or fails. then we return
2890 the toc contents. */
2891use_toc:
366ba7c7
DEP
2892 if (!CDROM_CAN(CDC_PLAY_AUDIO))
2893 return -ENOSYS;
2894
e1e60fda
JP
2895 toc.cdte_format = CDROM_MSF;
2896 toc.cdte_track = CDROM_LEADOUT;
2897 if ((ret = cdi->ops->audio_ioctl(cdi, CDROMREADTOCENTRY, &toc)))
2898 return ret;
2899 sanitize_format(&toc.cdte_addr, &toc.cdte_format, CDROM_LBA);
2900 *last_written = toc.cdte_addr.lba;
2901 return 0;
2902}
2903
dac1c5cf
JP
2904/* return the next writable block. also for udf file system. */
2905static int cdrom_get_next_writable(struct cdrom_device_info *cdi,
2906 long *next_writable)
2907{
2908 disc_information di;
2909 track_information ti;
2910 __u16 last_track;
2911 int ret, ti_size;
2912
2913 if (!CDROM_CAN(CDC_GENERIC_PACKET))
2914 goto use_last_written;
2915
2916 ret = cdrom_get_disc_info(cdi, &di);
2917 if (ret < 0 || ret < offsetof(typeof(di), last_track_lsb)
2918 + sizeof(di.last_track_lsb))
2919 goto use_last_written;
2920
2921 /* if unit didn't return msb, it's zeroed by cdrom_get_disc_info */
2922 last_track = (di.last_track_msb << 8) | di.last_track_lsb;
2923 ti_size = cdrom_get_track_info(cdi, last_track, 1, &ti);
2924 if (ti_size < 0 || ti_size < offsetof(typeof(ti), track_start))
2925 goto use_last_written;
2926
2927 /* if this track is blank, try the previous. */
2928 if (ti.blank) {
2929 if (last_track == 1)
2930 goto use_last_written;
2931 last_track--;
2932 ti_size = cdrom_get_track_info(cdi, last_track, 1, &ti);
2933 if (ti_size < 0)
2934 goto use_last_written;
2935 }
2936
2937 /* if next recordable address field is valid, use it. */
2938 if (ti.nwa_v && ti_size >= offsetof(typeof(ti), next_writable)
2939 + sizeof(ti.next_writable)) {
2940 *next_writable = be32_to_cpu(ti.next_writable);
2941 return 0;
2942 }
2943
2944use_last_written:
2945 ret = cdrom_get_last_written(cdi, next_writable);
2946 if (ret) {
2947 *next_writable = 0;
2948 return ret;
2949 } else {
2950 *next_writable += 7;
2951 return 0;
2952 }
2953}
2954
3147c531 2955static noinline int mmc_ioctl_cdrom_read_data(struct cdrom_device_info *cdi,
2e9aa08a
JP
2956 void __user *arg,
2957 struct packet_command *cgc,
2958 int cmd)
3147c531 2959{
e7d0748d 2960 struct scsi_sense_hdr sshdr;
3147c531
MS
2961 struct cdrom_msf msf;
2962 int blocksize = 0, format = 0, lba;
2963 int ret;
1da177e4 2964
1da177e4
LT
2965 switch (cmd) {
2966 case CDROMREADRAW:
3147c531
MS
2967 blocksize = CD_FRAMESIZE_RAW;
2968 break;
1da177e4 2969 case CDROMREADMODE1:
3147c531
MS
2970 blocksize = CD_FRAMESIZE;
2971 format = 2;
2972 break;
2973 case CDROMREADMODE2:
2974 blocksize = CD_FRAMESIZE_RAW0;
2975 break;
2976 }
a09c391d
JP
2977 if (copy_from_user(&msf, (struct cdrom_msf __user *)arg, sizeof(msf)))
2978 return -EFAULT;
3147c531
MS
2979 lba = msf_to_lba(msf.cdmsf_min0, msf.cdmsf_sec0, msf.cdmsf_frame0);
2980 /* FIXME: we need upper bound checking, too!! */
2981 if (lba < 0)
2982 return -EINVAL;
2983
542db015 2984 cgc->buffer = kzalloc(blocksize, GFP_KERNEL);
3147c531
MS
2985 if (cgc->buffer == NULL)
2986 return -ENOMEM;
2987
e7d0748d
KC
2988 memset(&sshdr, 0, sizeof(sshdr));
2989 cgc->sshdr = &sshdr;
3147c531
MS
2990 cgc->data_direction = CGC_DATA_READ;
2991 ret = cdrom_read_block(cdi, cgc, lba, 1, format, blocksize);
e7d0748d
KC
2992 if (ret && sshdr.sense_key == 0x05 &&
2993 sshdr.asc == 0x20 &&
2994 sshdr.ascq == 0x00) {
3147c531
MS
2995 /*
2996 * SCSI-II devices are not required to support
2997 * READ_CD, so let's try switching block size
2998 */
2999 /* FIXME: switch back again... */
3000 ret = cdrom_switch_blocksize(cdi, blocksize);
3001 if (ret)
3002 goto out;
e7d0748d 3003 cgc->sshdr = NULL;
3147c531
MS
3004 ret = cdrom_read_cd(cdi, cgc, lba, blocksize, 1);
3005 ret |= cdrom_switch_blocksize(cdi, blocksize);
3006 }
3007 if (!ret && copy_to_user(arg, cgc->buffer, blocksize))
3008 ret = -EFAULT;
3009out:
3010 kfree(cgc->buffer);
3011 return ret;
3012}
3013
3014static noinline int mmc_ioctl_cdrom_read_audio(struct cdrom_device_info *cdi,
2e9aa08a 3015 void __user *arg)
3147c531
MS
3016{
3017 struct cdrom_read_audio ra;
3018 int lba;
3019
8f8f5620
AB
3020#ifdef CONFIG_COMPAT
3021 if (in_compat_syscall()) {
3022 struct compat_cdrom_read_audio {
3023 union cdrom_addr addr;
3024 u8 addr_format;
3025 compat_int_t nframes;
3026 compat_caddr_t buf;
3027 } ra32;
3028
3029 if (copy_from_user(&ra32, arg, sizeof(ra32)))
3030 return -EFAULT;
3031
3032 ra = (struct cdrom_read_audio) {
3033 .addr = ra32.addr,
3034 .addr_format = ra32.addr_format,
3035 .nframes = ra32.nframes,
3036 .buf = compat_ptr(ra32.buf),
3037 };
3038 } else
3039#endif
3040 {
3041 if (copy_from_user(&ra, (struct cdrom_read_audio __user *)arg,
3042 sizeof(ra)))
3043 return -EFAULT;
3044 }
3147c531
MS
3045
3046 if (ra.addr_format == CDROM_MSF)
3047 lba = msf_to_lba(ra.addr.msf.minute,
3048 ra.addr.msf.second,
3049 ra.addr.msf.frame);
3050 else if (ra.addr_format == CDROM_LBA)
3051 lba = ra.addr.lba;
3052 else
3053 return -EINVAL;
3054
3055 /* FIXME: we need upper bound checking, too!! */
3056 if (lba < 0 || ra.nframes <= 0 || ra.nframes > CD_FRAMES)
3057 return -EINVAL;
3058
3059 return cdrom_read_cdda(cdi, ra.buf, lba, ra.nframes);
3060}
3061
3062static noinline int mmc_ioctl_cdrom_subchannel(struct cdrom_device_info *cdi,
2e9aa08a 3063 void __user *arg)
3147c531
MS
3064{
3065 int ret;
3066 struct cdrom_subchnl q;
3067 u_char requested, back;
a09c391d
JP
3068 if (copy_from_user(&q, (struct cdrom_subchnl __user *)arg, sizeof(q)))
3069 return -EFAULT;
3147c531
MS
3070 requested = q.cdsc_format;
3071 if (!((requested == CDROM_MSF) ||
3072 (requested == CDROM_LBA)))
3073 return -EINVAL;
bf294e83 3074
3147c531
MS
3075 ret = cdrom_read_subchannel(cdi, &q, 0);
3076 if (ret)
1da177e4 3077 return ret;
3147c531
MS
3078 back = q.cdsc_format; /* local copy */
3079 sanitize_format(&q.cdsc_absaddr, &back, requested);
3080 sanitize_format(&q.cdsc_reladdr, &q.cdsc_format, requested);
a09c391d
JP
3081 if (copy_to_user((struct cdrom_subchnl __user *)arg, &q, sizeof(q)))
3082 return -EFAULT;
5944b2ce 3083 /* cd_dbg(CD_DO_IOCTL, "CDROMSUBCHNL successful\n"); */
3147c531
MS
3084 return 0;
3085}
1da177e4 3086
3147c531 3087static noinline int mmc_ioctl_cdrom_play_msf(struct cdrom_device_info *cdi,
2e9aa08a
JP
3088 void __user *arg,
3089 struct packet_command *cgc)
3147c531 3090{
853fe1bf 3091 const struct cdrom_device_ops *cdo = cdi->ops;
3147c531 3092 struct cdrom_msf msf;
5944b2ce 3093 cd_dbg(CD_DO_IOCTL, "entering CDROMPLAYMSF\n");
a09c391d
JP
3094 if (copy_from_user(&msf, (struct cdrom_msf __user *)arg, sizeof(msf)))
3095 return -EFAULT;
3147c531
MS
3096 cgc->cmd[0] = GPCMD_PLAY_AUDIO_MSF;
3097 cgc->cmd[3] = msf.cdmsf_min0;
3098 cgc->cmd[4] = msf.cdmsf_sec0;
3099 cgc->cmd[5] = msf.cdmsf_frame0;
3100 cgc->cmd[6] = msf.cdmsf_min1;
3101 cgc->cmd[7] = msf.cdmsf_sec1;
3102 cgc->cmd[8] = msf.cdmsf_frame1;
3103 cgc->data_direction = CGC_DATA_NONE;
3104 return cdo->generic_packet(cdi, cgc);
3105}
1da177e4 3106
3147c531 3107static noinline int mmc_ioctl_cdrom_play_blk(struct cdrom_device_info *cdi,
2e9aa08a
JP
3108 void __user *arg,
3109 struct packet_command *cgc)
3147c531 3110{
853fe1bf 3111 const struct cdrom_device_ops *cdo = cdi->ops;
3147c531 3112 struct cdrom_blk blk;
5944b2ce 3113 cd_dbg(CD_DO_IOCTL, "entering CDROMPLAYBLK\n");
a09c391d
JP
3114 if (copy_from_user(&blk, (struct cdrom_blk __user *)arg, sizeof(blk)))
3115 return -EFAULT;
3147c531
MS
3116 cgc->cmd[0] = GPCMD_PLAY_AUDIO_10;
3117 cgc->cmd[2] = (blk.from >> 24) & 0xff;
3118 cgc->cmd[3] = (blk.from >> 16) & 0xff;
3119 cgc->cmd[4] = (blk.from >> 8) & 0xff;
3120 cgc->cmd[5] = blk.from & 0xff;
3121 cgc->cmd[7] = (blk.len >> 8) & 0xff;
3122 cgc->cmd[8] = blk.len & 0xff;
3123 cgc->data_direction = CGC_DATA_NONE;
3124 return cdo->generic_packet(cdi, cgc);
3125}
1da177e4 3126
3147c531 3127static noinline int mmc_ioctl_cdrom_volume(struct cdrom_device_info *cdi,
2e9aa08a
JP
3128 void __user *arg,
3129 struct packet_command *cgc,
3130 unsigned int cmd)
3147c531
MS
3131{
3132 struct cdrom_volctrl volctrl;
3133 unsigned char buffer[32];
3134 char mask[sizeof(buffer)];
3135 unsigned short offset;
3136 int ret;
3137
5944b2ce 3138 cd_dbg(CD_DO_IOCTL, "entering CDROMVOLUME\n");
1da177e4 3139
a09c391d
JP
3140 if (copy_from_user(&volctrl, (struct cdrom_volctrl __user *)arg,
3141 sizeof(volctrl)))
3142 return -EFAULT;
1da177e4 3143
3147c531
MS
3144 cgc->buffer = buffer;
3145 cgc->buflen = 24;
3146 ret = cdrom_mode_sense(cdi, cgc, GPMODE_AUDIO_CTL_PAGE, 0);
3147 if (ret)
3148 return ret;
1da177e4 3149
3147c531
MS
3150 /* originally the code depended on buffer[1] to determine
3151 how much data is available for transfer. buffer[1] is
3152 unfortunately ambigious and the only reliable way seem
3153 to be to simply skip over the block descriptor... */
3154 offset = 8 + be16_to_cpu(*(__be16 *)(buffer + 6));
3155
3156 if (offset + 16 > sizeof(buffer))
3157 return -E2BIG;
3158
3159 if (offset + 16 > cgc->buflen) {
3160 cgc->buflen = offset + 16;
3161 ret = cdrom_mode_sense(cdi, cgc,
2e9aa08a 3162 GPMODE_AUDIO_CTL_PAGE, 0);
3147c531
MS
3163 if (ret)
3164 return ret;
3165 }
1da177e4 3166
3147c531
MS
3167 /* sanity check */
3168 if ((buffer[offset] & 0x3f) != GPMODE_AUDIO_CTL_PAGE ||
2e9aa08a 3169 buffer[offset + 1] < 14)
3147c531 3170 return -EINVAL;
1da177e4 3171
3147c531
MS
3172 /* now we have the current volume settings. if it was only
3173 a CDROMVOLREAD, return these values */
3174 if (cmd == CDROMVOLREAD) {
3175 volctrl.channel0 = buffer[offset+9];
3176 volctrl.channel1 = buffer[offset+11];
3177 volctrl.channel2 = buffer[offset+13];
3178 volctrl.channel3 = buffer[offset+15];
a09c391d
JP
3179 if (copy_to_user((struct cdrom_volctrl __user *)arg, &volctrl,
3180 sizeof(volctrl)))
3181 return -EFAULT;
3147c531
MS
3182 return 0;
3183 }
1da177e4 3184
3147c531
MS
3185 /* get the volume mask */
3186 cgc->buffer = mask;
3187 ret = cdrom_mode_sense(cdi, cgc, GPMODE_AUDIO_CTL_PAGE, 1);
3188 if (ret)
3189 return ret;
1da177e4 3190
3147c531
MS
3191 buffer[offset + 9] = volctrl.channel0 & mask[offset + 9];
3192 buffer[offset + 11] = volctrl.channel1 & mask[offset + 11];
3193 buffer[offset + 13] = volctrl.channel2 & mask[offset + 13];
3194 buffer[offset + 15] = volctrl.channel3 & mask[offset + 15];
1da177e4 3195
3147c531
MS
3196 /* set volume */
3197 cgc->buffer = buffer + offset - 8;
3198 memset(cgc->buffer, 0, 8);
3199 return cdrom_mode_select(cdi, cgc);
3200}
1da177e4 3201
3147c531 3202static noinline int mmc_ioctl_cdrom_start_stop(struct cdrom_device_info *cdi,
2e9aa08a
JP
3203 struct packet_command *cgc,
3204 int cmd)
3147c531 3205{
853fe1bf 3206 const struct cdrom_device_ops *cdo = cdi->ops;
5944b2ce 3207 cd_dbg(CD_DO_IOCTL, "entering CDROMSTART/CDROMSTOP\n");
3147c531
MS
3208 cgc->cmd[0] = GPCMD_START_STOP_UNIT;
3209 cgc->cmd[1] = 1;
3210 cgc->cmd[4] = (cmd == CDROMSTART) ? 1 : 0;
3211 cgc->data_direction = CGC_DATA_NONE;
3212 return cdo->generic_packet(cdi, cgc);
3213}
1da177e4 3214
3147c531 3215static noinline int mmc_ioctl_cdrom_pause_resume(struct cdrom_device_info *cdi,
2e9aa08a
JP
3216 struct packet_command *cgc,
3217 int cmd)
3147c531 3218{
853fe1bf 3219 const struct cdrom_device_ops *cdo = cdi->ops;
5944b2ce 3220 cd_dbg(CD_DO_IOCTL, "entering CDROMPAUSE/CDROMRESUME\n");
3147c531
MS
3221 cgc->cmd[0] = GPCMD_PAUSE_RESUME;
3222 cgc->cmd[8] = (cmd == CDROMRESUME) ? 1 : 0;
3223 cgc->data_direction = CGC_DATA_NONE;
3224 return cdo->generic_packet(cdi, cgc);
3225}
1da177e4 3226
3147c531 3227static noinline int mmc_ioctl_dvd_read_struct(struct cdrom_device_info *cdi,
2e9aa08a
JP
3228 void __user *arg,
3229 struct packet_command *cgc)
3147c531
MS
3230{
3231 int ret;
3232 dvd_struct *s;
3233 int size = sizeof(dvd_struct);
3234
3235 if (!CDROM_CAN(CDC_DVD))
3236 return -ENOSYS;
3237
abb0f6a7
AV
3238 s = memdup_user(arg, size);
3239 if (IS_ERR(s))
3240 return PTR_ERR(s);
3147c531 3241
5944b2ce 3242 cd_dbg(CD_DO_IOCTL, "entering DVD_READ_STRUCT\n");
3147c531 3243
d194139c 3244 ret = dvd_read_struct(cdi, s, cgc);
3147c531
MS
3245 if (ret)
3246 goto out;
3247
3248 if (copy_to_user(arg, s, size))
3249 ret = -EFAULT;
3250out:
3251 kfree(s);
3252 return ret;
3253}
3254
3255static noinline int mmc_ioctl_dvd_auth(struct cdrom_device_info *cdi,
2e9aa08a 3256 void __user *arg)
3147c531
MS
3257{
3258 int ret;
3259 dvd_authinfo ai;
3260 if (!CDROM_CAN(CDC_DVD))
3261 return -ENOSYS;
5944b2ce 3262 cd_dbg(CD_DO_IOCTL, "entering DVD_AUTH\n");
a09c391d
JP
3263 if (copy_from_user(&ai, (dvd_authinfo __user *)arg, sizeof(ai)))
3264 return -EFAULT;
3147c531
MS
3265 ret = dvd_do_auth(cdi, &ai);
3266 if (ret)
1da177e4 3267 return ret;
a09c391d
JP
3268 if (copy_to_user((dvd_authinfo __user *)arg, &ai, sizeof(ai)))
3269 return -EFAULT;
3147c531
MS
3270 return 0;
3271}
1da177e4 3272
3147c531 3273static noinline int mmc_ioctl_cdrom_next_writable(struct cdrom_device_info *cdi,
2e9aa08a 3274 void __user *arg)
3147c531
MS
3275{
3276 int ret;
3277 long next = 0;
5944b2ce 3278 cd_dbg(CD_DO_IOCTL, "entering CDROM_NEXT_WRITABLE\n");
3147c531
MS
3279 ret = cdrom_get_next_writable(cdi, &next);
3280 if (ret)
3281 return ret;
a09c391d
JP
3282 if (copy_to_user((long __user *)arg, &next, sizeof(next)))
3283 return -EFAULT;
3147c531
MS
3284 return 0;
3285}
1da177e4 3286
3147c531 3287static noinline int mmc_ioctl_cdrom_last_written(struct cdrom_device_info *cdi,
2e9aa08a 3288 void __user *arg)
3147c531
MS
3289{
3290 int ret;
3291 long last = 0;
5944b2ce 3292 cd_dbg(CD_DO_IOCTL, "entering CDROM_LAST_WRITTEN\n");
3147c531
MS
3293 ret = cdrom_get_last_written(cdi, &last);
3294 if (ret)
3295 return ret;
ab8bc541
AB
3296 if (in_compat_syscall())
3297 return put_user(last, (__s32 __user *)arg);
3298
3299 return put_user(last, (long __user *)arg);
3147c531
MS
3300}
3301
3302static int mmc_ioctl(struct cdrom_device_info *cdi, unsigned int cmd,
3303 unsigned long arg)
3304{
3305 struct packet_command cgc;
3306 void __user *userptr = (void __user *)arg;
3307
3308 memset(&cgc, 0, sizeof(cgc));
3309
3310 /* build a unified command and queue it through
3311 cdo->generic_packet() */
3312 switch (cmd) {
3313 case CDROMREADRAW:
3314 case CDROMREADMODE1:
3315 case CDROMREADMODE2:
3316 return mmc_ioctl_cdrom_read_data(cdi, userptr, &cgc, cmd);
3317 case CDROMREADAUDIO:
3318 return mmc_ioctl_cdrom_read_audio(cdi, userptr);
3319 case CDROMSUBCHNL:
3320 return mmc_ioctl_cdrom_subchannel(cdi, userptr);
3321 case CDROMPLAYMSF:
3322 return mmc_ioctl_cdrom_play_msf(cdi, userptr, &cgc);
3323 case CDROMPLAYBLK:
3324 return mmc_ioctl_cdrom_play_blk(cdi, userptr, &cgc);
3325 case CDROMVOLCTRL:
3326 case CDROMVOLREAD:
3327 return mmc_ioctl_cdrom_volume(cdi, userptr, &cgc, cmd);
3328 case CDROMSTART:
3329 case CDROMSTOP:
3330 return mmc_ioctl_cdrom_start_stop(cdi, &cgc, cmd);
3331 case CDROMPAUSE:
3332 case CDROMRESUME:
3333 return mmc_ioctl_cdrom_pause_resume(cdi, &cgc, cmd);
3334 case DVD_READ_STRUCT:
d194139c 3335 return mmc_ioctl_dvd_read_struct(cdi, userptr, &cgc);
3147c531
MS
3336 case DVD_AUTH:
3337 return mmc_ioctl_dvd_auth(cdi, userptr);
3338 case CDROM_NEXT_WRITABLE:
3339 return mmc_ioctl_cdrom_next_writable(cdi, userptr);
3340 case CDROM_LAST_WRITTEN:
3341 return mmc_ioctl_cdrom_last_written(cdi, userptr);
3342 }
1da177e4
LT
3343
3344 return -ENOTTY;
3345}
3346
2e9aa08a
JP
3347/*
3348 * Just about every imaginable ioctl is supported in the Uniform layer
3349 * these days.
3350 * ATAPI / SCSI specific code now mainly resides in mmc_ioctl().
3351 */
3352int cdrom_ioctl(struct cdrom_device_info *cdi, struct block_device *bdev,
3353 fmode_t mode, unsigned int cmd, unsigned long arg)
3354{
3355 void __user *argp = (void __user *)arg;
3356 int ret;
3357
3358 /*
3359 * Try the generic SCSI command ioctl's first.
3360 */
3361 ret = scsi_cmd_blk_ioctl(bdev, mode, cmd, argp);
3362 if (ret != -ENOTTY)
3363 return ret;
3364
3365 switch (cmd) {
3366 case CDROMMULTISESSION:
3367 return cdrom_ioctl_multisession(cdi, argp);
3368 case CDROMEJECT:
3369 return cdrom_ioctl_eject(cdi);
3370 case CDROMCLOSETRAY:
3371 return cdrom_ioctl_closetray(cdi);
3372 case CDROMEJECT_SW:
3373 return cdrom_ioctl_eject_sw(cdi, arg);
3374 case CDROM_MEDIA_CHANGED:
3375 return cdrom_ioctl_media_changed(cdi, arg);
3376 case CDROM_SET_OPTIONS:
3377 return cdrom_ioctl_set_options(cdi, arg);
3378 case CDROM_CLEAR_OPTIONS:
3379 return cdrom_ioctl_clear_options(cdi, arg);
3380 case CDROM_SELECT_SPEED:
3381 return cdrom_ioctl_select_speed(cdi, arg);
3382 case CDROM_SELECT_DISC:
3383 return cdrom_ioctl_select_disc(cdi, arg);
3384 case CDROMRESET:
3385 return cdrom_ioctl_reset(cdi, bdev);
3386 case CDROM_LOCKDOOR:
3387 return cdrom_ioctl_lock_door(cdi, arg);
3388 case CDROM_DEBUG:
3389 return cdrom_ioctl_debug(cdi, arg);
3390 case CDROM_GET_CAPABILITY:
3391 return cdrom_ioctl_get_capability(cdi);
3392 case CDROM_GET_MCN:
3393 return cdrom_ioctl_get_mcn(cdi, argp);
3394 case CDROM_DRIVE_STATUS:
3395 return cdrom_ioctl_drive_status(cdi, arg);
3396 case CDROM_DISC_STATUS:
3397 return cdrom_ioctl_disc_status(cdi);
3398 case CDROM_CHANGER_NSLOTS:
3399 return cdrom_ioctl_changer_nslots(cdi);
3400 }
3401
3402 /*
3403 * Use the ioctls that are implemented through the generic_packet()
3404 * interface. this may look at bit funny, but if -ENOTTY is
3405 * returned that particular ioctl is not implemented and we
3406 * let it go through the device specific ones.
3407 */
3408 if (CDROM_CAN(CDC_GENERIC_PACKET)) {
3409 ret = mmc_ioctl(cdi, cmd, arg);
3410 if (ret != -ENOTTY)
3411 return ret;
3412 }
3413
3414 /*
3415 * Note: most of the cd_dbg() calls are commented out here,
3416 * because they fill up the sys log when CD players poll
3417 * the drive.
3418 */
3419 switch (cmd) {
3420 case CDROMSUBCHNL:
3421 return cdrom_ioctl_get_subchnl(cdi, argp);
3422 case CDROMREADTOCHDR:
3423 return cdrom_ioctl_read_tochdr(cdi, argp);
3424 case CDROMREADTOCENTRY:
3425 return cdrom_ioctl_read_tocentry(cdi, argp);
3426 case CDROMPLAYMSF:
3427 return cdrom_ioctl_play_msf(cdi, argp);
3428 case CDROMPLAYTRKIND:
3429 return cdrom_ioctl_play_trkind(cdi, argp);
3430 case CDROMVOLCTRL:
3431 return cdrom_ioctl_volctrl(cdi, argp);
3432 case CDROMVOLREAD:
3433 return cdrom_ioctl_volread(cdi, argp);
3434 case CDROMSTART:
3435 case CDROMSTOP:
3436 case CDROMPAUSE:
3437 case CDROMRESUME:
3438 return cdrom_ioctl_audioctl(cdi, cmd);
3439 }
3440
3441 return -ENOSYS;
3442}
3443
1da177e4
LT
3444EXPORT_SYMBOL(cdrom_get_last_written);
3445EXPORT_SYMBOL(register_cdrom);
3446EXPORT_SYMBOL(unregister_cdrom);
3447EXPORT_SYMBOL(cdrom_open);
3448EXPORT_SYMBOL(cdrom_release);
3449EXPORT_SYMBOL(cdrom_ioctl);
3450EXPORT_SYMBOL(cdrom_media_changed);
3451EXPORT_SYMBOL(cdrom_number_of_slots);
3452EXPORT_SYMBOL(cdrom_mode_select);
3453EXPORT_SYMBOL(cdrom_mode_sense);
3454EXPORT_SYMBOL(init_cdrom_command);
3455EXPORT_SYMBOL(cdrom_get_media_event);
3456
3457#ifdef CONFIG_SYSCTL
3458
3459#define CDROM_STR_SIZE 1000
3460
3461static struct cdrom_sysctl_settings {
3462 char info[CDROM_STR_SIZE]; /* general info */
3463 int autoclose; /* close tray upon mount, etc */
3464 int autoeject; /* eject on umount */
3465 int debug; /* turn on debugging messages */
3466 int lock; /* lock the door on device open */
3467 int check; /* check media type */
3468} cdrom_sysctl_settings;
3469
554988d6
DY
3470enum cdrom_print_option {
3471 CTL_NAME,
3472 CTL_SPEED,
3473 CTL_SLOTS,
3474 CTL_CAPABILITY
3475};
3476
3477static int cdrom_print_info(const char *header, int val, char *info,
3478 int *pos, enum cdrom_print_option option)
3479{
3480 const int max_size = sizeof(cdrom_sysctl_settings.info);
3481 struct cdrom_device_info *cdi;
3482 int ret;
3483
3484 ret = scnprintf(info + *pos, max_size - *pos, header);
3485 if (!ret)
3486 return 1;
3487
3488 *pos += ret;
3489
7fd097d4 3490 list_for_each_entry(cdi, &cdrom_list, list) {
554988d6
DY
3491 switch (option) {
3492 case CTL_NAME:
3493 ret = scnprintf(info + *pos, max_size - *pos,
3494 "\t%s", cdi->name);
3495 break;
3496 case CTL_SPEED:
3497 ret = scnprintf(info + *pos, max_size - *pos,
3498 "\t%d", cdi->speed);
3499 break;
3500 case CTL_SLOTS:
3501 ret = scnprintf(info + *pos, max_size - *pos,
3502 "\t%d", cdi->capacity);
3503 break;
3504 case CTL_CAPABILITY:
3505 ret = scnprintf(info + *pos, max_size - *pos,
3506 "\t%d", CDROM_CAN(val) != 0);
3507 break;
3508 default:
e597cd09 3509 pr_info("invalid option%d\n", option);
554988d6
DY
3510 return 1;
3511 }
3512 if (!ret)
3513 return 1;
3514 *pos += ret;
3515 }
3516
3517 return 0;
3518}
3519
90a3b89e 3520static int cdrom_sysctl_info(struct ctl_table *ctl, int write,
1da177e4
LT
3521 void __user *buffer, size_t *lenp, loff_t *ppos)
3522{
554988d6 3523 int pos;
1da177e4 3524 char *info = cdrom_sysctl_settings.info;
554988d6 3525 const int max_size = sizeof(cdrom_sysctl_settings.info);
1da177e4
LT
3526
3527 if (!*lenp || (*ppos && !write)) {
3528 *lenp = 0;
3529 return 0;
3530 }
3531
554988d6
DY
3532 mutex_lock(&cdrom_mutex);
3533
1da177e4
LT
3534 pos = sprintf(info, "CD-ROM information, " VERSION "\n");
3535
554988d6
DY
3536 if (cdrom_print_info("\ndrive name:\t", 0, info, &pos, CTL_NAME))
3537 goto done;
3538 if (cdrom_print_info("\ndrive speed:\t", 0, info, &pos, CTL_SPEED))
3539 goto done;
3540 if (cdrom_print_info("\ndrive # of slots:", 0, info, &pos, CTL_SLOTS))
3541 goto done;
3542 if (cdrom_print_info("\nCan close tray:\t",
3543 CDC_CLOSE_TRAY, info, &pos, CTL_CAPABILITY))
3544 goto done;
3545 if (cdrom_print_info("\nCan open tray:\t",
3546 CDC_OPEN_TRAY, info, &pos, CTL_CAPABILITY))
3547 goto done;
3548 if (cdrom_print_info("\nCan lock tray:\t",
3549 CDC_LOCK, info, &pos, CTL_CAPABILITY))
3550 goto done;
3551 if (cdrom_print_info("\nCan change speed:",
3552 CDC_SELECT_SPEED, info, &pos, CTL_CAPABILITY))
3553 goto done;
3554 if (cdrom_print_info("\nCan select disk:",
3555 CDC_SELECT_DISC, info, &pos, CTL_CAPABILITY))
3556 goto done;
3557 if (cdrom_print_info("\nCan read multisession:",
3558 CDC_MULTI_SESSION, info, &pos, CTL_CAPABILITY))
3559 goto done;
3560 if (cdrom_print_info("\nCan read MCN:\t",
3561 CDC_MCN, info, &pos, CTL_CAPABILITY))
3562 goto done;
3563 if (cdrom_print_info("\nReports media changed:",
3564 CDC_MEDIA_CHANGED, info, &pos, CTL_CAPABILITY))
3565 goto done;
3566 if (cdrom_print_info("\nCan play audio:\t",
3567 CDC_PLAY_AUDIO, info, &pos, CTL_CAPABILITY))
3568 goto done;
3569 if (cdrom_print_info("\nCan write CD-R:\t",
3570 CDC_CD_R, info, &pos, CTL_CAPABILITY))
3571 goto done;
3572 if (cdrom_print_info("\nCan write CD-RW:",
3573 CDC_CD_RW, info, &pos, CTL_CAPABILITY))
3574 goto done;
3575 if (cdrom_print_info("\nCan read DVD:\t",
3576 CDC_DVD, info, &pos, CTL_CAPABILITY))
3577 goto done;
3578 if (cdrom_print_info("\nCan write DVD-R:",
3579 CDC_DVD_R, info, &pos, CTL_CAPABILITY))
3580 goto done;
3581 if (cdrom_print_info("\nCan write DVD-RAM:",
3582 CDC_DVD_RAM, info, &pos, CTL_CAPABILITY))
3583 goto done;
3584 if (cdrom_print_info("\nCan read MRW:\t",
3585 CDC_MRW, info, &pos, CTL_CAPABILITY))
3586 goto done;
3587 if (cdrom_print_info("\nCan write MRW:\t",
3588 CDC_MRW_W, info, &pos, CTL_CAPABILITY))
3589 goto done;
3590 if (cdrom_print_info("\nCan write RAM:\t",
3591 CDC_RAM, info, &pos, CTL_CAPABILITY))
3592 goto done;
3593 if (!scnprintf(info + pos, max_size - pos, "\n\n"))
3594 goto done;
3595doit:
3596 mutex_unlock(&cdrom_mutex);
8d65af78 3597 return proc_dostring(ctl, write, buffer, lenp, ppos);
554988d6 3598done:
e597cd09 3599 pr_info("info buffer too small\n");
554988d6 3600 goto doit;
1da177e4
LT
3601}
3602
3603/* Unfortunately, per device settings are not implemented through
3604 procfs/sysctl yet. When they are, this will naturally disappear. For now
3605 just update all drives. Later this will become the template on which
3606 new registered drives will be based. */
3607static void cdrom_update_settings(void)
3608{
3609 struct cdrom_device_info *cdi;
3610
032d8d90 3611 mutex_lock(&cdrom_mutex);
7fd097d4 3612 list_for_each_entry(cdi, &cdrom_list, list) {
1da177e4
LT
3613 if (autoclose && CDROM_CAN(CDC_CLOSE_TRAY))
3614 cdi->options |= CDO_AUTO_CLOSE;
3615 else if (!autoclose)
3616 cdi->options &= ~CDO_AUTO_CLOSE;
3617 if (autoeject && CDROM_CAN(CDC_OPEN_TRAY))
3618 cdi->options |= CDO_AUTO_EJECT;
3619 else if (!autoeject)
3620 cdi->options &= ~CDO_AUTO_EJECT;
3621 if (lockdoor && CDROM_CAN(CDC_LOCK))
3622 cdi->options |= CDO_LOCK;
3623 else if (!lockdoor)
3624 cdi->options &= ~CDO_LOCK;
3625 if (check_media_type)
3626 cdi->options |= CDO_CHECK_TYPE;
3627 else
3628 cdi->options &= ~CDO_CHECK_TYPE;
3629 }
032d8d90 3630 mutex_unlock(&cdrom_mutex);
1da177e4
LT
3631}
3632
90a3b89e 3633static int cdrom_sysctl_handler(struct ctl_table *ctl, int write,
1da177e4
LT
3634 void __user *buffer, size_t *lenp, loff_t *ppos)
3635{
1da177e4
LT
3636 int ret;
3637
8d65af78 3638 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
1da177e4 3639
06489b4e 3640 if (write) {
1da177e4
LT
3641
3642 /* we only care for 1 or 0. */
06489b4e
EB
3643 autoclose = !!cdrom_sysctl_settings.autoclose;
3644 autoeject = !!cdrom_sysctl_settings.autoeject;
3645 debug = !!cdrom_sysctl_settings.debug;
3646 lockdoor = !!cdrom_sysctl_settings.lock;
3647 check_media_type = !!cdrom_sysctl_settings.check;
1da177e4 3648
1da177e4
LT
3649 /* update the option flags according to the changes. we
3650 don't have per device options through sysctl yet,
3651 but we will have and then this will disappear. */
3652 cdrom_update_settings();
3653 }
3654
3655 return ret;
3656}
3657
3658/* Place files in /proc/sys/dev/cdrom */
90a3b89e 3659static struct ctl_table cdrom_table[] = {
1da177e4 3660 {
1da177e4
LT
3661 .procname = "info",
3662 .data = &cdrom_sysctl_settings.info,
3663 .maxlen = CDROM_STR_SIZE,
3664 .mode = 0444,
6d456111 3665 .proc_handler = cdrom_sysctl_info,
1da177e4
LT
3666 },
3667 {
1da177e4
LT
3668 .procname = "autoclose",
3669 .data = &cdrom_sysctl_settings.autoclose,
3670 .maxlen = sizeof(int),
3671 .mode = 0644,
6d456111 3672 .proc_handler = cdrom_sysctl_handler,
1da177e4
LT
3673 },
3674 {
1da177e4
LT
3675 .procname = "autoeject",
3676 .data = &cdrom_sysctl_settings.autoeject,
3677 .maxlen = sizeof(int),
3678 .mode = 0644,
6d456111 3679 .proc_handler = cdrom_sysctl_handler,
1da177e4
LT
3680 },
3681 {
1da177e4
LT
3682 .procname = "debug",
3683 .data = &cdrom_sysctl_settings.debug,
3684 .maxlen = sizeof(int),
3685 .mode = 0644,
6d456111 3686 .proc_handler = cdrom_sysctl_handler,
1da177e4
LT
3687 },
3688 {
1da177e4
LT
3689 .procname = "lock",
3690 .data = &cdrom_sysctl_settings.lock,
3691 .maxlen = sizeof(int),
3692 .mode = 0644,
6d456111 3693 .proc_handler = cdrom_sysctl_handler,
1da177e4
LT
3694 },
3695 {
1da177e4
LT
3696 .procname = "check_media",
3697 .data = &cdrom_sysctl_settings.check,
3698 .maxlen = sizeof(int),
3699 .mode = 0644,
6d456111 3700 .proc_handler = cdrom_sysctl_handler
1da177e4 3701 },
894d2491 3702 { }
1da177e4
LT
3703};
3704
90a3b89e 3705static struct ctl_table cdrom_cdrom_table[] = {
1da177e4 3706 {
1da177e4
LT
3707 .procname = "cdrom",
3708 .maxlen = 0,
3709 .mode = 0555,
3710 .child = cdrom_table,
3711 },
894d2491 3712 { }
1da177e4
LT
3713};
3714
3715/* Make sure that /proc/sys/dev is there */
90a3b89e 3716static struct ctl_table cdrom_root_table[] = {
1da177e4 3717 {
1da177e4
LT
3718 .procname = "dev",
3719 .maxlen = 0,
3720 .mode = 0555,
3721 .child = cdrom_cdrom_table,
3722 },
894d2491 3723 { }
1da177e4
LT
3724};
3725static struct ctl_table_header *cdrom_sysctl_header;
3726
3727static void cdrom_sysctl_register(void)
3728{
f25191bb 3729 static atomic_t initialized = ATOMIC_INIT(0);
1da177e4 3730
f25191bb 3731 if (!atomic_add_unless(&initialized, 1, 1))
1da177e4
LT
3732 return;
3733
0b4d4147 3734 cdrom_sysctl_header = register_sysctl_table(cdrom_root_table);
1da177e4
LT
3735
3736 /* set the defaults */
3737 cdrom_sysctl_settings.autoclose = autoclose;
3738 cdrom_sysctl_settings.autoeject = autoeject;
3739 cdrom_sysctl_settings.debug = debug;
3740 cdrom_sysctl_settings.lock = lockdoor;
3741 cdrom_sysctl_settings.check = check_media_type;
1da177e4
LT
3742}
3743
3744static void cdrom_sysctl_unregister(void)
3745{
3746 if (cdrom_sysctl_header)
3747 unregister_sysctl_table(cdrom_sysctl_header);
3748}
3749
17672cf0
AM
3750#else /* CONFIG_SYSCTL */
3751
3752static void cdrom_sysctl_register(void)
3753{
3754}
3755
3756static void cdrom_sysctl_unregister(void)
3757{
3758}
3759
1da177e4
LT
3760#endif /* CONFIG_SYSCTL */
3761
3762static int __init cdrom_init(void)
3763{
1da177e4 3764 cdrom_sysctl_register();
17672cf0 3765
1da177e4
LT
3766 return 0;
3767}
3768
3769static void __exit cdrom_exit(void)
3770{
e597cd09 3771 pr_info("Uniform CD-ROM driver unloaded\n");
1da177e4 3772 cdrom_sysctl_unregister();
1da177e4
LT
3773}
3774
3775module_init(cdrom_init);
3776module_exit(cdrom_exit);
3777MODULE_LICENSE("GPL");