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