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>
6 May be copied or modified under the terms of the GNU General Public
7 License. See linux/COPYING for more information.
9 Uniform CD-ROM driver for Linux.
10 See Documentation/cdrom/cdrom-standard.tex for usage information.
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. ;-)
18 ----------------------------------
20 -- Modify sysctl/proc interface. I plan on having one directory per
21 drive, with entries for outputing general drive information, and sysctl
22 based tunable parameters such as whether the tray should auto-close for
23 that drive. Suggestions (or patches) for this welcome!
27 ----------------------------------
28 1.00 Date Unknown -- David van Leeuwen <david@tm.tno.nl>
29 -- Initial version by David A. van Leeuwen. I don't have a detailed
30 changelog for the 1.x series, David?
32 2.00 Dec 2, 1997 -- Erik Andersen <andersee@debian.org>
33 -- New maintainer! As David A. van Leeuwen has been too busy to activly
34 maintain and improve this driver, I am now carrying on the torch. If
35 you have a problem with this driver, please feel free to contact me.
37 -- Added (rudimentary) sysctl interface. I realize this is really weak
38 right now, and is _very_ badly implemented. It will be improved...
40 -- Modified CDROM_DISC_STATUS so that it is now incorporated into
41 the Uniform CD-ROM driver via the cdrom_count_tracks function.
42 The cdrom_count_tracks function helps resolve some of the false
43 assumptions of the CDROM_DISC_STATUS ioctl, and is also used to check
44 for the correct media type when mounting or playing audio from a CD.
46 -- Remove the calls to verify_area and only use the copy_from_user and
47 copy_to_user stuff, since these calls now provide their own memory
48 checking with the 2.1.x kernels.
50 -- Major update to return codes so that errors from low-level drivers
51 are passed on through (thanks to Gerd Knorr for pointing out this
54 -- Made it so if a function isn't implemented in a low-level driver,
55 ENOSYS is now returned instead of EINVAL.
57 -- Simplified some complex logic so that the source code is easier to read.
59 -- Other stuff I probably forgot to mention (lots of changes).
61 2.01 to 2.11 Dec 1997-Jan 1998
62 -- TO-DO! Write changelogs for 2.01 to 2.12.
64 2.12 Jan 24, 1998 -- Erik Andersen <andersee@debian.org>
65 -- Fixed a bug in the IOCTL_IN and IOCTL_OUT macros. It turns out that
66 copy_*_user does not return EFAULT on error, but instead returns the number
67 of bytes not copied. I was returning whatever non-zero stuff came back from
68 the copy_*_user functions directly, which would result in strange errors.
70 2.13 July 17, 1998 -- Erik Andersen <andersee@debian.org>
71 -- Fixed a bug in CDROM_SELECT_SPEED where you couldn't lower the speed
72 of the drive. Thanks to Tobias Ringstr|m <tori@prosolvia.se> for pointing
73 this out and providing a simple fix.
74 -- Fixed the procfs-unload-module bug with the fill_inode procfs callback.
75 thanks to Andrea Arcangeli
76 -- Fixed it so that the /proc entry now also shows up when cdrom is
77 compiled into the kernel. Before it only worked when loaded as a module.
79 2.14 August 17, 1998 -- Erik Andersen <andersee@debian.org>
80 -- Fixed a bug in cdrom_media_changed and handling of reporting that
81 the media had changed for devices that _don't_ implement media_changed.
82 Thanks to Grant R. Guenther <grant@torque.net> for spotting this bug.
83 -- Made a few things more pedanticly correct.
85 2.50 Oct 19, 1998 - Jens Axboe <axboe@image.dk>
86 -- New maintainers! Erik was too busy to continue the work on the driver,
87 so now Chris Zwilling <chris@cloudnet.com> and Jens Axboe <axboe@image.dk>
88 will do their best to follow in his footsteps
90 2.51 Dec 20, 1998 - Jens Axboe <axboe@image.dk>
91 -- Check if drive is capable of doing what we ask before blindly changing
92 cdi->options in various ioctl.
93 -- Added version to proc entry.
95 2.52 Jan 16, 1999 - Jens Axboe <axboe@image.dk>
96 -- Fixed an error in open_for_data where we would sometimes not return
97 the correct error value. Thanks Huba Gaspar <huba@softcell.hu>.
98 -- Fixed module usage count - usage was based on /proc/sys/dev
99 instead of /proc/sys/dev/cdrom. This could lead to an oops when other
100 modules had entries in dev. Feb 02 - real bug was in sysctl.c where
101 dev would be removed even though it was used. cdrom.c just illuminated
104 2.53 Feb 22, 1999 - Jens Axboe <axboe@image.dk>
105 -- Fixup of several ioctl calls, in particular CDROM_SET_OPTIONS has
106 been "rewritten" because capabilities and options aren't in sync. They
108 -- Added CDROM_LOCKDOOR ioctl. Locks the door and keeps it that way.
109 -- Added CDROM_RESET ioctl.
110 -- Added CDROM_DEBUG ioctl. Enable debug messages on-the-fly.
111 -- Added CDROM_GET_CAPABILITY ioctl. This relieves userspace programs
112 from parsing /proc/sys/dev/cdrom/info.
114 2.54 Mar 15, 1999 - Jens Axboe <axboe@image.dk>
115 -- Check capability mask from low level driver when counting tracks as
116 per suggestion from Corey J. Scotts <cstotts@blue.weeg.uiowa.edu>.
118 2.55 Apr 25, 1999 - Jens Axboe <axboe@image.dk>
119 -- autoclose was mistakenly checked against CDC_OPEN_TRAY instead of
121 -- proc info didn't mask against capabilities mask.
123 3.00 Aug 5, 1999 - Jens Axboe <axboe@image.dk>
124 -- Unified audio ioctl handling across CD-ROM drivers. A lot of the
125 code was duplicated before. Drives that support the generic packet
126 interface are now being fed packets from here instead.
127 -- First attempt at adding support for MMC2 commands - for DVD and
128 CD-R(W) drives. Only the DVD parts are in now - the interface used is
129 the same as for the audio ioctls.
130 -- ioctl cleanups. if a drive couldn't play audio, it didn't get
131 a change to perform device specific ioctls as well.
132 -- Defined CDROM_CAN(CDC_XXX) for checking the capabilities.
133 -- Put in sysctl files for autoclose, autoeject, check_media, debug,
135 -- /proc/sys/dev/cdrom/info has been updated to also contain info about
136 CD-Rx and DVD capabilities.
137 -- Now default to checking media type.
138 -- CDROM_SEND_PACKET ioctl added. The infrastructure was in place for
139 doing this anyway, with the generic_packet addition.
141 3.01 Aug 6, 1999 - Jens Axboe <axboe@image.dk>
142 -- Fix up the sysctl handling so that the option flags get set
144 -- Fix up ioctl handling so the device specific ones actually get
147 3.02 Aug 8, 1999 - Jens Axboe <axboe@image.dk>
148 -- Fixed volume control on SCSI drives (or others with longer audio
150 -- Fixed a couple of DVD minors. Thanks to Andrew T. Veliath
151 <andrewtv@usa.net> for telling me and for having defined the various
152 DVD structures and ioctls in the first place! He designed the original
153 DVD patches for ide-cd and while I rearranged and unified them, the
154 interface is still the same.
156 3.03 Sep 1, 1999 - Jens Axboe <axboe@image.dk>
157 -- Moved the rest of the audio ioctls from the CD-ROM drivers here. Only
158 CDROMREADTOCENTRY and CDROMREADTOCHDR are left.
159 -- Moved the CDROMREADxxx ioctls in here.
160 -- Defined the cdrom_get_last_written and cdrom_get_next_block as ioctls
161 and exported functions.
162 -- Erik Andersen <andersen@xmission.com> modified all SCMD_ commands
163 to now read GPCMD_ for the new generic packet interface. All low level
164 drivers are updated as well.
165 -- Various other cleanups.
167 3.04 Sep 12, 1999 - Jens Axboe <axboe@image.dk>
168 -- Fixed a couple of possible memory leaks (if an operation failed and
169 we didn't free the buffer before returning the error).
170 -- Integrated Uniform CD Changer handling from Richard Sharman
171 <rsharman@pobox.com>.
172 -- Defined CD_DVD and CD_CHANGER log levels.
173 -- Fixed the CDROMREADxxx ioctls.
174 -- CDROMPLAYTRKIND uses the GPCMD_PLAY_AUDIO_MSF command - too few
175 drives supported it. We lose the index part, however.
176 -- Small modifications to accommodate opens of /dev/hdc1, required
177 for ide-cd to handle multisession discs.
178 -- Export cdrom_mode_sense and cdrom_mode_select.
179 -- init_cdrom_command() for setting up a cgc command.
181 3.05 Oct 24, 1999 - Jens Axboe <axboe@image.dk>
182 -- Changed the interface for CDROM_SEND_PACKET. Before it was virtually
183 impossible to send the drive data in a sensible way.
184 -- Lowered stack usage in mmc_ioctl(), dvd_read_disckey(), and
186 -- Added setup of write mode for packet writing.
187 -- Fixed CDDA ripping with cdda2wav - accept much larger requests of
188 number of frames and split the reads in blocks of 8.
190 3.06 Dec 13, 1999 - Jens Axboe <axboe@image.dk>
191 -- Added support for changing the region of DVD drives.
192 -- Added sense data to generic command.
194 3.07 Feb 2, 2000 - Jens Axboe <axboe@suse.de>
195 -- Do same "read header length" trick in cdrom_get_disc_info() as
196 we do in cdrom_get_track_info() -- some drive don't obey specs and
197 fail if they can't supply the full Mt Fuji size table.
198 -- Deleted stuff related to setting up write modes. It has a different
200 -- Clear header length in mode_select unconditionally.
201 -- Removed the register_disk() that was added, not needed here.
203 3.08 May 1, 2000 - Jens Axboe <axboe@suse.de>
204 -- Fix direction flag in setup_send_key and setup_report_key. This
205 gave some SCSI adapters problems.
206 -- Always return -EROFS for write opens
207 -- Convert to module_init/module_exit style init and remove some
208 of the #ifdef MODULE stuff
209 -- Fix several dvd errors - DVD_LU_SEND_ASF should pass agid,
210 DVD_HOST_SEND_RPC_STATE did not set buffer size in cdb, and
211 dvd_do_auth passed uninitialized data to drive because init_cdrom_command
212 did not clear a 0 sized buffer.
214 3.09 May 12, 2000 - Jens Axboe <axboe@suse.de>
215 -- Fix Video-CD on SCSI drives that don't support READ_CD command. In
216 that case switch block size and issue plain READ_10 again, then switch
219 3.10 Jun 10, 2000 - Jens Axboe <axboe@suse.de>
220 -- Fix volume control on CD's - old SCSI-II drives now use their own
221 code, as doing MODE6 stuff in here is really not my intention.
222 -- Use READ_DISC_INFO for more reliable end-of-disc.
224 3.11 Jun 12, 2000 - Jens Axboe <axboe@suse.de>
225 -- Fix bug in getting rpc phase 2 region info.
226 -- Reinstate "correct" CDROMPLAYTRKIND
228 3.12 Oct 18, 2000 - Jens Axboe <axboe@suse.de>
229 -- Use quiet bit on packet commands not known to work
231 3.20 Dec 17, 2003 - Jens Axboe <axboe@suse.de>
232 -- Various fixes and lots of cleanups not listed :-)
234 -- Mt Rainier support
235 -- DVD-RAM write open fixes
237 -------------------------------------------------------------------------*/
239 #define REVISION "Revision: 3.20"
240 #define VERSION "Id: cdrom.c 3.20 2003/12/17"
242 /* I use an error-log mask to give fine grain control over the type of
243 messages dumped to the system logs. The available masks include: */
244 #define CD_NOTHING 0x0
245 #define CD_WARNING 0x1
246 #define CD_REG_UNREG 0x2
247 #define CD_DO_IOCTL 0x4
249 #define CD_CLOSE 0x10
250 #define CD_COUNT_TRACKS 0x20
251 #define CD_CHANGER 0x40
254 /* Define this to remove _all_ the debugging messages */
255 /* #define ERRLOGMASK CD_NOTHING */
256 #define ERRLOGMASK CD_WARNING
257 /* #define ERRLOGMASK (CD_WARNING|CD_OPEN|CD_COUNT_TRACKS|CD_CLOSE) */
258 /* #define ERRLOGMASK (CD_WARNING|CD_REG_UNREG|CD_DO_IOCTL|CD_OPEN|CD_CLOSE|CD_COUNT_TRACKS) */
260 #include <linux/config.h>
261 #include <linux/module.h>
262 #include <linux/fs.h>
263 #include <linux/buffer_head.h>
264 #include <linux/major.h>
265 #include <linux/types.h>
266 #include <linux/errno.h>
267 #include <linux/kernel.h>
268 #include <linux/mm.h>
269 #include <linux/slab.h>
270 #include <linux/cdrom.h>
271 #include <linux/sysctl.h>
272 #include <linux/proc_fs.h>
273 #include <linux/blkpg.h>
274 #include <linux/init.h>
275 #include <linux/fcntl.h>
276 #include <linux/blkdev.h>
277 #include <linux/times.h>
279 #include <asm/uaccess.h>
281 /* used to tell the module to turn on full debugging messages */
283 /* used to keep tray locked at all times */
284 static int keeplocked;
285 /* default compatibility mode */
286 static int autoclose=1;
287 static int autoeject;
288 static int lockdoor = 1;
289 /* will we ever get to use this... sigh. */
290 static int check_media_type;
291 /* automatically restart mrw format */
292 static int mrw_format_restart = 1;
293 MODULE_PARM(debug, "i");
294 MODULE_PARM(autoclose, "i");
295 MODULE_PARM(autoeject, "i");
296 MODULE_PARM(lockdoor, "i");
297 MODULE_PARM(check_media_type, "i");
298 MODULE_PARM(mrw_format_restart, "i");
300 static spinlock_t cdrom_lock = SPIN_LOCK_UNLOCKED;
302 static const char *mrw_format_status[] = {
309 static const char *mrw_address_space[] = { "DMA", "GAA" };
311 #if (ERRLOGMASK!=CD_NOTHING)
312 #define cdinfo(type, fmt, args...) \
313 if ((ERRLOGMASK & type) || debug==1 ) \
314 printk(KERN_INFO "cdrom: " fmt, ## args)
316 #define cdinfo(type, fmt, args...)
319 /* These are used to simplify getting data in from and back to user land */
320 #define IOCTL_IN(arg, type, in) \
321 if (copy_from_user(&(in), (type __user *) (arg), sizeof (in))) \
324 #define IOCTL_OUT(arg, type, out) \
325 if (copy_to_user((type __user *) (arg), &(out), sizeof (out))) \
328 /* The (cdo->capability & ~cdi->mask & CDC_XXX) construct was used in
329 a lot of places. This macro makes the code more clear. */
330 #define CDROM_CAN(type) (cdi->ops->capability & ~cdi->mask & (type))
332 /* used in the audio ioctls */
333 #define CHECKAUDIO if ((ret=check_for_audio_disc(cdi, cdo))) return ret
335 /* Not-exported routines. */
336 static int open_for_data(struct cdrom_device_info * cdi);
337 static int check_for_audio_disc(struct cdrom_device_info * cdi,
338 struct cdrom_device_ops * cdo);
339 static void sanitize_format(union cdrom_addr *addr,
340 u_char * curr, u_char requested);
341 static int mmc_ioctl(struct cdrom_device_info *cdi, unsigned int cmd,
344 int cdrom_get_last_written(struct cdrom_device_info *, long *);
345 static int cdrom_get_next_writable(struct cdrom_device_info *, long *);
346 static void cdrom_count_tracks(struct cdrom_device_info *, tracktype*);
348 static int cdrom_mrw_exit(struct cdrom_device_info *cdi);
350 static int cdrom_get_disc_info(struct cdrom_device_info *cdi, disc_information *di);
353 static void cdrom_sysctl_register(void);
354 #endif /* CONFIG_SYSCTL */
355 static struct cdrom_device_info *topCdromPtr;
357 /* This macro makes sure we don't have to check on cdrom_device_ops
358 * existence in the run-time routines below. Change_capability is a
359 * hack to have the capability flags defined const, while we can still
360 * change it here without gcc complaining at every line.
362 #define ENSURE(call, bits) if (cdo->call == NULL) *change_capability &= ~(bits)
364 int register_cdrom(struct cdrom_device_info *cdi)
366 static char banner_printed;
367 struct cdrom_device_ops *cdo = cdi->ops;
368 int *change_capability = (int *)&cdo->capability; /* hack */
370 cdinfo(CD_OPEN, "entering register_cdrom\n");
372 if (cdo->open == NULL || cdo->release == NULL)
374 if (!banner_printed) {
375 printk(KERN_INFO "Uniform CD-ROM driver " REVISION "\n");
378 cdrom_sysctl_register();
379 #endif /* CONFIG_SYSCTL */
382 ENSURE(drive_status, CDC_DRIVE_STATUS );
383 ENSURE(media_changed, CDC_MEDIA_CHANGED);
384 ENSURE(tray_move, CDC_CLOSE_TRAY | CDC_OPEN_TRAY);
385 ENSURE(lock_door, CDC_LOCK);
386 ENSURE(select_speed, CDC_SELECT_SPEED);
387 ENSURE(get_last_session, CDC_MULTI_SESSION);
388 ENSURE(get_mcn, CDC_MCN);
389 ENSURE(reset, CDC_RESET);
390 ENSURE(audio_ioctl, CDC_PLAY_AUDIO);
391 ENSURE(dev_ioctl, CDC_IOCTLS);
392 ENSURE(generic_packet, CDC_GENERIC_PACKET);
395 cdi->options = CDO_USE_FFLAGS;
397 if (autoclose==1 && CDROM_CAN(CDC_CLOSE_TRAY))
398 cdi->options |= (int) CDO_AUTO_CLOSE;
399 if (autoeject==1 && CDROM_CAN(CDC_OPEN_TRAY))
400 cdi->options |= (int) CDO_AUTO_EJECT;
402 cdi->options |= (int) CDO_LOCK;
403 if (check_media_type==1)
404 cdi->options |= (int) CDO_CHECK_TYPE;
406 if (CDROM_CAN(CDC_MRW_W))
407 cdi->exit = cdrom_mrw_exit;
410 cdi->cdda_method = CDDA_BPC_FULL;
412 cdi->cdda_method = CDDA_OLD;
414 cdinfo(CD_REG_UNREG, "drive \"/dev/%s\" registered\n", cdi->name);
415 spin_lock(&cdrom_lock);
416 cdi->next = topCdromPtr;
418 spin_unlock(&cdrom_lock);
423 int unregister_cdrom(struct cdrom_device_info *unreg)
425 struct cdrom_device_info *cdi, *prev;
426 cdinfo(CD_OPEN, "entering unregister_cdrom\n");
429 spin_lock(&cdrom_lock);
431 while (cdi && cdi != unreg) {
437 spin_unlock(&cdrom_lock);
441 prev->next = cdi->next;
443 topCdromPtr = cdi->next;
445 spin_unlock(&cdrom_lock);
450 cdi->ops->n_minors--;
451 cdinfo(CD_REG_UNREG, "drive \"/dev/%s\" unregistered\n", cdi->name);
455 int cdrom_get_media_event(struct cdrom_device_info *cdi,
456 struct media_event_desc *med)
458 struct packet_command cgc;
459 unsigned char buffer[8];
460 struct event_header *eh = (struct event_header *) buffer;
462 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_READ);
463 cgc.cmd[0] = GPCMD_GET_EVENT_STATUS_NOTIFICATION;
464 cgc.cmd[1] = 1; /* IMMED */
465 cgc.cmd[4] = 1 << 4; /* media event */
466 cgc.cmd[8] = sizeof(buffer);
469 if (cdi->ops->generic_packet(cdi, &cgc))
472 if (be16_to_cpu(eh->data_len) < sizeof(*med))
475 memcpy(med, &buffer[sizeof(*eh)], sizeof(*med));
480 * the first prototypes used 0x2c as the page code for the mrw mode page,
481 * subsequently this was changed to 0x03. probe the one used by this drive
483 int cdrom_mrw_probe_pc(struct cdrom_device_info *cdi)
485 struct packet_command cgc;
488 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_READ);
493 if (!cdrom_mode_sense(cdi, &cgc, MRW_MODE_PC, 0)) {
494 cdi->mrw_mode_page = MRW_MODE_PC;
496 } else if (!cdrom_mode_sense(cdi, &cgc, MRW_MODE_PC_PRE1, 0)) {
497 cdi->mrw_mode_page = MRW_MODE_PC_PRE1;
504 int cdrom_is_mrw(struct cdrom_device_info *cdi, int *write)
506 struct packet_command cgc;
507 struct mrw_feature_desc *mfd;
508 unsigned char buffer[16];
513 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_READ);
515 cgc.cmd[0] = GPCMD_GET_CONFIGURATION;
516 cgc.cmd[3] = CDF_MRW;
517 cgc.cmd[8] = sizeof(buffer);
520 if ((ret = cdi->ops->generic_packet(cdi, &cgc)))
523 mfd = (struct mrw_feature_desc *)&buffer[sizeof(struct feature_header)];
526 if ((ret = cdrom_mrw_probe_pc(cdi))) {
534 static int cdrom_mrw_bgformat(struct cdrom_device_info *cdi, int cont)
536 struct packet_command cgc;
537 unsigned char buffer[12];
540 printk(KERN_INFO "cdrom: %sstarting format\n", cont ? "Re" : "");
543 * FmtData bit set (bit 4), format type is 1
545 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_WRITE);
546 cgc.cmd[0] = GPCMD_FORMAT_UNIT;
547 cgc.cmd[1] = (1 << 4) | 1;
549 cgc.timeout = 5 * 60 * HZ;
552 * 4 byte format list header, 8 byte format list descriptor
565 buffer[8] = 0x24 << 2;
568 ret = cdi->ops->generic_packet(cdi, &cgc);
570 printk(KERN_INFO "cdrom: bgformat failed\n");
575 static int cdrom_mrw_bgformat_susp(struct cdrom_device_info *cdi, int immed)
577 struct packet_command cgc;
579 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
580 cgc.cmd[0] = GPCMD_CLOSE_TRACK;
583 * Session = 1, Track = 0
585 cgc.cmd[1] = !!immed;
588 cgc.timeout = 5 * 60 * HZ;
590 return cdi->ops->generic_packet(cdi, &cgc);
593 static int cdrom_flush_cache(struct cdrom_device_info *cdi)
595 struct packet_command cgc;
597 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
598 cgc.cmd[0] = GPCMD_FLUSH_CACHE;
600 cgc.timeout = 5 * 60 * HZ;
602 return cdi->ops->generic_packet(cdi, &cgc);
605 static int cdrom_mrw_exit(struct cdrom_device_info *cdi)
610 if (cdrom_get_disc_info(cdi, &di))
613 if (di.mrw_status == CDM_MRW_BGFORMAT_ACTIVE) {
614 printk(KERN_INFO "cdrom: issuing MRW back ground format suspend\n");
615 ret = cdrom_mrw_bgformat_susp(cdi, 0);
619 ret = cdrom_flush_cache(cdi);
624 static int cdrom_mrw_set_lba_space(struct cdrom_device_info *cdi, int space)
626 struct packet_command cgc;
627 struct mode_page_header *mph;
629 int ret, offset, size;
631 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_READ);
634 cgc.buflen = sizeof(buffer);
636 if ((ret = cdrom_mode_sense(cdi, &cgc, cdi->mrw_mode_page, 0)))
639 mph = (struct mode_page_header *) buffer;
640 offset = be16_to_cpu(mph->desc_length);
641 size = be16_to_cpu(mph->mode_data_length) + 2;
643 buffer[offset + 3] = space;
646 if ((ret = cdrom_mode_select(cdi, &cgc)))
649 printk(KERN_INFO "cdrom: %s: mrw address space %s selected\n", cdi->name, mrw_address_space[space]);
653 int cdrom_get_random_writable(struct cdrom_device_info *cdi,
654 struct rwrt_feature_desc *rfd)
656 struct packet_command cgc;
660 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_READ);
662 cgc.cmd[0] = GPCMD_GET_CONFIGURATION; /* often 0x46 */
663 cgc.cmd[3] = CDF_RWRT; /* often 0x0020 */
664 cgc.cmd[8] = sizeof(buffer); /* often 0x18 */
667 if ((ret = cdi->ops->generic_packet(cdi, &cgc)))
670 memcpy(rfd, &buffer[sizeof(struct feature_header)], sizeof (*rfd));
674 int cdrom_has_defect_mgt(struct cdrom_device_info *cdi)
676 struct packet_command cgc;
681 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_READ);
683 cgc.cmd[0] = GPCMD_GET_CONFIGURATION;
684 cgc.cmd[3] = CDF_HWDM;
685 cgc.cmd[8] = sizeof(buffer);
688 if ((ret = cdi->ops->generic_packet(cdi, &cgc)))
691 feature_code = (__u16 *) &buffer[sizeof(struct feature_header)];
692 if (be16_to_cpu(*feature_code) == CDF_HWDM)
699 int cdrom_is_random_writable(struct cdrom_device_info *cdi, int *write)
701 struct rwrt_feature_desc rfd;
706 if ((ret = cdrom_get_random_writable(cdi, &rfd)))
709 if (CDF_RWRT == be16_to_cpu(rfd.feature_code))
715 static int cdrom_media_erasable(struct cdrom_device_info *cdi)
719 if (cdrom_get_disc_info(cdi, &di))
726 * FIXME: check RO bit
728 static int cdrom_dvdram_open_write(struct cdrom_device_info *cdi)
730 int ret = cdrom_media_erasable(cdi);
733 * allow writable open if media info read worked and media is
734 * erasable, _or_ if it fails since not all drives support it
742 static int cdrom_mrw_open_write(struct cdrom_device_info *cdi)
748 * always reset to DMA lba space on open
750 if (cdrom_mrw_set_lba_space(cdi, MRW_LBA_DMA)) {
751 printk(KERN_ERR "cdrom: failed setting lba address space\n");
755 if (cdrom_get_disc_info(cdi, &di))
763 * 0 - not MRW formatted
764 * 1 - MRW bgformat started, but not running or complete
765 * 2 - MRW bgformat in progress
766 * 3 - MRW formatting complete
769 printk(KERN_INFO "cdrom open: mrw_status '%s'\n", mrw_format_status[di.mrw_status]);
772 else if (di.mrw_status == CDM_MRW_BGFORMAT_INACTIVE && mrw_format_restart)
773 ret = cdrom_mrw_bgformat(cdi, 1);
778 static int mo_open_write(struct cdrom_device_info *cdi)
780 struct packet_command cgc;
784 init_cdrom_command(&cgc, &buffer, 4, CGC_DATA_READ);
788 * obtain write protect information as per
789 * drivers/scsi/sd.c:sd_read_write_protect_flag
792 ret = cdrom_mode_sense(cdi, &cgc, GPMODE_ALL_PAGES, 0);
794 ret = cdrom_mode_sense(cdi, &cgc, GPMODE_VENDOR_PAGE, 0);
797 ret = cdrom_mode_sense(cdi, &cgc, GPMODE_ALL_PAGES, 0);
800 /* drive gave us no info, let the user go ahead */
804 return buffer[3] & 0x80;
807 static int cdrom_ram_open_write(struct cdrom_device_info *cdi)
809 struct rwrt_feature_desc rfd;
812 if ((ret = cdrom_has_defect_mgt(cdi)))
815 if ((ret = cdrom_get_random_writable(cdi, &rfd)))
817 else if (CDF_RWRT == be16_to_cpu(rfd.feature_code))
820 cdinfo(CD_OPEN, "can open for random write\n");
825 * returns 0 for ok to open write, non-0 to disallow
827 static int cdrom_open_write(struct cdrom_device_info *cdi)
829 int mrw, mrw_write, ram_write;
833 if (!cdrom_is_mrw(cdi, &mrw_write))
836 (void) cdrom_is_random_writable(cdi, &ram_write);
839 cdi->mask &= ~CDC_MRW;
841 cdi->mask |= CDC_MRW;
844 cdi->mask &= ~CDC_MRW_W;
846 cdi->mask |= CDC_MRW_W;
849 cdi->mask &= ~CDC_RAM;
851 cdi->mask |= CDC_RAM;
853 if (CDROM_CAN(CDC_MRW_W))
854 ret = cdrom_mrw_open_write(cdi);
855 else if (CDROM_CAN(CDC_DVD_RAM))
856 ret = cdrom_dvdram_open_write(cdi);
857 else if (CDROM_CAN(CDC_RAM) &&
858 !CDROM_CAN(CDC_CD_R|CDC_CD_RW|CDC_DVD|CDC_DVD_R|CDC_MRW))
859 ret = cdrom_ram_open_write(cdi);
860 else if (CDROM_CAN(CDC_MO_DRIVE))
861 ret = mo_open_write(cdi);
866 static int cdrom_close_write(struct cdrom_device_info *cdi)
869 return cdrom_flush_cache(cdi);
875 /* We use the open-option O_NONBLOCK to indicate that the
876 * purpose of opening is only for subsequent ioctl() calls; no device
877 * integrity checks are performed.
879 * We hope that all cd-player programs will adopt this convention. It
880 * is in their own interest: device control becomes a lot easier
883 int cdrom_open(struct cdrom_device_info *cdi, struct inode *ip, struct file *fp)
887 cdinfo(CD_OPEN, "entering cdrom_open\n");
889 /* if this was a O_NONBLOCK open and we should honor the flags,
890 * do a quick open without drive/disc integrity checks. */
892 if ((fp->f_flags & O_NONBLOCK) && (cdi->options & CDO_USE_FFLAGS)) {
893 ret = cdi->ops->open(cdi, 1);
895 ret = open_for_data(cdi);
898 if (fp->f_mode & FMODE_WRITE) {
900 if (!CDROM_CAN(CDC_RAM))
902 if (cdrom_open_write(cdi))
910 cdinfo(CD_OPEN, "Use count for \"/dev/%s\" now %d\n",
911 cdi->name, cdi->use_count);
912 /* Do this on open. Don't wait for mount, because they might
913 not be mounting, but opening with O_NONBLOCK */
914 check_disk_change(ip->i_bdev);
922 int open_for_data(struct cdrom_device_info * cdi)
925 struct cdrom_device_ops *cdo = cdi->ops;
927 cdinfo(CD_OPEN, "entering open_for_data\n");
928 /* Check if the driver can report drive status. If it can, we
929 can do clever things. If it can't, well, we at least tried! */
930 if (cdo->drive_status != NULL) {
931 ret = cdo->drive_status(cdi, CDSL_CURRENT);
932 cdinfo(CD_OPEN, "drive_status=%d\n", ret);
933 if (ret == CDS_TRAY_OPEN) {
934 cdinfo(CD_OPEN, "the tray is open...\n");
935 /* can/may i close it? */
936 if (CDROM_CAN(CDC_CLOSE_TRAY) &&
937 cdi->options & CDO_AUTO_CLOSE) {
938 cdinfo(CD_OPEN, "trying to close the tray.\n");
939 ret=cdo->tray_move(cdi,0);
941 cdinfo(CD_OPEN, "bummer. tried to close the tray but failed.\n");
942 /* Ignore the error from the low
943 level driver. We don't care why it
944 couldn't close the tray. We only care
945 that there is no disc in the drive,
946 since that is the _REAL_ problem here.*/
948 goto clean_up_and_return;
951 cdinfo(CD_OPEN, "bummer. this drive can't close the tray.\n");
953 goto clean_up_and_return;
955 /* Ok, the door should be closed now.. Check again */
956 ret = cdo->drive_status(cdi, CDSL_CURRENT);
957 if ((ret == CDS_NO_DISC) || (ret==CDS_TRAY_OPEN)) {
958 cdinfo(CD_OPEN, "bummer. the tray is still not closed.\n");
959 cdinfo(CD_OPEN, "tray might not contain a medium.\n");
961 goto clean_up_and_return;
963 cdinfo(CD_OPEN, "the tray is now closed.\n");
965 if (ret!=CDS_DISC_OK) {
967 goto clean_up_and_return;
970 cdrom_count_tracks(cdi, &tracks);
971 if (tracks.error == CDS_NO_DISC) {
972 cdinfo(CD_OPEN, "bummer. no disc.\n");
974 goto clean_up_and_return;
976 /* CD-Players which don't use O_NONBLOCK, workman
977 * for example, need bit CDO_CHECK_TYPE cleared! */
978 if (tracks.data==0) {
979 if (cdi->options & CDO_CHECK_TYPE) {
980 /* give people a warning shot, now that CDO_CHECK_TYPE
981 is the default case! */
982 cdinfo(CD_OPEN, "bummer. wrong media type.\n");
983 cdinfo(CD_WARNING, "pid %d must open device O_NONBLOCK!\n",
984 (unsigned int)current->pid);
986 goto clean_up_and_return;
989 cdinfo(CD_OPEN, "wrong media type, but CDO_CHECK_TYPE not set.\n");
993 cdinfo(CD_OPEN, "all seems well, opening the device.\n");
995 /* all seems well, we can open the device */
996 ret = cdo->open(cdi, 0); /* open for data */
997 cdinfo(CD_OPEN, "opening the device gave me %d.\n", ret);
998 /* After all this careful checking, we shouldn't have problems
999 opening the device, but we don't want the device locked if
1000 this somehow fails... */
1002 cdinfo(CD_OPEN, "open device failed.\n");
1003 goto clean_up_and_return;
1005 if (CDROM_CAN(CDC_LOCK) && (cdi->options & CDO_LOCK)) {
1006 cdo->lock_door(cdi, 1);
1007 cdinfo(CD_OPEN, "door locked.\n");
1009 cdinfo(CD_OPEN, "device opened successfully.\n");
1012 /* Something failed. Try to unlock the drive, because some drivers
1013 (notably ide-cd) lock the drive after every command. This produced
1014 a nasty bug where after mount failed, the drive would remain locked!
1015 This ensures that the drive gets unlocked after a mount fails. This
1016 is a goto to avoid bloating the driver with redundant code. */
1017 clean_up_and_return:
1018 cdinfo(CD_WARNING, "open failed.\n");
1019 if (CDROM_CAN(CDC_LOCK) && cdi->options & CDO_LOCK) {
1020 cdo->lock_door(cdi, 0);
1021 cdinfo(CD_OPEN, "door unlocked.\n");
1026 /* This code is similar to that in open_for_data. The routine is called
1027 whenever an audio play operation is requested.
1029 int check_for_audio_disc(struct cdrom_device_info * cdi,
1030 struct cdrom_device_ops * cdo)
1034 cdinfo(CD_OPEN, "entering check_for_audio_disc\n");
1035 if (!(cdi->options & CDO_CHECK_TYPE))
1037 if (cdo->drive_status != NULL) {
1038 ret = cdo->drive_status(cdi, CDSL_CURRENT);
1039 cdinfo(CD_OPEN, "drive_status=%d\n", ret);
1040 if (ret == CDS_TRAY_OPEN) {
1041 cdinfo(CD_OPEN, "the tray is open...\n");
1042 /* can/may i close it? */
1043 if (CDROM_CAN(CDC_CLOSE_TRAY) &&
1044 cdi->options & CDO_AUTO_CLOSE) {
1045 cdinfo(CD_OPEN, "trying to close the tray.\n");
1046 ret=cdo->tray_move(cdi,0);
1048 cdinfo(CD_OPEN, "bummer. tried to close tray but failed.\n");
1049 /* Ignore the error from the low
1050 level driver. We don't care why it
1051 couldn't close the tray. We only care
1052 that there is no disc in the drive,
1053 since that is the _REAL_ problem here.*/
1057 cdinfo(CD_OPEN, "bummer. this driver can't close the tray.\n");
1060 /* Ok, the door should be closed now.. Check again */
1061 ret = cdo->drive_status(cdi, CDSL_CURRENT);
1062 if ((ret == CDS_NO_DISC) || (ret==CDS_TRAY_OPEN)) {
1063 cdinfo(CD_OPEN, "bummer. the tray is still not closed.\n");
1066 if (ret!=CDS_DISC_OK) {
1067 cdinfo(CD_OPEN, "bummer. disc isn't ready.\n");
1070 cdinfo(CD_OPEN, "the tray is now closed.\n");
1073 cdrom_count_tracks(cdi, &tracks);
1075 return(tracks.error);
1077 if (tracks.audio==0)
1078 return -EMEDIUMTYPE;
1083 /* Admittedly, the logic below could be performed in a nicer way. */
1084 int cdrom_release(struct cdrom_device_info *cdi, struct file *fp)
1086 struct cdrom_device_ops *cdo = cdi->ops;
1087 int opened_for_data;
1089 cdinfo(CD_CLOSE, "entering cdrom_release\n");
1091 if (cdi->use_count > 0)
1093 if (cdi->use_count == 0)
1094 cdinfo(CD_CLOSE, "Use count for \"/dev/%s\" now zero\n", cdi->name);
1095 if (cdi->use_count == 0 &&
1096 (cdo->capability & CDC_LOCK) && !keeplocked) {
1097 cdinfo(CD_CLOSE, "Unlocking door!\n");
1098 cdo->lock_door(cdi, 0);
1100 opened_for_data = !(cdi->options & CDO_USE_FFLAGS) ||
1101 !(fp && fp->f_flags & O_NONBLOCK);
1104 * flush cache on last write release
1106 if (CDROM_CAN(CDC_RAM) && !cdi->use_count && cdi->for_data)
1107 cdrom_close_write(cdi);
1110 if (cdi->use_count == 0) { /* last process that closes dev*/
1111 if (opened_for_data &&
1112 cdi->options & CDO_AUTO_EJECT && CDROM_CAN(CDC_OPEN_TRAY))
1113 cdo->tray_move(cdi, 1);
1118 static int cdrom_read_mech_status(struct cdrom_device_info *cdi,
1119 struct cdrom_changer_info *buf)
1121 struct packet_command cgc;
1122 struct cdrom_device_ops *cdo = cdi->ops;
1126 * Sanyo changer isn't spec compliant (doesn't use regular change
1127 * LOAD_UNLOAD command, and it doesn't implement the mech status
1130 if (cdi->sanyo_slot) {
1131 buf->hdr.nslots = 3;
1132 buf->hdr.curslot = cdi->sanyo_slot == 3 ? 0 : cdi->sanyo_slot;
1133 for (length = 0; length < 3; length++) {
1134 buf->slots[length].disc_present = 1;
1135 buf->slots[length].change = 0;
1140 length = sizeof(struct cdrom_mechstat_header) +
1141 cdi->capacity * sizeof(struct cdrom_slot);
1143 init_cdrom_command(&cgc, buf, length, CGC_DATA_READ);
1144 cgc.cmd[0] = GPCMD_MECHANISM_STATUS;
1145 cgc.cmd[8] = (length >> 8) & 0xff;
1146 cgc.cmd[9] = length & 0xff;
1147 return cdo->generic_packet(cdi, &cgc);
1150 static int cdrom_slot_status(struct cdrom_device_info *cdi, int slot)
1152 struct cdrom_changer_info *info;
1155 cdinfo(CD_CHANGER, "entering cdrom_slot_status()\n");
1156 if (cdi->sanyo_slot)
1159 info = kmalloc(sizeof(*info), GFP_KERNEL);
1163 if ((ret = cdrom_read_mech_status(cdi, info)))
1166 if (info->slots[slot].disc_present)
1176 /* Return the number of slots for an ATAPI/SCSI cdrom,
1177 * return 1 if not a changer.
1179 int cdrom_number_of_slots(struct cdrom_device_info *cdi)
1183 struct cdrom_changer_info *info;
1185 cdinfo(CD_CHANGER, "entering cdrom_number_of_slots()\n");
1186 /* cdrom_read_mech_status requires a valid value for capacity: */
1189 info = kmalloc(sizeof(*info), GFP_KERNEL);
1193 if ((status = cdrom_read_mech_status(cdi, info)) == 0)
1194 nslots = info->hdr.nslots;
1201 /* If SLOT < 0, unload the current slot. Otherwise, try to load SLOT. */
1202 static int cdrom_load_unload(struct cdrom_device_info *cdi, int slot)
1204 struct packet_command cgc;
1206 cdinfo(CD_CHANGER, "entering cdrom_load_unload()\n");
1207 if (cdi->sanyo_slot && slot < 0)
1210 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
1211 cgc.cmd[0] = GPCMD_LOAD_UNLOAD;
1212 cgc.cmd[4] = 2 + (slot >= 0);
1214 cgc.timeout = 60 * HZ;
1216 /* The Sanyo 3 CD changer uses byte 7 of the
1217 GPCMD_TEST_UNIT_READY to command to switch CDs instead of
1218 using the GPCMD_LOAD_UNLOAD opcode. */
1219 if (cdi->sanyo_slot && -1 < slot) {
1220 cgc.cmd[0] = GPCMD_TEST_UNIT_READY;
1222 cgc.cmd[4] = cgc.cmd[8] = 0;
1223 cdi->sanyo_slot = slot ? slot : 3;
1226 return cdi->ops->generic_packet(cdi, &cgc);
1229 static int cdrom_select_disc(struct cdrom_device_info *cdi, int slot)
1231 struct cdrom_changer_info *info;
1235 cdinfo(CD_CHANGER, "entering cdrom_select_disc()\n");
1236 if (!CDROM_CAN(CDC_SELECT_DISC))
1237 return -EDRIVE_CANT_DO_THIS;
1239 (void) cdi->ops->media_changed(cdi, slot);
1241 if (slot == CDSL_NONE) {
1242 /* set media changed bits, on both queues */
1243 cdi->mc_flags = 0x3;
1244 return cdrom_load_unload(cdi, -1);
1247 info = kmalloc(sizeof(*info), GFP_KERNEL);
1251 if ((ret = cdrom_read_mech_status(cdi, info))) {
1256 curslot = info->hdr.curslot;
1259 if (cdi->use_count > 1 || keeplocked) {
1260 if (slot == CDSL_CURRENT) {
1267 /* Specifying CDSL_CURRENT will attempt to load the currnet slot,
1268 which is useful if it had been previously unloaded.
1269 Whether it can or not, it returns the current slot.
1270 Similarly, if slot happens to be the current one, we still
1272 if (slot == CDSL_CURRENT)
1275 /* set media changed bits on both queues */
1276 cdi->mc_flags = 0x3;
1277 if ((ret = cdrom_load_unload(cdi, slot)))
1283 /* We want to make media_changed accessible to the user through an
1284 * ioctl. The main problem now is that we must double-buffer the
1285 * low-level implementation, to assure that the VFS and the user both
1286 * see a medium change once.
1290 int media_changed(struct cdrom_device_info *cdi, int queue)
1292 unsigned int mask = (1 << (queue & 1));
1293 int ret = !!(cdi->mc_flags & mask);
1295 if (!CDROM_CAN(CDC_MEDIA_CHANGED))
1297 /* changed since last call? */
1298 if (cdi->ops->media_changed(cdi, CDSL_CURRENT)) {
1299 cdi->mc_flags = 0x3; /* set bit on both queues */
1302 cdi->mc_flags &= ~mask; /* clear bit */
1306 int cdrom_media_changed(struct cdrom_device_info *cdi)
1308 /* This talks to the VFS, which doesn't like errors - just 1 or 0.
1309 * Returning "0" is always safe (media hasn't been changed). Do that
1310 * if the low-level cdrom driver dosn't support media changed. */
1311 if (cdi == NULL || cdi->ops->media_changed == NULL)
1313 if (!CDROM_CAN(CDC_MEDIA_CHANGED))
1315 return media_changed(cdi, 0);
1318 /* badly broken, I know. Is due for a fixup anytime. */
1319 static void cdrom_count_tracks(struct cdrom_device_info *cdi, tracktype* tracks)
1321 struct cdrom_tochdr header;
1322 struct cdrom_tocentry entry;
1329 cdinfo(CD_COUNT_TRACKS, "entering cdrom_count_tracks\n");
1330 if (!CDROM_CAN(CDC_PLAY_AUDIO)) {
1331 tracks->error=CDS_NO_INFO;
1334 /* Grab the TOC header so we can see how many tracks there are */
1335 if ((ret = cdi->ops->audio_ioctl(cdi, CDROMREADTOCHDR, &header))) {
1336 if (ret == -ENOMEDIUM)
1337 tracks->error = CDS_NO_DISC;
1339 tracks->error = CDS_NO_INFO;
1342 /* check what type of tracks are on this disc */
1343 entry.cdte_format = CDROM_MSF;
1344 for (i = header.cdth_trk0; i <= header.cdth_trk1; i++) {
1345 entry.cdte_track = i;
1346 if (cdi->ops->audio_ioctl(cdi, CDROMREADTOCENTRY, &entry)) {
1347 tracks->error=CDS_NO_INFO;
1350 if (entry.cdte_ctrl & CDROM_DATA_TRACK) {
1351 if (entry.cdte_format == 0x10)
1353 else if (entry.cdte_format == 0x20)
1359 cdinfo(CD_COUNT_TRACKS, "track %d: format=%d, ctrl=%d\n",
1360 i, entry.cdte_format, entry.cdte_ctrl);
1362 cdinfo(CD_COUNT_TRACKS, "disc has %d tracks: %d=audio %d=data %d=Cd-I %d=XA\n",
1363 header.cdth_trk1, tracks->audio, tracks->data,
1364 tracks->cdi, tracks->xa);
1367 /* Requests to the low-level drivers will /always/ be done in the
1368 following format convention:
1370 CDROM_LBA: all data-related requests.
1371 CDROM_MSF: all audio-related requests.
1373 However, a low-level implementation is allowed to refuse this
1374 request, and return information in its own favorite format.
1376 It doesn't make sense /at all/ to ask for a play_audio in LBA
1377 format, or ask for multi-session info in MSF format. However, for
1378 backward compatibility these format requests will be satisfied, but
1379 the requests to the low-level drivers will be sanitized in the more
1380 meaningful format indicated above.
1384 void sanitize_format(union cdrom_addr *addr,
1385 u_char * curr, u_char requested)
1387 if (*curr == requested)
1388 return; /* nothing to be done! */
1389 if (requested == CDROM_LBA) {
1390 addr->lba = (int) addr->msf.frame +
1391 75 * (addr->msf.second - 2 + 60 * addr->msf.minute);
1392 } else { /* CDROM_MSF */
1393 int lba = addr->lba;
1394 addr->msf.frame = lba % 75;
1397 addr->msf.second = lba % 60;
1398 addr->msf.minute = lba / 60;
1403 void init_cdrom_command(struct packet_command *cgc, void *buf, int len,
1406 memset(cgc, 0, sizeof(struct packet_command));
1408 memset(buf, 0, len);
1409 cgc->buffer = (char *) buf;
1411 cgc->data_direction = type;
1412 cgc->timeout = 5*HZ;
1417 #define copy_key(dest,src) memcpy((dest), (src), sizeof(dvd_key))
1418 #define copy_chal(dest,src) memcpy((dest), (src), sizeof(dvd_challenge))
1420 static void setup_report_key(struct packet_command *cgc, unsigned agid, unsigned type)
1422 cgc->cmd[0] = GPCMD_REPORT_KEY;
1423 cgc->cmd[10] = type | (agid << 6);
1425 case 0: case 8: case 5: {
1438 cgc->cmd[9] = cgc->buflen;
1439 cgc->data_direction = CGC_DATA_READ;
1442 static void setup_send_key(struct packet_command *cgc, unsigned agid, unsigned type)
1444 cgc->cmd[0] = GPCMD_SEND_KEY;
1445 cgc->cmd[10] = type | (agid << 6);
1460 cgc->cmd[9] = cgc->buflen;
1461 cgc->data_direction = CGC_DATA_WRITE;
1464 static int dvd_do_auth(struct cdrom_device_info *cdi, dvd_authinfo *ai)
1468 struct packet_command cgc;
1469 struct cdrom_device_ops *cdo = cdi->ops;
1470 rpc_state_t rpc_state;
1472 memset(buf, 0, sizeof(buf));
1473 init_cdrom_command(&cgc, buf, 0, CGC_DATA_READ);
1477 case DVD_LU_SEND_AGID:
1478 cdinfo(CD_DVD, "entering DVD_LU_SEND_AGID\n");
1479 setup_report_key(&cgc, ai->lsa.agid, 0);
1481 if ((ret = cdo->generic_packet(cdi, &cgc)))
1484 ai->lsa.agid = buf[7] >> 6;
1485 /* Returning data, let host change state */
1488 case DVD_LU_SEND_KEY1:
1489 cdinfo(CD_DVD, "entering DVD_LU_SEND_KEY1\n");
1490 setup_report_key(&cgc, ai->lsk.agid, 2);
1492 if ((ret = cdo->generic_packet(cdi, &cgc)))
1495 copy_key(ai->lsk.key, &buf[4]);
1496 /* Returning data, let host change state */
1499 case DVD_LU_SEND_CHALLENGE:
1500 cdinfo(CD_DVD, "entering DVD_LU_SEND_CHALLENGE\n");
1501 setup_report_key(&cgc, ai->lsc.agid, 1);
1503 if ((ret = cdo->generic_packet(cdi, &cgc)))
1506 copy_chal(ai->lsc.chal, &buf[4]);
1507 /* Returning data, let host change state */
1511 case DVD_LU_SEND_TITLE_KEY:
1512 cdinfo(CD_DVD, "entering DVD_LU_SEND_TITLE_KEY\n");
1513 setup_report_key(&cgc, ai->lstk.agid, 4);
1514 cgc.cmd[5] = ai->lstk.lba;
1515 cgc.cmd[4] = ai->lstk.lba >> 8;
1516 cgc.cmd[3] = ai->lstk.lba >> 16;
1517 cgc.cmd[2] = ai->lstk.lba >> 24;
1519 if ((ret = cdo->generic_packet(cdi, &cgc)))
1522 ai->lstk.cpm = (buf[4] >> 7) & 1;
1523 ai->lstk.cp_sec = (buf[4] >> 6) & 1;
1524 ai->lstk.cgms = (buf[4] >> 4) & 3;
1525 copy_key(ai->lstk.title_key, &buf[5]);
1526 /* Returning data, let host change state */
1529 case DVD_LU_SEND_ASF:
1530 cdinfo(CD_DVD, "entering DVD_LU_SEND_ASF\n");
1531 setup_report_key(&cgc, ai->lsasf.agid, 5);
1533 if ((ret = cdo->generic_packet(cdi, &cgc)))
1536 ai->lsasf.asf = buf[7] & 1;
1539 /* LU data receive (LU changes state) */
1540 case DVD_HOST_SEND_CHALLENGE:
1541 cdinfo(CD_DVD, "entering DVD_HOST_SEND_CHALLENGE\n");
1542 setup_send_key(&cgc, ai->hsc.agid, 1);
1544 copy_chal(&buf[4], ai->hsc.chal);
1546 if ((ret = cdo->generic_packet(cdi, &cgc)))
1549 ai->type = DVD_LU_SEND_KEY1;
1552 case DVD_HOST_SEND_KEY2:
1553 cdinfo(CD_DVD, "entering DVD_HOST_SEND_KEY2\n");
1554 setup_send_key(&cgc, ai->hsk.agid, 3);
1556 copy_key(&buf[4], ai->hsk.key);
1558 if ((ret = cdo->generic_packet(cdi, &cgc))) {
1559 ai->type = DVD_AUTH_FAILURE;
1562 ai->type = DVD_AUTH_ESTABLISHED;
1566 case DVD_INVALIDATE_AGID:
1568 cdinfo(CD_DVD, "entering DVD_INVALIDATE_AGID\n");
1569 setup_report_key(&cgc, ai->lsa.agid, 0x3f);
1570 if ((ret = cdo->generic_packet(cdi, &cgc)))
1574 /* Get region settings */
1575 case DVD_LU_SEND_RPC_STATE:
1576 cdinfo(CD_DVD, "entering DVD_LU_SEND_RPC_STATE\n");
1577 setup_report_key(&cgc, 0, 8);
1578 memset(&rpc_state, 0, sizeof(rpc_state_t));
1579 cgc.buffer = (char *) &rpc_state;
1581 if ((ret = cdo->generic_packet(cdi, &cgc)))
1584 ai->lrpcs.type = rpc_state.type_code;
1585 ai->lrpcs.vra = rpc_state.vra;
1586 ai->lrpcs.ucca = rpc_state.ucca;
1587 ai->lrpcs.region_mask = rpc_state.region_mask;
1588 ai->lrpcs.rpc_scheme = rpc_state.rpc_scheme;
1591 /* Set region settings */
1592 case DVD_HOST_SEND_RPC_STATE:
1593 cdinfo(CD_DVD, "entering DVD_HOST_SEND_RPC_STATE\n");
1594 setup_send_key(&cgc, 0, 6);
1596 buf[4] = ai->hrpcs.pdrc;
1598 if ((ret = cdo->generic_packet(cdi, &cgc)))
1603 cdinfo(CD_WARNING, "Invalid DVD key ioctl (%d)\n", ai->type);
1610 static int dvd_read_physical(struct cdrom_device_info *cdi, dvd_struct *s)
1612 unsigned char buf[21], *base;
1613 struct dvd_layer *layer;
1614 struct packet_command cgc;
1615 struct cdrom_device_ops *cdo = cdi->ops;
1616 int ret, layer_num = s->physical.layer_num;
1618 if (layer_num >= DVD_LAYERS)
1621 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_READ);
1622 cgc.cmd[0] = GPCMD_READ_DVD_STRUCTURE;
1623 cgc.cmd[6] = layer_num;
1624 cgc.cmd[7] = s->type;
1625 cgc.cmd[9] = cgc.buflen & 0xff;
1628 * refrain from reporting errors on non-existing layers (mainly)
1632 if ((ret = cdo->generic_packet(cdi, &cgc)))
1636 layer = &s->physical.layer[layer_num];
1639 * place the data... really ugly, but at least we won't have to
1640 * worry about endianess in userspace.
1642 memset(layer, 0, sizeof(*layer));
1643 layer->book_version = base[0] & 0xf;
1644 layer->book_type = base[0] >> 4;
1645 layer->min_rate = base[1] & 0xf;
1646 layer->disc_size = base[1] >> 4;
1647 layer->layer_type = base[2] & 0xf;
1648 layer->track_path = (base[2] >> 4) & 1;
1649 layer->nlayers = (base[2] >> 5) & 3;
1650 layer->track_density = base[3] & 0xf;
1651 layer->linear_density = base[3] >> 4;
1652 layer->start_sector = base[5] << 16 | base[6] << 8 | base[7];
1653 layer->end_sector = base[9] << 16 | base[10] << 8 | base[11];
1654 layer->end_sector_l0 = base[13] << 16 | base[14] << 8 | base[15];
1655 layer->bca = base[16] >> 7;
1660 static int dvd_read_copyright(struct cdrom_device_info *cdi, dvd_struct *s)
1664 struct packet_command cgc;
1665 struct cdrom_device_ops *cdo = cdi->ops;
1667 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_READ);
1668 cgc.cmd[0] = GPCMD_READ_DVD_STRUCTURE;
1669 cgc.cmd[6] = s->copyright.layer_num;
1670 cgc.cmd[7] = s->type;
1671 cgc.cmd[8] = cgc.buflen >> 8;
1672 cgc.cmd[9] = cgc.buflen & 0xff;
1674 if ((ret = cdo->generic_packet(cdi, &cgc)))
1677 s->copyright.cpst = buf[4];
1678 s->copyright.rmi = buf[5];
1683 static int dvd_read_disckey(struct cdrom_device_info *cdi, dvd_struct *s)
1687 struct packet_command cgc;
1688 struct cdrom_device_ops *cdo = cdi->ops;
1690 size = sizeof(s->disckey.value) + 4;
1692 if ((buf = (u_char *) kmalloc(size, GFP_KERNEL)) == NULL)
1695 init_cdrom_command(&cgc, buf, size, CGC_DATA_READ);
1696 cgc.cmd[0] = GPCMD_READ_DVD_STRUCTURE;
1697 cgc.cmd[7] = s->type;
1698 cgc.cmd[8] = size >> 8;
1699 cgc.cmd[9] = size & 0xff;
1700 cgc.cmd[10] = s->disckey.agid << 6;
1702 if (!(ret = cdo->generic_packet(cdi, &cgc)))
1703 memcpy(s->disckey.value, &buf[4], sizeof(s->disckey.value));
1709 static int dvd_read_bca(struct cdrom_device_info *cdi, dvd_struct *s)
1712 u_char buf[4 + 188];
1713 struct packet_command cgc;
1714 struct cdrom_device_ops *cdo = cdi->ops;
1716 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_READ);
1717 cgc.cmd[0] = GPCMD_READ_DVD_STRUCTURE;
1718 cgc.cmd[7] = s->type;
1719 cgc.cmd[9] = cgc.buflen = 0xff;
1721 if ((ret = cdo->generic_packet(cdi, &cgc)))
1724 s->bca.len = buf[0] << 8 | buf[1];
1725 if (s->bca.len < 12 || s->bca.len > 188) {
1726 cdinfo(CD_WARNING, "Received invalid BCA length (%d)\n", s->bca.len);
1729 memcpy(s->bca.value, &buf[4], s->bca.len);
1734 static int dvd_read_manufact(struct cdrom_device_info *cdi, dvd_struct *s)
1738 struct packet_command cgc;
1739 struct cdrom_device_ops *cdo = cdi->ops;
1741 size = sizeof(s->manufact.value) + 4;
1743 if ((buf = (u_char *) kmalloc(size, GFP_KERNEL)) == NULL)
1746 init_cdrom_command(&cgc, buf, size, CGC_DATA_READ);
1747 cgc.cmd[0] = GPCMD_READ_DVD_STRUCTURE;
1748 cgc.cmd[7] = s->type;
1749 cgc.cmd[8] = size >> 8;
1750 cgc.cmd[9] = size & 0xff;
1752 if ((ret = cdo->generic_packet(cdi, &cgc))) {
1757 s->manufact.len = buf[0] << 8 | buf[1];
1758 if (s->manufact.len < 0 || s->manufact.len > 2048) {
1759 cdinfo(CD_WARNING, "Received invalid manufacture info length"
1760 " (%d)\n", s->bca.len);
1763 memcpy(s->manufact.value, &buf[4], s->manufact.len);
1770 static int dvd_read_struct(struct cdrom_device_info *cdi, dvd_struct *s)
1773 case DVD_STRUCT_PHYSICAL:
1774 return dvd_read_physical(cdi, s);
1776 case DVD_STRUCT_COPYRIGHT:
1777 return dvd_read_copyright(cdi, s);
1779 case DVD_STRUCT_DISCKEY:
1780 return dvd_read_disckey(cdi, s);
1782 case DVD_STRUCT_BCA:
1783 return dvd_read_bca(cdi, s);
1785 case DVD_STRUCT_MANUFACT:
1786 return dvd_read_manufact(cdi, s);
1789 cdinfo(CD_WARNING, ": Invalid DVD structure read requested (%d)\n",
1795 int cdrom_mode_sense(struct cdrom_device_info *cdi,
1796 struct packet_command *cgc,
1797 int page_code, int page_control)
1799 struct cdrom_device_ops *cdo = cdi->ops;
1801 memset(cgc->cmd, 0, sizeof(cgc->cmd));
1803 cgc->cmd[0] = GPCMD_MODE_SENSE_10;
1804 cgc->cmd[2] = page_code | (page_control << 6);
1805 cgc->cmd[7] = cgc->buflen >> 8;
1806 cgc->cmd[8] = cgc->buflen & 0xff;
1807 cgc->data_direction = CGC_DATA_READ;
1808 return cdo->generic_packet(cdi, cgc);
1811 int cdrom_mode_select(struct cdrom_device_info *cdi,
1812 struct packet_command *cgc)
1814 struct cdrom_device_ops *cdo = cdi->ops;
1816 memset(cgc->cmd, 0, sizeof(cgc->cmd));
1817 memset(cgc->buffer, 0, 2);
1818 cgc->cmd[0] = GPCMD_MODE_SELECT_10;
1819 cgc->cmd[1] = 0x10; /* PF */
1820 cgc->cmd[7] = cgc->buflen >> 8;
1821 cgc->cmd[8] = cgc->buflen & 0xff;
1822 cgc->data_direction = CGC_DATA_WRITE;
1823 return cdo->generic_packet(cdi, cgc);
1826 static int cdrom_read_subchannel(struct cdrom_device_info *cdi,
1827 struct cdrom_subchnl *subchnl, int mcn)
1829 struct cdrom_device_ops *cdo = cdi->ops;
1830 struct packet_command cgc;
1834 init_cdrom_command(&cgc, buffer, 16, CGC_DATA_READ);
1835 cgc.cmd[0] = GPCMD_READ_SUBCHANNEL;
1836 cgc.cmd[1] = 2; /* MSF addressing */
1837 cgc.cmd[2] = 0x40; /* request subQ data */
1838 cgc.cmd[3] = mcn ? 2 : 1;
1841 if ((ret = cdo->generic_packet(cdi, &cgc)))
1844 subchnl->cdsc_audiostatus = cgc.buffer[1];
1845 subchnl->cdsc_format = CDROM_MSF;
1846 subchnl->cdsc_ctrl = cgc.buffer[5] & 0xf;
1847 subchnl->cdsc_trk = cgc.buffer[6];
1848 subchnl->cdsc_ind = cgc.buffer[7];
1850 subchnl->cdsc_reladdr.msf.minute = cgc.buffer[13];
1851 subchnl->cdsc_reladdr.msf.second = cgc.buffer[14];
1852 subchnl->cdsc_reladdr.msf.frame = cgc.buffer[15];
1853 subchnl->cdsc_absaddr.msf.minute = cgc.buffer[9];
1854 subchnl->cdsc_absaddr.msf.second = cgc.buffer[10];
1855 subchnl->cdsc_absaddr.msf.frame = cgc.buffer[11];
1861 * Specific READ_10 interface
1863 static int cdrom_read_cd(struct cdrom_device_info *cdi,
1864 struct packet_command *cgc, int lba,
1865 int blocksize, int nblocks)
1867 struct cdrom_device_ops *cdo = cdi->ops;
1869 memset(&cgc->cmd, 0, sizeof(cgc->cmd));
1870 cgc->cmd[0] = GPCMD_READ_10;
1871 cgc->cmd[2] = (lba >> 24) & 0xff;
1872 cgc->cmd[3] = (lba >> 16) & 0xff;
1873 cgc->cmd[4] = (lba >> 8) & 0xff;
1874 cgc->cmd[5] = lba & 0xff;
1875 cgc->cmd[6] = (nblocks >> 16) & 0xff;
1876 cgc->cmd[7] = (nblocks >> 8) & 0xff;
1877 cgc->cmd[8] = nblocks & 0xff;
1878 cgc->buflen = blocksize * nblocks;
1879 return cdo->generic_packet(cdi, cgc);
1882 /* very generic interface for reading the various types of blocks */
1883 static int cdrom_read_block(struct cdrom_device_info *cdi,
1884 struct packet_command *cgc,
1885 int lba, int nblocks, int format, int blksize)
1887 struct cdrom_device_ops *cdo = cdi->ops;
1889 memset(&cgc->cmd, 0, sizeof(cgc->cmd));
1890 cgc->cmd[0] = GPCMD_READ_CD;
1891 /* expected sector size - cdda,mode1,etc. */
1892 cgc->cmd[1] = format << 2;
1893 /* starting address */
1894 cgc->cmd[2] = (lba >> 24) & 0xff;
1895 cgc->cmd[3] = (lba >> 16) & 0xff;
1896 cgc->cmd[4] = (lba >> 8) & 0xff;
1897 cgc->cmd[5] = lba & 0xff;
1898 /* number of blocks */
1899 cgc->cmd[6] = (nblocks >> 16) & 0xff;
1900 cgc->cmd[7] = (nblocks >> 8) & 0xff;
1901 cgc->cmd[8] = nblocks & 0xff;
1902 cgc->buflen = blksize * nblocks;
1904 /* set the header info returned */
1906 case CD_FRAMESIZE_RAW0 : cgc->cmd[9] = 0x58; break;
1907 case CD_FRAMESIZE_RAW1 : cgc->cmd[9] = 0x78; break;
1908 case CD_FRAMESIZE_RAW : cgc->cmd[9] = 0xf8; break;
1909 default : cgc->cmd[9] = 0x10;
1912 return cdo->generic_packet(cdi, cgc);
1915 static int cdrom_read_cdda_old(struct cdrom_device_info *cdi, __u8 __user *ubuf,
1916 int lba, int nframes)
1918 struct packet_command cgc;
1921 memset(&cgc, 0, sizeof(cgc));
1924 * start with will ra.nframes size, back down if alloc fails
1928 cgc.buffer = kmalloc(CD_FRAMESIZE_RAW * nr, GFP_KERNEL);
1938 if (!access_ok(VERIFY_WRITE, ubuf, nframes * CD_FRAMESIZE_RAW)) {
1943 cgc.data_direction = CGC_DATA_READ;
1944 while (nframes > 0) {
1948 ret = cdrom_read_block(cdi, &cgc, lba, nr, 1, CD_FRAMESIZE_RAW);
1951 __copy_to_user(ubuf, cgc.buffer, CD_FRAMESIZE_RAW * nr);
1952 ubuf += CD_FRAMESIZE_RAW * nr;
1960 static int cdrom_read_cdda_bpc(struct cdrom_device_info *cdi, __u8 __user *ubuf,
1961 int lba, int nframes)
1963 request_queue_t *q = cdi->disk->queue;
1974 if (cdi->cdda_method == CDDA_BPC_SINGLE)
1976 if (nr * CD_FRAMESIZE_RAW > (q->max_sectors << 9))
1977 nr = (q->max_sectors << 9) / CD_FRAMESIZE_RAW;
1979 len = nr * CD_FRAMESIZE_RAW;
1981 rq = blk_rq_map_user(q, READ, ubuf, len);
1985 memset(rq->cmd, 0, sizeof(rq->cmd));
1986 rq->cmd[0] = GPCMD_READ_CD;
1987 rq->cmd[1] = 1 << 2;
1988 rq->cmd[2] = (lba >> 24) & 0xff;
1989 rq->cmd[3] = (lba >> 16) & 0xff;
1990 rq->cmd[4] = (lba >> 8) & 0xff;
1991 rq->cmd[5] = lba & 0xff;
1992 rq->cmd[6] = (nr >> 16) & 0xff;
1993 rq->cmd[7] = (nr >> 8) & 0xff;
1994 rq->cmd[8] = nr & 0xff;
1998 rq->flags |= REQ_BLOCK_PC;
1999 rq->timeout = 60 * HZ;
2002 if (blk_execute_rq(q, cdi->disk, rq)) {
2003 struct request_sense *s = rq->sense;
2005 cdi->last_sense = s->sense_key;
2008 if (blk_rq_unmap_user(rq, ubuf, bio, len))
2021 static int cdrom_read_cdda(struct cdrom_device_info *cdi, __u8 __user *ubuf,
2022 int lba, int nframes)
2026 if (cdi->cdda_method == CDDA_OLD)
2027 return cdrom_read_cdda_old(cdi, ubuf, lba, nframes);
2031 * for anything else than success and io error, we need to retry
2033 ret = cdrom_read_cdda_bpc(cdi, ubuf, lba, nframes);
2034 if (!ret || ret != -EIO)
2038 * I've seen drives get sense 4/8/3 udma crc errors on multi
2039 * frame dma, so drop to single frame dma if we need to
2041 if (cdi->cdda_method == CDDA_BPC_FULL && nframes > 1) {
2042 printk("cdrom: dropping to single frame dma\n");
2043 cdi->cdda_method = CDDA_BPC_SINGLE;
2048 * so we have an io error of some sort with multi frame dma. if the
2049 * condition wasn't a hardware error
2050 * problems, not for any error
2052 if (cdi->last_sense != 0x04 && cdi->last_sense != 0x0b)
2055 printk("cdrom: dropping to old style cdda (sense=%x)\n", cdi->last_sense);
2056 cdi->cdda_method = CDDA_OLD;
2057 return cdrom_read_cdda_old(cdi, ubuf, lba, nframes);
2060 /* Just about every imaginable ioctl is supported in the Uniform layer
2061 * these days. ATAPI / SCSI specific code now mainly resides in
2064 int cdrom_ioctl(struct cdrom_device_info *cdi, struct inode *ip,
2065 unsigned int cmd, unsigned long arg)
2067 struct cdrom_device_ops *cdo = cdi->ops;
2070 /* Try the generic SCSI command ioctl's first.. */
2071 ret = scsi_cmd_ioctl(ip->i_bdev->bd_disk, cmd, (void __user *)arg);
2075 /* the first few commands do not deal with audio drive_info, but
2076 only with routines in cdrom device operations. */
2078 case CDROMMULTISESSION: {
2079 struct cdrom_multisession ms_info;
2080 u_char requested_format;
2081 cdinfo(CD_DO_IOCTL, "entering CDROMMULTISESSION\n");
2082 if (!(cdo->capability & CDC_MULTI_SESSION))
2084 IOCTL_IN(arg, struct cdrom_multisession, ms_info);
2085 requested_format = ms_info.addr_format;
2086 if (!((requested_format == CDROM_MSF) ||
2087 (requested_format == CDROM_LBA)))
2089 ms_info.addr_format = CDROM_LBA;
2090 if ((ret=cdo->get_last_session(cdi, &ms_info)))
2092 sanitize_format(&ms_info.addr, &ms_info.addr_format,
2094 IOCTL_OUT(arg, struct cdrom_multisession, ms_info);
2095 cdinfo(CD_DO_IOCTL, "CDROMMULTISESSION successful\n");
2100 cdinfo(CD_DO_IOCTL, "entering CDROMEJECT\n");
2101 if (!CDROM_CAN(CDC_OPEN_TRAY))
2103 if (cdi->use_count != 1 || keeplocked)
2105 if (CDROM_CAN(CDC_LOCK))
2106 if ((ret=cdo->lock_door(cdi, 0)))
2109 return cdo->tray_move(cdi, 1);
2112 case CDROMCLOSETRAY: {
2113 cdinfo(CD_DO_IOCTL, "entering CDROMCLOSETRAY\n");
2114 if (!CDROM_CAN(CDC_CLOSE_TRAY))
2116 return cdo->tray_move(cdi, 0);
2119 case CDROMEJECT_SW: {
2120 cdinfo(CD_DO_IOCTL, "entering CDROMEJECT_SW\n");
2121 if (!CDROM_CAN(CDC_OPEN_TRAY))
2125 cdi->options &= ~(CDO_AUTO_CLOSE | CDO_AUTO_EJECT);
2127 cdi->options |= CDO_AUTO_CLOSE | CDO_AUTO_EJECT;
2131 case CDROM_MEDIA_CHANGED: {
2132 struct cdrom_changer_info *info;
2135 cdinfo(CD_DO_IOCTL, "entering CDROM_MEDIA_CHANGED\n");
2136 if (!CDROM_CAN(CDC_MEDIA_CHANGED))
2139 /* cannot select disc or select current disc */
2140 if (!CDROM_CAN(CDC_SELECT_DISC) || arg == CDSL_CURRENT)
2141 return media_changed(cdi, 1);
2143 if ((unsigned int)arg >= cdi->capacity)
2146 info = kmalloc(sizeof(*info), GFP_KERNEL);
2150 if ((ret = cdrom_read_mech_status(cdi, info))) {
2155 changed = info->slots[arg].change;
2160 case CDROM_SET_OPTIONS: {
2161 cdinfo(CD_DO_IOCTL, "entering CDROM_SET_OPTIONS\n");
2162 /* options need to be in sync with capability. too late for
2163 that, so we have to check each one separately... */
2165 case CDO_USE_FFLAGS:
2166 case CDO_CHECK_TYPE:
2169 if (!CDROM_CAN(CDC_LOCK))
2173 return cdi->options;
2174 /* default is basically CDO_[AUTO_CLOSE|AUTO_EJECT] */
2176 if (!CDROM_CAN(arg))
2179 cdi->options |= (int) arg;
2180 return cdi->options;
2183 case CDROM_CLEAR_OPTIONS: {
2184 cdinfo(CD_DO_IOCTL, "entering CDROM_CLEAR_OPTIONS\n");
2185 cdi->options &= ~(int) arg;
2186 return cdi->options;
2189 case CDROM_SELECT_SPEED: {
2190 cdinfo(CD_DO_IOCTL, "entering CDROM_SELECT_SPEED\n");
2191 if (!CDROM_CAN(CDC_SELECT_SPEED))
2193 return cdo->select_speed(cdi, arg);
2196 case CDROM_SELECT_DISC: {
2197 cdinfo(CD_DO_IOCTL, "entering CDROM_SELECT_DISC\n");
2198 if (!CDROM_CAN(CDC_SELECT_DISC))
2201 if ((arg != CDSL_CURRENT) && (arg != CDSL_NONE))
2202 if ((int)arg >= cdi->capacity)
2205 /* cdo->select_disc is a hook to allow a driver-specific
2206 * way of seleting disc. However, since there is no
2207 * equiv hook for cdrom_slot_status this may not
2208 * actually be useful...
2210 if (cdo->select_disc != NULL)
2211 return cdo->select_disc(cdi, arg);
2213 /* no driver specific select_disc(), call our own */
2214 cdinfo(CD_CHANGER, "Using generic cdrom_select_disc()\n");
2215 return cdrom_select_disc(cdi, arg);
2219 if (!capable(CAP_SYS_ADMIN))
2221 cdinfo(CD_DO_IOCTL, "entering CDROM_RESET\n");
2222 if (!CDROM_CAN(CDC_RESET))
2224 invalidate_bdev(ip->i_bdev, 0);
2225 return cdo->reset(cdi);
2228 case CDROM_LOCKDOOR: {
2229 cdinfo(CD_DO_IOCTL, "%socking door.\n", arg ? "L" : "Unl");
2230 if (!CDROM_CAN(CDC_LOCK))
2231 return -EDRIVE_CANT_DO_THIS;
2232 keeplocked = arg ? 1 : 0;
2233 /* don't unlock the door on multiple opens,but allow root
2235 if ((cdi->use_count != 1) && !arg && !capable(CAP_SYS_ADMIN))
2237 return cdo->lock_door(cdi, arg);
2241 if (!capable(CAP_SYS_ADMIN))
2243 cdinfo(CD_DO_IOCTL, "%sabling debug.\n", arg ? "En" : "Dis");
2244 debug = arg ? 1 : 0;
2248 case CDROM_GET_CAPABILITY: {
2249 cdinfo(CD_DO_IOCTL, "entering CDROM_GET_CAPABILITY\n");
2250 return (cdo->capability & ~cdi->mask);
2253 /* The following function is implemented, although very few audio
2254 * discs give Universal Product Code information, which should just be
2255 * the Medium Catalog Number on the box. Note, that the way the code
2256 * is written on the CD is /not/ uniform across all discs!
2258 case CDROM_GET_MCN: {
2259 struct cdrom_mcn mcn;
2260 cdinfo(CD_DO_IOCTL, "entering CDROM_GET_MCN\n");
2261 if (!(cdo->capability & CDC_MCN))
2263 if ((ret=cdo->get_mcn(cdi, &mcn)))
2265 IOCTL_OUT(arg, struct cdrom_mcn, mcn);
2266 cdinfo(CD_DO_IOCTL, "CDROM_GET_MCN successful\n");
2270 case CDROM_DRIVE_STATUS: {
2271 cdinfo(CD_DO_IOCTL, "entering CDROM_DRIVE_STATUS\n");
2272 if (!(cdo->capability & CDC_DRIVE_STATUS))
2274 if (!CDROM_CAN(CDC_SELECT_DISC))
2275 return cdo->drive_status(cdi, CDSL_CURRENT);
2276 if ((arg == CDSL_CURRENT) || (arg == CDSL_NONE))
2277 return cdo->drive_status(cdi, CDSL_CURRENT);
2278 if (((int)arg >= cdi->capacity))
2280 return cdrom_slot_status(cdi, arg);
2283 /* Ok, this is where problems start. The current interface for the
2284 CDROM_DISC_STATUS ioctl is flawed. It makes the false assumption
2285 that CDs are all CDS_DATA_1 or all CDS_AUDIO, etc. Unfortunatly,
2286 while this is often the case, it is also very common for CDs to
2287 have some tracks with data, and some tracks with audio. Just
2288 because I feel like it, I declare the following to be the best
2289 way to cope. If the CD has ANY data tracks on it, it will be
2290 returned as a data CD. If it has any XA tracks, I will return
2291 it as that. Now I could simplify this interface by combining these
2292 returns with the above, but this more clearly demonstrates
2293 the problem with the current interface. Too bad this wasn't
2294 designed to use bitmasks... -Erik
2296 Well, now we have the option CDS_MIXED: a mixed-type CD.
2297 User level programmers might feel the ioctl is not very useful.
2300 case CDROM_DISC_STATUS: {
2302 cdinfo(CD_DO_IOCTL, "entering CDROM_DISC_STATUS\n");
2303 cdrom_count_tracks(cdi, &tracks);
2305 return(tracks.error);
2307 /* Policy mode on */
2308 if (tracks.audio > 0) {
2309 if (tracks.data==0 && tracks.cdi==0 && tracks.xa==0)
2314 if (tracks.cdi > 0) return CDS_XA_2_2;
2315 if (tracks.xa > 0) return CDS_XA_2_1;
2316 if (tracks.data > 0) return CDS_DATA_1;
2317 /* Policy mode off */
2319 cdinfo(CD_WARNING,"This disc doesn't have any tracks I recognize!\n");
2323 case CDROM_CHANGER_NSLOTS: {
2324 cdinfo(CD_DO_IOCTL, "entering CDROM_CHANGER_NSLOTS\n");
2325 return cdi->capacity;
2329 /* use the ioctls that are implemented through the generic_packet()
2330 interface. this may look at bit funny, but if -ENOTTY is
2331 returned that particular ioctl is not implemented and we
2332 let it go through the device specific ones. */
2333 if (CDROM_CAN(CDC_GENERIC_PACKET)) {
2334 ret = mmc_ioctl(cdi, cmd, arg);
2335 if (ret != -ENOTTY) {
2340 /* note: most of the cdinfo() calls are commented out here,
2341 because they fill up the sys log when CD players poll
2344 case CDROMSUBCHNL: {
2345 struct cdrom_subchnl q;
2346 u_char requested, back;
2347 if (!CDROM_CAN(CDC_PLAY_AUDIO))
2349 /* cdinfo(CD_DO_IOCTL,"entering CDROMSUBCHNL\n");*/
2350 IOCTL_IN(arg, struct cdrom_subchnl, q);
2351 requested = q.cdsc_format;
2352 if (!((requested == CDROM_MSF) ||
2353 (requested == CDROM_LBA)))
2355 q.cdsc_format = CDROM_MSF;
2356 if ((ret=cdo->audio_ioctl(cdi, cmd, &q)))
2358 back = q.cdsc_format; /* local copy */
2359 sanitize_format(&q.cdsc_absaddr, &back, requested);
2360 sanitize_format(&q.cdsc_reladdr, &q.cdsc_format, requested);
2361 IOCTL_OUT(arg, struct cdrom_subchnl, q);
2362 /* cdinfo(CD_DO_IOCTL, "CDROMSUBCHNL successful\n"); */
2365 case CDROMREADTOCHDR: {
2366 struct cdrom_tochdr header;
2367 if (!CDROM_CAN(CDC_PLAY_AUDIO))
2369 /* cdinfo(CD_DO_IOCTL, "entering CDROMREADTOCHDR\n"); */
2370 IOCTL_IN(arg, struct cdrom_tochdr, header);
2371 if ((ret=cdo->audio_ioctl(cdi, cmd, &header)))
2373 IOCTL_OUT(arg, struct cdrom_tochdr, header);
2374 /* cdinfo(CD_DO_IOCTL, "CDROMREADTOCHDR successful\n"); */
2377 case CDROMREADTOCENTRY: {
2378 struct cdrom_tocentry entry;
2379 u_char requested_format;
2380 if (!CDROM_CAN(CDC_PLAY_AUDIO))
2382 /* cdinfo(CD_DO_IOCTL, "entering CDROMREADTOCENTRY\n"); */
2383 IOCTL_IN(arg, struct cdrom_tocentry, entry);
2384 requested_format = entry.cdte_format;
2385 if (!((requested_format == CDROM_MSF) ||
2386 (requested_format == CDROM_LBA)))
2388 /* make interface to low-level uniform */
2389 entry.cdte_format = CDROM_MSF;
2390 if ((ret=cdo->audio_ioctl(cdi, cmd, &entry)))
2392 sanitize_format(&entry.cdte_addr,
2393 &entry.cdte_format, requested_format);
2394 IOCTL_OUT(arg, struct cdrom_tocentry, entry);
2395 /* cdinfo(CD_DO_IOCTL, "CDROMREADTOCENTRY successful\n"); */
2398 case CDROMPLAYMSF: {
2399 struct cdrom_msf msf;
2400 if (!CDROM_CAN(CDC_PLAY_AUDIO))
2402 cdinfo(CD_DO_IOCTL, "entering CDROMPLAYMSF\n");
2403 IOCTL_IN(arg, struct cdrom_msf, msf);
2404 return cdo->audio_ioctl(cdi, cmd, &msf);
2406 case CDROMPLAYTRKIND: {
2408 if (!CDROM_CAN(CDC_PLAY_AUDIO))
2410 cdinfo(CD_DO_IOCTL, "entering CDROMPLAYTRKIND\n");
2411 IOCTL_IN(arg, struct cdrom_ti, ti);
2413 return cdo->audio_ioctl(cdi, cmd, &ti);
2415 case CDROMVOLCTRL: {
2416 struct cdrom_volctrl volume;
2417 if (!CDROM_CAN(CDC_PLAY_AUDIO))
2419 cdinfo(CD_DO_IOCTL, "entering CDROMVOLCTRL\n");
2420 IOCTL_IN(arg, struct cdrom_volctrl, volume);
2421 return cdo->audio_ioctl(cdi, cmd, &volume);
2423 case CDROMVOLREAD: {
2424 struct cdrom_volctrl volume;
2425 if (!CDROM_CAN(CDC_PLAY_AUDIO))
2427 cdinfo(CD_DO_IOCTL, "entering CDROMVOLREAD\n");
2428 if ((ret=cdo->audio_ioctl(cdi, cmd, &volume)))
2430 IOCTL_OUT(arg, struct cdrom_volctrl, volume);
2437 if (!CDROM_CAN(CDC_PLAY_AUDIO))
2439 cdinfo(CD_DO_IOCTL, "doing audio ioctl (start/stop/pause/resume)\n");
2441 return cdo->audio_ioctl(cdi, cmd, NULL);
2445 /* do the device specific ioctls */
2446 if (CDROM_CAN(CDC_IOCTLS))
2447 return cdo->dev_ioctl(cdi, cmd, arg);
2453 int msf_to_lba(char m, char s, char f)
2455 return (((m * CD_SECS) + s) * CD_FRAMES + f) - CD_MSF_OFFSET;
2459 * Required when we need to use READ_10 to issue other than 2048 block
2462 static int cdrom_switch_blocksize(struct cdrom_device_info *cdi, int size)
2464 struct cdrom_device_ops *cdo = cdi->ops;
2465 struct packet_command cgc;
2466 struct modesel_head mh;
2468 memset(&mh, 0, sizeof(mh));
2469 mh.block_desc_length = 0x08;
2470 mh.block_length_med = (size >> 8) & 0xff;
2471 mh.block_length_lo = size & 0xff;
2473 memset(&cgc, 0, sizeof(cgc));
2475 cgc.cmd[1] = 1 << 4;
2477 cgc.buflen = sizeof(mh);
2478 cgc.buffer = (char *) &mh;
2479 cgc.data_direction = CGC_DATA_WRITE;
2480 mh.block_desc_length = 0x08;
2481 mh.block_length_med = (size >> 8) & 0xff;
2482 mh.block_length_lo = size & 0xff;
2484 return cdo->generic_packet(cdi, &cgc);
2487 static int mmc_ioctl(struct cdrom_device_info *cdi, unsigned int cmd,
2490 struct cdrom_device_ops *cdo = cdi->ops;
2491 struct packet_command cgc;
2492 struct request_sense sense;
2496 memset(&cgc, 0, sizeof(cgc));
2498 /* build a unified command and queue it through
2499 cdo->generic_packet() */
2502 case CDROMREADMODE1:
2503 case CDROMREADMODE2: {
2504 struct cdrom_msf msf;
2505 int blocksize = 0, format = 0, lba;
2509 blocksize = CD_FRAMESIZE_RAW;
2511 case CDROMREADMODE1:
2512 blocksize = CD_FRAMESIZE;
2515 case CDROMREADMODE2:
2516 blocksize = CD_FRAMESIZE_RAW0;
2519 IOCTL_IN(arg, struct cdrom_msf, msf);
2520 lba = msf_to_lba(msf.cdmsf_min0,msf.cdmsf_sec0,msf.cdmsf_frame0);
2521 /* FIXME: we need upper bound checking, too!! */
2524 cgc.buffer = (char *) kmalloc(blocksize, GFP_KERNEL);
2525 if (cgc.buffer == NULL)
2527 memset(&sense, 0, sizeof(sense));
2529 cgc.data_direction = CGC_DATA_READ;
2530 ret = cdrom_read_block(cdi, &cgc, lba, 1, format, blocksize);
2531 if (ret && sense.sense_key==0x05 && sense.asc==0x20 && sense.ascq==0x00) {
2533 * SCSI-II devices are not required to support
2534 * READ_CD, so let's try switching block size
2536 /* FIXME: switch back again... */
2537 if ((ret = cdrom_switch_blocksize(cdi, blocksize))) {
2542 ret = cdrom_read_cd(cdi, &cgc, lba, blocksize, 1);
2543 ret |= cdrom_switch_blocksize(cdi, blocksize);
2545 if (!ret && copy_to_user((char __user *)arg, cgc.buffer, blocksize))
2550 case CDROMREADAUDIO: {
2551 struct cdrom_read_audio ra;
2554 IOCTL_IN(arg, struct cdrom_read_audio, ra);
2556 if (ra.addr_format == CDROM_MSF)
2557 lba = msf_to_lba(ra.addr.msf.minute,
2560 else if (ra.addr_format == CDROM_LBA)
2565 /* FIXME: we need upper bound checking, too!! */
2566 if (lba < 0 || ra.nframes <= 0 || ra.nframes > CD_FRAMES)
2569 return cdrom_read_cdda(cdi, ra.buf, lba, ra.nframes);
2571 case CDROMSUBCHNL: {
2572 struct cdrom_subchnl q;
2573 u_char requested, back;
2574 IOCTL_IN(arg, struct cdrom_subchnl, q);
2575 requested = q.cdsc_format;
2576 if (!((requested == CDROM_MSF) ||
2577 (requested == CDROM_LBA)))
2579 q.cdsc_format = CDROM_MSF;
2580 if ((ret = cdrom_read_subchannel(cdi, &q, 0)))
2582 back = q.cdsc_format; /* local copy */
2583 sanitize_format(&q.cdsc_absaddr, &back, requested);
2584 sanitize_format(&q.cdsc_reladdr, &q.cdsc_format, requested);
2585 IOCTL_OUT(arg, struct cdrom_subchnl, q);
2586 /* cdinfo(CD_DO_IOCTL, "CDROMSUBCHNL successful\n"); */
2589 case CDROMPLAYMSF: {
2590 struct cdrom_msf msf;
2591 cdinfo(CD_DO_IOCTL, "entering CDROMPLAYMSF\n");
2592 IOCTL_IN(arg, struct cdrom_msf, msf);
2593 cgc.cmd[0] = GPCMD_PLAY_AUDIO_MSF;
2594 cgc.cmd[3] = msf.cdmsf_min0;
2595 cgc.cmd[4] = msf.cdmsf_sec0;
2596 cgc.cmd[5] = msf.cdmsf_frame0;
2597 cgc.cmd[6] = msf.cdmsf_min1;
2598 cgc.cmd[7] = msf.cdmsf_sec1;
2599 cgc.cmd[8] = msf.cdmsf_frame1;
2600 cgc.data_direction = CGC_DATA_NONE;
2601 return cdo->generic_packet(cdi, &cgc);
2603 case CDROMPLAYBLK: {
2604 struct cdrom_blk blk;
2605 cdinfo(CD_DO_IOCTL, "entering CDROMPLAYBLK\n");
2606 IOCTL_IN(arg, struct cdrom_blk, blk);
2607 cgc.cmd[0] = GPCMD_PLAY_AUDIO_10;
2608 cgc.cmd[2] = (blk.from >> 24) & 0xff;
2609 cgc.cmd[3] = (blk.from >> 16) & 0xff;
2610 cgc.cmd[4] = (blk.from >> 8) & 0xff;
2611 cgc.cmd[5] = blk.from & 0xff;
2612 cgc.cmd[7] = (blk.len >> 8) & 0xff;
2613 cgc.cmd[8] = blk.len & 0xff;
2614 cgc.data_direction = CGC_DATA_NONE;
2615 return cdo->generic_packet(cdi, &cgc);
2618 case CDROMVOLREAD: {
2619 struct cdrom_volctrl volctrl;
2621 unsigned short offset;
2622 cdinfo(CD_DO_IOCTL, "entering CDROMVOLUME\n");
2624 IOCTL_IN(arg, struct cdrom_volctrl, volctrl);
2626 cgc.buffer = buffer;
2628 if ((ret = cdrom_mode_sense(cdi, &cgc, GPMODE_AUDIO_CTL_PAGE, 0)))
2631 /* some drives have longer pages, adjust and reread. */
2632 if (buffer[1] > cgc.buflen) {
2633 cgc.buflen = buffer[1] + 2;
2634 if ((ret = cdrom_mode_sense(cdi, &cgc,
2635 GPMODE_AUDIO_CTL_PAGE, 0)))
2639 /* get the offset from the length of the page. length
2640 is measure from byte 2 an on, thus the 14. */
2641 offset = buffer[1] - 14;
2643 /* now we have the current volume settings. if it was only
2644 a CDROMVOLREAD, return these values */
2645 if (cmd == CDROMVOLREAD) {
2646 volctrl.channel0 = buffer[offset+9];
2647 volctrl.channel1 = buffer[offset+11];
2648 volctrl.channel2 = buffer[offset+13];
2649 volctrl.channel3 = buffer[offset+15];
2650 IOCTL_OUT(arg, struct cdrom_volctrl, volctrl);
2654 /* get the volume mask */
2656 if ((ret = cdrom_mode_sense(cdi, &cgc,
2657 GPMODE_AUDIO_CTL_PAGE, 1)))
2660 buffer[offset+9] = volctrl.channel0 & mask[offset+9];
2661 buffer[offset+11] = volctrl.channel1 & mask[offset+11];
2662 buffer[offset+13] = volctrl.channel2 & mask[offset+13];
2663 buffer[offset+15] = volctrl.channel3 & mask[offset+15];
2666 cgc.buffer = buffer;
2667 return cdrom_mode_select(cdi, &cgc);
2672 cdinfo(CD_DO_IOCTL, "entering CDROMSTART/CDROMSTOP\n");
2673 cgc.cmd[0] = GPCMD_START_STOP_UNIT;
2675 cgc.cmd[4] = (cmd == CDROMSTART) ? 1 : 0;
2676 cgc.data_direction = CGC_DATA_NONE;
2677 return cdo->generic_packet(cdi, &cgc);
2682 cdinfo(CD_DO_IOCTL, "entering CDROMPAUSE/CDROMRESUME\n");
2683 cgc.cmd[0] = GPCMD_PAUSE_RESUME;
2684 cgc.cmd[8] = (cmd == CDROMRESUME) ? 1 : 0;
2685 cgc.data_direction = CGC_DATA_NONE;
2686 return cdo->generic_packet(cdi, &cgc);
2689 case DVD_READ_STRUCT: {
2691 int size = sizeof(dvd_struct);
2692 if (!CDROM_CAN(CDC_DVD))
2694 if ((s = (dvd_struct *) kmalloc(size, GFP_KERNEL)) == NULL)
2696 cdinfo(CD_DO_IOCTL, "entering DVD_READ_STRUCT\n");
2697 if (copy_from_user(s, (dvd_struct __user *)arg, size)) {
2701 if ((ret = dvd_read_struct(cdi, s))) {
2705 if (copy_to_user((dvd_struct __user *)arg, s, size))
2713 if (!CDROM_CAN(CDC_DVD))
2715 cdinfo(CD_DO_IOCTL, "entering DVD_AUTH\n");
2716 IOCTL_IN(arg, dvd_authinfo, ai);
2717 if ((ret = dvd_do_auth (cdi, &ai)))
2719 IOCTL_OUT(arg, dvd_authinfo, ai);
2723 case CDROM_NEXT_WRITABLE: {
2725 cdinfo(CD_DO_IOCTL, "entering CDROM_NEXT_WRITABLE\n");
2726 if ((ret = cdrom_get_next_writable(cdi, &next)))
2728 IOCTL_OUT(arg, long, next);
2731 case CDROM_LAST_WRITTEN: {
2733 cdinfo(CD_DO_IOCTL, "entering CDROM_LAST_WRITTEN\n");
2734 if ((ret = cdrom_get_last_written(cdi, &last)))
2736 IOCTL_OUT(arg, long, last);
2744 static int cdrom_get_track_info(struct cdrom_device_info *cdi, __u16 track, __u8 type,
2745 track_information *ti)
2747 struct cdrom_device_ops *cdo = cdi->ops;
2748 struct packet_command cgc;
2751 init_cdrom_command(&cgc, ti, 8, CGC_DATA_READ);
2752 cgc.cmd[0] = GPCMD_READ_TRACK_RZONE_INFO;
2753 cgc.cmd[1] = type & 3;
2754 cgc.cmd[4] = (track & 0xff00) >> 8;
2755 cgc.cmd[5] = track & 0xff;
2759 if ((ret = cdo->generic_packet(cdi, &cgc)))
2762 cgc.buflen = be16_to_cpu(ti->track_information_length) +
2763 sizeof(ti->track_information_length);
2765 if (cgc.buflen > sizeof(track_information))
2766 cgc.buflen = sizeof(track_information);
2768 cgc.cmd[8] = cgc.buflen;
2769 return cdo->generic_packet(cdi, &cgc);
2772 /* requires CD R/RW */
2773 static int cdrom_get_disc_info(struct cdrom_device_info *cdi, disc_information *di)
2775 struct cdrom_device_ops *cdo = cdi->ops;
2776 struct packet_command cgc;
2779 /* set up command and get the disc info */
2780 init_cdrom_command(&cgc, di, sizeof(*di), CGC_DATA_READ);
2781 cgc.cmd[0] = GPCMD_READ_DISC_INFO;
2782 cgc.cmd[8] = cgc.buflen = 2;
2785 if ((ret = cdo->generic_packet(cdi, &cgc)))
2788 /* not all drives have the same disc_info length, so requeue
2789 * packet with the length the drive tells us it can supply
2791 cgc.buflen = be16_to_cpu(di->disc_information_length) +
2792 sizeof(di->disc_information_length);
2794 if (cgc.buflen > sizeof(disc_information))
2795 cgc.buflen = sizeof(disc_information);
2797 cgc.cmd[8] = cgc.buflen;
2798 return cdo->generic_packet(cdi, &cgc);
2801 /* return the last written block on the CD-R media. this is for the udf
2803 int cdrom_get_last_written(struct cdrom_device_info *cdi, long *last_written)
2805 struct cdrom_tocentry toc;
2806 disc_information di;
2807 track_information ti;
2811 if (!CDROM_CAN(CDC_GENERIC_PACKET))
2814 if ((ret = cdrom_get_disc_info(cdi, &di)))
2817 last_track = (di.last_track_msb << 8) | di.last_track_lsb;
2818 if ((ret = cdrom_get_track_info(cdi, last_track, 1, &ti)))
2821 /* if this track is blank, try the previous. */
2824 if ((ret = cdrom_get_track_info(cdi, last_track, 1, &ti)))
2828 /* if last recorded field is valid, return it. */
2830 *last_written = be32_to_cpu(ti.last_rec_address);
2832 /* make it up instead */
2833 *last_written = be32_to_cpu(ti.track_start) +
2834 be32_to_cpu(ti.track_size);
2836 *last_written -= (be32_to_cpu(ti.free_blocks) + 7);
2840 /* this is where we end up if the drive either can't do a
2841 GPCMD_READ_DISC_INFO or GPCMD_READ_TRACK_RZONE_INFO or if
2842 it fails. then we return the toc contents. */
2844 toc.cdte_format = CDROM_MSF;
2845 toc.cdte_track = CDROM_LEADOUT;
2846 if (cdi->ops->audio_ioctl(cdi, CDROMREADTOCENTRY, &toc))
2848 sanitize_format(&toc.cdte_addr, &toc.cdte_format, CDROM_LBA);
2849 *last_written = toc.cdte_addr.lba;
2853 /* return the next writable block. also for udf file system. */
2854 static int cdrom_get_next_writable(struct cdrom_device_info *cdi, long *next_writable)
2856 disc_information di;
2857 track_information ti;
2861 if (!CDROM_CAN(CDC_GENERIC_PACKET))
2862 goto use_last_written;
2864 if ((ret = cdrom_get_disc_info(cdi, &di)))
2865 goto use_last_written;
2867 last_track = (di.last_track_msb << 8) | di.last_track_lsb;
2868 if ((ret = cdrom_get_track_info(cdi, last_track, 1, &ti)))
2869 goto use_last_written;
2871 /* if this track is blank, try the previous. */
2874 if ((ret = cdrom_get_track_info(cdi, last_track, 1, &ti)))
2875 goto use_last_written;
2878 /* if next recordable address field is valid, use it. */
2880 *next_writable = be32_to_cpu(ti.next_writable);
2882 goto use_last_written;
2887 if ((ret = cdrom_get_last_written(cdi, next_writable))) {
2891 *next_writable += 7;
2896 EXPORT_SYMBOL(cdrom_get_last_written);
2897 EXPORT_SYMBOL(register_cdrom);
2898 EXPORT_SYMBOL(unregister_cdrom);
2899 EXPORT_SYMBOL(cdrom_open);
2900 EXPORT_SYMBOL(cdrom_release);
2901 EXPORT_SYMBOL(cdrom_ioctl);
2902 EXPORT_SYMBOL(cdrom_media_changed);
2903 EXPORT_SYMBOL(cdrom_number_of_slots);
2904 EXPORT_SYMBOL(cdrom_mode_select);
2905 EXPORT_SYMBOL(cdrom_mode_sense);
2906 EXPORT_SYMBOL(init_cdrom_command);
2907 EXPORT_SYMBOL(cdrom_get_media_event);
2908 EXPORT_SYMBOL(cdrom_is_mrw);
2909 EXPORT_SYMBOL(cdrom_is_random_writable);
2911 #ifdef CONFIG_SYSCTL
2913 #define CDROM_STR_SIZE 1000
2915 struct cdrom_sysctl_settings {
2916 char info[CDROM_STR_SIZE]; /* general info */
2917 int autoclose; /* close tray upon mount, etc */
2918 int autoeject; /* eject on umount */
2919 int debug; /* turn on debugging messages */
2920 int lock; /* lock the door on device open */
2921 int check; /* check media type */
2922 } cdrom_sysctl_settings;
2924 int cdrom_sysctl_info(ctl_table *ctl, int write, struct file * filp,
2925 void __user *buffer, size_t *lenp)
2928 struct cdrom_device_info *cdi;
2929 char *info = cdrom_sysctl_settings.info;
2931 if (!*lenp || (filp->f_pos && !write)) {
2936 pos = sprintf(info, "CD-ROM information, " VERSION "\n");
2938 pos += sprintf(info+pos, "\ndrive name:\t");
2939 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
2940 pos += sprintf(info+pos, "\t%s", cdi->name);
2942 pos += sprintf(info+pos, "\ndrive speed:\t");
2943 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
2944 pos += sprintf(info+pos, "\t%d", cdi->speed);
2946 pos += sprintf(info+pos, "\ndrive # of slots:");
2947 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
2948 pos += sprintf(info+pos, "\t%d", cdi->capacity);
2950 pos += sprintf(info+pos, "\nCan close tray:\t");
2951 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
2952 pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_CLOSE_TRAY) != 0);
2954 pos += sprintf(info+pos, "\nCan open tray:\t");
2955 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
2956 pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_OPEN_TRAY) != 0);
2958 pos += sprintf(info+pos, "\nCan lock tray:\t");
2959 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
2960 pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_LOCK) != 0);
2962 pos += sprintf(info+pos, "\nCan change speed:");
2963 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
2964 pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_SELECT_SPEED) != 0);
2966 pos += sprintf(info+pos, "\nCan select disk:");
2967 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
2968 pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_SELECT_DISC) != 0);
2970 pos += sprintf(info+pos, "\nCan read multisession:");
2971 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
2972 pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_MULTI_SESSION) != 0);
2974 pos += sprintf(info+pos, "\nCan read MCN:\t");
2975 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
2976 pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_MCN) != 0);
2978 pos += sprintf(info+pos, "\nReports media changed:");
2979 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
2980 pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_MEDIA_CHANGED) != 0);
2982 pos += sprintf(info+pos, "\nCan play audio:\t");
2983 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
2984 pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_PLAY_AUDIO) != 0);
2986 pos += sprintf(info+pos, "\nCan write CD-R:\t");
2987 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
2988 pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_CD_R) != 0);
2990 pos += sprintf(info+pos, "\nCan write CD-RW:");
2991 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
2992 pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_CD_RW) != 0);
2994 pos += sprintf(info+pos, "\nCan read DVD:\t");
2995 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
2996 pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_DVD) != 0);
2998 pos += sprintf(info+pos, "\nCan write DVD-R:");
2999 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
3000 pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_DVD_R) != 0);
3002 pos += sprintf(info+pos, "\nCan write DVD-RAM:");
3003 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
3004 pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_DVD_RAM) != 0);
3006 pos += sprintf(info+pos, "\nCan read MRW:\t");
3007 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
3008 pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_MRW) != 0);
3010 pos += sprintf(info+pos, "\nCan write MRW:\t");
3011 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
3012 pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_MRW_W) != 0);
3014 pos += sprintf(info+pos, "\nCan write RAM:\t");
3015 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
3016 pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_RAM) != 0);
3018 strcpy(info+pos,"\n\n");
3020 return proc_dostring(ctl, write, filp, buffer, lenp);
3023 /* Unfortunately, per device settings are not implemented through
3024 procfs/sysctl yet. When they are, this will naturally disappear. For now
3025 just update all drives. Later this will become the template on which
3026 new registered drives will be based. */
3027 void cdrom_update_settings(void)
3029 struct cdrom_device_info *cdi;
3031 for (cdi = topCdromPtr; cdi != NULL; cdi = cdi->next) {
3032 if (autoclose && CDROM_CAN(CDC_CLOSE_TRAY))
3033 cdi->options |= CDO_AUTO_CLOSE;
3034 else if (!autoclose)
3035 cdi->options &= ~CDO_AUTO_CLOSE;
3036 if (autoeject && CDROM_CAN(CDC_OPEN_TRAY))
3037 cdi->options |= CDO_AUTO_EJECT;
3038 else if (!autoeject)
3039 cdi->options &= ~CDO_AUTO_EJECT;
3040 if (lockdoor && CDROM_CAN(CDC_LOCK))
3041 cdi->options |= CDO_LOCK;
3043 cdi->options &= ~CDO_LOCK;
3044 if (check_media_type)
3045 cdi->options |= CDO_CHECK_TYPE;
3047 cdi->options &= ~CDO_CHECK_TYPE;
3051 static int cdrom_sysctl_handler(ctl_table *ctl, int write, struct file * filp,
3052 void __user *buffer, size_t *lenp)
3054 int *valp = ctl->data;
3058 ret = proc_dointvec(ctl, write, filp, buffer, lenp);
3060 if (write && *valp != val) {
3062 /* we only care for 1 or 0. */
3068 switch (ctl->ctl_name) {
3069 case DEV_CDROM_AUTOCLOSE: {
3070 if (valp == &cdrom_sysctl_settings.autoclose)
3071 autoclose = cdrom_sysctl_settings.autoclose;
3074 case DEV_CDROM_AUTOEJECT: {
3075 if (valp == &cdrom_sysctl_settings.autoeject)
3076 autoeject = cdrom_sysctl_settings.autoeject;
3079 case DEV_CDROM_DEBUG: {
3080 if (valp == &cdrom_sysctl_settings.debug)
3081 debug = cdrom_sysctl_settings.debug;
3084 case DEV_CDROM_LOCK: {
3085 if (valp == &cdrom_sysctl_settings.lock)
3086 lockdoor = cdrom_sysctl_settings.lock;
3089 case DEV_CDROM_CHECK_MEDIA: {
3090 if (valp == &cdrom_sysctl_settings.check)
3091 check_media_type = cdrom_sysctl_settings.check;
3095 /* update the option flags according to the changes. we
3096 don't have per device options through sysctl yet,
3097 but we will have and then this will disappear. */
3098 cdrom_update_settings();
3104 /* Place files in /proc/sys/dev/cdrom */
3105 ctl_table cdrom_table[] = {
3107 .ctl_name = DEV_CDROM_INFO,
3109 .data = &cdrom_sysctl_settings.info,
3110 .maxlen = CDROM_STR_SIZE,
3112 .proc_handler = &cdrom_sysctl_info,
3115 .ctl_name = DEV_CDROM_AUTOCLOSE,
3116 .procname = "autoclose",
3117 .data = &cdrom_sysctl_settings.autoclose,
3118 .maxlen = sizeof(int),
3120 .proc_handler = &cdrom_sysctl_handler,
3123 .ctl_name = DEV_CDROM_AUTOEJECT,
3124 .procname = "autoeject",
3125 .data = &cdrom_sysctl_settings.autoeject,
3126 .maxlen = sizeof(int),
3128 .proc_handler = &cdrom_sysctl_handler,
3131 .ctl_name = DEV_CDROM_DEBUG,
3132 .procname = "debug",
3133 .data = &cdrom_sysctl_settings.debug,
3134 .maxlen = sizeof(int),
3136 .proc_handler = &cdrom_sysctl_handler,
3139 .ctl_name = DEV_CDROM_LOCK,
3141 .data = &cdrom_sysctl_settings.lock,
3142 .maxlen = sizeof(int),
3144 .proc_handler = &cdrom_sysctl_handler,
3147 .ctl_name = DEV_CDROM_CHECK_MEDIA,
3148 .procname = "check_media",
3149 .data = &cdrom_sysctl_settings.check,
3150 .maxlen = sizeof(int),
3152 .proc_handler = &cdrom_sysctl_handler
3157 ctl_table cdrom_cdrom_table[] = {
3159 .ctl_name = DEV_CDROM,
3160 .procname = "cdrom",
3163 .child = cdrom_table,
3168 /* Make sure that /proc/sys/dev is there */
3169 ctl_table cdrom_root_table[] = {
3170 #ifdef CONFIG_PROC_FS
3172 .ctl_name = CTL_DEV,
3176 .child = cdrom_cdrom_table,
3178 #endif /* CONFIG_PROC_FS */
3181 static struct ctl_table_header *cdrom_sysctl_header;
3183 static void cdrom_sysctl_register(void)
3185 static int initialized;
3187 if (initialized == 1)
3190 cdrom_sysctl_header = register_sysctl_table(cdrom_root_table, 1);
3191 if (cdrom_root_table->ctl_name && cdrom_root_table->child->de)
3192 cdrom_root_table->child->de->owner = THIS_MODULE;
3194 /* set the defaults */
3195 cdrom_sysctl_settings.autoclose = autoclose;
3196 cdrom_sysctl_settings.autoeject = autoeject;
3197 cdrom_sysctl_settings.debug = debug;
3198 cdrom_sysctl_settings.lock = lockdoor;
3199 cdrom_sysctl_settings.check = check_media_type;
3204 static void cdrom_sysctl_unregister(void)
3206 if (cdrom_sysctl_header)
3207 unregister_sysctl_table(cdrom_sysctl_header);
3210 #endif /* CONFIG_SYSCTL */
3212 static int __init cdrom_init(void)
3214 #ifdef CONFIG_SYSCTL
3215 cdrom_sysctl_register();
3220 static void __exit cdrom_exit(void)
3222 printk(KERN_INFO "Uniform CD-ROM driver unloaded\n");
3223 #ifdef CONFIG_SYSCTL
3224 cdrom_sysctl_unregister();
3228 module_init(cdrom_init);
3229 module_exit(cdrom_exit);
3230 MODULE_LICENSE("GPL");