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 static int cdrom_dummy_generic_packet(struct cdrom_device_info *cdi,
358 struct packet_command *cgc)
361 cgc->sense->sense_key = 0x05;
362 cgc->sense->asc = 0x20;
363 cgc->sense->ascq = 0x00;
370 /* This macro makes sure we don't have to check on cdrom_device_ops
371 * existence in the run-time routines below. Change_capability is a
372 * hack to have the capability flags defined const, while we can still
373 * change it here without gcc complaining at every line.
375 #define ENSURE(call, bits) if (cdo->call == NULL) *change_capability &= ~(bits)
377 int register_cdrom(struct cdrom_device_info *cdi)
379 static char banner_printed;
380 struct cdrom_device_ops *cdo = cdi->ops;
381 int *change_capability = (int *)&cdo->capability; /* hack */
383 cdinfo(CD_OPEN, "entering register_cdrom\n");
385 if (cdo->open == NULL || cdo->release == NULL)
387 if (!banner_printed) {
388 printk(KERN_INFO "Uniform CD-ROM driver " REVISION "\n");
391 cdrom_sysctl_register();
392 #endif /* CONFIG_SYSCTL */
395 ENSURE(drive_status, CDC_DRIVE_STATUS );
396 ENSURE(media_changed, CDC_MEDIA_CHANGED);
397 ENSURE(tray_move, CDC_CLOSE_TRAY | CDC_OPEN_TRAY);
398 ENSURE(lock_door, CDC_LOCK);
399 ENSURE(select_speed, CDC_SELECT_SPEED);
400 ENSURE(get_last_session, CDC_MULTI_SESSION);
401 ENSURE(get_mcn, CDC_MCN);
402 ENSURE(reset, CDC_RESET);
403 ENSURE(audio_ioctl, CDC_PLAY_AUDIO);
404 ENSURE(dev_ioctl, CDC_IOCTLS);
405 ENSURE(generic_packet, CDC_GENERIC_PACKET);
408 cdi->options = CDO_USE_FFLAGS;
410 if (autoclose==1 && CDROM_CAN(CDC_CLOSE_TRAY))
411 cdi->options |= (int) CDO_AUTO_CLOSE;
412 if (autoeject==1 && CDROM_CAN(CDC_OPEN_TRAY))
413 cdi->options |= (int) CDO_AUTO_EJECT;
415 cdi->options |= (int) CDO_LOCK;
416 if (check_media_type==1)
417 cdi->options |= (int) CDO_CHECK_TYPE;
419 if (CDROM_CAN(CDC_MRW_W))
420 cdi->exit = cdrom_mrw_exit;
423 cdi->cdda_method = CDDA_BPC_FULL;
425 cdi->cdda_method = CDDA_OLD;
427 if (!cdo->generic_packet)
428 cdo->generic_packet = cdrom_dummy_generic_packet;
430 cdinfo(CD_REG_UNREG, "drive \"/dev/%s\" registered\n", cdi->name);
431 spin_lock(&cdrom_lock);
432 cdi->next = topCdromPtr;
434 spin_unlock(&cdrom_lock);
439 int unregister_cdrom(struct cdrom_device_info *unreg)
441 struct cdrom_device_info *cdi, *prev;
442 cdinfo(CD_OPEN, "entering unregister_cdrom\n");
445 spin_lock(&cdrom_lock);
447 while (cdi && cdi != unreg) {
453 spin_unlock(&cdrom_lock);
457 prev->next = cdi->next;
459 topCdromPtr = cdi->next;
461 spin_unlock(&cdrom_lock);
466 cdi->ops->n_minors--;
467 cdinfo(CD_REG_UNREG, "drive \"/dev/%s\" unregistered\n", cdi->name);
471 int cdrom_get_media_event(struct cdrom_device_info *cdi,
472 struct media_event_desc *med)
474 struct packet_command cgc;
475 unsigned char buffer[8];
476 struct event_header *eh = (struct event_header *) buffer;
478 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_READ);
479 cgc.cmd[0] = GPCMD_GET_EVENT_STATUS_NOTIFICATION;
480 cgc.cmd[1] = 1; /* IMMED */
481 cgc.cmd[4] = 1 << 4; /* media event */
482 cgc.cmd[8] = sizeof(buffer);
485 if (cdi->ops->generic_packet(cdi, &cgc))
488 if (be16_to_cpu(eh->data_len) < sizeof(*med))
491 if (eh->nea || eh->notification_class != 0x4)
494 memcpy(med, &buffer[sizeof(*eh)], sizeof(*med));
499 * the first prototypes used 0x2c as the page code for the mrw mode page,
500 * subsequently this was changed to 0x03. probe the one used by this drive
502 int cdrom_mrw_probe_pc(struct cdrom_device_info *cdi)
504 struct packet_command cgc;
507 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_READ);
512 if (!cdrom_mode_sense(cdi, &cgc, MRW_MODE_PC, 0)) {
513 cdi->mrw_mode_page = MRW_MODE_PC;
515 } else if (!cdrom_mode_sense(cdi, &cgc, MRW_MODE_PC_PRE1, 0)) {
516 cdi->mrw_mode_page = MRW_MODE_PC_PRE1;
523 int cdrom_is_mrw(struct cdrom_device_info *cdi, int *write)
525 struct packet_command cgc;
526 struct mrw_feature_desc *mfd;
527 unsigned char buffer[16];
532 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_READ);
534 cgc.cmd[0] = GPCMD_GET_CONFIGURATION;
535 cgc.cmd[3] = CDF_MRW;
536 cgc.cmd[8] = sizeof(buffer);
539 if ((ret = cdi->ops->generic_packet(cdi, &cgc)))
542 mfd = (struct mrw_feature_desc *)&buffer[sizeof(struct feature_header)];
543 if (be16_to_cpu(mfd->feature_code) != CDF_MRW)
547 if ((ret = cdrom_mrw_probe_pc(cdi))) {
555 static int cdrom_mrw_bgformat(struct cdrom_device_info *cdi, int cont)
557 struct packet_command cgc;
558 unsigned char buffer[12];
561 printk(KERN_INFO "cdrom: %sstarting format\n", cont ? "Re" : "");
564 * FmtData bit set (bit 4), format type is 1
566 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_WRITE);
567 cgc.cmd[0] = GPCMD_FORMAT_UNIT;
568 cgc.cmd[1] = (1 << 4) | 1;
570 cgc.timeout = 5 * 60 * HZ;
573 * 4 byte format list header, 8 byte format list descriptor
586 buffer[8] = 0x24 << 2;
589 ret = cdi->ops->generic_packet(cdi, &cgc);
591 printk(KERN_INFO "cdrom: bgformat failed\n");
596 static int cdrom_mrw_bgformat_susp(struct cdrom_device_info *cdi, int immed)
598 struct packet_command cgc;
600 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
601 cgc.cmd[0] = GPCMD_CLOSE_TRACK;
604 * Session = 1, Track = 0
606 cgc.cmd[1] = !!immed;
609 cgc.timeout = 5 * 60 * HZ;
611 return cdi->ops->generic_packet(cdi, &cgc);
614 static int cdrom_flush_cache(struct cdrom_device_info *cdi)
616 struct packet_command cgc;
618 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
619 cgc.cmd[0] = GPCMD_FLUSH_CACHE;
621 cgc.timeout = 5 * 60 * HZ;
623 return cdi->ops->generic_packet(cdi, &cgc);
626 static int cdrom_mrw_exit(struct cdrom_device_info *cdi)
631 ret = cdrom_get_disc_info(cdi, &di);
632 if (ret < 0 || ret < (int)offsetof(typeof(di),disc_type))
636 if (di.mrw_status == CDM_MRW_BGFORMAT_ACTIVE) {
637 printk(KERN_INFO "cdrom: issuing MRW back ground "
639 ret = cdrom_mrw_bgformat_susp(cdi, 0);
643 ret = cdrom_flush_cache(cdi);
648 static int cdrom_mrw_set_lba_space(struct cdrom_device_info *cdi, int space)
650 struct packet_command cgc;
651 struct mode_page_header *mph;
653 int ret, offset, size;
655 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_READ);
658 cgc.buflen = sizeof(buffer);
660 if ((ret = cdrom_mode_sense(cdi, &cgc, cdi->mrw_mode_page, 0)))
663 mph = (struct mode_page_header *) buffer;
664 offset = be16_to_cpu(mph->desc_length);
665 size = be16_to_cpu(mph->mode_data_length) + 2;
667 buffer[offset + 3] = space;
670 if ((ret = cdrom_mode_select(cdi, &cgc)))
673 printk(KERN_INFO "cdrom: %s: mrw address space %s selected\n", cdi->name, mrw_address_space[space]);
677 int cdrom_get_random_writable(struct cdrom_device_info *cdi,
678 struct rwrt_feature_desc *rfd)
680 struct packet_command cgc;
684 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_READ);
686 cgc.cmd[0] = GPCMD_GET_CONFIGURATION; /* often 0x46 */
687 cgc.cmd[3] = CDF_RWRT; /* often 0x0020 */
688 cgc.cmd[8] = sizeof(buffer); /* often 0x18 */
691 if ((ret = cdi->ops->generic_packet(cdi, &cgc)))
694 memcpy(rfd, &buffer[sizeof(struct feature_header)], sizeof (*rfd));
698 int cdrom_has_defect_mgt(struct cdrom_device_info *cdi)
700 struct packet_command cgc;
705 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_READ);
707 cgc.cmd[0] = GPCMD_GET_CONFIGURATION;
708 cgc.cmd[3] = CDF_HWDM;
709 cgc.cmd[8] = sizeof(buffer);
712 if ((ret = cdi->ops->generic_packet(cdi, &cgc)))
715 feature_code = (__u16 *) &buffer[sizeof(struct feature_header)];
716 if (be16_to_cpu(*feature_code) == CDF_HWDM)
723 int cdrom_is_random_writable(struct cdrom_device_info *cdi, int *write)
725 struct rwrt_feature_desc rfd;
730 if ((ret = cdrom_get_random_writable(cdi, &rfd)))
733 if (CDF_RWRT == be16_to_cpu(rfd.feature_code))
739 static int cdrom_media_erasable(struct cdrom_device_info *cdi)
744 ret = cdrom_get_disc_info(cdi, &di);
745 if (ret < 0 || ret < offsetof(typeof(di), n_first_track))
752 * FIXME: check RO bit
754 static int cdrom_dvdram_open_write(struct cdrom_device_info *cdi)
756 int ret = cdrom_media_erasable(cdi);
759 * allow writable open if media info read worked and media is
760 * erasable, _or_ if it fails since not all drives support it
768 static int cdrom_mrw_open_write(struct cdrom_device_info *cdi)
774 * always reset to DMA lba space on open
776 if (cdrom_mrw_set_lba_space(cdi, MRW_LBA_DMA)) {
777 printk(KERN_ERR "cdrom: failed setting lba address space\n");
781 ret = cdrom_get_disc_info(cdi, &di);
782 if (ret < 0 || ret < offsetof(typeof(di),disc_type))
790 * 0 - not MRW formatted
791 * 1 - MRW bgformat started, but not running or complete
792 * 2 - MRW bgformat in progress
793 * 3 - MRW formatting complete
796 printk(KERN_INFO "cdrom open: mrw_status '%s'\n",
797 mrw_format_status[di.mrw_status]);
800 else if (di.mrw_status == CDM_MRW_BGFORMAT_INACTIVE &&
802 ret = cdrom_mrw_bgformat(cdi, 1);
807 static int mo_open_write(struct cdrom_device_info *cdi)
809 struct packet_command cgc;
813 init_cdrom_command(&cgc, &buffer, 4, CGC_DATA_READ);
817 * obtain write protect information as per
818 * drivers/scsi/sd.c:sd_read_write_protect_flag
821 ret = cdrom_mode_sense(cdi, &cgc, GPMODE_ALL_PAGES, 0);
823 ret = cdrom_mode_sense(cdi, &cgc, GPMODE_VENDOR_PAGE, 0);
826 ret = cdrom_mode_sense(cdi, &cgc, GPMODE_ALL_PAGES, 0);
829 /* drive gave us no info, let the user go ahead */
833 return buffer[3] & 0x80;
836 static int cdrom_ram_open_write(struct cdrom_device_info *cdi)
838 struct rwrt_feature_desc rfd;
841 if ((ret = cdrom_has_defect_mgt(cdi)))
844 if ((ret = cdrom_get_random_writable(cdi, &rfd)))
846 else if (CDF_RWRT == be16_to_cpu(rfd.feature_code))
849 cdinfo(CD_OPEN, "can open for random write\n");
854 * returns 0 for ok to open write, non-0 to disallow
856 static int cdrom_open_write(struct cdrom_device_info *cdi)
858 int mrw, mrw_write, ram_write;
862 if (!cdrom_is_mrw(cdi, &mrw_write))
865 if (CDROM_CAN(CDC_MO_DRIVE))
868 (void) cdrom_is_random_writable(cdi, &ram_write);
871 cdi->mask &= ~CDC_MRW;
873 cdi->mask |= CDC_MRW;
876 cdi->mask &= ~CDC_MRW_W;
878 cdi->mask |= CDC_MRW_W;
881 cdi->mask &= ~CDC_RAM;
883 cdi->mask |= CDC_RAM;
885 if (CDROM_CAN(CDC_MRW_W))
886 ret = cdrom_mrw_open_write(cdi);
887 else if (CDROM_CAN(CDC_DVD_RAM))
888 ret = cdrom_dvdram_open_write(cdi);
889 else if (CDROM_CAN(CDC_RAM) &&
890 !CDROM_CAN(CDC_CD_R|CDC_CD_RW|CDC_DVD|CDC_DVD_R|CDC_MRW|CDC_MO_DRIVE))
891 ret = cdrom_ram_open_write(cdi);
892 else if (CDROM_CAN(CDC_MO_DRIVE))
893 ret = mo_open_write(cdi);
898 static int cdrom_close_write(struct cdrom_device_info *cdi)
901 return cdrom_flush_cache(cdi);
907 /* We use the open-option O_NONBLOCK to indicate that the
908 * purpose of opening is only for subsequent ioctl() calls; no device
909 * integrity checks are performed.
911 * We hope that all cd-player programs will adopt this convention. It
912 * is in their own interest: device control becomes a lot easier
915 int cdrom_open(struct cdrom_device_info *cdi, struct inode *ip, struct file *fp)
919 cdinfo(CD_OPEN, "entering cdrom_open\n");
921 /* if this was a O_NONBLOCK open and we should honor the flags,
922 * do a quick open without drive/disc integrity checks. */
924 if ((fp->f_flags & O_NONBLOCK) && (cdi->options & CDO_USE_FFLAGS)) {
925 ret = cdi->ops->open(cdi, 1);
927 ret = open_for_data(cdi);
930 if (fp->f_mode & FMODE_WRITE) {
932 if (cdrom_open_write(cdi))
934 if (!CDROM_CAN(CDC_RAM))
943 cdinfo(CD_OPEN, "Use count for \"/dev/%s\" now %d\n",
944 cdi->name, cdi->use_count);
945 /* Do this on open. Don't wait for mount, because they might
946 not be mounting, but opening with O_NONBLOCK */
947 check_disk_change(ip->i_bdev);
955 int open_for_data(struct cdrom_device_info * cdi)
958 struct cdrom_device_ops *cdo = cdi->ops;
960 cdinfo(CD_OPEN, "entering open_for_data\n");
961 /* Check if the driver can report drive status. If it can, we
962 can do clever things. If it can't, well, we at least tried! */
963 if (cdo->drive_status != NULL) {
964 ret = cdo->drive_status(cdi, CDSL_CURRENT);
965 cdinfo(CD_OPEN, "drive_status=%d\n", ret);
966 if (ret == CDS_TRAY_OPEN) {
967 cdinfo(CD_OPEN, "the tray is open...\n");
968 /* can/may i close it? */
969 if (CDROM_CAN(CDC_CLOSE_TRAY) &&
970 cdi->options & CDO_AUTO_CLOSE) {
971 cdinfo(CD_OPEN, "trying to close the tray.\n");
972 ret=cdo->tray_move(cdi,0);
974 cdinfo(CD_OPEN, "bummer. tried to close the tray but failed.\n");
975 /* Ignore the error from the low
976 level driver. We don't care why it
977 couldn't close the tray. We only care
978 that there is no disc in the drive,
979 since that is the _REAL_ problem here.*/
981 goto clean_up_and_return;
984 cdinfo(CD_OPEN, "bummer. this drive can't close the tray.\n");
986 goto clean_up_and_return;
988 /* Ok, the door should be closed now.. Check again */
989 ret = cdo->drive_status(cdi, CDSL_CURRENT);
990 if ((ret == CDS_NO_DISC) || (ret==CDS_TRAY_OPEN)) {
991 cdinfo(CD_OPEN, "bummer. the tray is still not closed.\n");
992 cdinfo(CD_OPEN, "tray might not contain a medium.\n");
994 goto clean_up_and_return;
996 cdinfo(CD_OPEN, "the tray is now closed.\n");
998 if (ret!=CDS_DISC_OK) {
1000 goto clean_up_and_return;
1003 cdrom_count_tracks(cdi, &tracks);
1004 if (tracks.error == CDS_NO_DISC) {
1005 cdinfo(CD_OPEN, "bummer. no disc.\n");
1007 goto clean_up_and_return;
1009 /* CD-Players which don't use O_NONBLOCK, workman
1010 * for example, need bit CDO_CHECK_TYPE cleared! */
1011 if (tracks.data==0) {
1012 if (cdi->options & CDO_CHECK_TYPE) {
1013 /* give people a warning shot, now that CDO_CHECK_TYPE
1014 is the default case! */
1015 cdinfo(CD_OPEN, "bummer. wrong media type.\n");
1016 cdinfo(CD_WARNING, "pid %d must open device O_NONBLOCK!\n",
1017 (unsigned int)current->pid);
1019 goto clean_up_and_return;
1022 cdinfo(CD_OPEN, "wrong media type, but CDO_CHECK_TYPE not set.\n");
1026 cdinfo(CD_OPEN, "all seems well, opening the device.\n");
1028 /* all seems well, we can open the device */
1029 ret = cdo->open(cdi, 0); /* open for data */
1030 cdinfo(CD_OPEN, "opening the device gave me %d.\n", ret);
1031 /* After all this careful checking, we shouldn't have problems
1032 opening the device, but we don't want the device locked if
1033 this somehow fails... */
1035 cdinfo(CD_OPEN, "open device failed.\n");
1036 goto clean_up_and_return;
1038 if (CDROM_CAN(CDC_LOCK) && (cdi->options & CDO_LOCK)) {
1039 cdo->lock_door(cdi, 1);
1040 cdinfo(CD_OPEN, "door locked.\n");
1042 cdinfo(CD_OPEN, "device opened successfully.\n");
1045 /* Something failed. Try to unlock the drive, because some drivers
1046 (notably ide-cd) lock the drive after every command. This produced
1047 a nasty bug where after mount failed, the drive would remain locked!
1048 This ensures that the drive gets unlocked after a mount fails. This
1049 is a goto to avoid bloating the driver with redundant code. */
1050 clean_up_and_return:
1051 cdinfo(CD_WARNING, "open failed.\n");
1052 if (CDROM_CAN(CDC_LOCK) && cdi->options & CDO_LOCK) {
1053 cdo->lock_door(cdi, 0);
1054 cdinfo(CD_OPEN, "door unlocked.\n");
1059 /* This code is similar to that in open_for_data. The routine is called
1060 whenever an audio play operation is requested.
1062 int check_for_audio_disc(struct cdrom_device_info * cdi,
1063 struct cdrom_device_ops * cdo)
1067 cdinfo(CD_OPEN, "entering check_for_audio_disc\n");
1068 if (!(cdi->options & CDO_CHECK_TYPE))
1070 if (cdo->drive_status != NULL) {
1071 ret = cdo->drive_status(cdi, CDSL_CURRENT);
1072 cdinfo(CD_OPEN, "drive_status=%d\n", ret);
1073 if (ret == CDS_TRAY_OPEN) {
1074 cdinfo(CD_OPEN, "the tray is open...\n");
1075 /* can/may i close it? */
1076 if (CDROM_CAN(CDC_CLOSE_TRAY) &&
1077 cdi->options & CDO_AUTO_CLOSE) {
1078 cdinfo(CD_OPEN, "trying to close the tray.\n");
1079 ret=cdo->tray_move(cdi,0);
1081 cdinfo(CD_OPEN, "bummer. tried to close tray but failed.\n");
1082 /* Ignore the error from the low
1083 level driver. We don't care why it
1084 couldn't close the tray. We only care
1085 that there is no disc in the drive,
1086 since that is the _REAL_ problem here.*/
1090 cdinfo(CD_OPEN, "bummer. this driver can't close the tray.\n");
1093 /* Ok, the door should be closed now.. Check again */
1094 ret = cdo->drive_status(cdi, CDSL_CURRENT);
1095 if ((ret == CDS_NO_DISC) || (ret==CDS_TRAY_OPEN)) {
1096 cdinfo(CD_OPEN, "bummer. the tray is still not closed.\n");
1099 if (ret!=CDS_DISC_OK) {
1100 cdinfo(CD_OPEN, "bummer. disc isn't ready.\n");
1103 cdinfo(CD_OPEN, "the tray is now closed.\n");
1106 cdrom_count_tracks(cdi, &tracks);
1108 return(tracks.error);
1110 if (tracks.audio==0)
1111 return -EMEDIUMTYPE;
1116 /* Admittedly, the logic below could be performed in a nicer way. */
1117 int cdrom_release(struct cdrom_device_info *cdi, struct file *fp)
1119 struct cdrom_device_ops *cdo = cdi->ops;
1120 int opened_for_data;
1122 cdinfo(CD_CLOSE, "entering cdrom_release\n");
1124 if (cdi->use_count > 0)
1126 if (cdi->use_count == 0)
1127 cdinfo(CD_CLOSE, "Use count for \"/dev/%s\" now zero\n", cdi->name);
1128 if (cdi->use_count == 0 &&
1129 (cdo->capability & CDC_LOCK) && !keeplocked) {
1130 cdinfo(CD_CLOSE, "Unlocking door!\n");
1131 cdo->lock_door(cdi, 0);
1133 opened_for_data = !(cdi->options & CDO_USE_FFLAGS) ||
1134 !(fp && fp->f_flags & O_NONBLOCK);
1137 * flush cache on last write release
1139 if (CDROM_CAN(CDC_RAM) && !cdi->use_count && cdi->for_data)
1140 cdrom_close_write(cdi);
1143 if (cdi->use_count == 0) { /* last process that closes dev*/
1144 if (opened_for_data &&
1145 cdi->options & CDO_AUTO_EJECT && CDROM_CAN(CDC_OPEN_TRAY))
1146 cdo->tray_move(cdi, 1);
1151 static int cdrom_read_mech_status(struct cdrom_device_info *cdi,
1152 struct cdrom_changer_info *buf)
1154 struct packet_command cgc;
1155 struct cdrom_device_ops *cdo = cdi->ops;
1159 * Sanyo changer isn't spec compliant (doesn't use regular change
1160 * LOAD_UNLOAD command, and it doesn't implement the mech status
1163 if (cdi->sanyo_slot) {
1164 buf->hdr.nslots = 3;
1165 buf->hdr.curslot = cdi->sanyo_slot == 3 ? 0 : cdi->sanyo_slot;
1166 for (length = 0; length < 3; length++) {
1167 buf->slots[length].disc_present = 1;
1168 buf->slots[length].change = 0;
1173 length = sizeof(struct cdrom_mechstat_header) +
1174 cdi->capacity * sizeof(struct cdrom_slot);
1176 init_cdrom_command(&cgc, buf, length, CGC_DATA_READ);
1177 cgc.cmd[0] = GPCMD_MECHANISM_STATUS;
1178 cgc.cmd[8] = (length >> 8) & 0xff;
1179 cgc.cmd[9] = length & 0xff;
1180 return cdo->generic_packet(cdi, &cgc);
1183 static int cdrom_slot_status(struct cdrom_device_info *cdi, int slot)
1185 struct cdrom_changer_info *info;
1188 cdinfo(CD_CHANGER, "entering cdrom_slot_status()\n");
1189 if (cdi->sanyo_slot)
1192 info = kmalloc(sizeof(*info), GFP_KERNEL);
1196 if ((ret = cdrom_read_mech_status(cdi, info)))
1199 if (info->slots[slot].disc_present)
1209 /* Return the number of slots for an ATAPI/SCSI cdrom,
1210 * return 1 if not a changer.
1212 int cdrom_number_of_slots(struct cdrom_device_info *cdi)
1216 struct cdrom_changer_info *info;
1218 cdinfo(CD_CHANGER, "entering cdrom_number_of_slots()\n");
1219 /* cdrom_read_mech_status requires a valid value for capacity: */
1222 info = kmalloc(sizeof(*info), GFP_KERNEL);
1226 if ((status = cdrom_read_mech_status(cdi, info)) == 0)
1227 nslots = info->hdr.nslots;
1234 /* If SLOT < 0, unload the current slot. Otherwise, try to load SLOT. */
1235 static int cdrom_load_unload(struct cdrom_device_info *cdi, int slot)
1237 struct packet_command cgc;
1239 cdinfo(CD_CHANGER, "entering cdrom_load_unload()\n");
1240 if (cdi->sanyo_slot && slot < 0)
1243 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
1244 cgc.cmd[0] = GPCMD_LOAD_UNLOAD;
1245 cgc.cmd[4] = 2 + (slot >= 0);
1247 cgc.timeout = 60 * HZ;
1249 /* The Sanyo 3 CD changer uses byte 7 of the
1250 GPCMD_TEST_UNIT_READY to command to switch CDs instead of
1251 using the GPCMD_LOAD_UNLOAD opcode. */
1252 if (cdi->sanyo_slot && -1 < slot) {
1253 cgc.cmd[0] = GPCMD_TEST_UNIT_READY;
1255 cgc.cmd[4] = cgc.cmd[8] = 0;
1256 cdi->sanyo_slot = slot ? slot : 3;
1259 return cdi->ops->generic_packet(cdi, &cgc);
1262 static int cdrom_select_disc(struct cdrom_device_info *cdi, int slot)
1264 struct cdrom_changer_info *info;
1268 cdinfo(CD_CHANGER, "entering cdrom_select_disc()\n");
1269 if (!CDROM_CAN(CDC_SELECT_DISC))
1270 return -EDRIVE_CANT_DO_THIS;
1272 (void) cdi->ops->media_changed(cdi, slot);
1274 if (slot == CDSL_NONE) {
1275 /* set media changed bits, on both queues */
1276 cdi->mc_flags = 0x3;
1277 return cdrom_load_unload(cdi, -1);
1280 info = kmalloc(sizeof(*info), GFP_KERNEL);
1284 if ((ret = cdrom_read_mech_status(cdi, info))) {
1289 curslot = info->hdr.curslot;
1292 if (cdi->use_count > 1 || keeplocked) {
1293 if (slot == CDSL_CURRENT) {
1300 /* Specifying CDSL_CURRENT will attempt to load the currnet slot,
1301 which is useful if it had been previously unloaded.
1302 Whether it can or not, it returns the current slot.
1303 Similarly, if slot happens to be the current one, we still
1305 if (slot == CDSL_CURRENT)
1308 /* set media changed bits on both queues */
1309 cdi->mc_flags = 0x3;
1310 if ((ret = cdrom_load_unload(cdi, slot)))
1316 /* We want to make media_changed accessible to the user through an
1317 * ioctl. The main problem now is that we must double-buffer the
1318 * low-level implementation, to assure that the VFS and the user both
1319 * see a medium change once.
1323 int media_changed(struct cdrom_device_info *cdi, int queue)
1325 unsigned int mask = (1 << (queue & 1));
1326 int ret = !!(cdi->mc_flags & mask);
1328 if (!CDROM_CAN(CDC_MEDIA_CHANGED))
1330 /* changed since last call? */
1331 if (cdi->ops->media_changed(cdi, CDSL_CURRENT)) {
1332 cdi->mc_flags = 0x3; /* set bit on both queues */
1335 cdi->mc_flags &= ~mask; /* clear bit */
1339 int cdrom_media_changed(struct cdrom_device_info *cdi)
1341 /* This talks to the VFS, which doesn't like errors - just 1 or 0.
1342 * Returning "0" is always safe (media hasn't been changed). Do that
1343 * if the low-level cdrom driver dosn't support media changed. */
1344 if (cdi == NULL || cdi->ops->media_changed == NULL)
1346 if (!CDROM_CAN(CDC_MEDIA_CHANGED))
1348 return media_changed(cdi, 0);
1351 /* badly broken, I know. Is due for a fixup anytime. */
1352 static void cdrom_count_tracks(struct cdrom_device_info *cdi, tracktype* tracks)
1354 struct cdrom_tochdr header;
1355 struct cdrom_tocentry entry;
1362 cdinfo(CD_COUNT_TRACKS, "entering cdrom_count_tracks\n");
1363 if (!CDROM_CAN(CDC_PLAY_AUDIO)) {
1364 tracks->error=CDS_NO_INFO;
1367 /* Grab the TOC header so we can see how many tracks there are */
1368 if ((ret = cdi->ops->audio_ioctl(cdi, CDROMREADTOCHDR, &header))) {
1369 if (ret == -ENOMEDIUM)
1370 tracks->error = CDS_NO_DISC;
1372 tracks->error = CDS_NO_INFO;
1375 /* check what type of tracks are on this disc */
1376 entry.cdte_format = CDROM_MSF;
1377 for (i = header.cdth_trk0; i <= header.cdth_trk1; i++) {
1378 entry.cdte_track = i;
1379 if (cdi->ops->audio_ioctl(cdi, CDROMREADTOCENTRY, &entry)) {
1380 tracks->error=CDS_NO_INFO;
1383 if (entry.cdte_ctrl & CDROM_DATA_TRACK) {
1384 if (entry.cdte_format == 0x10)
1386 else if (entry.cdte_format == 0x20)
1392 cdinfo(CD_COUNT_TRACKS, "track %d: format=%d, ctrl=%d\n",
1393 i, entry.cdte_format, entry.cdte_ctrl);
1395 cdinfo(CD_COUNT_TRACKS, "disc has %d tracks: %d=audio %d=data %d=Cd-I %d=XA\n",
1396 header.cdth_trk1, tracks->audio, tracks->data,
1397 tracks->cdi, tracks->xa);
1400 /* Requests to the low-level drivers will /always/ be done in the
1401 following format convention:
1403 CDROM_LBA: all data-related requests.
1404 CDROM_MSF: all audio-related requests.
1406 However, a low-level implementation is allowed to refuse this
1407 request, and return information in its own favorite format.
1409 It doesn't make sense /at all/ to ask for a play_audio in LBA
1410 format, or ask for multi-session info in MSF format. However, for
1411 backward compatibility these format requests will be satisfied, but
1412 the requests to the low-level drivers will be sanitized in the more
1413 meaningful format indicated above.
1417 void sanitize_format(union cdrom_addr *addr,
1418 u_char * curr, u_char requested)
1420 if (*curr == requested)
1421 return; /* nothing to be done! */
1422 if (requested == CDROM_LBA) {
1423 addr->lba = (int) addr->msf.frame +
1424 75 * (addr->msf.second - 2 + 60 * addr->msf.minute);
1425 } else { /* CDROM_MSF */
1426 int lba = addr->lba;
1427 addr->msf.frame = lba % 75;
1430 addr->msf.second = lba % 60;
1431 addr->msf.minute = lba / 60;
1436 void init_cdrom_command(struct packet_command *cgc, void *buf, int len,
1439 memset(cgc, 0, sizeof(struct packet_command));
1441 memset(buf, 0, len);
1442 cgc->buffer = (char *) buf;
1444 cgc->data_direction = type;
1445 cgc->timeout = 5*HZ;
1450 #define copy_key(dest,src) memcpy((dest), (src), sizeof(dvd_key))
1451 #define copy_chal(dest,src) memcpy((dest), (src), sizeof(dvd_challenge))
1453 static void setup_report_key(struct packet_command *cgc, unsigned agid, unsigned type)
1455 cgc->cmd[0] = GPCMD_REPORT_KEY;
1456 cgc->cmd[10] = type | (agid << 6);
1458 case 0: case 8: case 5: {
1471 cgc->cmd[9] = cgc->buflen;
1472 cgc->data_direction = CGC_DATA_READ;
1475 static void setup_send_key(struct packet_command *cgc, unsigned agid, unsigned type)
1477 cgc->cmd[0] = GPCMD_SEND_KEY;
1478 cgc->cmd[10] = type | (agid << 6);
1493 cgc->cmd[9] = cgc->buflen;
1494 cgc->data_direction = CGC_DATA_WRITE;
1497 static int dvd_do_auth(struct cdrom_device_info *cdi, dvd_authinfo *ai)
1501 struct packet_command cgc;
1502 struct cdrom_device_ops *cdo = cdi->ops;
1503 rpc_state_t rpc_state;
1505 memset(buf, 0, sizeof(buf));
1506 init_cdrom_command(&cgc, buf, 0, CGC_DATA_READ);
1510 case DVD_LU_SEND_AGID:
1511 cdinfo(CD_DVD, "entering DVD_LU_SEND_AGID\n");
1513 setup_report_key(&cgc, ai->lsa.agid, 0);
1515 if ((ret = cdo->generic_packet(cdi, &cgc)))
1518 ai->lsa.agid = buf[7] >> 6;
1519 /* Returning data, let host change state */
1522 case DVD_LU_SEND_KEY1:
1523 cdinfo(CD_DVD, "entering DVD_LU_SEND_KEY1\n");
1524 setup_report_key(&cgc, ai->lsk.agid, 2);
1526 if ((ret = cdo->generic_packet(cdi, &cgc)))
1529 copy_key(ai->lsk.key, &buf[4]);
1530 /* Returning data, let host change state */
1533 case DVD_LU_SEND_CHALLENGE:
1534 cdinfo(CD_DVD, "entering DVD_LU_SEND_CHALLENGE\n");
1535 setup_report_key(&cgc, ai->lsc.agid, 1);
1537 if ((ret = cdo->generic_packet(cdi, &cgc)))
1540 copy_chal(ai->lsc.chal, &buf[4]);
1541 /* Returning data, let host change state */
1545 case DVD_LU_SEND_TITLE_KEY:
1546 cdinfo(CD_DVD, "entering DVD_LU_SEND_TITLE_KEY\n");
1548 setup_report_key(&cgc, ai->lstk.agid, 4);
1549 cgc.cmd[5] = ai->lstk.lba;
1550 cgc.cmd[4] = ai->lstk.lba >> 8;
1551 cgc.cmd[3] = ai->lstk.lba >> 16;
1552 cgc.cmd[2] = ai->lstk.lba >> 24;
1554 if ((ret = cdo->generic_packet(cdi, &cgc)))
1557 ai->lstk.cpm = (buf[4] >> 7) & 1;
1558 ai->lstk.cp_sec = (buf[4] >> 6) & 1;
1559 ai->lstk.cgms = (buf[4] >> 4) & 3;
1560 copy_key(ai->lstk.title_key, &buf[5]);
1561 /* Returning data, let host change state */
1564 case DVD_LU_SEND_ASF:
1565 cdinfo(CD_DVD, "entering DVD_LU_SEND_ASF\n");
1566 setup_report_key(&cgc, ai->lsasf.agid, 5);
1568 if ((ret = cdo->generic_packet(cdi, &cgc)))
1571 ai->lsasf.asf = buf[7] & 1;
1574 /* LU data receive (LU changes state) */
1575 case DVD_HOST_SEND_CHALLENGE:
1576 cdinfo(CD_DVD, "entering DVD_HOST_SEND_CHALLENGE\n");
1577 setup_send_key(&cgc, ai->hsc.agid, 1);
1579 copy_chal(&buf[4], ai->hsc.chal);
1581 if ((ret = cdo->generic_packet(cdi, &cgc)))
1584 ai->type = DVD_LU_SEND_KEY1;
1587 case DVD_HOST_SEND_KEY2:
1588 cdinfo(CD_DVD, "entering DVD_HOST_SEND_KEY2\n");
1589 setup_send_key(&cgc, ai->hsk.agid, 3);
1591 copy_key(&buf[4], ai->hsk.key);
1593 if ((ret = cdo->generic_packet(cdi, &cgc))) {
1594 ai->type = DVD_AUTH_FAILURE;
1597 ai->type = DVD_AUTH_ESTABLISHED;
1601 case DVD_INVALIDATE_AGID:
1603 cdinfo(CD_DVD, "entering DVD_INVALIDATE_AGID\n");
1604 setup_report_key(&cgc, ai->lsa.agid, 0x3f);
1605 if ((ret = cdo->generic_packet(cdi, &cgc)))
1609 /* Get region settings */
1610 case DVD_LU_SEND_RPC_STATE:
1611 cdinfo(CD_DVD, "entering DVD_LU_SEND_RPC_STATE\n");
1612 setup_report_key(&cgc, 0, 8);
1613 memset(&rpc_state, 0, sizeof(rpc_state_t));
1614 cgc.buffer = (char *) &rpc_state;
1616 if ((ret = cdo->generic_packet(cdi, &cgc)))
1619 ai->lrpcs.type = rpc_state.type_code;
1620 ai->lrpcs.vra = rpc_state.vra;
1621 ai->lrpcs.ucca = rpc_state.ucca;
1622 ai->lrpcs.region_mask = rpc_state.region_mask;
1623 ai->lrpcs.rpc_scheme = rpc_state.rpc_scheme;
1626 /* Set region settings */
1627 case DVD_HOST_SEND_RPC_STATE:
1628 cdinfo(CD_DVD, "entering DVD_HOST_SEND_RPC_STATE\n");
1629 setup_send_key(&cgc, 0, 6);
1631 buf[4] = ai->hrpcs.pdrc;
1633 if ((ret = cdo->generic_packet(cdi, &cgc)))
1638 cdinfo(CD_WARNING, "Invalid DVD key ioctl (%d)\n", ai->type);
1645 static int dvd_read_physical(struct cdrom_device_info *cdi, dvd_struct *s)
1647 unsigned char buf[21], *base;
1648 struct dvd_layer *layer;
1649 struct packet_command cgc;
1650 struct cdrom_device_ops *cdo = cdi->ops;
1651 int ret, layer_num = s->physical.layer_num;
1653 if (layer_num >= DVD_LAYERS)
1656 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_READ);
1657 cgc.cmd[0] = GPCMD_READ_DVD_STRUCTURE;
1658 cgc.cmd[6] = layer_num;
1659 cgc.cmd[7] = s->type;
1660 cgc.cmd[9] = cgc.buflen & 0xff;
1663 * refrain from reporting errors on non-existing layers (mainly)
1667 if ((ret = cdo->generic_packet(cdi, &cgc)))
1671 layer = &s->physical.layer[layer_num];
1674 * place the data... really ugly, but at least we won't have to
1675 * worry about endianess in userspace.
1677 memset(layer, 0, sizeof(*layer));
1678 layer->book_version = base[0] & 0xf;
1679 layer->book_type = base[0] >> 4;
1680 layer->min_rate = base[1] & 0xf;
1681 layer->disc_size = base[1] >> 4;
1682 layer->layer_type = base[2] & 0xf;
1683 layer->track_path = (base[2] >> 4) & 1;
1684 layer->nlayers = (base[2] >> 5) & 3;
1685 layer->track_density = base[3] & 0xf;
1686 layer->linear_density = base[3] >> 4;
1687 layer->start_sector = base[5] << 16 | base[6] << 8 | base[7];
1688 layer->end_sector = base[9] << 16 | base[10] << 8 | base[11];
1689 layer->end_sector_l0 = base[13] << 16 | base[14] << 8 | base[15];
1690 layer->bca = base[16] >> 7;
1695 static int dvd_read_copyright(struct cdrom_device_info *cdi, dvd_struct *s)
1699 struct packet_command cgc;
1700 struct cdrom_device_ops *cdo = cdi->ops;
1702 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_READ);
1703 cgc.cmd[0] = GPCMD_READ_DVD_STRUCTURE;
1704 cgc.cmd[6] = s->copyright.layer_num;
1705 cgc.cmd[7] = s->type;
1706 cgc.cmd[8] = cgc.buflen >> 8;
1707 cgc.cmd[9] = cgc.buflen & 0xff;
1709 if ((ret = cdo->generic_packet(cdi, &cgc)))
1712 s->copyright.cpst = buf[4];
1713 s->copyright.rmi = buf[5];
1718 static int dvd_read_disckey(struct cdrom_device_info *cdi, dvd_struct *s)
1722 struct packet_command cgc;
1723 struct cdrom_device_ops *cdo = cdi->ops;
1725 size = sizeof(s->disckey.value) + 4;
1727 if ((buf = (u_char *) kmalloc(size, GFP_KERNEL)) == NULL)
1730 init_cdrom_command(&cgc, buf, size, CGC_DATA_READ);
1731 cgc.cmd[0] = GPCMD_READ_DVD_STRUCTURE;
1732 cgc.cmd[7] = s->type;
1733 cgc.cmd[8] = size >> 8;
1734 cgc.cmd[9] = size & 0xff;
1735 cgc.cmd[10] = s->disckey.agid << 6;
1737 if (!(ret = cdo->generic_packet(cdi, &cgc)))
1738 memcpy(s->disckey.value, &buf[4], sizeof(s->disckey.value));
1744 static int dvd_read_bca(struct cdrom_device_info *cdi, dvd_struct *s)
1747 u_char buf[4 + 188];
1748 struct packet_command cgc;
1749 struct cdrom_device_ops *cdo = cdi->ops;
1751 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_READ);
1752 cgc.cmd[0] = GPCMD_READ_DVD_STRUCTURE;
1753 cgc.cmd[7] = s->type;
1754 cgc.cmd[9] = cgc.buflen = 0xff;
1756 if ((ret = cdo->generic_packet(cdi, &cgc)))
1759 s->bca.len = buf[0] << 8 | buf[1];
1760 if (s->bca.len < 12 || s->bca.len > 188) {
1761 cdinfo(CD_WARNING, "Received invalid BCA length (%d)\n", s->bca.len);
1764 memcpy(s->bca.value, &buf[4], s->bca.len);
1769 static int dvd_read_manufact(struct cdrom_device_info *cdi, dvd_struct *s)
1773 struct packet_command cgc;
1774 struct cdrom_device_ops *cdo = cdi->ops;
1776 size = sizeof(s->manufact.value) + 4;
1778 if ((buf = (u_char *) kmalloc(size, GFP_KERNEL)) == NULL)
1781 init_cdrom_command(&cgc, buf, size, CGC_DATA_READ);
1782 cgc.cmd[0] = GPCMD_READ_DVD_STRUCTURE;
1783 cgc.cmd[7] = s->type;
1784 cgc.cmd[8] = size >> 8;
1785 cgc.cmd[9] = size & 0xff;
1787 if ((ret = cdo->generic_packet(cdi, &cgc))) {
1792 s->manufact.len = buf[0] << 8 | buf[1];
1793 if (s->manufact.len < 0 || s->manufact.len > 2048) {
1794 cdinfo(CD_WARNING, "Received invalid manufacture info length"
1795 " (%d)\n", s->bca.len);
1798 memcpy(s->manufact.value, &buf[4], s->manufact.len);
1805 static int dvd_read_struct(struct cdrom_device_info *cdi, dvd_struct *s)
1808 case DVD_STRUCT_PHYSICAL:
1809 return dvd_read_physical(cdi, s);
1811 case DVD_STRUCT_COPYRIGHT:
1812 return dvd_read_copyright(cdi, s);
1814 case DVD_STRUCT_DISCKEY:
1815 return dvd_read_disckey(cdi, s);
1817 case DVD_STRUCT_BCA:
1818 return dvd_read_bca(cdi, s);
1820 case DVD_STRUCT_MANUFACT:
1821 return dvd_read_manufact(cdi, s);
1824 cdinfo(CD_WARNING, ": Invalid DVD structure read requested (%d)\n",
1830 int cdrom_mode_sense(struct cdrom_device_info *cdi,
1831 struct packet_command *cgc,
1832 int page_code, int page_control)
1834 struct cdrom_device_ops *cdo = cdi->ops;
1836 memset(cgc->cmd, 0, sizeof(cgc->cmd));
1838 cgc->cmd[0] = GPCMD_MODE_SENSE_10;
1839 cgc->cmd[2] = page_code | (page_control << 6);
1840 cgc->cmd[7] = cgc->buflen >> 8;
1841 cgc->cmd[8] = cgc->buflen & 0xff;
1842 cgc->data_direction = CGC_DATA_READ;
1843 return cdo->generic_packet(cdi, cgc);
1846 int cdrom_mode_select(struct cdrom_device_info *cdi,
1847 struct packet_command *cgc)
1849 struct cdrom_device_ops *cdo = cdi->ops;
1851 memset(cgc->cmd, 0, sizeof(cgc->cmd));
1852 memset(cgc->buffer, 0, 2);
1853 cgc->cmd[0] = GPCMD_MODE_SELECT_10;
1854 cgc->cmd[1] = 0x10; /* PF */
1855 cgc->cmd[7] = cgc->buflen >> 8;
1856 cgc->cmd[8] = cgc->buflen & 0xff;
1857 cgc->data_direction = CGC_DATA_WRITE;
1858 return cdo->generic_packet(cdi, cgc);
1861 static int cdrom_read_subchannel(struct cdrom_device_info *cdi,
1862 struct cdrom_subchnl *subchnl, int mcn)
1864 struct cdrom_device_ops *cdo = cdi->ops;
1865 struct packet_command cgc;
1869 init_cdrom_command(&cgc, buffer, 16, CGC_DATA_READ);
1870 cgc.cmd[0] = GPCMD_READ_SUBCHANNEL;
1871 cgc.cmd[1] = 2; /* MSF addressing */
1872 cgc.cmd[2] = 0x40; /* request subQ data */
1873 cgc.cmd[3] = mcn ? 2 : 1;
1876 if ((ret = cdo->generic_packet(cdi, &cgc)))
1879 subchnl->cdsc_audiostatus = cgc.buffer[1];
1880 subchnl->cdsc_format = CDROM_MSF;
1881 subchnl->cdsc_ctrl = cgc.buffer[5] & 0xf;
1882 subchnl->cdsc_trk = cgc.buffer[6];
1883 subchnl->cdsc_ind = cgc.buffer[7];
1885 subchnl->cdsc_reladdr.msf.minute = cgc.buffer[13];
1886 subchnl->cdsc_reladdr.msf.second = cgc.buffer[14];
1887 subchnl->cdsc_reladdr.msf.frame = cgc.buffer[15];
1888 subchnl->cdsc_absaddr.msf.minute = cgc.buffer[9];
1889 subchnl->cdsc_absaddr.msf.second = cgc.buffer[10];
1890 subchnl->cdsc_absaddr.msf.frame = cgc.buffer[11];
1896 * Specific READ_10 interface
1898 static int cdrom_read_cd(struct cdrom_device_info *cdi,
1899 struct packet_command *cgc, int lba,
1900 int blocksize, int nblocks)
1902 struct cdrom_device_ops *cdo = cdi->ops;
1904 memset(&cgc->cmd, 0, sizeof(cgc->cmd));
1905 cgc->cmd[0] = GPCMD_READ_10;
1906 cgc->cmd[2] = (lba >> 24) & 0xff;
1907 cgc->cmd[3] = (lba >> 16) & 0xff;
1908 cgc->cmd[4] = (lba >> 8) & 0xff;
1909 cgc->cmd[5] = lba & 0xff;
1910 cgc->cmd[6] = (nblocks >> 16) & 0xff;
1911 cgc->cmd[7] = (nblocks >> 8) & 0xff;
1912 cgc->cmd[8] = nblocks & 0xff;
1913 cgc->buflen = blocksize * nblocks;
1914 return cdo->generic_packet(cdi, cgc);
1917 /* very generic interface for reading the various types of blocks */
1918 static int cdrom_read_block(struct cdrom_device_info *cdi,
1919 struct packet_command *cgc,
1920 int lba, int nblocks, int format, int blksize)
1922 struct cdrom_device_ops *cdo = cdi->ops;
1924 memset(&cgc->cmd, 0, sizeof(cgc->cmd));
1925 cgc->cmd[0] = GPCMD_READ_CD;
1926 /* expected sector size - cdda,mode1,etc. */
1927 cgc->cmd[1] = format << 2;
1928 /* starting address */
1929 cgc->cmd[2] = (lba >> 24) & 0xff;
1930 cgc->cmd[3] = (lba >> 16) & 0xff;
1931 cgc->cmd[4] = (lba >> 8) & 0xff;
1932 cgc->cmd[5] = lba & 0xff;
1933 /* number of blocks */
1934 cgc->cmd[6] = (nblocks >> 16) & 0xff;
1935 cgc->cmd[7] = (nblocks >> 8) & 0xff;
1936 cgc->cmd[8] = nblocks & 0xff;
1937 cgc->buflen = blksize * nblocks;
1939 /* set the header info returned */
1941 case CD_FRAMESIZE_RAW0 : cgc->cmd[9] = 0x58; break;
1942 case CD_FRAMESIZE_RAW1 : cgc->cmd[9] = 0x78; break;
1943 case CD_FRAMESIZE_RAW : cgc->cmd[9] = 0xf8; break;
1944 default : cgc->cmd[9] = 0x10;
1947 return cdo->generic_packet(cdi, cgc);
1950 static int cdrom_read_cdda_old(struct cdrom_device_info *cdi, __u8 __user *ubuf,
1951 int lba, int nframes)
1953 struct packet_command cgc;
1957 cdi->last_sense = 0;
1959 memset(&cgc, 0, sizeof(cgc));
1962 * start with will ra.nframes size, back down if alloc fails
1966 cgc.buffer = kmalloc(CD_FRAMESIZE_RAW * nr, GFP_KERNEL);
1976 if (!access_ok(VERIFY_WRITE, ubuf, nframes * CD_FRAMESIZE_RAW)) {
1981 cgc.data_direction = CGC_DATA_READ;
1982 while (nframes > 0) {
1986 ret = cdrom_read_block(cdi, &cgc, lba, nr, 1, CD_FRAMESIZE_RAW);
1989 if (__copy_to_user(ubuf, cgc.buffer, CD_FRAMESIZE_RAW * nr)) {
1993 ubuf += CD_FRAMESIZE_RAW * nr;
2002 static int cdrom_read_cdda_bpc(struct cdrom_device_info *cdi, __u8 __user *ubuf,
2003 int lba, int nframes)
2005 request_queue_t *q = cdi->disk->queue;
2014 cdi->last_sense = 0;
2018 if (cdi->cdda_method == CDDA_BPC_SINGLE)
2020 if (nr * CD_FRAMESIZE_RAW > (q->max_sectors << 9))
2021 nr = (q->max_sectors << 9) / CD_FRAMESIZE_RAW;
2023 len = nr * CD_FRAMESIZE_RAW;
2025 rq = blk_rq_map_user(q, READ, ubuf, len);
2029 memset(rq->cmd, 0, sizeof(rq->cmd));
2030 rq->cmd[0] = GPCMD_READ_CD;
2031 rq->cmd[1] = 1 << 2;
2032 rq->cmd[2] = (lba >> 24) & 0xff;
2033 rq->cmd[3] = (lba >> 16) & 0xff;
2034 rq->cmd[4] = (lba >> 8) & 0xff;
2035 rq->cmd[5] = lba & 0xff;
2036 rq->cmd[6] = (nr >> 16) & 0xff;
2037 rq->cmd[7] = (nr >> 8) & 0xff;
2038 rq->cmd[8] = nr & 0xff;
2042 rq->flags |= REQ_BLOCK_PC;
2043 rq->timeout = 60 * HZ;
2047 blk_queue_bounce(q, &rq->bio);
2049 if (blk_execute_rq(q, cdi->disk, rq)) {
2050 struct request_sense *s = rq->sense;
2052 cdi->last_sense = s->sense_key;
2055 if (blk_rq_unmap_user(rq, bio, len))
2069 static int cdrom_read_cdda(struct cdrom_device_info *cdi, __u8 __user *ubuf,
2070 int lba, int nframes)
2074 if (cdi->cdda_method == CDDA_OLD)
2075 return cdrom_read_cdda_old(cdi, ubuf, lba, nframes);
2079 * for anything else than success and io error, we need to retry
2081 ret = cdrom_read_cdda_bpc(cdi, ubuf, lba, nframes);
2082 if (!ret || ret != -EIO)
2086 * I've seen drives get sense 4/8/3 udma crc errors on multi
2087 * frame dma, so drop to single frame dma if we need to
2089 if (cdi->cdda_method == CDDA_BPC_FULL && nframes > 1) {
2090 printk("cdrom: dropping to single frame dma\n");
2091 cdi->cdda_method = CDDA_BPC_SINGLE;
2096 * so we have an io error of some sort with multi frame dma. if the
2097 * condition wasn't a hardware error
2098 * problems, not for any error
2100 if (cdi->last_sense != 0x04 && cdi->last_sense != 0x0b)
2103 printk("cdrom: dropping to old style cdda (sense=%x)\n", cdi->last_sense);
2104 cdi->cdda_method = CDDA_OLD;
2105 return cdrom_read_cdda_old(cdi, ubuf, lba, nframes);
2108 /* Just about every imaginable ioctl is supported in the Uniform layer
2109 * these days. ATAPI / SCSI specific code now mainly resides in
2112 int cdrom_ioctl(struct file * file, struct cdrom_device_info *cdi,
2113 struct inode *ip, unsigned int cmd, unsigned long arg)
2115 struct cdrom_device_ops *cdo = cdi->ops;
2118 /* Try the generic SCSI command ioctl's first.. */
2119 ret = scsi_cmd_ioctl(file, ip->i_bdev->bd_disk, cmd, (void __user *)arg);
2123 /* the first few commands do not deal with audio drive_info, but
2124 only with routines in cdrom device operations. */
2126 case CDROMMULTISESSION: {
2127 struct cdrom_multisession ms_info;
2128 u_char requested_format;
2129 cdinfo(CD_DO_IOCTL, "entering CDROMMULTISESSION\n");
2130 if (!(cdo->capability & CDC_MULTI_SESSION))
2132 IOCTL_IN(arg, struct cdrom_multisession, ms_info);
2133 requested_format = ms_info.addr_format;
2134 if (!((requested_format == CDROM_MSF) ||
2135 (requested_format == CDROM_LBA)))
2137 ms_info.addr_format = CDROM_LBA;
2138 if ((ret=cdo->get_last_session(cdi, &ms_info)))
2140 sanitize_format(&ms_info.addr, &ms_info.addr_format,
2142 IOCTL_OUT(arg, struct cdrom_multisession, ms_info);
2143 cdinfo(CD_DO_IOCTL, "CDROMMULTISESSION successful\n");
2148 cdinfo(CD_DO_IOCTL, "entering CDROMEJECT\n");
2149 if (!CDROM_CAN(CDC_OPEN_TRAY))
2151 if (cdi->use_count != 1 || keeplocked)
2153 if (CDROM_CAN(CDC_LOCK))
2154 if ((ret=cdo->lock_door(cdi, 0)))
2157 return cdo->tray_move(cdi, 1);
2160 case CDROMCLOSETRAY: {
2161 cdinfo(CD_DO_IOCTL, "entering CDROMCLOSETRAY\n");
2162 if (!CDROM_CAN(CDC_CLOSE_TRAY))
2164 return cdo->tray_move(cdi, 0);
2167 case CDROMEJECT_SW: {
2168 cdinfo(CD_DO_IOCTL, "entering CDROMEJECT_SW\n");
2169 if (!CDROM_CAN(CDC_OPEN_TRAY))
2173 cdi->options &= ~(CDO_AUTO_CLOSE | CDO_AUTO_EJECT);
2175 cdi->options |= CDO_AUTO_CLOSE | CDO_AUTO_EJECT;
2179 case CDROM_MEDIA_CHANGED: {
2180 struct cdrom_changer_info *info;
2183 cdinfo(CD_DO_IOCTL, "entering CDROM_MEDIA_CHANGED\n");
2184 if (!CDROM_CAN(CDC_MEDIA_CHANGED))
2187 /* cannot select disc or select current disc */
2188 if (!CDROM_CAN(CDC_SELECT_DISC) || arg == CDSL_CURRENT)
2189 return media_changed(cdi, 1);
2191 if ((unsigned int)arg >= cdi->capacity)
2194 info = kmalloc(sizeof(*info), GFP_KERNEL);
2198 if ((ret = cdrom_read_mech_status(cdi, info))) {
2203 changed = info->slots[arg].change;
2208 case CDROM_SET_OPTIONS: {
2209 cdinfo(CD_DO_IOCTL, "entering CDROM_SET_OPTIONS\n");
2210 /* options need to be in sync with capability. too late for
2211 that, so we have to check each one separately... */
2213 case CDO_USE_FFLAGS:
2214 case CDO_CHECK_TYPE:
2217 if (!CDROM_CAN(CDC_LOCK))
2221 return cdi->options;
2222 /* default is basically CDO_[AUTO_CLOSE|AUTO_EJECT] */
2224 if (!CDROM_CAN(arg))
2227 cdi->options |= (int) arg;
2228 return cdi->options;
2231 case CDROM_CLEAR_OPTIONS: {
2232 cdinfo(CD_DO_IOCTL, "entering CDROM_CLEAR_OPTIONS\n");
2233 cdi->options &= ~(int) arg;
2234 return cdi->options;
2237 case CDROM_SELECT_SPEED: {
2238 cdinfo(CD_DO_IOCTL, "entering CDROM_SELECT_SPEED\n");
2239 if (!CDROM_CAN(CDC_SELECT_SPEED))
2241 return cdo->select_speed(cdi, arg);
2244 case CDROM_SELECT_DISC: {
2245 cdinfo(CD_DO_IOCTL, "entering CDROM_SELECT_DISC\n");
2246 if (!CDROM_CAN(CDC_SELECT_DISC))
2249 if ((arg != CDSL_CURRENT) && (arg != CDSL_NONE))
2250 if ((int)arg >= cdi->capacity)
2253 /* cdo->select_disc is a hook to allow a driver-specific
2254 * way of seleting disc. However, since there is no
2255 * equiv hook for cdrom_slot_status this may not
2256 * actually be useful...
2258 if (cdo->select_disc != NULL)
2259 return cdo->select_disc(cdi, arg);
2261 /* no driver specific select_disc(), call our own */
2262 cdinfo(CD_CHANGER, "Using generic cdrom_select_disc()\n");
2263 return cdrom_select_disc(cdi, arg);
2267 if (!capable(CAP_SYS_ADMIN))
2269 cdinfo(CD_DO_IOCTL, "entering CDROM_RESET\n");
2270 if (!CDROM_CAN(CDC_RESET))
2272 invalidate_bdev(ip->i_bdev, 0);
2273 return cdo->reset(cdi);
2276 case CDROM_LOCKDOOR: {
2277 cdinfo(CD_DO_IOCTL, "%socking door.\n", arg ? "L" : "Unl");
2278 if (!CDROM_CAN(CDC_LOCK))
2279 return -EDRIVE_CANT_DO_THIS;
2280 keeplocked = arg ? 1 : 0;
2281 /* don't unlock the door on multiple opens,but allow root
2283 if ((cdi->use_count != 1) && !arg && !capable(CAP_SYS_ADMIN))
2285 return cdo->lock_door(cdi, arg);
2289 if (!capable(CAP_SYS_ADMIN))
2291 cdinfo(CD_DO_IOCTL, "%sabling debug.\n", arg ? "En" : "Dis");
2292 debug = arg ? 1 : 0;
2296 case CDROM_GET_CAPABILITY: {
2297 cdinfo(CD_DO_IOCTL, "entering CDROM_GET_CAPABILITY\n");
2298 return (cdo->capability & ~cdi->mask);
2301 /* The following function is implemented, although very few audio
2302 * discs give Universal Product Code information, which should just be
2303 * the Medium Catalog Number on the box. Note, that the way the code
2304 * is written on the CD is /not/ uniform across all discs!
2306 case CDROM_GET_MCN: {
2307 struct cdrom_mcn mcn;
2308 cdinfo(CD_DO_IOCTL, "entering CDROM_GET_MCN\n");
2309 if (!(cdo->capability & CDC_MCN))
2311 if ((ret=cdo->get_mcn(cdi, &mcn)))
2313 IOCTL_OUT(arg, struct cdrom_mcn, mcn);
2314 cdinfo(CD_DO_IOCTL, "CDROM_GET_MCN successful\n");
2318 case CDROM_DRIVE_STATUS: {
2319 cdinfo(CD_DO_IOCTL, "entering CDROM_DRIVE_STATUS\n");
2320 if (!(cdo->capability & CDC_DRIVE_STATUS))
2322 if (!CDROM_CAN(CDC_SELECT_DISC))
2323 return cdo->drive_status(cdi, CDSL_CURRENT);
2324 if ((arg == CDSL_CURRENT) || (arg == CDSL_NONE))
2325 return cdo->drive_status(cdi, CDSL_CURRENT);
2326 if (((int)arg >= cdi->capacity))
2328 return cdrom_slot_status(cdi, arg);
2331 /* Ok, this is where problems start. The current interface for the
2332 CDROM_DISC_STATUS ioctl is flawed. It makes the false assumption
2333 that CDs are all CDS_DATA_1 or all CDS_AUDIO, etc. Unfortunatly,
2334 while this is often the case, it is also very common for CDs to
2335 have some tracks with data, and some tracks with audio. Just
2336 because I feel like it, I declare the following to be the best
2337 way to cope. If the CD has ANY data tracks on it, it will be
2338 returned as a data CD. If it has any XA tracks, I will return
2339 it as that. Now I could simplify this interface by combining these
2340 returns with the above, but this more clearly demonstrates
2341 the problem with the current interface. Too bad this wasn't
2342 designed to use bitmasks... -Erik
2344 Well, now we have the option CDS_MIXED: a mixed-type CD.
2345 User level programmers might feel the ioctl is not very useful.
2348 case CDROM_DISC_STATUS: {
2350 cdinfo(CD_DO_IOCTL, "entering CDROM_DISC_STATUS\n");
2351 cdrom_count_tracks(cdi, &tracks);
2353 return(tracks.error);
2355 /* Policy mode on */
2356 if (tracks.audio > 0) {
2357 if (tracks.data==0 && tracks.cdi==0 && tracks.xa==0)
2362 if (tracks.cdi > 0) return CDS_XA_2_2;
2363 if (tracks.xa > 0) return CDS_XA_2_1;
2364 if (tracks.data > 0) return CDS_DATA_1;
2365 /* Policy mode off */
2367 cdinfo(CD_WARNING,"This disc doesn't have any tracks I recognize!\n");
2371 case CDROM_CHANGER_NSLOTS: {
2372 cdinfo(CD_DO_IOCTL, "entering CDROM_CHANGER_NSLOTS\n");
2373 return cdi->capacity;
2377 /* use the ioctls that are implemented through the generic_packet()
2378 interface. this may look at bit funny, but if -ENOTTY is
2379 returned that particular ioctl is not implemented and we
2380 let it go through the device specific ones. */
2381 if (CDROM_CAN(CDC_GENERIC_PACKET)) {
2382 ret = mmc_ioctl(cdi, cmd, arg);
2383 if (ret != -ENOTTY) {
2388 /* note: most of the cdinfo() calls are commented out here,
2389 because they fill up the sys log when CD players poll
2392 case CDROMSUBCHNL: {
2393 struct cdrom_subchnl q;
2394 u_char requested, back;
2395 if (!CDROM_CAN(CDC_PLAY_AUDIO))
2397 /* cdinfo(CD_DO_IOCTL,"entering CDROMSUBCHNL\n");*/
2398 IOCTL_IN(arg, struct cdrom_subchnl, q);
2399 requested = q.cdsc_format;
2400 if (!((requested == CDROM_MSF) ||
2401 (requested == CDROM_LBA)))
2403 q.cdsc_format = CDROM_MSF;
2404 if ((ret=cdo->audio_ioctl(cdi, cmd, &q)))
2406 back = q.cdsc_format; /* local copy */
2407 sanitize_format(&q.cdsc_absaddr, &back, requested);
2408 sanitize_format(&q.cdsc_reladdr, &q.cdsc_format, requested);
2409 IOCTL_OUT(arg, struct cdrom_subchnl, q);
2410 /* cdinfo(CD_DO_IOCTL, "CDROMSUBCHNL successful\n"); */
2413 case CDROMREADTOCHDR: {
2414 struct cdrom_tochdr header;
2415 if (!CDROM_CAN(CDC_PLAY_AUDIO))
2417 /* cdinfo(CD_DO_IOCTL, "entering CDROMREADTOCHDR\n"); */
2418 IOCTL_IN(arg, struct cdrom_tochdr, header);
2419 if ((ret=cdo->audio_ioctl(cdi, cmd, &header)))
2421 IOCTL_OUT(arg, struct cdrom_tochdr, header);
2422 /* cdinfo(CD_DO_IOCTL, "CDROMREADTOCHDR successful\n"); */
2425 case CDROMREADTOCENTRY: {
2426 struct cdrom_tocentry entry;
2427 u_char requested_format;
2428 if (!CDROM_CAN(CDC_PLAY_AUDIO))
2430 /* cdinfo(CD_DO_IOCTL, "entering CDROMREADTOCENTRY\n"); */
2431 IOCTL_IN(arg, struct cdrom_tocentry, entry);
2432 requested_format = entry.cdte_format;
2433 if (!((requested_format == CDROM_MSF) ||
2434 (requested_format == CDROM_LBA)))
2436 /* make interface to low-level uniform */
2437 entry.cdte_format = CDROM_MSF;
2438 if ((ret=cdo->audio_ioctl(cdi, cmd, &entry)))
2440 sanitize_format(&entry.cdte_addr,
2441 &entry.cdte_format, requested_format);
2442 IOCTL_OUT(arg, struct cdrom_tocentry, entry);
2443 /* cdinfo(CD_DO_IOCTL, "CDROMREADTOCENTRY successful\n"); */
2446 case CDROMPLAYMSF: {
2447 struct cdrom_msf msf;
2448 if (!CDROM_CAN(CDC_PLAY_AUDIO))
2450 cdinfo(CD_DO_IOCTL, "entering CDROMPLAYMSF\n");
2451 IOCTL_IN(arg, struct cdrom_msf, msf);
2452 return cdo->audio_ioctl(cdi, cmd, &msf);
2454 case CDROMPLAYTRKIND: {
2456 if (!CDROM_CAN(CDC_PLAY_AUDIO))
2458 cdinfo(CD_DO_IOCTL, "entering CDROMPLAYTRKIND\n");
2459 IOCTL_IN(arg, struct cdrom_ti, ti);
2461 return cdo->audio_ioctl(cdi, cmd, &ti);
2463 case CDROMVOLCTRL: {
2464 struct cdrom_volctrl volume;
2465 if (!CDROM_CAN(CDC_PLAY_AUDIO))
2467 cdinfo(CD_DO_IOCTL, "entering CDROMVOLCTRL\n");
2468 IOCTL_IN(arg, struct cdrom_volctrl, volume);
2469 return cdo->audio_ioctl(cdi, cmd, &volume);
2471 case CDROMVOLREAD: {
2472 struct cdrom_volctrl volume;
2473 if (!CDROM_CAN(CDC_PLAY_AUDIO))
2475 cdinfo(CD_DO_IOCTL, "entering CDROMVOLREAD\n");
2476 if ((ret=cdo->audio_ioctl(cdi, cmd, &volume)))
2478 IOCTL_OUT(arg, struct cdrom_volctrl, volume);
2485 if (!CDROM_CAN(CDC_PLAY_AUDIO))
2487 cdinfo(CD_DO_IOCTL, "doing audio ioctl (start/stop/pause/resume)\n");
2489 return cdo->audio_ioctl(cdi, cmd, NULL);
2493 /* do the device specific ioctls */
2494 if (CDROM_CAN(CDC_IOCTLS))
2495 return cdo->dev_ioctl(cdi, cmd, arg);
2501 int msf_to_lba(char m, char s, char f)
2503 return (((m * CD_SECS) + s) * CD_FRAMES + f) - CD_MSF_OFFSET;
2507 * Required when we need to use READ_10 to issue other than 2048 block
2510 static int cdrom_switch_blocksize(struct cdrom_device_info *cdi, int size)
2512 struct cdrom_device_ops *cdo = cdi->ops;
2513 struct packet_command cgc;
2514 struct modesel_head mh;
2516 memset(&mh, 0, sizeof(mh));
2517 mh.block_desc_length = 0x08;
2518 mh.block_length_med = (size >> 8) & 0xff;
2519 mh.block_length_lo = size & 0xff;
2521 memset(&cgc, 0, sizeof(cgc));
2523 cgc.cmd[1] = 1 << 4;
2525 cgc.buflen = sizeof(mh);
2526 cgc.buffer = (char *) &mh;
2527 cgc.data_direction = CGC_DATA_WRITE;
2528 mh.block_desc_length = 0x08;
2529 mh.block_length_med = (size >> 8) & 0xff;
2530 mh.block_length_lo = size & 0xff;
2532 return cdo->generic_packet(cdi, &cgc);
2535 static int mmc_ioctl(struct cdrom_device_info *cdi, unsigned int cmd,
2538 struct cdrom_device_ops *cdo = cdi->ops;
2539 struct packet_command cgc;
2540 struct request_sense sense;
2541 unsigned char buffer[32];
2544 memset(&cgc, 0, sizeof(cgc));
2546 /* build a unified command and queue it through
2547 cdo->generic_packet() */
2550 case CDROMREADMODE1:
2551 case CDROMREADMODE2: {
2552 struct cdrom_msf msf;
2553 int blocksize = 0, format = 0, lba;
2557 blocksize = CD_FRAMESIZE_RAW;
2559 case CDROMREADMODE1:
2560 blocksize = CD_FRAMESIZE;
2563 case CDROMREADMODE2:
2564 blocksize = CD_FRAMESIZE_RAW0;
2567 IOCTL_IN(arg, struct cdrom_msf, msf);
2568 lba = msf_to_lba(msf.cdmsf_min0,msf.cdmsf_sec0,msf.cdmsf_frame0);
2569 /* FIXME: we need upper bound checking, too!! */
2572 cgc.buffer = (char *) kmalloc(blocksize, GFP_KERNEL);
2573 if (cgc.buffer == NULL)
2575 memset(&sense, 0, sizeof(sense));
2577 cgc.data_direction = CGC_DATA_READ;
2578 ret = cdrom_read_block(cdi, &cgc, lba, 1, format, blocksize);
2579 if (ret && sense.sense_key==0x05 && sense.asc==0x20 && sense.ascq==0x00) {
2581 * SCSI-II devices are not required to support
2582 * READ_CD, so let's try switching block size
2584 /* FIXME: switch back again... */
2585 if ((ret = cdrom_switch_blocksize(cdi, blocksize))) {
2590 ret = cdrom_read_cd(cdi, &cgc, lba, blocksize, 1);
2591 ret |= cdrom_switch_blocksize(cdi, blocksize);
2593 if (!ret && copy_to_user((char __user *)arg, cgc.buffer, blocksize))
2598 case CDROMREADAUDIO: {
2599 struct cdrom_read_audio ra;
2602 IOCTL_IN(arg, struct cdrom_read_audio, ra);
2604 if (ra.addr_format == CDROM_MSF)
2605 lba = msf_to_lba(ra.addr.msf.minute,
2608 else if (ra.addr_format == CDROM_LBA)
2613 /* FIXME: we need upper bound checking, too!! */
2614 if (lba < 0 || ra.nframes <= 0 || ra.nframes > CD_FRAMES)
2617 return cdrom_read_cdda(cdi, ra.buf, lba, ra.nframes);
2619 case CDROMSUBCHNL: {
2620 struct cdrom_subchnl q;
2621 u_char requested, back;
2622 IOCTL_IN(arg, struct cdrom_subchnl, q);
2623 requested = q.cdsc_format;
2624 if (!((requested == CDROM_MSF) ||
2625 (requested == CDROM_LBA)))
2627 q.cdsc_format = CDROM_MSF;
2628 if ((ret = cdrom_read_subchannel(cdi, &q, 0)))
2630 back = q.cdsc_format; /* local copy */
2631 sanitize_format(&q.cdsc_absaddr, &back, requested);
2632 sanitize_format(&q.cdsc_reladdr, &q.cdsc_format, requested);
2633 IOCTL_OUT(arg, struct cdrom_subchnl, q);
2634 /* cdinfo(CD_DO_IOCTL, "CDROMSUBCHNL successful\n"); */
2637 case CDROMPLAYMSF: {
2638 struct cdrom_msf msf;
2639 cdinfo(CD_DO_IOCTL, "entering CDROMPLAYMSF\n");
2640 IOCTL_IN(arg, struct cdrom_msf, msf);
2641 cgc.cmd[0] = GPCMD_PLAY_AUDIO_MSF;
2642 cgc.cmd[3] = msf.cdmsf_min0;
2643 cgc.cmd[4] = msf.cdmsf_sec0;
2644 cgc.cmd[5] = msf.cdmsf_frame0;
2645 cgc.cmd[6] = msf.cdmsf_min1;
2646 cgc.cmd[7] = msf.cdmsf_sec1;
2647 cgc.cmd[8] = msf.cdmsf_frame1;
2648 cgc.data_direction = CGC_DATA_NONE;
2649 return cdo->generic_packet(cdi, &cgc);
2651 case CDROMPLAYBLK: {
2652 struct cdrom_blk blk;
2653 cdinfo(CD_DO_IOCTL, "entering CDROMPLAYBLK\n");
2654 IOCTL_IN(arg, struct cdrom_blk, blk);
2655 cgc.cmd[0] = GPCMD_PLAY_AUDIO_10;
2656 cgc.cmd[2] = (blk.from >> 24) & 0xff;
2657 cgc.cmd[3] = (blk.from >> 16) & 0xff;
2658 cgc.cmd[4] = (blk.from >> 8) & 0xff;
2659 cgc.cmd[5] = blk.from & 0xff;
2660 cgc.cmd[7] = (blk.len >> 8) & 0xff;
2661 cgc.cmd[8] = blk.len & 0xff;
2662 cgc.data_direction = CGC_DATA_NONE;
2663 return cdo->generic_packet(cdi, &cgc);
2666 case CDROMVOLREAD: {
2667 struct cdrom_volctrl volctrl;
2668 char mask[sizeof(buffer)];
2669 unsigned short offset;
2671 cdinfo(CD_DO_IOCTL, "entering CDROMVOLUME\n");
2673 IOCTL_IN(arg, struct cdrom_volctrl, volctrl);
2675 cgc.buffer = buffer;
2677 if ((ret = cdrom_mode_sense(cdi, &cgc, GPMODE_AUDIO_CTL_PAGE, 0)))
2680 /* originally the code depended on buffer[1] to determine
2681 how much data is available for transfer. buffer[1] is
2682 unfortunately ambigious and the only reliable way seem
2683 to be to simply skip over the block descriptor... */
2684 offset = 8 + be16_to_cpu(*(unsigned short *)(buffer+6));
2686 if (offset + 16 > sizeof(buffer))
2689 if (offset + 16 > cgc.buflen) {
2690 cgc.buflen = offset+16;
2691 ret = cdrom_mode_sense(cdi, &cgc,
2692 GPMODE_AUDIO_CTL_PAGE, 0);
2698 if ((buffer[offset] & 0x3f) != GPMODE_AUDIO_CTL_PAGE ||
2699 buffer[offset+1] < 14)
2702 /* now we have the current volume settings. if it was only
2703 a CDROMVOLREAD, return these values */
2704 if (cmd == CDROMVOLREAD) {
2705 volctrl.channel0 = buffer[offset+9];
2706 volctrl.channel1 = buffer[offset+11];
2707 volctrl.channel2 = buffer[offset+13];
2708 volctrl.channel3 = buffer[offset+15];
2709 IOCTL_OUT(arg, struct cdrom_volctrl, volctrl);
2713 /* get the volume mask */
2715 if ((ret = cdrom_mode_sense(cdi, &cgc,
2716 GPMODE_AUDIO_CTL_PAGE, 1)))
2719 buffer[offset+9] = volctrl.channel0 & mask[offset+9];
2720 buffer[offset+11] = volctrl.channel1 & mask[offset+11];
2721 buffer[offset+13] = volctrl.channel2 & mask[offset+13];
2722 buffer[offset+15] = volctrl.channel3 & mask[offset+15];
2725 cgc.buffer = buffer + offset - 8;
2726 memset(cgc.buffer, 0, 8);
2727 return cdrom_mode_select(cdi, &cgc);
2732 cdinfo(CD_DO_IOCTL, "entering CDROMSTART/CDROMSTOP\n");
2733 cgc.cmd[0] = GPCMD_START_STOP_UNIT;
2735 cgc.cmd[4] = (cmd == CDROMSTART) ? 1 : 0;
2736 cgc.data_direction = CGC_DATA_NONE;
2737 return cdo->generic_packet(cdi, &cgc);
2742 cdinfo(CD_DO_IOCTL, "entering CDROMPAUSE/CDROMRESUME\n");
2743 cgc.cmd[0] = GPCMD_PAUSE_RESUME;
2744 cgc.cmd[8] = (cmd == CDROMRESUME) ? 1 : 0;
2745 cgc.data_direction = CGC_DATA_NONE;
2746 return cdo->generic_packet(cdi, &cgc);
2749 case DVD_READ_STRUCT: {
2751 int size = sizeof(dvd_struct);
2752 if (!CDROM_CAN(CDC_DVD))
2754 if ((s = (dvd_struct *) kmalloc(size, GFP_KERNEL)) == NULL)
2756 cdinfo(CD_DO_IOCTL, "entering DVD_READ_STRUCT\n");
2757 if (copy_from_user(s, (dvd_struct __user *)arg, size)) {
2761 if ((ret = dvd_read_struct(cdi, s))) {
2765 if (copy_to_user((dvd_struct __user *)arg, s, size))
2773 if (!CDROM_CAN(CDC_DVD))
2775 cdinfo(CD_DO_IOCTL, "entering DVD_AUTH\n");
2776 IOCTL_IN(arg, dvd_authinfo, ai);
2777 if ((ret = dvd_do_auth (cdi, &ai)))
2779 IOCTL_OUT(arg, dvd_authinfo, ai);
2783 case CDROM_NEXT_WRITABLE: {
2785 cdinfo(CD_DO_IOCTL, "entering CDROM_NEXT_WRITABLE\n");
2786 if ((ret = cdrom_get_next_writable(cdi, &next)))
2788 IOCTL_OUT(arg, long, next);
2791 case CDROM_LAST_WRITTEN: {
2793 cdinfo(CD_DO_IOCTL, "entering CDROM_LAST_WRITTEN\n");
2794 if ((ret = cdrom_get_last_written(cdi, &last)))
2796 IOCTL_OUT(arg, long, last);
2804 static int cdrom_get_track_info(struct cdrom_device_info *cdi, __u16 track, __u8 type,
2805 track_information *ti)
2807 struct cdrom_device_ops *cdo = cdi->ops;
2808 struct packet_command cgc;
2811 init_cdrom_command(&cgc, ti, 8, CGC_DATA_READ);
2812 cgc.cmd[0] = GPCMD_READ_TRACK_RZONE_INFO;
2813 cgc.cmd[1] = type & 3;
2814 cgc.cmd[4] = (track & 0xff00) >> 8;
2815 cgc.cmd[5] = track & 0xff;
2819 if ((ret = cdo->generic_packet(cdi, &cgc)))
2822 buflen = be16_to_cpu(ti->track_information_length) +
2823 sizeof(ti->track_information_length);
2825 if (buflen > sizeof(track_information))
2826 buflen = sizeof(track_information);
2828 cgc.cmd[8] = cgc.buflen = buflen;
2829 if ((ret = cdo->generic_packet(cdi, &cgc)))
2832 /* return actual fill size */
2836 /* requires CD R/RW */
2837 static int cdrom_get_disc_info(struct cdrom_device_info *cdi, disc_information *di)
2839 struct cdrom_device_ops *cdo = cdi->ops;
2840 struct packet_command cgc;
2843 /* set up command and get the disc info */
2844 init_cdrom_command(&cgc, di, sizeof(*di), CGC_DATA_READ);
2845 cgc.cmd[0] = GPCMD_READ_DISC_INFO;
2846 cgc.cmd[8] = cgc.buflen = 2;
2849 if ((ret = cdo->generic_packet(cdi, &cgc)))
2852 /* not all drives have the same disc_info length, so requeue
2853 * packet with the length the drive tells us it can supply
2855 buflen = be16_to_cpu(di->disc_information_length) +
2856 sizeof(di->disc_information_length);
2858 if (buflen > sizeof(disc_information))
2859 buflen = sizeof(disc_information);
2861 cgc.cmd[8] = cgc.buflen = buflen;
2862 if ((ret = cdo->generic_packet(cdi, &cgc)))
2865 /* return actual fill size */
2869 /* return the last written block on the CD-R media. this is for the udf
2871 int cdrom_get_last_written(struct cdrom_device_info *cdi, long *last_written)
2873 struct cdrom_tocentry toc;
2874 disc_information di;
2875 track_information ti;
2877 int ret = -1, ti_size;
2879 if (!CDROM_CAN(CDC_GENERIC_PACKET))
2882 ret = cdrom_get_disc_info(cdi, &di);
2883 if (ret < (int)(offsetof(typeof(di), last_track_lsb)
2884 + sizeof(di.last_track_lsb)))
2887 /* if unit didn't return msb, it's zeroed by cdrom_get_disc_info */
2888 last_track = (di.last_track_msb << 8) | di.last_track_lsb;
2889 ti_size = cdrom_get_track_info(cdi, last_track, 1, &ti);
2890 if (ti_size < (int)offsetof(typeof(ti), track_start))
2893 /* if this track is blank, try the previous. */
2898 ti_size = cdrom_get_track_info(cdi, last_track, 1, &ti);
2901 if (ti_size < (int)(offsetof(typeof(ti), track_size)
2902 + sizeof(ti.track_size)))
2905 /* if last recorded field is valid, return it. */
2906 if (ti.lra_v && ti_size >= (int)(offsetof(typeof(ti), last_rec_address)
2907 + sizeof(ti.last_rec_address))) {
2908 *last_written = be32_to_cpu(ti.last_rec_address);
2910 /* make it up instead */
2911 *last_written = be32_to_cpu(ti.track_start) +
2912 be32_to_cpu(ti.track_size);
2914 *last_written -= (be32_to_cpu(ti.free_blocks) + 7);
2918 /* this is where we end up if the drive either can't do a
2919 GPCMD_READ_DISC_INFO or GPCMD_READ_TRACK_RZONE_INFO or if
2920 it doesn't give enough information or fails. then we return
2921 the toc contents. */
2923 toc.cdte_format = CDROM_MSF;
2924 toc.cdte_track = CDROM_LEADOUT;
2925 if ((ret = cdi->ops->audio_ioctl(cdi, CDROMREADTOCENTRY, &toc)))
2927 sanitize_format(&toc.cdte_addr, &toc.cdte_format, CDROM_LBA);
2928 *last_written = toc.cdte_addr.lba;
2932 /* return the next writable block. also for udf file system. */
2933 static int cdrom_get_next_writable(struct cdrom_device_info *cdi, long *next_writable)
2935 disc_information di;
2936 track_information ti;
2940 if (!CDROM_CAN(CDC_GENERIC_PACKET))
2941 goto use_last_written;
2943 ret = cdrom_get_disc_info(cdi, &di);
2944 if (ret < 0 || ret < offsetof(typeof(di), last_track_lsb)
2945 + sizeof(di.last_track_lsb))
2946 goto use_last_written;
2948 /* if unit didn't return msb, it's zeroed by cdrom_get_disc_info */
2949 last_track = (di.last_track_msb << 8) | di.last_track_lsb;
2950 ti_size = cdrom_get_track_info(cdi, last_track, 1, &ti);
2951 if (ti_size < 0 || ti_size < offsetof(typeof(ti), track_start))
2952 goto use_last_written;
2954 /* if this track is blank, try the previous. */
2956 if (last_track == 1)
2957 goto use_last_written;
2959 ti_size = cdrom_get_track_info(cdi, last_track, 1, &ti);
2961 goto use_last_written;
2964 /* if next recordable address field is valid, use it. */
2965 if (ti.nwa_v && ti_size >= offsetof(typeof(ti), next_writable)
2966 + sizeof(ti.next_writable)) {
2967 *next_writable = be32_to_cpu(ti.next_writable);
2972 if ((ret = cdrom_get_last_written(cdi, next_writable))) {
2976 *next_writable += 7;
2981 EXPORT_SYMBOL(cdrom_get_last_written);
2982 EXPORT_SYMBOL(register_cdrom);
2983 EXPORT_SYMBOL(unregister_cdrom);
2984 EXPORT_SYMBOL(cdrom_open);
2985 EXPORT_SYMBOL(cdrom_release);
2986 EXPORT_SYMBOL(cdrom_ioctl);
2987 EXPORT_SYMBOL(cdrom_media_changed);
2988 EXPORT_SYMBOL(cdrom_number_of_slots);
2989 EXPORT_SYMBOL(cdrom_mode_select);
2990 EXPORT_SYMBOL(cdrom_mode_sense);
2991 EXPORT_SYMBOL(init_cdrom_command);
2992 EXPORT_SYMBOL(cdrom_get_media_event);
2993 EXPORT_SYMBOL(cdrom_is_mrw);
2994 EXPORT_SYMBOL(cdrom_is_random_writable);
2996 #ifdef CONFIG_SYSCTL
2998 #define CDROM_STR_SIZE 1000
3000 struct cdrom_sysctl_settings {
3001 char info[CDROM_STR_SIZE]; /* general info */
3002 int autoclose; /* close tray upon mount, etc */
3003 int autoeject; /* eject on umount */
3004 int debug; /* turn on debugging messages */
3005 int lock; /* lock the door on device open */
3006 int check; /* check media type */
3007 } cdrom_sysctl_settings;
3009 int cdrom_sysctl_info(ctl_table *ctl, int write, struct file * filp,
3010 void __user *buffer, size_t *lenp, loff_t *ppos)
3013 struct cdrom_device_info *cdi;
3014 char *info = cdrom_sysctl_settings.info;
3016 if (!*lenp || (*ppos && !write)) {
3021 pos = sprintf(info, "CD-ROM information, " VERSION "\n");
3023 pos += sprintf(info+pos, "\ndrive name:\t");
3024 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
3025 pos += sprintf(info+pos, "\t%s", cdi->name);
3027 pos += sprintf(info+pos, "\ndrive speed:\t");
3028 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
3029 pos += sprintf(info+pos, "\t%d", cdi->speed);
3031 pos += sprintf(info+pos, "\ndrive # of slots:");
3032 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
3033 pos += sprintf(info+pos, "\t%d", cdi->capacity);
3035 pos += sprintf(info+pos, "\nCan close tray:\t");
3036 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
3037 pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_CLOSE_TRAY) != 0);
3039 pos += sprintf(info+pos, "\nCan open tray:\t");
3040 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
3041 pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_OPEN_TRAY) != 0);
3043 pos += sprintf(info+pos, "\nCan lock tray:\t");
3044 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
3045 pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_LOCK) != 0);
3047 pos += sprintf(info+pos, "\nCan change speed:");
3048 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
3049 pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_SELECT_SPEED) != 0);
3051 pos += sprintf(info+pos, "\nCan select disk:");
3052 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
3053 pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_SELECT_DISC) != 0);
3055 pos += sprintf(info+pos, "\nCan read multisession:");
3056 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
3057 pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_MULTI_SESSION) != 0);
3059 pos += sprintf(info+pos, "\nCan read MCN:\t");
3060 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
3061 pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_MCN) != 0);
3063 pos += sprintf(info+pos, "\nReports media changed:");
3064 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
3065 pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_MEDIA_CHANGED) != 0);
3067 pos += sprintf(info+pos, "\nCan play audio:\t");
3068 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
3069 pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_PLAY_AUDIO) != 0);
3071 pos += sprintf(info+pos, "\nCan write CD-R:\t");
3072 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
3073 pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_CD_R) != 0);
3075 pos += sprintf(info+pos, "\nCan write CD-RW:");
3076 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
3077 pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_CD_RW) != 0);
3079 pos += sprintf(info+pos, "\nCan read DVD:\t");
3080 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
3081 pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_DVD) != 0);
3083 pos += sprintf(info+pos, "\nCan write DVD-R:");
3084 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
3085 pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_DVD_R) != 0);
3087 pos += sprintf(info+pos, "\nCan write DVD-RAM:");
3088 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
3089 pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_DVD_RAM) != 0);
3091 pos += sprintf(info+pos, "\nCan read MRW:\t");
3092 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
3093 pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_MRW) != 0);
3095 pos += sprintf(info+pos, "\nCan write MRW:\t");
3096 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
3097 pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_MRW_W) != 0);
3099 pos += sprintf(info+pos, "\nCan write RAM:\t");
3100 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
3101 pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_RAM) != 0);
3103 strcpy(info+pos,"\n\n");
3105 return proc_dostring(ctl, write, filp, buffer, lenp, ppos);
3108 /* Unfortunately, per device settings are not implemented through
3109 procfs/sysctl yet. When they are, this will naturally disappear. For now
3110 just update all drives. Later this will become the template on which
3111 new registered drives will be based. */
3112 void cdrom_update_settings(void)
3114 struct cdrom_device_info *cdi;
3116 for (cdi = topCdromPtr; cdi != NULL; cdi = cdi->next) {
3117 if (autoclose && CDROM_CAN(CDC_CLOSE_TRAY))
3118 cdi->options |= CDO_AUTO_CLOSE;
3119 else if (!autoclose)
3120 cdi->options &= ~CDO_AUTO_CLOSE;
3121 if (autoeject && CDROM_CAN(CDC_OPEN_TRAY))
3122 cdi->options |= CDO_AUTO_EJECT;
3123 else if (!autoeject)
3124 cdi->options &= ~CDO_AUTO_EJECT;
3125 if (lockdoor && CDROM_CAN(CDC_LOCK))
3126 cdi->options |= CDO_LOCK;
3128 cdi->options &= ~CDO_LOCK;
3129 if (check_media_type)
3130 cdi->options |= CDO_CHECK_TYPE;
3132 cdi->options &= ~CDO_CHECK_TYPE;
3136 static int cdrom_sysctl_handler(ctl_table *ctl, int write, struct file * filp,
3137 void __user *buffer, size_t *lenp, loff_t *ppos)
3139 int *valp = ctl->data;
3143 ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
3145 if (write && *valp != val) {
3147 /* we only care for 1 or 0. */
3153 switch (ctl->ctl_name) {
3154 case DEV_CDROM_AUTOCLOSE: {
3155 if (valp == &cdrom_sysctl_settings.autoclose)
3156 autoclose = cdrom_sysctl_settings.autoclose;
3159 case DEV_CDROM_AUTOEJECT: {
3160 if (valp == &cdrom_sysctl_settings.autoeject)
3161 autoeject = cdrom_sysctl_settings.autoeject;
3164 case DEV_CDROM_DEBUG: {
3165 if (valp == &cdrom_sysctl_settings.debug)
3166 debug = cdrom_sysctl_settings.debug;
3169 case DEV_CDROM_LOCK: {
3170 if (valp == &cdrom_sysctl_settings.lock)
3171 lockdoor = cdrom_sysctl_settings.lock;
3174 case DEV_CDROM_CHECK_MEDIA: {
3175 if (valp == &cdrom_sysctl_settings.check)
3176 check_media_type = cdrom_sysctl_settings.check;
3180 /* update the option flags according to the changes. we
3181 don't have per device options through sysctl yet,
3182 but we will have and then this will disappear. */
3183 cdrom_update_settings();
3189 /* Place files in /proc/sys/dev/cdrom */
3190 ctl_table cdrom_table[] = {
3192 .ctl_name = DEV_CDROM_INFO,
3194 .data = &cdrom_sysctl_settings.info,
3195 .maxlen = CDROM_STR_SIZE,
3197 .proc_handler = &cdrom_sysctl_info,
3200 .ctl_name = DEV_CDROM_AUTOCLOSE,
3201 .procname = "autoclose",
3202 .data = &cdrom_sysctl_settings.autoclose,
3203 .maxlen = sizeof(int),
3205 .proc_handler = &cdrom_sysctl_handler,
3208 .ctl_name = DEV_CDROM_AUTOEJECT,
3209 .procname = "autoeject",
3210 .data = &cdrom_sysctl_settings.autoeject,
3211 .maxlen = sizeof(int),
3213 .proc_handler = &cdrom_sysctl_handler,
3216 .ctl_name = DEV_CDROM_DEBUG,
3217 .procname = "debug",
3218 .data = &cdrom_sysctl_settings.debug,
3219 .maxlen = sizeof(int),
3221 .proc_handler = &cdrom_sysctl_handler,
3224 .ctl_name = DEV_CDROM_LOCK,
3226 .data = &cdrom_sysctl_settings.lock,
3227 .maxlen = sizeof(int),
3229 .proc_handler = &cdrom_sysctl_handler,
3232 .ctl_name = DEV_CDROM_CHECK_MEDIA,
3233 .procname = "check_media",
3234 .data = &cdrom_sysctl_settings.check,
3235 .maxlen = sizeof(int),
3237 .proc_handler = &cdrom_sysctl_handler
3242 ctl_table cdrom_cdrom_table[] = {
3244 .ctl_name = DEV_CDROM,
3245 .procname = "cdrom",
3248 .child = cdrom_table,
3253 /* Make sure that /proc/sys/dev is there */
3254 ctl_table cdrom_root_table[] = {
3255 #ifdef CONFIG_PROC_FS
3257 .ctl_name = CTL_DEV,
3261 .child = cdrom_cdrom_table,
3263 #endif /* CONFIG_PROC_FS */
3266 static struct ctl_table_header *cdrom_sysctl_header;
3268 static void cdrom_sysctl_register(void)
3270 static int initialized;
3272 if (initialized == 1)
3275 cdrom_sysctl_header = register_sysctl_table(cdrom_root_table, 1);
3276 if (cdrom_root_table->ctl_name && cdrom_root_table->child->de)
3277 cdrom_root_table->child->de->owner = THIS_MODULE;
3279 /* set the defaults */
3280 cdrom_sysctl_settings.autoclose = autoclose;
3281 cdrom_sysctl_settings.autoeject = autoeject;
3282 cdrom_sysctl_settings.debug = debug;
3283 cdrom_sysctl_settings.lock = lockdoor;
3284 cdrom_sysctl_settings.check = check_media_type;
3289 static void cdrom_sysctl_unregister(void)
3291 if (cdrom_sysctl_header)
3292 unregister_sysctl_table(cdrom_sysctl_header);
3295 #endif /* CONFIG_SYSCTL */
3297 static int __init cdrom_init(void)
3299 #ifdef CONFIG_SYSCTL
3300 cdrom_sysctl_register();
3305 static void __exit cdrom_exit(void)
3307 printk(KERN_INFO "Uniform CD-ROM driver unloaded\n");
3308 #ifdef CONFIG_SYSCTL
3309 cdrom_sysctl_unregister();
3313 module_init(cdrom_init);
3314 module_exit(cdrom_exit);
3315 MODULE_LICENSE("GPL");