cdrom: remove ifdef CONFIG_SYSCTL
[linux-2.6/sactl.git] / drivers / cdrom / cdrom.c
blob82c7d212409ceb8b95f98ce30e48dcfc92519cba
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. ;-)
17 To Do List:
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!
26 Revision History
27 ----------------------------------
28 1.00 Date Unknown -- David van Leeuwen <david@tm.tno.nl>
29 -- Initial version by David A. van Leeuwen. I don't have a detailed
30 changelog for the 1.x series, David?
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
52 problem).
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
102 that bug.
104 2.53 Feb 22, 1999 - Jens Axboe <axboe@image.dk>
105 -- Fixup of several ioctl calls, in particular CDROM_SET_OPTIONS has
106 been "rewritten" because capabilities and options aren't in sync. They
107 should be...
108 -- Added CDROM_LOCKDOOR ioctl. Locks the door and keeps it that way.
109 -- Added CDROM_RESET ioctl.
110 -- Added CDROM_DEBUG ioctl. Enable debug messages on-the-fly.
111 -- Added CDROM_GET_CAPABILITY ioctl. This relieves userspace programs
112 from parsing /proc/sys/dev/cdrom/info.
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
120 CDC_CLOSE_TRAY.
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,
134 and lock.
135 -- /proc/sys/dev/cdrom/info has been updated to also contain info about
136 CD-Rx and DVD capabilities.
137 -- Now default to checking media type.
138 -- CDROM_SEND_PACKET ioctl added. The infrastructure was in place for
139 doing this anyway, with the generic_packet addition.
141 3.01 Aug 6, 1999 - Jens Axboe <axboe@image.dk>
142 -- Fix up the sysctl handling so that the option flags get set
143 correctly.
144 -- Fix up ioctl handling so the device specific ones actually get
145 called :).
147 3.02 Aug 8, 1999 - Jens Axboe <axboe@image.dk>
148 -- Fixed volume control on SCSI drives (or others with longer audio
149 page).
150 -- Fixed a couple of DVD minors. Thanks to Andrew T. Veliath
151 <andrewtv@usa.net> for telling me and for having defined the various
152 DVD structures and ioctls in the first place! He designed the original
153 DVD patches for ide-cd and while I rearranged and unified them, the
154 interface is still the same.
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
185 dvd_read_manufact.
186 -- Added setup of write mode for packet writing.
187 -- Fixed CDDA ripping with cdda2wav - accept much larger requests of
188 number of frames and split the reads in blocks of 8.
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
199 home now.
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
217 back.
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 :-)
233 -- Locking fixes
234 -- Mt Rainier support
235 -- DVD-RAM write open fixes
237 Nov 5 2001, Aug 8 2002. Modified by Andy Polyakov
238 <appro@fy.chalmers.se> to support MMC-3 compliant DVD+RW units.
240 Modified by Nigel Kukard <nkukard@lbsd.net> - support DVD+RW
241 2.4.x patch by Andy Polyakov <appro@fy.chalmers.se>
243 -------------------------------------------------------------------------*/
245 #define REVISION "Revision: 3.20"
246 #define VERSION "Id: cdrom.c 3.20 2003/12/17"
248 /* I use an error-log mask to give fine grain control over the type of
249 messages dumped to the system logs. The available masks include: */
250 #define CD_NOTHING 0x0
251 #define CD_WARNING 0x1
252 #define CD_REG_UNREG 0x2
253 #define CD_DO_IOCTL 0x4
254 #define CD_OPEN 0x8
255 #define CD_CLOSE 0x10
256 #define CD_COUNT_TRACKS 0x20
257 #define CD_CHANGER 0x40
258 #define CD_DVD 0x80
260 /* Define this to remove _all_ the debugging messages */
261 /* #define ERRLOGMASK CD_NOTHING */
262 #define ERRLOGMASK CD_WARNING
263 /* #define ERRLOGMASK (CD_WARNING|CD_OPEN|CD_COUNT_TRACKS|CD_CLOSE) */
264 /* #define ERRLOGMASK (CD_WARNING|CD_REG_UNREG|CD_DO_IOCTL|CD_OPEN|CD_CLOSE|CD_COUNT_TRACKS) */
266 #include <linux/module.h>
267 #include <linux/fs.h>
268 #include <linux/buffer_head.h>
269 #include <linux/major.h>
270 #include <linux/types.h>
271 #include <linux/errno.h>
272 #include <linux/kernel.h>
273 #include <linux/mm.h>
274 #include <linux/slab.h>
275 #include <linux/cdrom.h>
276 #include <linux/sysctl.h>
277 #include <linux/proc_fs.h>
278 #include <linux/blkpg.h>
279 #include <linux/init.h>
280 #include <linux/fcntl.h>
281 #include <linux/blkdev.h>
282 #include <linux/times.h>
284 #include <asm/uaccess.h>
286 /* used to tell the module to turn on full debugging messages */
287 static int debug;
288 /* used to keep tray locked at all times */
289 static int keeplocked;
290 /* default compatibility mode */
291 static int autoclose=1;
292 static int autoeject;
293 static int lockdoor = 1;
294 /* will we ever get to use this... sigh. */
295 static int check_media_type;
296 /* automatically restart mrw format */
297 static int mrw_format_restart = 1;
298 module_param(debug, bool, 0);
299 module_param(autoclose, bool, 0);
300 module_param(autoeject, bool, 0);
301 module_param(lockdoor, bool, 0);
302 module_param(check_media_type, bool, 0);
303 module_param(mrw_format_restart, bool, 0);
305 static DEFINE_MUTEX(cdrom_mutex);
307 static const char *mrw_format_status[] = {
308 "not mrw",
309 "bgformat inactive",
310 "bgformat active",
311 "mrw complete",
314 static const char *mrw_address_space[] = { "DMA", "GAA" };
316 #if (ERRLOGMASK!=CD_NOTHING)
317 #define cdinfo(type, fmt, args...) \
318 if ((ERRLOGMASK & type) || debug==1 ) \
319 printk(KERN_INFO "cdrom: " fmt, ## args)
320 #else
321 #define cdinfo(type, fmt, args...)
322 #endif
324 /* These are used to simplify getting data in from and back to user land */
325 #define IOCTL_IN(arg, type, in) \
326 if (copy_from_user(&(in), (type __user *) (arg), sizeof (in))) \
327 return -EFAULT;
329 #define IOCTL_OUT(arg, type, out) \
330 if (copy_to_user((type __user *) (arg), &(out), sizeof (out))) \
331 return -EFAULT;
333 /* The (cdo->capability & ~cdi->mask & CDC_XXX) construct was used in
334 a lot of places. This macro makes the code more clear. */
335 #define CDROM_CAN(type) (cdi->ops->capability & ~cdi->mask & (type))
337 /* used in the audio ioctls */
338 #define CHECKAUDIO if ((ret=check_for_audio_disc(cdi, cdo))) return ret
341 * Another popular OS uses 7 seconds as the hard timeout for default
342 * commands, so it is a good choice for us as well.
344 #define CDROM_DEF_TIMEOUT (7 * HZ)
346 /* Not-exported routines. */
347 static int open_for_data(struct cdrom_device_info * cdi);
348 static int check_for_audio_disc(struct cdrom_device_info * cdi,
349 struct cdrom_device_ops * cdo);
350 static void sanitize_format(union cdrom_addr *addr,
351 u_char * curr, u_char requested);
352 static int mmc_ioctl(struct cdrom_device_info *cdi, unsigned int cmd,
353 unsigned long arg);
355 int cdrom_get_last_written(struct cdrom_device_info *, long *);
356 static int cdrom_get_next_writable(struct cdrom_device_info *, long *);
357 static void cdrom_count_tracks(struct cdrom_device_info *, tracktype*);
359 static int cdrom_mrw_exit(struct cdrom_device_info *cdi);
361 static int cdrom_get_disc_info(struct cdrom_device_info *cdi, disc_information *di);
363 static void cdrom_sysctl_register(void);
365 static struct cdrom_device_info *topCdromPtr;
367 static int cdrom_dummy_generic_packet(struct cdrom_device_info *cdi,
368 struct packet_command *cgc)
370 if (cgc->sense) {
371 cgc->sense->sense_key = 0x05;
372 cgc->sense->asc = 0x20;
373 cgc->sense->ascq = 0x00;
376 cgc->stat = -EIO;
377 return -EIO;
380 /* This macro makes sure we don't have to check on cdrom_device_ops
381 * existence in the run-time routines below. Change_capability is a
382 * hack to have the capability flags defined const, while we can still
383 * change it here without gcc complaining at every line.
385 #define ENSURE(call, bits) if (cdo->call == NULL) *change_capability &= ~(bits)
387 int register_cdrom(struct cdrom_device_info *cdi)
389 static char banner_printed;
390 struct cdrom_device_ops *cdo = cdi->ops;
391 int *change_capability = (int *)&cdo->capability; /* hack */
393 cdinfo(CD_OPEN, "entering register_cdrom\n");
395 if (cdo->open == NULL || cdo->release == NULL)
396 return -2;
397 if (!banner_printed) {
398 printk(KERN_INFO "Uniform CD-ROM driver " REVISION "\n");
399 banner_printed = 1;
400 cdrom_sysctl_register();
403 ENSURE(drive_status, CDC_DRIVE_STATUS );
404 ENSURE(media_changed, CDC_MEDIA_CHANGED);
405 ENSURE(tray_move, CDC_CLOSE_TRAY | CDC_OPEN_TRAY);
406 ENSURE(lock_door, CDC_LOCK);
407 ENSURE(select_speed, CDC_SELECT_SPEED);
408 ENSURE(get_last_session, CDC_MULTI_SESSION);
409 ENSURE(get_mcn, CDC_MCN);
410 ENSURE(reset, CDC_RESET);
411 ENSURE(audio_ioctl, CDC_PLAY_AUDIO);
412 ENSURE(generic_packet, CDC_GENERIC_PACKET);
413 cdi->mc_flags = 0;
414 cdo->n_minors = 0;
415 cdi->options = CDO_USE_FFLAGS;
417 if (autoclose==1 && CDROM_CAN(CDC_CLOSE_TRAY))
418 cdi->options |= (int) CDO_AUTO_CLOSE;
419 if (autoeject==1 && CDROM_CAN(CDC_OPEN_TRAY))
420 cdi->options |= (int) CDO_AUTO_EJECT;
421 if (lockdoor==1)
422 cdi->options |= (int) CDO_LOCK;
423 if (check_media_type==1)
424 cdi->options |= (int) CDO_CHECK_TYPE;
426 if (CDROM_CAN(CDC_MRW_W))
427 cdi->exit = cdrom_mrw_exit;
429 if (cdi->disk)
430 cdi->cdda_method = CDDA_BPC_FULL;
431 else
432 cdi->cdda_method = CDDA_OLD;
434 if (!cdo->generic_packet)
435 cdo->generic_packet = cdrom_dummy_generic_packet;
437 cdinfo(CD_REG_UNREG, "drive \"/dev/%s\" registered\n", cdi->name);
438 mutex_lock(&cdrom_mutex);
439 cdi->next = topCdromPtr;
440 topCdromPtr = cdi;
441 mutex_unlock(&cdrom_mutex);
442 return 0;
444 #undef ENSURE
446 int unregister_cdrom(struct cdrom_device_info *unreg)
448 struct cdrom_device_info *cdi, *prev;
449 cdinfo(CD_OPEN, "entering unregister_cdrom\n");
451 prev = NULL;
452 mutex_lock(&cdrom_mutex);
453 cdi = topCdromPtr;
454 while (cdi && cdi != unreg) {
455 prev = cdi;
456 cdi = cdi->next;
459 if (cdi == NULL) {
460 mutex_unlock(&cdrom_mutex);
461 return -2;
463 if (prev)
464 prev->next = cdi->next;
465 else
466 topCdromPtr = cdi->next;
468 mutex_unlock(&cdrom_mutex);
470 if (cdi->exit)
471 cdi->exit(cdi);
473 cdi->ops->n_minors--;
474 cdinfo(CD_REG_UNREG, "drive \"/dev/%s\" unregistered\n", cdi->name);
475 return 0;
478 int cdrom_get_media_event(struct cdrom_device_info *cdi,
479 struct media_event_desc *med)
481 struct packet_command cgc;
482 unsigned char buffer[8];
483 struct event_header *eh = (struct event_header *) buffer;
485 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_READ);
486 cgc.cmd[0] = GPCMD_GET_EVENT_STATUS_NOTIFICATION;
487 cgc.cmd[1] = 1; /* IMMED */
488 cgc.cmd[4] = 1 << 4; /* media event */
489 cgc.cmd[8] = sizeof(buffer);
490 cgc.quiet = 1;
492 if (cdi->ops->generic_packet(cdi, &cgc))
493 return 1;
495 if (be16_to_cpu(eh->data_len) < sizeof(*med))
496 return 1;
498 if (eh->nea || eh->notification_class != 0x4)
499 return 1;
501 memcpy(med, &buffer[sizeof(*eh)], sizeof(*med));
502 return 0;
506 * the first prototypes used 0x2c as the page code for the mrw mode page,
507 * subsequently this was changed to 0x03. probe the one used by this drive
509 static int cdrom_mrw_probe_pc(struct cdrom_device_info *cdi)
511 struct packet_command cgc;
512 char buffer[16];
514 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_READ);
516 cgc.timeout = HZ;
517 cgc.quiet = 1;
519 if (!cdrom_mode_sense(cdi, &cgc, MRW_MODE_PC, 0)) {
520 cdi->mrw_mode_page = MRW_MODE_PC;
521 return 0;
522 } else if (!cdrom_mode_sense(cdi, &cgc, MRW_MODE_PC_PRE1, 0)) {
523 cdi->mrw_mode_page = MRW_MODE_PC_PRE1;
524 return 0;
527 return 1;
530 static int cdrom_is_mrw(struct cdrom_device_info *cdi, int *write)
532 struct packet_command cgc;
533 struct mrw_feature_desc *mfd;
534 unsigned char buffer[16];
535 int ret;
537 *write = 0;
539 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_READ);
541 cgc.cmd[0] = GPCMD_GET_CONFIGURATION;
542 cgc.cmd[3] = CDF_MRW;
543 cgc.cmd[8] = sizeof(buffer);
544 cgc.quiet = 1;
546 if ((ret = cdi->ops->generic_packet(cdi, &cgc)))
547 return ret;
549 mfd = (struct mrw_feature_desc *)&buffer[sizeof(struct feature_header)];
550 if (be16_to_cpu(mfd->feature_code) != CDF_MRW)
551 return 1;
552 *write = mfd->write;
554 if ((ret = cdrom_mrw_probe_pc(cdi))) {
555 *write = 0;
556 return ret;
559 return 0;
562 static int cdrom_mrw_bgformat(struct cdrom_device_info *cdi, int cont)
564 struct packet_command cgc;
565 unsigned char buffer[12];
566 int ret;
568 printk(KERN_INFO "cdrom: %sstarting format\n", cont ? "Re" : "");
571 * FmtData bit set (bit 4), format type is 1
573 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_WRITE);
574 cgc.cmd[0] = GPCMD_FORMAT_UNIT;
575 cgc.cmd[1] = (1 << 4) | 1;
577 cgc.timeout = 5 * 60 * HZ;
580 * 4 byte format list header, 8 byte format list descriptor
582 buffer[1] = 1 << 1;
583 buffer[3] = 8;
586 * nr_blocks field
588 buffer[4] = 0xff;
589 buffer[5] = 0xff;
590 buffer[6] = 0xff;
591 buffer[7] = 0xff;
593 buffer[8] = 0x24 << 2;
594 buffer[11] = cont;
596 ret = cdi->ops->generic_packet(cdi, &cgc);
597 if (ret)
598 printk(KERN_INFO "cdrom: bgformat failed\n");
600 return ret;
603 static int cdrom_mrw_bgformat_susp(struct cdrom_device_info *cdi, int immed)
605 struct packet_command cgc;
607 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
608 cgc.cmd[0] = GPCMD_CLOSE_TRACK;
611 * Session = 1, Track = 0
613 cgc.cmd[1] = !!immed;
614 cgc.cmd[2] = 1 << 1;
616 cgc.timeout = 5 * 60 * HZ;
618 return cdi->ops->generic_packet(cdi, &cgc);
621 static int cdrom_flush_cache(struct cdrom_device_info *cdi)
623 struct packet_command cgc;
625 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
626 cgc.cmd[0] = GPCMD_FLUSH_CACHE;
628 cgc.timeout = 5 * 60 * HZ;
630 return cdi->ops->generic_packet(cdi, &cgc);
633 static int cdrom_mrw_exit(struct cdrom_device_info *cdi)
635 disc_information di;
636 int ret;
638 ret = cdrom_get_disc_info(cdi, &di);
639 if (ret < 0 || ret < (int)offsetof(typeof(di),disc_type))
640 return 1;
642 ret = 0;
643 if (di.mrw_status == CDM_MRW_BGFORMAT_ACTIVE) {
644 printk(KERN_INFO "cdrom: issuing MRW back ground "
645 "format suspend\n");
646 ret = cdrom_mrw_bgformat_susp(cdi, 0);
649 if (!ret && cdi->media_written)
650 ret = cdrom_flush_cache(cdi);
652 return ret;
655 static int cdrom_mrw_set_lba_space(struct cdrom_device_info *cdi, int space)
657 struct packet_command cgc;
658 struct mode_page_header *mph;
659 char buffer[16];
660 int ret, offset, size;
662 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_READ);
664 cgc.buffer = buffer;
665 cgc.buflen = sizeof(buffer);
667 if ((ret = cdrom_mode_sense(cdi, &cgc, cdi->mrw_mode_page, 0)))
668 return ret;
670 mph = (struct mode_page_header *) buffer;
671 offset = be16_to_cpu(mph->desc_length);
672 size = be16_to_cpu(mph->mode_data_length) + 2;
674 buffer[offset + 3] = space;
675 cgc.buflen = size;
677 if ((ret = cdrom_mode_select(cdi, &cgc)))
678 return ret;
680 printk(KERN_INFO "cdrom: %s: mrw address space %s selected\n", cdi->name, mrw_address_space[space]);
681 return 0;
684 static int cdrom_get_random_writable(struct cdrom_device_info *cdi,
685 struct rwrt_feature_desc *rfd)
687 struct packet_command cgc;
688 char buffer[24];
689 int ret;
691 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_READ);
693 cgc.cmd[0] = GPCMD_GET_CONFIGURATION; /* often 0x46 */
694 cgc.cmd[3] = CDF_RWRT; /* often 0x0020 */
695 cgc.cmd[8] = sizeof(buffer); /* often 0x18 */
696 cgc.quiet = 1;
698 if ((ret = cdi->ops->generic_packet(cdi, &cgc)))
699 return ret;
701 memcpy(rfd, &buffer[sizeof(struct feature_header)], sizeof (*rfd));
702 return 0;
705 static int cdrom_has_defect_mgt(struct cdrom_device_info *cdi)
707 struct packet_command cgc;
708 char buffer[16];
709 __be16 *feature_code;
710 int ret;
712 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_READ);
714 cgc.cmd[0] = GPCMD_GET_CONFIGURATION;
715 cgc.cmd[3] = CDF_HWDM;
716 cgc.cmd[8] = sizeof(buffer);
717 cgc.quiet = 1;
719 if ((ret = cdi->ops->generic_packet(cdi, &cgc)))
720 return ret;
722 feature_code = (__be16 *) &buffer[sizeof(struct feature_header)];
723 if (be16_to_cpu(*feature_code) == CDF_HWDM)
724 return 0;
726 return 1;
730 static int cdrom_is_random_writable(struct cdrom_device_info *cdi, int *write)
732 struct rwrt_feature_desc rfd;
733 int ret;
735 *write = 0;
737 if ((ret = cdrom_get_random_writable(cdi, &rfd)))
738 return ret;
740 if (CDF_RWRT == be16_to_cpu(rfd.feature_code))
741 *write = 1;
743 return 0;
746 static int cdrom_media_erasable(struct cdrom_device_info *cdi)
748 disc_information di;
749 int ret;
751 ret = cdrom_get_disc_info(cdi, &di);
752 if (ret < 0 || ret < offsetof(typeof(di), n_first_track))
753 return -1;
755 return di.erasable;
759 * FIXME: check RO bit
761 static int cdrom_dvdram_open_write(struct cdrom_device_info *cdi)
763 int ret = cdrom_media_erasable(cdi);
766 * allow writable open if media info read worked and media is
767 * erasable, _or_ if it fails since not all drives support it
769 if (!ret)
770 return 1;
772 return 0;
775 static int cdrom_mrw_open_write(struct cdrom_device_info *cdi)
777 disc_information di;
778 int ret;
781 * always reset to DMA lba space on open
783 if (cdrom_mrw_set_lba_space(cdi, MRW_LBA_DMA)) {
784 printk(KERN_ERR "cdrom: failed setting lba address space\n");
785 return 1;
788 ret = cdrom_get_disc_info(cdi, &di);
789 if (ret < 0 || ret < offsetof(typeof(di),disc_type))
790 return 1;
792 if (!di.erasable)
793 return 1;
796 * mrw_status
797 * 0 - not MRW formatted
798 * 1 - MRW bgformat started, but not running or complete
799 * 2 - MRW bgformat in progress
800 * 3 - MRW formatting complete
802 ret = 0;
803 printk(KERN_INFO "cdrom open: mrw_status '%s'\n",
804 mrw_format_status[di.mrw_status]);
805 if (!di.mrw_status)
806 ret = 1;
807 else if (di.mrw_status == CDM_MRW_BGFORMAT_INACTIVE &&
808 mrw_format_restart)
809 ret = cdrom_mrw_bgformat(cdi, 1);
811 return ret;
814 static int mo_open_write(struct cdrom_device_info *cdi)
816 struct packet_command cgc;
817 char buffer[255];
818 int ret;
820 init_cdrom_command(&cgc, &buffer, 4, CGC_DATA_READ);
821 cgc.quiet = 1;
824 * obtain write protect information as per
825 * drivers/scsi/sd.c:sd_read_write_protect_flag
828 ret = cdrom_mode_sense(cdi, &cgc, GPMODE_ALL_PAGES, 0);
829 if (ret)
830 ret = cdrom_mode_sense(cdi, &cgc, GPMODE_VENDOR_PAGE, 0);
831 if (ret) {
832 cgc.buflen = 255;
833 ret = cdrom_mode_sense(cdi, &cgc, GPMODE_ALL_PAGES, 0);
836 /* drive gave us no info, let the user go ahead */
837 if (ret)
838 return 0;
840 return buffer[3] & 0x80;
843 static int cdrom_ram_open_write(struct cdrom_device_info *cdi)
845 struct rwrt_feature_desc rfd;
846 int ret;
848 if ((ret = cdrom_has_defect_mgt(cdi)))
849 return ret;
851 if ((ret = cdrom_get_random_writable(cdi, &rfd)))
852 return ret;
853 else if (CDF_RWRT == be16_to_cpu(rfd.feature_code))
854 ret = !rfd.curr;
856 cdinfo(CD_OPEN, "can open for random write\n");
857 return ret;
860 static void cdrom_mmc3_profile(struct cdrom_device_info *cdi)
862 struct packet_command cgc;
863 char buffer[32];
864 int ret, mmc3_profile;
866 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_READ);
868 cgc.cmd[0] = GPCMD_GET_CONFIGURATION;
869 cgc.cmd[1] = 0;
870 cgc.cmd[2] = cgc.cmd[3] = 0; /* Starting Feature Number */
871 cgc.cmd[8] = sizeof(buffer); /* Allocation Length */
872 cgc.quiet = 1;
874 if ((ret = cdi->ops->generic_packet(cdi, &cgc)))
875 mmc3_profile = 0xffff;
876 else
877 mmc3_profile = (buffer[6] << 8) | buffer[7];
879 cdi->mmc3_profile = mmc3_profile;
882 static int cdrom_is_dvd_rw(struct cdrom_device_info *cdi)
884 switch (cdi->mmc3_profile) {
885 case 0x12: /* DVD-RAM */
886 case 0x1A: /* DVD+RW */
887 return 0;
888 default:
889 return 1;
894 * returns 0 for ok to open write, non-0 to disallow
896 static int cdrom_open_write(struct cdrom_device_info *cdi)
898 int mrw, mrw_write, ram_write;
899 int ret = 1;
901 mrw = 0;
902 if (!cdrom_is_mrw(cdi, &mrw_write))
903 mrw = 1;
905 if (CDROM_CAN(CDC_MO_DRIVE))
906 ram_write = 1;
907 else
908 (void) cdrom_is_random_writable(cdi, &ram_write);
910 if (mrw)
911 cdi->mask &= ~CDC_MRW;
912 else
913 cdi->mask |= CDC_MRW;
915 if (mrw_write)
916 cdi->mask &= ~CDC_MRW_W;
917 else
918 cdi->mask |= CDC_MRW_W;
920 if (ram_write)
921 cdi->mask &= ~CDC_RAM;
922 else
923 cdi->mask |= CDC_RAM;
925 if (CDROM_CAN(CDC_MRW_W))
926 ret = cdrom_mrw_open_write(cdi);
927 else if (CDROM_CAN(CDC_DVD_RAM))
928 ret = cdrom_dvdram_open_write(cdi);
929 else if (CDROM_CAN(CDC_RAM) &&
930 !CDROM_CAN(CDC_CD_R|CDC_CD_RW|CDC_DVD|CDC_DVD_R|CDC_MRW|CDC_MO_DRIVE))
931 ret = cdrom_ram_open_write(cdi);
932 else if (CDROM_CAN(CDC_MO_DRIVE))
933 ret = mo_open_write(cdi);
934 else if (!cdrom_is_dvd_rw(cdi))
935 ret = 0;
937 return ret;
940 static void cdrom_dvd_rw_close_write(struct cdrom_device_info *cdi)
942 struct packet_command cgc;
944 if (cdi->mmc3_profile != 0x1a) {
945 cdinfo(CD_CLOSE, "%s: No DVD+RW\n", cdi->name);
946 return;
949 if (!cdi->media_written) {
950 cdinfo(CD_CLOSE, "%s: DVD+RW media clean\n", cdi->name);
951 return;
954 printk(KERN_INFO "cdrom: %s: dirty DVD+RW media, \"finalizing\"\n",
955 cdi->name);
957 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
958 cgc.cmd[0] = GPCMD_FLUSH_CACHE;
959 cgc.timeout = 30*HZ;
960 cdi->ops->generic_packet(cdi, &cgc);
962 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
963 cgc.cmd[0] = GPCMD_CLOSE_TRACK;
964 cgc.timeout = 3000*HZ;
965 cgc.quiet = 1;
966 cdi->ops->generic_packet(cdi, &cgc);
968 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
969 cgc.cmd[0] = GPCMD_CLOSE_TRACK;
970 cgc.cmd[2] = 2; /* Close session */
971 cgc.quiet = 1;
972 cgc.timeout = 3000*HZ;
973 cdi->ops->generic_packet(cdi, &cgc);
975 cdi->media_written = 0;
978 static int cdrom_close_write(struct cdrom_device_info *cdi)
980 #if 0
981 return cdrom_flush_cache(cdi);
982 #else
983 return 0;
984 #endif
987 /* We use the open-option O_NONBLOCK to indicate that the
988 * purpose of opening is only for subsequent ioctl() calls; no device
989 * integrity checks are performed.
991 * We hope that all cd-player programs will adopt this convention. It
992 * is in their own interest: device control becomes a lot easier
993 * this way.
995 int cdrom_open(struct cdrom_device_info *cdi, struct inode *ip, struct file *fp)
997 int ret;
999 cdinfo(CD_OPEN, "entering cdrom_open\n");
1001 /* if this was a O_NONBLOCK open and we should honor the flags,
1002 * do a quick open without drive/disc integrity checks. */
1003 cdi->use_count++;
1004 if ((fp->f_flags & O_NONBLOCK) && (cdi->options & CDO_USE_FFLAGS)) {
1005 ret = cdi->ops->open(cdi, 1);
1006 } else {
1007 ret = open_for_data(cdi);
1008 if (ret)
1009 goto err;
1010 cdrom_mmc3_profile(cdi);
1011 if (fp->f_mode & FMODE_WRITE) {
1012 ret = -EROFS;
1013 if (cdrom_open_write(cdi))
1014 goto err_release;
1015 if (!CDROM_CAN(CDC_RAM))
1016 goto err_release;
1017 ret = 0;
1018 cdi->media_written = 0;
1022 if (ret)
1023 goto err;
1025 cdinfo(CD_OPEN, "Use count for \"/dev/%s\" now %d\n",
1026 cdi->name, cdi->use_count);
1027 /* Do this on open. Don't wait for mount, because they might
1028 not be mounting, but opening with O_NONBLOCK */
1029 check_disk_change(ip->i_bdev);
1030 return 0;
1031 err_release:
1032 if (CDROM_CAN(CDC_LOCK) && cdi->options & CDO_LOCK) {
1033 cdi->ops->lock_door(cdi, 0);
1034 cdinfo(CD_OPEN, "door unlocked.\n");
1036 cdi->ops->release(cdi);
1037 err:
1038 cdi->use_count--;
1039 return ret;
1042 static
1043 int open_for_data(struct cdrom_device_info * cdi)
1045 int ret;
1046 struct cdrom_device_ops *cdo = cdi->ops;
1047 tracktype tracks;
1048 cdinfo(CD_OPEN, "entering open_for_data\n");
1049 /* Check if the driver can report drive status. If it can, we
1050 can do clever things. If it can't, well, we at least tried! */
1051 if (cdo->drive_status != NULL) {
1052 ret = cdo->drive_status(cdi, CDSL_CURRENT);
1053 cdinfo(CD_OPEN, "drive_status=%d\n", ret);
1054 if (ret == CDS_TRAY_OPEN) {
1055 cdinfo(CD_OPEN, "the tray is open...\n");
1056 /* can/may i close it? */
1057 if (CDROM_CAN(CDC_CLOSE_TRAY) &&
1058 cdi->options & CDO_AUTO_CLOSE) {
1059 cdinfo(CD_OPEN, "trying to close the tray.\n");
1060 ret=cdo->tray_move(cdi,0);
1061 if (ret) {
1062 cdinfo(CD_OPEN, "bummer. tried to close the tray but failed.\n");
1063 /* Ignore the error from the low
1064 level driver. We don't care why it
1065 couldn't close the tray. We only care
1066 that there is no disc in the drive,
1067 since that is the _REAL_ problem here.*/
1068 ret=-ENOMEDIUM;
1069 goto clean_up_and_return;
1071 } else {
1072 cdinfo(CD_OPEN, "bummer. this drive can't close the tray.\n");
1073 ret=-ENOMEDIUM;
1074 goto clean_up_and_return;
1076 /* Ok, the door should be closed now.. Check again */
1077 ret = cdo->drive_status(cdi, CDSL_CURRENT);
1078 if ((ret == CDS_NO_DISC) || (ret==CDS_TRAY_OPEN)) {
1079 cdinfo(CD_OPEN, "bummer. the tray is still not closed.\n");
1080 cdinfo(CD_OPEN, "tray might not contain a medium.\n");
1081 ret=-ENOMEDIUM;
1082 goto clean_up_and_return;
1084 cdinfo(CD_OPEN, "the tray is now closed.\n");
1086 /* the door should be closed now, check for the disc */
1087 ret = cdo->drive_status(cdi, CDSL_CURRENT);
1088 if (ret!=CDS_DISC_OK) {
1089 ret = -ENOMEDIUM;
1090 goto clean_up_and_return;
1093 cdrom_count_tracks(cdi, &tracks);
1094 if (tracks.error == CDS_NO_DISC) {
1095 cdinfo(CD_OPEN, "bummer. no disc.\n");
1096 ret=-ENOMEDIUM;
1097 goto clean_up_and_return;
1099 /* CD-Players which don't use O_NONBLOCK, workman
1100 * for example, need bit CDO_CHECK_TYPE cleared! */
1101 if (tracks.data==0) {
1102 if (cdi->options & CDO_CHECK_TYPE) {
1103 /* give people a warning shot, now that CDO_CHECK_TYPE
1104 is the default case! */
1105 cdinfo(CD_OPEN, "bummer. wrong media type.\n");
1106 cdinfo(CD_WARNING, "pid %d must open device O_NONBLOCK!\n",
1107 (unsigned int)task_pid_nr(current));
1108 ret=-EMEDIUMTYPE;
1109 goto clean_up_and_return;
1111 else {
1112 cdinfo(CD_OPEN, "wrong media type, but CDO_CHECK_TYPE not set.\n");
1116 cdinfo(CD_OPEN, "all seems well, opening the device.\n");
1118 /* all seems well, we can open the device */
1119 ret = cdo->open(cdi, 0); /* open for data */
1120 cdinfo(CD_OPEN, "opening the device gave me %d.\n", ret);
1121 /* After all this careful checking, we shouldn't have problems
1122 opening the device, but we don't want the device locked if
1123 this somehow fails... */
1124 if (ret) {
1125 cdinfo(CD_OPEN, "open device failed.\n");
1126 goto clean_up_and_return;
1128 if (CDROM_CAN(CDC_LOCK) && (cdi->options & CDO_LOCK)) {
1129 cdo->lock_door(cdi, 1);
1130 cdinfo(CD_OPEN, "door locked.\n");
1132 cdinfo(CD_OPEN, "device opened successfully.\n");
1133 return ret;
1135 /* Something failed. Try to unlock the drive, because some drivers
1136 (notably ide-cd) lock the drive after every command. This produced
1137 a nasty bug where after mount failed, the drive would remain locked!
1138 This ensures that the drive gets unlocked after a mount fails. This
1139 is a goto to avoid bloating the driver with redundant code. */
1140 clean_up_and_return:
1141 cdinfo(CD_OPEN, "open failed.\n");
1142 if (CDROM_CAN(CDC_LOCK) && cdi->options & CDO_LOCK) {
1143 cdo->lock_door(cdi, 0);
1144 cdinfo(CD_OPEN, "door unlocked.\n");
1146 return ret;
1149 /* This code is similar to that in open_for_data. The routine is called
1150 whenever an audio play operation is requested.
1152 static int check_for_audio_disc(struct cdrom_device_info * cdi,
1153 struct cdrom_device_ops * cdo)
1155 int ret;
1156 tracktype tracks;
1157 cdinfo(CD_OPEN, "entering check_for_audio_disc\n");
1158 if (!(cdi->options & CDO_CHECK_TYPE))
1159 return 0;
1160 if (cdo->drive_status != NULL) {
1161 ret = cdo->drive_status(cdi, CDSL_CURRENT);
1162 cdinfo(CD_OPEN, "drive_status=%d\n", ret);
1163 if (ret == CDS_TRAY_OPEN) {
1164 cdinfo(CD_OPEN, "the tray is open...\n");
1165 /* can/may i close it? */
1166 if (CDROM_CAN(CDC_CLOSE_TRAY) &&
1167 cdi->options & CDO_AUTO_CLOSE) {
1168 cdinfo(CD_OPEN, "trying to close the tray.\n");
1169 ret=cdo->tray_move(cdi,0);
1170 if (ret) {
1171 cdinfo(CD_OPEN, "bummer. tried to close tray but failed.\n");
1172 /* Ignore the error from the low
1173 level driver. We don't care why it
1174 couldn't close the tray. We only care
1175 that there is no disc in the drive,
1176 since that is the _REAL_ problem here.*/
1177 return -ENOMEDIUM;
1179 } else {
1180 cdinfo(CD_OPEN, "bummer. this driver can't close the tray.\n");
1181 return -ENOMEDIUM;
1183 /* Ok, the door should be closed now.. Check again */
1184 ret = cdo->drive_status(cdi, CDSL_CURRENT);
1185 if ((ret == CDS_NO_DISC) || (ret==CDS_TRAY_OPEN)) {
1186 cdinfo(CD_OPEN, "bummer. the tray is still not closed.\n");
1187 return -ENOMEDIUM;
1189 if (ret!=CDS_DISC_OK) {
1190 cdinfo(CD_OPEN, "bummer. disc isn't ready.\n");
1191 return -EIO;
1193 cdinfo(CD_OPEN, "the tray is now closed.\n");
1196 cdrom_count_tracks(cdi, &tracks);
1197 if (tracks.error)
1198 return(tracks.error);
1200 if (tracks.audio==0)
1201 return -EMEDIUMTYPE;
1203 return 0;
1206 int cdrom_release(struct cdrom_device_info *cdi, struct file *fp)
1208 struct cdrom_device_ops *cdo = cdi->ops;
1209 int opened_for_data;
1211 cdinfo(CD_CLOSE, "entering cdrom_release\n");
1213 if (cdi->use_count > 0)
1214 cdi->use_count--;
1216 if (cdi->use_count == 0) {
1217 cdinfo(CD_CLOSE, "Use count for \"/dev/%s\" now zero\n", cdi->name);
1218 cdrom_dvd_rw_close_write(cdi);
1220 if ((cdo->capability & CDC_LOCK) && !keeplocked) {
1221 cdinfo(CD_CLOSE, "Unlocking door!\n");
1222 cdo->lock_door(cdi, 0);
1226 opened_for_data = !(cdi->options & CDO_USE_FFLAGS) ||
1227 !(fp && fp->f_flags & O_NONBLOCK);
1230 * flush cache on last write release
1232 if (CDROM_CAN(CDC_RAM) && !cdi->use_count && cdi->for_data)
1233 cdrom_close_write(cdi);
1235 cdo->release(cdi);
1236 if (cdi->use_count == 0) { /* last process that closes dev*/
1237 if (opened_for_data &&
1238 cdi->options & CDO_AUTO_EJECT && CDROM_CAN(CDC_OPEN_TRAY))
1239 cdo->tray_move(cdi, 1);
1241 return 0;
1244 static int cdrom_read_mech_status(struct cdrom_device_info *cdi,
1245 struct cdrom_changer_info *buf)
1247 struct packet_command cgc;
1248 struct cdrom_device_ops *cdo = cdi->ops;
1249 int length;
1252 * Sanyo changer isn't spec compliant (doesn't use regular change
1253 * LOAD_UNLOAD command, and it doesn't implement the mech status
1254 * command below
1256 if (cdi->sanyo_slot) {
1257 buf->hdr.nslots = 3;
1258 buf->hdr.curslot = cdi->sanyo_slot == 3 ? 0 : cdi->sanyo_slot;
1259 for (length = 0; length < 3; length++) {
1260 buf->slots[length].disc_present = 1;
1261 buf->slots[length].change = 0;
1263 return 0;
1266 length = sizeof(struct cdrom_mechstat_header) +
1267 cdi->capacity * sizeof(struct cdrom_slot);
1269 init_cdrom_command(&cgc, buf, length, CGC_DATA_READ);
1270 cgc.cmd[0] = GPCMD_MECHANISM_STATUS;
1271 cgc.cmd[8] = (length >> 8) & 0xff;
1272 cgc.cmd[9] = length & 0xff;
1273 return cdo->generic_packet(cdi, &cgc);
1276 static int cdrom_slot_status(struct cdrom_device_info *cdi, int slot)
1278 struct cdrom_changer_info *info;
1279 int ret;
1281 cdinfo(CD_CHANGER, "entering cdrom_slot_status()\n");
1282 if (cdi->sanyo_slot)
1283 return CDS_NO_INFO;
1285 info = kmalloc(sizeof(*info), GFP_KERNEL);
1286 if (!info)
1287 return -ENOMEM;
1289 if ((ret = cdrom_read_mech_status(cdi, info)))
1290 goto out_free;
1292 if (info->slots[slot].disc_present)
1293 ret = CDS_DISC_OK;
1294 else
1295 ret = CDS_NO_DISC;
1297 out_free:
1298 kfree(info);
1299 return ret;
1302 /* Return the number of slots for an ATAPI/SCSI cdrom,
1303 * return 1 if not a changer.
1305 int cdrom_number_of_slots(struct cdrom_device_info *cdi)
1307 int status;
1308 int nslots = 1;
1309 struct cdrom_changer_info *info;
1311 cdinfo(CD_CHANGER, "entering cdrom_number_of_slots()\n");
1312 /* cdrom_read_mech_status requires a valid value for capacity: */
1313 cdi->capacity = 0;
1315 info = kmalloc(sizeof(*info), GFP_KERNEL);
1316 if (!info)
1317 return -ENOMEM;
1319 if ((status = cdrom_read_mech_status(cdi, info)) == 0)
1320 nslots = info->hdr.nslots;
1322 kfree(info);
1323 return nslots;
1327 /* If SLOT < 0, unload the current slot. Otherwise, try to load SLOT. */
1328 static int cdrom_load_unload(struct cdrom_device_info *cdi, int slot)
1330 struct packet_command cgc;
1332 cdinfo(CD_CHANGER, "entering cdrom_load_unload()\n");
1333 if (cdi->sanyo_slot && slot < 0)
1334 return 0;
1336 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
1337 cgc.cmd[0] = GPCMD_LOAD_UNLOAD;
1338 cgc.cmd[4] = 2 + (slot >= 0);
1339 cgc.cmd[8] = slot;
1340 cgc.timeout = 60 * HZ;
1342 /* The Sanyo 3 CD changer uses byte 7 of the
1343 GPCMD_TEST_UNIT_READY to command to switch CDs instead of
1344 using the GPCMD_LOAD_UNLOAD opcode. */
1345 if (cdi->sanyo_slot && -1 < slot) {
1346 cgc.cmd[0] = GPCMD_TEST_UNIT_READY;
1347 cgc.cmd[7] = slot;
1348 cgc.cmd[4] = cgc.cmd[8] = 0;
1349 cdi->sanyo_slot = slot ? slot : 3;
1352 return cdi->ops->generic_packet(cdi, &cgc);
1355 static int cdrom_select_disc(struct cdrom_device_info *cdi, int slot)
1357 struct cdrom_changer_info *info;
1358 int curslot;
1359 int ret;
1361 cdinfo(CD_CHANGER, "entering cdrom_select_disc()\n");
1362 if (!CDROM_CAN(CDC_SELECT_DISC))
1363 return -EDRIVE_CANT_DO_THIS;
1365 (void) cdi->ops->media_changed(cdi, slot);
1367 if (slot == CDSL_NONE) {
1368 /* set media changed bits, on both queues */
1369 cdi->mc_flags = 0x3;
1370 return cdrom_load_unload(cdi, -1);
1373 info = kmalloc(sizeof(*info), GFP_KERNEL);
1374 if (!info)
1375 return -ENOMEM;
1377 if ((ret = cdrom_read_mech_status(cdi, info))) {
1378 kfree(info);
1379 return ret;
1382 curslot = info->hdr.curslot;
1383 kfree(info);
1385 if (cdi->use_count > 1 || keeplocked) {
1386 if (slot == CDSL_CURRENT) {
1387 return curslot;
1388 } else {
1389 return -EBUSY;
1393 /* Specifying CDSL_CURRENT will attempt to load the currnet slot,
1394 which is useful if it had been previously unloaded.
1395 Whether it can or not, it returns the current slot.
1396 Similarly, if slot happens to be the current one, we still
1397 try and load it. */
1398 if (slot == CDSL_CURRENT)
1399 slot = curslot;
1401 /* set media changed bits on both queues */
1402 cdi->mc_flags = 0x3;
1403 if ((ret = cdrom_load_unload(cdi, slot)))
1404 return ret;
1406 return slot;
1409 /* We want to make media_changed accessible to the user through an
1410 * ioctl. The main problem now is that we must double-buffer the
1411 * low-level implementation, to assure that the VFS and the user both
1412 * see a medium change once.
1415 static
1416 int media_changed(struct cdrom_device_info *cdi, int queue)
1418 unsigned int mask = (1 << (queue & 1));
1419 int ret = !!(cdi->mc_flags & mask);
1421 if (!CDROM_CAN(CDC_MEDIA_CHANGED))
1422 return ret;
1423 /* changed since last call? */
1424 if (cdi->ops->media_changed(cdi, CDSL_CURRENT)) {
1425 cdi->mc_flags = 0x3; /* set bit on both queues */
1426 ret |= 1;
1427 cdi->media_written = 0;
1429 cdi->mc_flags &= ~mask; /* clear bit */
1430 return ret;
1433 int cdrom_media_changed(struct cdrom_device_info *cdi)
1435 /* This talks to the VFS, which doesn't like errors - just 1 or 0.
1436 * Returning "0" is always safe (media hasn't been changed). Do that
1437 * if the low-level cdrom driver dosn't support media changed. */
1438 if (cdi == NULL || cdi->ops->media_changed == NULL)
1439 return 0;
1440 if (!CDROM_CAN(CDC_MEDIA_CHANGED))
1441 return 0;
1442 return media_changed(cdi, 0);
1445 /* badly broken, I know. Is due for a fixup anytime. */
1446 static void cdrom_count_tracks(struct cdrom_device_info *cdi, tracktype* tracks)
1448 struct cdrom_tochdr header;
1449 struct cdrom_tocentry entry;
1450 int ret, i;
1451 tracks->data=0;
1452 tracks->audio=0;
1453 tracks->cdi=0;
1454 tracks->xa=0;
1455 tracks->error=0;
1456 cdinfo(CD_COUNT_TRACKS, "entering cdrom_count_tracks\n");
1457 if (!CDROM_CAN(CDC_PLAY_AUDIO)) {
1458 tracks->error=CDS_NO_INFO;
1459 return;
1461 /* Grab the TOC header so we can see how many tracks there are */
1462 if ((ret = cdi->ops->audio_ioctl(cdi, CDROMREADTOCHDR, &header))) {
1463 if (ret == -ENOMEDIUM)
1464 tracks->error = CDS_NO_DISC;
1465 else
1466 tracks->error = CDS_NO_INFO;
1467 return;
1469 /* check what type of tracks are on this disc */
1470 entry.cdte_format = CDROM_MSF;
1471 for (i = header.cdth_trk0; i <= header.cdth_trk1; i++) {
1472 entry.cdte_track = i;
1473 if (cdi->ops->audio_ioctl(cdi, CDROMREADTOCENTRY, &entry)) {
1474 tracks->error=CDS_NO_INFO;
1475 return;
1477 if (entry.cdte_ctrl & CDROM_DATA_TRACK) {
1478 if (entry.cdte_format == 0x10)
1479 tracks->cdi++;
1480 else if (entry.cdte_format == 0x20)
1481 tracks->xa++;
1482 else
1483 tracks->data++;
1484 } else
1485 tracks->audio++;
1486 cdinfo(CD_COUNT_TRACKS, "track %d: format=%d, ctrl=%d\n",
1487 i, entry.cdte_format, entry.cdte_ctrl);
1489 cdinfo(CD_COUNT_TRACKS, "disc has %d tracks: %d=audio %d=data %d=Cd-I %d=XA\n",
1490 header.cdth_trk1, tracks->audio, tracks->data,
1491 tracks->cdi, tracks->xa);
1494 /* Requests to the low-level drivers will /always/ be done in the
1495 following format convention:
1497 CDROM_LBA: all data-related requests.
1498 CDROM_MSF: all audio-related requests.
1500 However, a low-level implementation is allowed to refuse this
1501 request, and return information in its own favorite format.
1503 It doesn't make sense /at all/ to ask for a play_audio in LBA
1504 format, or ask for multi-session info in MSF format. However, for
1505 backward compatibility these format requests will be satisfied, but
1506 the requests to the low-level drivers will be sanitized in the more
1507 meaningful format indicated above.
1510 static
1511 void sanitize_format(union cdrom_addr *addr,
1512 u_char * curr, u_char requested)
1514 if (*curr == requested)
1515 return; /* nothing to be done! */
1516 if (requested == CDROM_LBA) {
1517 addr->lba = (int) addr->msf.frame +
1518 75 * (addr->msf.second - 2 + 60 * addr->msf.minute);
1519 } else { /* CDROM_MSF */
1520 int lba = addr->lba;
1521 addr->msf.frame = lba % 75;
1522 lba /= 75;
1523 lba += 2;
1524 addr->msf.second = lba % 60;
1525 addr->msf.minute = lba / 60;
1527 *curr = requested;
1530 void init_cdrom_command(struct packet_command *cgc, void *buf, int len,
1531 int type)
1533 memset(cgc, 0, sizeof(struct packet_command));
1534 if (buf)
1535 memset(buf, 0, len);
1536 cgc->buffer = (char *) buf;
1537 cgc->buflen = len;
1538 cgc->data_direction = type;
1539 cgc->timeout = CDROM_DEF_TIMEOUT;
1542 /* DVD handling */
1544 #define copy_key(dest,src) memcpy((dest), (src), sizeof(dvd_key))
1545 #define copy_chal(dest,src) memcpy((dest), (src), sizeof(dvd_challenge))
1547 static void setup_report_key(struct packet_command *cgc, unsigned agid, unsigned type)
1549 cgc->cmd[0] = GPCMD_REPORT_KEY;
1550 cgc->cmd[10] = type | (agid << 6);
1551 switch (type) {
1552 case 0: case 8: case 5: {
1553 cgc->buflen = 8;
1554 break;
1556 case 1: {
1557 cgc->buflen = 16;
1558 break;
1560 case 2: case 4: {
1561 cgc->buflen = 12;
1562 break;
1565 cgc->cmd[9] = cgc->buflen;
1566 cgc->data_direction = CGC_DATA_READ;
1569 static void setup_send_key(struct packet_command *cgc, unsigned agid, unsigned type)
1571 cgc->cmd[0] = GPCMD_SEND_KEY;
1572 cgc->cmd[10] = type | (agid << 6);
1573 switch (type) {
1574 case 1: {
1575 cgc->buflen = 16;
1576 break;
1578 case 3: {
1579 cgc->buflen = 12;
1580 break;
1582 case 6: {
1583 cgc->buflen = 8;
1584 break;
1587 cgc->cmd[9] = cgc->buflen;
1588 cgc->data_direction = CGC_DATA_WRITE;
1591 static int dvd_do_auth(struct cdrom_device_info *cdi, dvd_authinfo *ai)
1593 int ret;
1594 u_char buf[20];
1595 struct packet_command cgc;
1596 struct cdrom_device_ops *cdo = cdi->ops;
1597 rpc_state_t rpc_state;
1599 memset(buf, 0, sizeof(buf));
1600 init_cdrom_command(&cgc, buf, 0, CGC_DATA_READ);
1602 switch (ai->type) {
1603 /* LU data send */
1604 case DVD_LU_SEND_AGID:
1605 cdinfo(CD_DVD, "entering DVD_LU_SEND_AGID\n");
1606 cgc.quiet = 1;
1607 setup_report_key(&cgc, ai->lsa.agid, 0);
1609 if ((ret = cdo->generic_packet(cdi, &cgc)))
1610 return ret;
1612 ai->lsa.agid = buf[7] >> 6;
1613 /* Returning data, let host change state */
1614 break;
1616 case DVD_LU_SEND_KEY1:
1617 cdinfo(CD_DVD, "entering DVD_LU_SEND_KEY1\n");
1618 setup_report_key(&cgc, ai->lsk.agid, 2);
1620 if ((ret = cdo->generic_packet(cdi, &cgc)))
1621 return ret;
1623 copy_key(ai->lsk.key, &buf[4]);
1624 /* Returning data, let host change state */
1625 break;
1627 case DVD_LU_SEND_CHALLENGE:
1628 cdinfo(CD_DVD, "entering DVD_LU_SEND_CHALLENGE\n");
1629 setup_report_key(&cgc, ai->lsc.agid, 1);
1631 if ((ret = cdo->generic_packet(cdi, &cgc)))
1632 return ret;
1634 copy_chal(ai->lsc.chal, &buf[4]);
1635 /* Returning data, let host change state */
1636 break;
1638 /* Post-auth key */
1639 case DVD_LU_SEND_TITLE_KEY:
1640 cdinfo(CD_DVD, "entering DVD_LU_SEND_TITLE_KEY\n");
1641 cgc.quiet = 1;
1642 setup_report_key(&cgc, ai->lstk.agid, 4);
1643 cgc.cmd[5] = ai->lstk.lba;
1644 cgc.cmd[4] = ai->lstk.lba >> 8;
1645 cgc.cmd[3] = ai->lstk.lba >> 16;
1646 cgc.cmd[2] = ai->lstk.lba >> 24;
1648 if ((ret = cdo->generic_packet(cdi, &cgc)))
1649 return ret;
1651 ai->lstk.cpm = (buf[4] >> 7) & 1;
1652 ai->lstk.cp_sec = (buf[4] >> 6) & 1;
1653 ai->lstk.cgms = (buf[4] >> 4) & 3;
1654 copy_key(ai->lstk.title_key, &buf[5]);
1655 /* Returning data, let host change state */
1656 break;
1658 case DVD_LU_SEND_ASF:
1659 cdinfo(CD_DVD, "entering DVD_LU_SEND_ASF\n");
1660 setup_report_key(&cgc, ai->lsasf.agid, 5);
1662 if ((ret = cdo->generic_packet(cdi, &cgc)))
1663 return ret;
1665 ai->lsasf.asf = buf[7] & 1;
1666 break;
1668 /* LU data receive (LU changes state) */
1669 case DVD_HOST_SEND_CHALLENGE:
1670 cdinfo(CD_DVD, "entering DVD_HOST_SEND_CHALLENGE\n");
1671 setup_send_key(&cgc, ai->hsc.agid, 1);
1672 buf[1] = 0xe;
1673 copy_chal(&buf[4], ai->hsc.chal);
1675 if ((ret = cdo->generic_packet(cdi, &cgc)))
1676 return ret;
1678 ai->type = DVD_LU_SEND_KEY1;
1679 break;
1681 case DVD_HOST_SEND_KEY2:
1682 cdinfo(CD_DVD, "entering DVD_HOST_SEND_KEY2\n");
1683 setup_send_key(&cgc, ai->hsk.agid, 3);
1684 buf[1] = 0xa;
1685 copy_key(&buf[4], ai->hsk.key);
1687 if ((ret = cdo->generic_packet(cdi, &cgc))) {
1688 ai->type = DVD_AUTH_FAILURE;
1689 return ret;
1691 ai->type = DVD_AUTH_ESTABLISHED;
1692 break;
1694 /* Misc */
1695 case DVD_INVALIDATE_AGID:
1696 cgc.quiet = 1;
1697 cdinfo(CD_DVD, "entering DVD_INVALIDATE_AGID\n");
1698 setup_report_key(&cgc, ai->lsa.agid, 0x3f);
1699 if ((ret = cdo->generic_packet(cdi, &cgc)))
1700 return ret;
1701 break;
1703 /* Get region settings */
1704 case DVD_LU_SEND_RPC_STATE:
1705 cdinfo(CD_DVD, "entering DVD_LU_SEND_RPC_STATE\n");
1706 setup_report_key(&cgc, 0, 8);
1707 memset(&rpc_state, 0, sizeof(rpc_state_t));
1708 cgc.buffer = (char *) &rpc_state;
1710 if ((ret = cdo->generic_packet(cdi, &cgc)))
1711 return ret;
1713 ai->lrpcs.type = rpc_state.type_code;
1714 ai->lrpcs.vra = rpc_state.vra;
1715 ai->lrpcs.ucca = rpc_state.ucca;
1716 ai->lrpcs.region_mask = rpc_state.region_mask;
1717 ai->lrpcs.rpc_scheme = rpc_state.rpc_scheme;
1718 break;
1720 /* Set region settings */
1721 case DVD_HOST_SEND_RPC_STATE:
1722 cdinfo(CD_DVD, "entering DVD_HOST_SEND_RPC_STATE\n");
1723 setup_send_key(&cgc, 0, 6);
1724 buf[1] = 6;
1725 buf[4] = ai->hrpcs.pdrc;
1727 if ((ret = cdo->generic_packet(cdi, &cgc)))
1728 return ret;
1729 break;
1731 default:
1732 cdinfo(CD_WARNING, "Invalid DVD key ioctl (%d)\n", ai->type);
1733 return -ENOTTY;
1736 return 0;
1739 static int dvd_read_physical(struct cdrom_device_info *cdi, dvd_struct *s)
1741 unsigned char buf[21], *base;
1742 struct dvd_layer *layer;
1743 struct packet_command cgc;
1744 struct cdrom_device_ops *cdo = cdi->ops;
1745 int ret, layer_num = s->physical.layer_num;
1747 if (layer_num >= DVD_LAYERS)
1748 return -EINVAL;
1750 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_READ);
1751 cgc.cmd[0] = GPCMD_READ_DVD_STRUCTURE;
1752 cgc.cmd[6] = layer_num;
1753 cgc.cmd[7] = s->type;
1754 cgc.cmd[9] = cgc.buflen & 0xff;
1757 * refrain from reporting errors on non-existing layers (mainly)
1759 cgc.quiet = 1;
1761 if ((ret = cdo->generic_packet(cdi, &cgc)))
1762 return ret;
1764 base = &buf[4];
1765 layer = &s->physical.layer[layer_num];
1768 * place the data... really ugly, but at least we won't have to
1769 * worry about endianess in userspace.
1771 memset(layer, 0, sizeof(*layer));
1772 layer->book_version = base[0] & 0xf;
1773 layer->book_type = base[0] >> 4;
1774 layer->min_rate = base[1] & 0xf;
1775 layer->disc_size = base[1] >> 4;
1776 layer->layer_type = base[2] & 0xf;
1777 layer->track_path = (base[2] >> 4) & 1;
1778 layer->nlayers = (base[2] >> 5) & 3;
1779 layer->track_density = base[3] & 0xf;
1780 layer->linear_density = base[3] >> 4;
1781 layer->start_sector = base[5] << 16 | base[6] << 8 | base[7];
1782 layer->end_sector = base[9] << 16 | base[10] << 8 | base[11];
1783 layer->end_sector_l0 = base[13] << 16 | base[14] << 8 | base[15];
1784 layer->bca = base[16] >> 7;
1786 return 0;
1789 static int dvd_read_copyright(struct cdrom_device_info *cdi, dvd_struct *s)
1791 int ret;
1792 u_char buf[8];
1793 struct packet_command cgc;
1794 struct cdrom_device_ops *cdo = cdi->ops;
1796 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_READ);
1797 cgc.cmd[0] = GPCMD_READ_DVD_STRUCTURE;
1798 cgc.cmd[6] = s->copyright.layer_num;
1799 cgc.cmd[7] = s->type;
1800 cgc.cmd[8] = cgc.buflen >> 8;
1801 cgc.cmd[9] = cgc.buflen & 0xff;
1803 if ((ret = cdo->generic_packet(cdi, &cgc)))
1804 return ret;
1806 s->copyright.cpst = buf[4];
1807 s->copyright.rmi = buf[5];
1809 return 0;
1812 static int dvd_read_disckey(struct cdrom_device_info *cdi, dvd_struct *s)
1814 int ret, size;
1815 u_char *buf;
1816 struct packet_command cgc;
1817 struct cdrom_device_ops *cdo = cdi->ops;
1819 size = sizeof(s->disckey.value) + 4;
1821 if ((buf = kmalloc(size, GFP_KERNEL)) == NULL)
1822 return -ENOMEM;
1824 init_cdrom_command(&cgc, buf, size, CGC_DATA_READ);
1825 cgc.cmd[0] = GPCMD_READ_DVD_STRUCTURE;
1826 cgc.cmd[7] = s->type;
1827 cgc.cmd[8] = size >> 8;
1828 cgc.cmd[9] = size & 0xff;
1829 cgc.cmd[10] = s->disckey.agid << 6;
1831 if (!(ret = cdo->generic_packet(cdi, &cgc)))
1832 memcpy(s->disckey.value, &buf[4], sizeof(s->disckey.value));
1834 kfree(buf);
1835 return ret;
1838 static int dvd_read_bca(struct cdrom_device_info *cdi, dvd_struct *s)
1840 int ret;
1841 u_char buf[4 + 188];
1842 struct packet_command cgc;
1843 struct cdrom_device_ops *cdo = cdi->ops;
1845 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_READ);
1846 cgc.cmd[0] = GPCMD_READ_DVD_STRUCTURE;
1847 cgc.cmd[7] = s->type;
1848 cgc.cmd[9] = cgc.buflen & 0xff;
1850 if ((ret = cdo->generic_packet(cdi, &cgc)))
1851 return ret;
1853 s->bca.len = buf[0] << 8 | buf[1];
1854 if (s->bca.len < 12 || s->bca.len > 188) {
1855 cdinfo(CD_WARNING, "Received invalid BCA length (%d)\n", s->bca.len);
1856 return -EIO;
1858 memcpy(s->bca.value, &buf[4], s->bca.len);
1860 return 0;
1863 static int dvd_read_manufact(struct cdrom_device_info *cdi, dvd_struct *s)
1865 int ret = 0, size;
1866 u_char *buf;
1867 struct packet_command cgc;
1868 struct cdrom_device_ops *cdo = cdi->ops;
1870 size = sizeof(s->manufact.value) + 4;
1872 if ((buf = kmalloc(size, GFP_KERNEL)) == NULL)
1873 return -ENOMEM;
1875 init_cdrom_command(&cgc, buf, size, CGC_DATA_READ);
1876 cgc.cmd[0] = GPCMD_READ_DVD_STRUCTURE;
1877 cgc.cmd[7] = s->type;
1878 cgc.cmd[8] = size >> 8;
1879 cgc.cmd[9] = size & 0xff;
1881 if ((ret = cdo->generic_packet(cdi, &cgc))) {
1882 kfree(buf);
1883 return ret;
1886 s->manufact.len = buf[0] << 8 | buf[1];
1887 if (s->manufact.len < 0 || s->manufact.len > 2048) {
1888 cdinfo(CD_WARNING, "Received invalid manufacture info length"
1889 " (%d)\n", s->manufact.len);
1890 ret = -EIO;
1891 } else {
1892 memcpy(s->manufact.value, &buf[4], s->manufact.len);
1895 kfree(buf);
1896 return ret;
1899 static int dvd_read_struct(struct cdrom_device_info *cdi, dvd_struct *s)
1901 switch (s->type) {
1902 case DVD_STRUCT_PHYSICAL:
1903 return dvd_read_physical(cdi, s);
1905 case DVD_STRUCT_COPYRIGHT:
1906 return dvd_read_copyright(cdi, s);
1908 case DVD_STRUCT_DISCKEY:
1909 return dvd_read_disckey(cdi, s);
1911 case DVD_STRUCT_BCA:
1912 return dvd_read_bca(cdi, s);
1914 case DVD_STRUCT_MANUFACT:
1915 return dvd_read_manufact(cdi, s);
1917 default:
1918 cdinfo(CD_WARNING, ": Invalid DVD structure read requested (%d)\n",
1919 s->type);
1920 return -EINVAL;
1924 int cdrom_mode_sense(struct cdrom_device_info *cdi,
1925 struct packet_command *cgc,
1926 int page_code, int page_control)
1928 struct cdrom_device_ops *cdo = cdi->ops;
1930 memset(cgc->cmd, 0, sizeof(cgc->cmd));
1932 cgc->cmd[0] = GPCMD_MODE_SENSE_10;
1933 cgc->cmd[2] = page_code | (page_control << 6);
1934 cgc->cmd[7] = cgc->buflen >> 8;
1935 cgc->cmd[8] = cgc->buflen & 0xff;
1936 cgc->data_direction = CGC_DATA_READ;
1937 return cdo->generic_packet(cdi, cgc);
1940 int cdrom_mode_select(struct cdrom_device_info *cdi,
1941 struct packet_command *cgc)
1943 struct cdrom_device_ops *cdo = cdi->ops;
1945 memset(cgc->cmd, 0, sizeof(cgc->cmd));
1946 memset(cgc->buffer, 0, 2);
1947 cgc->cmd[0] = GPCMD_MODE_SELECT_10;
1948 cgc->cmd[1] = 0x10; /* PF */
1949 cgc->cmd[7] = cgc->buflen >> 8;
1950 cgc->cmd[8] = cgc->buflen & 0xff;
1951 cgc->data_direction = CGC_DATA_WRITE;
1952 return cdo->generic_packet(cdi, cgc);
1955 static int cdrom_read_subchannel(struct cdrom_device_info *cdi,
1956 struct cdrom_subchnl *subchnl, int mcn)
1958 struct cdrom_device_ops *cdo = cdi->ops;
1959 struct packet_command cgc;
1960 char buffer[32];
1961 int ret;
1963 init_cdrom_command(&cgc, buffer, 16, CGC_DATA_READ);
1964 cgc.cmd[0] = GPCMD_READ_SUBCHANNEL;
1965 cgc.cmd[1] = 2; /* MSF addressing */
1966 cgc.cmd[2] = 0x40; /* request subQ data */
1967 cgc.cmd[3] = mcn ? 2 : 1;
1968 cgc.cmd[8] = 16;
1970 if ((ret = cdo->generic_packet(cdi, &cgc)))
1971 return ret;
1973 subchnl->cdsc_audiostatus = cgc.buffer[1];
1974 subchnl->cdsc_format = CDROM_MSF;
1975 subchnl->cdsc_ctrl = cgc.buffer[5] & 0xf;
1976 subchnl->cdsc_trk = cgc.buffer[6];
1977 subchnl->cdsc_ind = cgc.buffer[7];
1979 subchnl->cdsc_reladdr.msf.minute = cgc.buffer[13];
1980 subchnl->cdsc_reladdr.msf.second = cgc.buffer[14];
1981 subchnl->cdsc_reladdr.msf.frame = cgc.buffer[15];
1982 subchnl->cdsc_absaddr.msf.minute = cgc.buffer[9];
1983 subchnl->cdsc_absaddr.msf.second = cgc.buffer[10];
1984 subchnl->cdsc_absaddr.msf.frame = cgc.buffer[11];
1986 return 0;
1990 * Specific READ_10 interface
1992 static int cdrom_read_cd(struct cdrom_device_info *cdi,
1993 struct packet_command *cgc, int lba,
1994 int blocksize, int nblocks)
1996 struct cdrom_device_ops *cdo = cdi->ops;
1998 memset(&cgc->cmd, 0, sizeof(cgc->cmd));
1999 cgc->cmd[0] = GPCMD_READ_10;
2000 cgc->cmd[2] = (lba >> 24) & 0xff;
2001 cgc->cmd[3] = (lba >> 16) & 0xff;
2002 cgc->cmd[4] = (lba >> 8) & 0xff;
2003 cgc->cmd[5] = lba & 0xff;
2004 cgc->cmd[6] = (nblocks >> 16) & 0xff;
2005 cgc->cmd[7] = (nblocks >> 8) & 0xff;
2006 cgc->cmd[8] = nblocks & 0xff;
2007 cgc->buflen = blocksize * nblocks;
2008 return cdo->generic_packet(cdi, cgc);
2011 /* very generic interface for reading the various types of blocks */
2012 static int cdrom_read_block(struct cdrom_device_info *cdi,
2013 struct packet_command *cgc,
2014 int lba, int nblocks, int format, int blksize)
2016 struct cdrom_device_ops *cdo = cdi->ops;
2018 memset(&cgc->cmd, 0, sizeof(cgc->cmd));
2019 cgc->cmd[0] = GPCMD_READ_CD;
2020 /* expected sector size - cdda,mode1,etc. */
2021 cgc->cmd[1] = format << 2;
2022 /* starting address */
2023 cgc->cmd[2] = (lba >> 24) & 0xff;
2024 cgc->cmd[3] = (lba >> 16) & 0xff;
2025 cgc->cmd[4] = (lba >> 8) & 0xff;
2026 cgc->cmd[5] = lba & 0xff;
2027 /* number of blocks */
2028 cgc->cmd[6] = (nblocks >> 16) & 0xff;
2029 cgc->cmd[7] = (nblocks >> 8) & 0xff;
2030 cgc->cmd[8] = nblocks & 0xff;
2031 cgc->buflen = blksize * nblocks;
2033 /* set the header info returned */
2034 switch (blksize) {
2035 case CD_FRAMESIZE_RAW0 : cgc->cmd[9] = 0x58; break;
2036 case CD_FRAMESIZE_RAW1 : cgc->cmd[9] = 0x78; break;
2037 case CD_FRAMESIZE_RAW : cgc->cmd[9] = 0xf8; break;
2038 default : cgc->cmd[9] = 0x10;
2041 return cdo->generic_packet(cdi, cgc);
2044 static int cdrom_read_cdda_old(struct cdrom_device_info *cdi, __u8 __user *ubuf,
2045 int lba, int nframes)
2047 struct packet_command cgc;
2048 int ret = 0;
2049 int nr;
2051 cdi->last_sense = 0;
2053 memset(&cgc, 0, sizeof(cgc));
2056 * start with will ra.nframes size, back down if alloc fails
2058 nr = nframes;
2059 do {
2060 cgc.buffer = kmalloc(CD_FRAMESIZE_RAW * nr, GFP_KERNEL);
2061 if (cgc.buffer)
2062 break;
2064 nr >>= 1;
2065 } while (nr);
2067 if (!nr)
2068 return -ENOMEM;
2070 if (!access_ok(VERIFY_WRITE, ubuf, nframes * CD_FRAMESIZE_RAW)) {
2071 ret = -EFAULT;
2072 goto out;
2075 cgc.data_direction = CGC_DATA_READ;
2076 while (nframes > 0) {
2077 if (nr > nframes)
2078 nr = nframes;
2080 ret = cdrom_read_block(cdi, &cgc, lba, nr, 1, CD_FRAMESIZE_RAW);
2081 if (ret)
2082 break;
2083 if (__copy_to_user(ubuf, cgc.buffer, CD_FRAMESIZE_RAW * nr)) {
2084 ret = -EFAULT;
2085 break;
2087 ubuf += CD_FRAMESIZE_RAW * nr;
2088 nframes -= nr;
2089 lba += nr;
2091 out:
2092 kfree(cgc.buffer);
2093 return ret;
2096 static int cdrom_read_cdda_bpc(struct cdrom_device_info *cdi, __u8 __user *ubuf,
2097 int lba, int nframes)
2099 struct request_queue *q = cdi->disk->queue;
2100 struct request *rq;
2101 struct bio *bio;
2102 unsigned int len;
2103 int nr, ret = 0;
2105 if (!q)
2106 return -ENXIO;
2108 rq = blk_get_request(q, READ, GFP_KERNEL);
2109 if (!rq)
2110 return -ENOMEM;
2112 cdi->last_sense = 0;
2114 while (nframes) {
2115 nr = nframes;
2116 if (cdi->cdda_method == CDDA_BPC_SINGLE)
2117 nr = 1;
2118 if (nr * CD_FRAMESIZE_RAW > (q->max_sectors << 9))
2119 nr = (q->max_sectors << 9) / CD_FRAMESIZE_RAW;
2121 len = nr * CD_FRAMESIZE_RAW;
2123 ret = blk_rq_map_user(q, rq, ubuf, len);
2124 if (ret)
2125 break;
2127 memset(rq->cmd, 0, sizeof(rq->cmd));
2128 rq->cmd[0] = GPCMD_READ_CD;
2129 rq->cmd[1] = 1 << 2;
2130 rq->cmd[2] = (lba >> 24) & 0xff;
2131 rq->cmd[3] = (lba >> 16) & 0xff;
2132 rq->cmd[4] = (lba >> 8) & 0xff;
2133 rq->cmd[5] = lba & 0xff;
2134 rq->cmd[6] = (nr >> 16) & 0xff;
2135 rq->cmd[7] = (nr >> 8) & 0xff;
2136 rq->cmd[8] = nr & 0xff;
2137 rq->cmd[9] = 0xf8;
2139 rq->cmd_len = 12;
2140 rq->cmd_type = REQ_TYPE_BLOCK_PC;
2141 rq->timeout = 60 * HZ;
2142 bio = rq->bio;
2144 if (blk_execute_rq(q, cdi->disk, rq, 0)) {
2145 struct request_sense *s = rq->sense;
2146 ret = -EIO;
2147 cdi->last_sense = s->sense_key;
2150 if (blk_rq_unmap_user(bio))
2151 ret = -EFAULT;
2153 if (ret)
2154 break;
2156 nframes -= nr;
2157 lba += nr;
2158 ubuf += len;
2161 blk_put_request(rq);
2162 return ret;
2165 static int cdrom_read_cdda(struct cdrom_device_info *cdi, __u8 __user *ubuf,
2166 int lba, int nframes)
2168 int ret;
2170 if (cdi->cdda_method == CDDA_OLD)
2171 return cdrom_read_cdda_old(cdi, ubuf, lba, nframes);
2173 retry:
2175 * for anything else than success and io error, we need to retry
2177 ret = cdrom_read_cdda_bpc(cdi, ubuf, lba, nframes);
2178 if (!ret || ret != -EIO)
2179 return ret;
2182 * I've seen drives get sense 4/8/3 udma crc errors on multi
2183 * frame dma, so drop to single frame dma if we need to
2185 if (cdi->cdda_method == CDDA_BPC_FULL && nframes > 1) {
2186 printk("cdrom: dropping to single frame dma\n");
2187 cdi->cdda_method = CDDA_BPC_SINGLE;
2188 goto retry;
2192 * so we have an io error of some sort with multi frame dma. if the
2193 * condition wasn't a hardware error
2194 * problems, not for any error
2196 if (cdi->last_sense != 0x04 && cdi->last_sense != 0x0b)
2197 return ret;
2199 printk("cdrom: dropping to old style cdda (sense=%x)\n", cdi->last_sense);
2200 cdi->cdda_method = CDDA_OLD;
2201 return cdrom_read_cdda_old(cdi, ubuf, lba, nframes);
2204 static int cdrom_ioctl_multisession(struct cdrom_device_info *cdi,
2205 void __user *argp)
2207 struct cdrom_multisession ms_info;
2208 u8 requested_format;
2209 int ret;
2211 cdinfo(CD_DO_IOCTL, "entering CDROMMULTISESSION\n");
2213 if (!(cdi->ops->capability & CDC_MULTI_SESSION))
2214 return -ENOSYS;
2216 if (copy_from_user(&ms_info, argp, sizeof(ms_info)))
2217 return -EFAULT;
2219 requested_format = ms_info.addr_format;
2220 if (requested_format != CDROM_MSF && requested_format != CDROM_LBA)
2221 return -EINVAL;
2222 ms_info.addr_format = CDROM_LBA;
2224 ret = cdi->ops->get_last_session(cdi, &ms_info);
2225 if (ret)
2226 return ret;
2228 sanitize_format(&ms_info.addr, &ms_info.addr_format, requested_format);
2230 if (copy_to_user(argp, &ms_info, sizeof(ms_info)))
2231 return -EFAULT;
2233 cdinfo(CD_DO_IOCTL, "CDROMMULTISESSION successful\n");
2234 return 0;
2237 static int cdrom_ioctl_eject(struct cdrom_device_info *cdi)
2239 cdinfo(CD_DO_IOCTL, "entering CDROMEJECT\n");
2241 if (!CDROM_CAN(CDC_OPEN_TRAY))
2242 return -ENOSYS;
2243 if (cdi->use_count != 1 || keeplocked)
2244 return -EBUSY;
2245 if (CDROM_CAN(CDC_LOCK)) {
2246 int ret = cdi->ops->lock_door(cdi, 0);
2247 if (ret)
2248 return ret;
2251 return cdi->ops->tray_move(cdi, 1);
2254 static int cdrom_ioctl_closetray(struct cdrom_device_info *cdi)
2256 cdinfo(CD_DO_IOCTL, "entering CDROMCLOSETRAY\n");
2258 if (!CDROM_CAN(CDC_CLOSE_TRAY))
2259 return -ENOSYS;
2260 return cdi->ops->tray_move(cdi, 0);
2263 static int cdrom_ioctl_eject_sw(struct cdrom_device_info *cdi,
2264 unsigned long arg)
2266 cdinfo(CD_DO_IOCTL, "entering CDROMEJECT_SW\n");
2268 if (!CDROM_CAN(CDC_OPEN_TRAY))
2269 return -ENOSYS;
2270 if (keeplocked)
2271 return -EBUSY;
2273 cdi->options &= ~(CDO_AUTO_CLOSE | CDO_AUTO_EJECT);
2274 if (arg)
2275 cdi->options |= CDO_AUTO_CLOSE | CDO_AUTO_EJECT;
2276 return 0;
2279 static int cdrom_ioctl_media_changed(struct cdrom_device_info *cdi,
2280 unsigned long arg)
2282 struct cdrom_changer_info *info;
2283 int ret;
2285 cdinfo(CD_DO_IOCTL, "entering CDROM_MEDIA_CHANGED\n");
2287 if (!CDROM_CAN(CDC_MEDIA_CHANGED))
2288 return -ENOSYS;
2290 /* cannot select disc or select current disc */
2291 if (!CDROM_CAN(CDC_SELECT_DISC) || arg == CDSL_CURRENT)
2292 return media_changed(cdi, 1);
2294 if ((unsigned int)arg >= cdi->capacity)
2295 return -EINVAL;
2297 info = kmalloc(sizeof(*info), GFP_KERNEL);
2298 if (!info)
2299 return -ENOMEM;
2301 ret = cdrom_read_mech_status(cdi, info);
2302 if (!ret)
2303 ret = info->slots[arg].change;
2304 kfree(info);
2305 return ret;
2308 static int cdrom_ioctl_set_options(struct cdrom_device_info *cdi,
2309 unsigned long arg)
2311 cdinfo(CD_DO_IOCTL, "entering CDROM_SET_OPTIONS\n");
2314 * Options need to be in sync with capability.
2315 * Too late for that, so we have to check each one separately.
2317 switch (arg) {
2318 case CDO_USE_FFLAGS:
2319 case CDO_CHECK_TYPE:
2320 break;
2321 case CDO_LOCK:
2322 if (!CDROM_CAN(CDC_LOCK))
2323 return -ENOSYS;
2324 break;
2325 case 0:
2326 return cdi->options;
2327 /* default is basically CDO_[AUTO_CLOSE|AUTO_EJECT] */
2328 default:
2329 if (!CDROM_CAN(arg))
2330 return -ENOSYS;
2332 cdi->options |= (int) arg;
2333 return cdi->options;
2336 static int cdrom_ioctl_clear_options(struct cdrom_device_info *cdi,
2337 unsigned long arg)
2339 cdinfo(CD_DO_IOCTL, "entering CDROM_CLEAR_OPTIONS\n");
2341 cdi->options &= ~(int) arg;
2342 return cdi->options;
2345 static int cdrom_ioctl_select_speed(struct cdrom_device_info *cdi,
2346 unsigned long arg)
2348 cdinfo(CD_DO_IOCTL, "entering CDROM_SELECT_SPEED\n");
2350 if (!CDROM_CAN(CDC_SELECT_SPEED))
2351 return -ENOSYS;
2352 return cdi->ops->select_speed(cdi, arg);
2355 static int cdrom_ioctl_select_disc(struct cdrom_device_info *cdi,
2356 unsigned long arg)
2358 cdinfo(CD_DO_IOCTL, "entering CDROM_SELECT_DISC\n");
2360 if (!CDROM_CAN(CDC_SELECT_DISC))
2361 return -ENOSYS;
2363 if (arg != CDSL_CURRENT && arg != CDSL_NONE) {
2364 if ((int)arg >= cdi->capacity)
2365 return -EINVAL;
2369 * ->select_disc is a hook to allow a driver-specific way of
2370 * seleting disc. However, since there is no equivalent hook for
2371 * cdrom_slot_status this may not actually be useful...
2373 if (cdi->ops->select_disc)
2374 return cdi->ops->select_disc(cdi, arg);
2376 cdinfo(CD_CHANGER, "Using generic cdrom_select_disc()\n");
2377 return cdrom_select_disc(cdi, arg);
2380 static int cdrom_ioctl_reset(struct cdrom_device_info *cdi,
2381 struct block_device *bdev)
2383 cdinfo(CD_DO_IOCTL, "entering CDROM_RESET\n");
2385 if (!capable(CAP_SYS_ADMIN))
2386 return -EACCES;
2387 if (!CDROM_CAN(CDC_RESET))
2388 return -ENOSYS;
2389 invalidate_bdev(bdev);
2390 return cdi->ops->reset(cdi);
2393 static int cdrom_ioctl_lock_door(struct cdrom_device_info *cdi,
2394 unsigned long arg)
2396 cdinfo(CD_DO_IOCTL, "%socking door.\n", arg ? "L" : "Unl");
2398 if (!CDROM_CAN(CDC_LOCK))
2399 return -EDRIVE_CANT_DO_THIS;
2401 keeplocked = arg ? 1 : 0;
2404 * Don't unlock the door on multiple opens by default, but allow
2405 * root to do so.
2407 if (cdi->use_count != 1 && !arg && !capable(CAP_SYS_ADMIN))
2408 return -EBUSY;
2409 return cdi->ops->lock_door(cdi, arg);
2412 static int cdrom_ioctl_debug(struct cdrom_device_info *cdi,
2413 unsigned long arg)
2415 cdinfo(CD_DO_IOCTL, "%sabling debug.\n", arg ? "En" : "Dis");
2417 if (!capable(CAP_SYS_ADMIN))
2418 return -EACCES;
2419 debug = arg ? 1 : 0;
2420 return debug;
2423 static int cdrom_ioctl_get_capability(struct cdrom_device_info *cdi)
2425 cdinfo(CD_DO_IOCTL, "entering CDROM_GET_CAPABILITY\n");
2426 return (cdi->ops->capability & ~cdi->mask);
2430 * The following function is implemented, although very few audio
2431 * discs give Universal Product Code information, which should just be
2432 * the Medium Catalog Number on the box. Note, that the way the code
2433 * is written on the CD is /not/ uniform across all discs!
2435 static int cdrom_ioctl_get_mcn(struct cdrom_device_info *cdi,
2436 void __user *argp)
2438 struct cdrom_mcn mcn;
2439 int ret;
2441 cdinfo(CD_DO_IOCTL, "entering CDROM_GET_MCN\n");
2443 if (!(cdi->ops->capability & CDC_MCN))
2444 return -ENOSYS;
2445 ret = cdi->ops->get_mcn(cdi, &mcn);
2446 if (ret)
2447 return ret;
2449 if (copy_to_user(argp, &mcn, sizeof(mcn)))
2450 return -EFAULT;
2451 cdinfo(CD_DO_IOCTL, "CDROM_GET_MCN successful\n");
2452 return 0;
2455 static int cdrom_ioctl_drive_status(struct cdrom_device_info *cdi,
2456 unsigned long arg)
2458 cdinfo(CD_DO_IOCTL, "entering CDROM_DRIVE_STATUS\n");
2460 if (!(cdi->ops->capability & CDC_DRIVE_STATUS))
2461 return -ENOSYS;
2462 if (!CDROM_CAN(CDC_SELECT_DISC) ||
2463 (arg == CDSL_CURRENT || arg == CDSL_NONE))
2464 return cdi->ops->drive_status(cdi, CDSL_CURRENT);
2465 if (((int)arg >= cdi->capacity))
2466 return -EINVAL;
2467 return cdrom_slot_status(cdi, arg);
2471 * Ok, this is where problems start. The current interface for the
2472 * CDROM_DISC_STATUS ioctl is flawed. It makes the false assumption that
2473 * CDs are all CDS_DATA_1 or all CDS_AUDIO, etc. Unfortunatly, while this
2474 * is often the case, it is also very common for CDs to have some tracks
2475 * with data, and some tracks with audio. Just because I feel like it,
2476 * I declare the following to be the best way to cope. If the CD has ANY
2477 * data tracks on it, it will be returned as a data CD. If it has any XA
2478 * tracks, I will return it as that. Now I could simplify this interface
2479 * by combining these returns with the above, but this more clearly
2480 * demonstrates the problem with the current interface. Too bad this
2481 * wasn't designed to use bitmasks... -Erik
2483 * Well, now we have the option CDS_MIXED: a mixed-type CD.
2484 * User level programmers might feel the ioctl is not very useful.
2485 * ---david
2487 static int cdrom_ioctl_disc_status(struct cdrom_device_info *cdi)
2489 tracktype tracks;
2491 cdinfo(CD_DO_IOCTL, "entering CDROM_DISC_STATUS\n");
2493 cdrom_count_tracks(cdi, &tracks);
2494 if (tracks.error)
2495 return tracks.error;
2497 /* Policy mode on */
2498 if (tracks.audio > 0) {
2499 if (!tracks.data && !tracks.cdi && !tracks.xa)
2500 return CDS_AUDIO;
2501 else
2502 return CDS_MIXED;
2505 if (tracks.cdi > 0)
2506 return CDS_XA_2_2;
2507 if (tracks.xa > 0)
2508 return CDS_XA_2_1;
2509 if (tracks.data > 0)
2510 return CDS_DATA_1;
2511 /* Policy mode off */
2513 cdinfo(CD_WARNING,"This disc doesn't have any tracks I recognize!\n");
2514 return CDS_NO_INFO;
2517 static int cdrom_ioctl_changer_nslots(struct cdrom_device_info *cdi)
2519 cdinfo(CD_DO_IOCTL, "entering CDROM_CHANGER_NSLOTS\n");
2520 return cdi->capacity;
2523 static int cdrom_ioctl_get_subchnl(struct cdrom_device_info *cdi,
2524 void __user *argp)
2526 struct cdrom_subchnl q;
2527 u8 requested, back;
2528 int ret;
2530 /* cdinfo(CD_DO_IOCTL,"entering CDROMSUBCHNL\n");*/
2532 if (!CDROM_CAN(CDC_PLAY_AUDIO))
2533 return -ENOSYS;
2534 if (copy_from_user(&q, argp, sizeof(q)))
2535 return -EFAULT;
2537 requested = q.cdsc_format;
2538 if (requested != CDROM_MSF && requested != CDROM_LBA)
2539 return -EINVAL;
2540 q.cdsc_format = CDROM_MSF;
2542 ret = cdi->ops->audio_ioctl(cdi, CDROMSUBCHNL, &q);
2543 if (ret)
2544 return ret;
2546 back = q.cdsc_format; /* local copy */
2547 sanitize_format(&q.cdsc_absaddr, &back, requested);
2548 sanitize_format(&q.cdsc_reladdr, &q.cdsc_format, requested);
2550 if (copy_to_user(argp, &q, sizeof(q)))
2551 return -EFAULT;
2552 /* cdinfo(CD_DO_IOCTL, "CDROMSUBCHNL successful\n"); */
2553 return 0;
2556 static int cdrom_ioctl_read_tochdr(struct cdrom_device_info *cdi,
2557 void __user *argp)
2559 struct cdrom_tochdr header;
2560 int ret;
2562 /* cdinfo(CD_DO_IOCTL, "entering CDROMREADTOCHDR\n"); */
2564 if (!CDROM_CAN(CDC_PLAY_AUDIO))
2565 return -ENOSYS;
2566 if (copy_from_user(&header, argp, sizeof(header)))
2567 return -EFAULT;
2569 ret = cdi->ops->audio_ioctl(cdi, CDROMREADTOCHDR, &header);
2570 if (ret)
2571 return ret;
2573 if (copy_to_user(argp, &header, sizeof(header)))
2574 return -EFAULT;
2575 /* cdinfo(CD_DO_IOCTL, "CDROMREADTOCHDR successful\n"); */
2576 return 0;
2579 static int cdrom_ioctl_read_tocentry(struct cdrom_device_info *cdi,
2580 void __user *argp)
2582 struct cdrom_tocentry entry;
2583 u8 requested_format;
2584 int ret;
2586 /* cdinfo(CD_DO_IOCTL, "entering CDROMREADTOCENTRY\n"); */
2588 if (!CDROM_CAN(CDC_PLAY_AUDIO))
2589 return -ENOSYS;
2590 if (copy_from_user(&entry, argp, sizeof(entry)))
2591 return -EFAULT;
2593 requested_format = entry.cdte_format;
2594 if (requested_format != CDROM_MSF && requested_format != CDROM_LBA)
2595 return -EINVAL;
2596 /* make interface to low-level uniform */
2597 entry.cdte_format = CDROM_MSF;
2598 ret = cdi->ops->audio_ioctl(cdi, CDROMREADTOCENTRY, &entry);
2599 if (ret)
2600 return ret;
2601 sanitize_format(&entry.cdte_addr, &entry.cdte_format, requested_format);
2603 if (copy_to_user(argp, &entry, sizeof(entry)))
2604 return -EFAULT;
2605 /* cdinfo(CD_DO_IOCTL, "CDROMREADTOCENTRY successful\n"); */
2606 return 0;
2609 static int cdrom_ioctl_play_msf(struct cdrom_device_info *cdi,
2610 void __user *argp)
2612 struct cdrom_msf msf;
2614 cdinfo(CD_DO_IOCTL, "entering CDROMPLAYMSF\n");
2616 if (!CDROM_CAN(CDC_PLAY_AUDIO))
2617 return -ENOSYS;
2618 if (copy_from_user(&msf, argp, sizeof(msf)))
2619 return -EFAULT;
2620 return cdi->ops->audio_ioctl(cdi, CDROMPLAYMSF, &msf);
2623 static int cdrom_ioctl_play_trkind(struct cdrom_device_info *cdi,
2624 void __user *argp)
2626 struct cdrom_ti ti;
2627 int ret;
2629 cdinfo(CD_DO_IOCTL, "entering CDROMPLAYTRKIND\n");
2631 if (!CDROM_CAN(CDC_PLAY_AUDIO))
2632 return -ENOSYS;
2633 if (copy_from_user(&ti, argp, sizeof(ti)))
2634 return -EFAULT;
2636 ret = check_for_audio_disc(cdi, cdi->ops);
2637 if (ret)
2638 return ret;
2639 return cdi->ops->audio_ioctl(cdi, CDROMPLAYTRKIND, &ti);
2641 static int cdrom_ioctl_volctrl(struct cdrom_device_info *cdi,
2642 void __user *argp)
2644 struct cdrom_volctrl volume;
2646 cdinfo(CD_DO_IOCTL, "entering CDROMVOLCTRL\n");
2648 if (!CDROM_CAN(CDC_PLAY_AUDIO))
2649 return -ENOSYS;
2650 if (copy_from_user(&volume, argp, sizeof(volume)))
2651 return -EFAULT;
2652 return cdi->ops->audio_ioctl(cdi, CDROMVOLCTRL, &volume);
2655 static int cdrom_ioctl_volread(struct cdrom_device_info *cdi,
2656 void __user *argp)
2658 struct cdrom_volctrl volume;
2659 int ret;
2661 cdinfo(CD_DO_IOCTL, "entering CDROMVOLREAD\n");
2663 if (!CDROM_CAN(CDC_PLAY_AUDIO))
2664 return -ENOSYS;
2666 ret = cdi->ops->audio_ioctl(cdi, CDROMVOLREAD, &volume);
2667 if (ret)
2668 return ret;
2670 if (copy_to_user(argp, &volume, sizeof(volume)))
2671 return -EFAULT;
2672 return 0;
2675 static int cdrom_ioctl_audioctl(struct cdrom_device_info *cdi,
2676 unsigned int cmd)
2678 int ret;
2680 cdinfo(CD_DO_IOCTL, "doing audio ioctl (start/stop/pause/resume)\n");
2682 if (!CDROM_CAN(CDC_PLAY_AUDIO))
2683 return -ENOSYS;
2684 ret = check_for_audio_disc(cdi, cdi->ops);
2685 if (ret)
2686 return ret;
2687 return cdi->ops->audio_ioctl(cdi, cmd, NULL);
2691 * Just about every imaginable ioctl is supported in the Uniform layer
2692 * these days.
2693 * ATAPI / SCSI specific code now mainly resides in mmc_ioctl().
2695 int cdrom_ioctl(struct file * file, struct cdrom_device_info *cdi,
2696 struct inode *ip, unsigned int cmd, unsigned long arg)
2698 void __user *argp = (void __user *)arg;
2699 int ret;
2700 struct gendisk *disk = ip->i_bdev->bd_disk;
2703 * Try the generic SCSI command ioctl's first.
2705 ret = scsi_cmd_ioctl(file, disk->queue, disk, cmd, argp);
2706 if (ret != -ENOTTY)
2707 return ret;
2709 switch (cmd) {
2710 case CDROMMULTISESSION:
2711 return cdrom_ioctl_multisession(cdi, argp);
2712 case CDROMEJECT:
2713 return cdrom_ioctl_eject(cdi);
2714 case CDROMCLOSETRAY:
2715 return cdrom_ioctl_closetray(cdi);
2716 case CDROMEJECT_SW:
2717 return cdrom_ioctl_eject_sw(cdi, arg);
2718 case CDROM_MEDIA_CHANGED:
2719 return cdrom_ioctl_media_changed(cdi, arg);
2720 case CDROM_SET_OPTIONS:
2721 return cdrom_ioctl_set_options(cdi, arg);
2722 case CDROM_CLEAR_OPTIONS:
2723 return cdrom_ioctl_clear_options(cdi, arg);
2724 case CDROM_SELECT_SPEED:
2725 return cdrom_ioctl_select_speed(cdi, arg);
2726 case CDROM_SELECT_DISC:
2727 return cdrom_ioctl_select_disc(cdi, arg);
2728 case CDROMRESET:
2729 return cdrom_ioctl_reset(cdi, ip->i_bdev);
2730 case CDROM_LOCKDOOR:
2731 return cdrom_ioctl_lock_door(cdi, arg);
2732 case CDROM_DEBUG:
2733 return cdrom_ioctl_debug(cdi, arg);
2734 case CDROM_GET_CAPABILITY:
2735 return cdrom_ioctl_get_capability(cdi);
2736 case CDROM_GET_MCN:
2737 return cdrom_ioctl_get_mcn(cdi, argp);
2738 case CDROM_DRIVE_STATUS:
2739 return cdrom_ioctl_drive_status(cdi, arg);
2740 case CDROM_DISC_STATUS:
2741 return cdrom_ioctl_disc_status(cdi);
2742 case CDROM_CHANGER_NSLOTS:
2743 return cdrom_ioctl_changer_nslots(cdi);
2747 * Use the ioctls that are implemented through the generic_packet()
2748 * interface. this may look at bit funny, but if -ENOTTY is
2749 * returned that particular ioctl is not implemented and we
2750 * let it go through the device specific ones.
2752 if (CDROM_CAN(CDC_GENERIC_PACKET)) {
2753 ret = mmc_ioctl(cdi, cmd, arg);
2754 if (ret != -ENOTTY)
2755 return ret;
2759 * Note: most of the cdinfo() calls are commented out here,
2760 * because they fill up the sys log when CD players poll
2761 * the drive.
2763 switch (cmd) {
2764 case CDROMSUBCHNL:
2765 return cdrom_ioctl_get_subchnl(cdi, argp);
2766 case CDROMREADTOCHDR:
2767 return cdrom_ioctl_read_tochdr(cdi, argp);
2768 case CDROMREADTOCENTRY:
2769 return cdrom_ioctl_read_tocentry(cdi, argp);
2770 case CDROMPLAYMSF:
2771 return cdrom_ioctl_play_msf(cdi, argp);
2772 case CDROMPLAYTRKIND:
2773 return cdrom_ioctl_play_trkind(cdi, argp);
2774 case CDROMVOLCTRL:
2775 return cdrom_ioctl_volctrl(cdi, argp);
2776 case CDROMVOLREAD:
2777 return cdrom_ioctl_volread(cdi, argp);
2778 case CDROMSTART:
2779 case CDROMSTOP:
2780 case CDROMPAUSE:
2781 case CDROMRESUME:
2782 return cdrom_ioctl_audioctl(cdi, cmd);
2785 return -ENOSYS;
2789 * Required when we need to use READ_10 to issue other than 2048 block
2790 * reads
2792 static int cdrom_switch_blocksize(struct cdrom_device_info *cdi, int size)
2794 struct cdrom_device_ops *cdo = cdi->ops;
2795 struct packet_command cgc;
2796 struct modesel_head mh;
2798 memset(&mh, 0, sizeof(mh));
2799 mh.block_desc_length = 0x08;
2800 mh.block_length_med = (size >> 8) & 0xff;
2801 mh.block_length_lo = size & 0xff;
2803 memset(&cgc, 0, sizeof(cgc));
2804 cgc.cmd[0] = 0x15;
2805 cgc.cmd[1] = 1 << 4;
2806 cgc.cmd[4] = 12;
2807 cgc.buflen = sizeof(mh);
2808 cgc.buffer = (char *) &mh;
2809 cgc.data_direction = CGC_DATA_WRITE;
2810 mh.block_desc_length = 0x08;
2811 mh.block_length_med = (size >> 8) & 0xff;
2812 mh.block_length_lo = size & 0xff;
2814 return cdo->generic_packet(cdi, &cgc);
2817 static int mmc_ioctl(struct cdrom_device_info *cdi, unsigned int cmd,
2818 unsigned long arg)
2820 struct cdrom_device_ops *cdo = cdi->ops;
2821 struct packet_command cgc;
2822 struct request_sense sense;
2823 unsigned char buffer[32];
2824 int ret = 0;
2826 memset(&cgc, 0, sizeof(cgc));
2828 /* build a unified command and queue it through
2829 cdo->generic_packet() */
2830 switch (cmd) {
2831 case CDROMREADRAW:
2832 case CDROMREADMODE1:
2833 case CDROMREADMODE2: {
2834 struct cdrom_msf msf;
2835 int blocksize = 0, format = 0, lba;
2837 switch (cmd) {
2838 case CDROMREADRAW:
2839 blocksize = CD_FRAMESIZE_RAW;
2840 break;
2841 case CDROMREADMODE1:
2842 blocksize = CD_FRAMESIZE;
2843 format = 2;
2844 break;
2845 case CDROMREADMODE2:
2846 blocksize = CD_FRAMESIZE_RAW0;
2847 break;
2849 IOCTL_IN(arg, struct cdrom_msf, msf);
2850 lba = msf_to_lba(msf.cdmsf_min0,msf.cdmsf_sec0,msf.cdmsf_frame0);
2851 /* FIXME: we need upper bound checking, too!! */
2852 if (lba < 0)
2853 return -EINVAL;
2854 cgc.buffer = kmalloc(blocksize, GFP_KERNEL);
2855 if (cgc.buffer == NULL)
2856 return -ENOMEM;
2857 memset(&sense, 0, sizeof(sense));
2858 cgc.sense = &sense;
2859 cgc.data_direction = CGC_DATA_READ;
2860 ret = cdrom_read_block(cdi, &cgc, lba, 1, format, blocksize);
2861 if (ret && sense.sense_key==0x05 && sense.asc==0x20 && sense.ascq==0x00) {
2863 * SCSI-II devices are not required to support
2864 * READ_CD, so let's try switching block size
2866 /* FIXME: switch back again... */
2867 if ((ret = cdrom_switch_blocksize(cdi, blocksize))) {
2868 kfree(cgc.buffer);
2869 return ret;
2871 cgc.sense = NULL;
2872 ret = cdrom_read_cd(cdi, &cgc, lba, blocksize, 1);
2873 ret |= cdrom_switch_blocksize(cdi, blocksize);
2875 if (!ret && copy_to_user((char __user *)arg, cgc.buffer, blocksize))
2876 ret = -EFAULT;
2877 kfree(cgc.buffer);
2878 return ret;
2880 case CDROMREADAUDIO: {
2881 struct cdrom_read_audio ra;
2882 int lba;
2884 IOCTL_IN(arg, struct cdrom_read_audio, ra);
2886 if (ra.addr_format == CDROM_MSF)
2887 lba = msf_to_lba(ra.addr.msf.minute,
2888 ra.addr.msf.second,
2889 ra.addr.msf.frame);
2890 else if (ra.addr_format == CDROM_LBA)
2891 lba = ra.addr.lba;
2892 else
2893 return -EINVAL;
2895 /* FIXME: we need upper bound checking, too!! */
2896 if (lba < 0 || ra.nframes <= 0 || ra.nframes > CD_FRAMES)
2897 return -EINVAL;
2899 return cdrom_read_cdda(cdi, ra.buf, lba, ra.nframes);
2901 case CDROMSUBCHNL: {
2902 struct cdrom_subchnl q;
2903 u_char requested, back;
2904 IOCTL_IN(arg, struct cdrom_subchnl, q);
2905 requested = q.cdsc_format;
2906 if (!((requested == CDROM_MSF) ||
2907 (requested == CDROM_LBA)))
2908 return -EINVAL;
2909 q.cdsc_format = CDROM_MSF;
2910 if ((ret = cdrom_read_subchannel(cdi, &q, 0)))
2911 return ret;
2912 back = q.cdsc_format; /* local copy */
2913 sanitize_format(&q.cdsc_absaddr, &back, requested);
2914 sanitize_format(&q.cdsc_reladdr, &q.cdsc_format, requested);
2915 IOCTL_OUT(arg, struct cdrom_subchnl, q);
2916 /* cdinfo(CD_DO_IOCTL, "CDROMSUBCHNL successful\n"); */
2917 return 0;
2919 case CDROMPLAYMSF: {
2920 struct cdrom_msf msf;
2921 cdinfo(CD_DO_IOCTL, "entering CDROMPLAYMSF\n");
2922 IOCTL_IN(arg, struct cdrom_msf, msf);
2923 cgc.cmd[0] = GPCMD_PLAY_AUDIO_MSF;
2924 cgc.cmd[3] = msf.cdmsf_min0;
2925 cgc.cmd[4] = msf.cdmsf_sec0;
2926 cgc.cmd[5] = msf.cdmsf_frame0;
2927 cgc.cmd[6] = msf.cdmsf_min1;
2928 cgc.cmd[7] = msf.cdmsf_sec1;
2929 cgc.cmd[8] = msf.cdmsf_frame1;
2930 cgc.data_direction = CGC_DATA_NONE;
2931 return cdo->generic_packet(cdi, &cgc);
2933 case CDROMPLAYBLK: {
2934 struct cdrom_blk blk;
2935 cdinfo(CD_DO_IOCTL, "entering CDROMPLAYBLK\n");
2936 IOCTL_IN(arg, struct cdrom_blk, blk);
2937 cgc.cmd[0] = GPCMD_PLAY_AUDIO_10;
2938 cgc.cmd[2] = (blk.from >> 24) & 0xff;
2939 cgc.cmd[3] = (blk.from >> 16) & 0xff;
2940 cgc.cmd[4] = (blk.from >> 8) & 0xff;
2941 cgc.cmd[5] = blk.from & 0xff;
2942 cgc.cmd[7] = (blk.len >> 8) & 0xff;
2943 cgc.cmd[8] = blk.len & 0xff;
2944 cgc.data_direction = CGC_DATA_NONE;
2945 return cdo->generic_packet(cdi, &cgc);
2947 case CDROMVOLCTRL:
2948 case CDROMVOLREAD: {
2949 struct cdrom_volctrl volctrl;
2950 char mask[sizeof(buffer)];
2951 unsigned short offset;
2953 cdinfo(CD_DO_IOCTL, "entering CDROMVOLUME\n");
2955 IOCTL_IN(arg, struct cdrom_volctrl, volctrl);
2957 cgc.buffer = buffer;
2958 cgc.buflen = 24;
2959 if ((ret = cdrom_mode_sense(cdi, &cgc, GPMODE_AUDIO_CTL_PAGE, 0)))
2960 return ret;
2962 /* originally the code depended on buffer[1] to determine
2963 how much data is available for transfer. buffer[1] is
2964 unfortunately ambigious and the only reliable way seem
2965 to be to simply skip over the block descriptor... */
2966 offset = 8 + be16_to_cpu(*(__be16 *)(buffer+6));
2968 if (offset + 16 > sizeof(buffer))
2969 return -E2BIG;
2971 if (offset + 16 > cgc.buflen) {
2972 cgc.buflen = offset+16;
2973 ret = cdrom_mode_sense(cdi, &cgc,
2974 GPMODE_AUDIO_CTL_PAGE, 0);
2975 if (ret)
2976 return ret;
2979 /* sanity check */
2980 if ((buffer[offset] & 0x3f) != GPMODE_AUDIO_CTL_PAGE ||
2981 buffer[offset+1] < 14)
2982 return -EINVAL;
2984 /* now we have the current volume settings. if it was only
2985 a CDROMVOLREAD, return these values */
2986 if (cmd == CDROMVOLREAD) {
2987 volctrl.channel0 = buffer[offset+9];
2988 volctrl.channel1 = buffer[offset+11];
2989 volctrl.channel2 = buffer[offset+13];
2990 volctrl.channel3 = buffer[offset+15];
2991 IOCTL_OUT(arg, struct cdrom_volctrl, volctrl);
2992 return 0;
2995 /* get the volume mask */
2996 cgc.buffer = mask;
2997 if ((ret = cdrom_mode_sense(cdi, &cgc,
2998 GPMODE_AUDIO_CTL_PAGE, 1)))
2999 return ret;
3001 buffer[offset+9] = volctrl.channel0 & mask[offset+9];
3002 buffer[offset+11] = volctrl.channel1 & mask[offset+11];
3003 buffer[offset+13] = volctrl.channel2 & mask[offset+13];
3004 buffer[offset+15] = volctrl.channel3 & mask[offset+15];
3006 /* set volume */
3007 cgc.buffer = buffer + offset - 8;
3008 memset(cgc.buffer, 0, 8);
3009 return cdrom_mode_select(cdi, &cgc);
3012 case CDROMSTART:
3013 case CDROMSTOP: {
3014 cdinfo(CD_DO_IOCTL, "entering CDROMSTART/CDROMSTOP\n");
3015 cgc.cmd[0] = GPCMD_START_STOP_UNIT;
3016 cgc.cmd[1] = 1;
3017 cgc.cmd[4] = (cmd == CDROMSTART) ? 1 : 0;
3018 cgc.data_direction = CGC_DATA_NONE;
3019 return cdo->generic_packet(cdi, &cgc);
3022 case CDROMPAUSE:
3023 case CDROMRESUME: {
3024 cdinfo(CD_DO_IOCTL, "entering CDROMPAUSE/CDROMRESUME\n");
3025 cgc.cmd[0] = GPCMD_PAUSE_RESUME;
3026 cgc.cmd[8] = (cmd == CDROMRESUME) ? 1 : 0;
3027 cgc.data_direction = CGC_DATA_NONE;
3028 return cdo->generic_packet(cdi, &cgc);
3031 case DVD_READ_STRUCT: {
3032 dvd_struct *s;
3033 int size = sizeof(dvd_struct);
3034 if (!CDROM_CAN(CDC_DVD))
3035 return -ENOSYS;
3036 if ((s = kmalloc(size, GFP_KERNEL)) == NULL)
3037 return -ENOMEM;
3038 cdinfo(CD_DO_IOCTL, "entering DVD_READ_STRUCT\n");
3039 if (copy_from_user(s, (dvd_struct __user *)arg, size)) {
3040 kfree(s);
3041 return -EFAULT;
3043 if ((ret = dvd_read_struct(cdi, s))) {
3044 kfree(s);
3045 return ret;
3047 if (copy_to_user((dvd_struct __user *)arg, s, size))
3048 ret = -EFAULT;
3049 kfree(s);
3050 return ret;
3053 case DVD_AUTH: {
3054 dvd_authinfo ai;
3055 if (!CDROM_CAN(CDC_DVD))
3056 return -ENOSYS;
3057 cdinfo(CD_DO_IOCTL, "entering DVD_AUTH\n");
3058 IOCTL_IN(arg, dvd_authinfo, ai);
3059 if ((ret = dvd_do_auth (cdi, &ai)))
3060 return ret;
3061 IOCTL_OUT(arg, dvd_authinfo, ai);
3062 return 0;
3065 case CDROM_NEXT_WRITABLE: {
3066 long next = 0;
3067 cdinfo(CD_DO_IOCTL, "entering CDROM_NEXT_WRITABLE\n");
3068 if ((ret = cdrom_get_next_writable(cdi, &next)))
3069 return ret;
3070 IOCTL_OUT(arg, long, next);
3071 return 0;
3073 case CDROM_LAST_WRITTEN: {
3074 long last = 0;
3075 cdinfo(CD_DO_IOCTL, "entering CDROM_LAST_WRITTEN\n");
3076 if ((ret = cdrom_get_last_written(cdi, &last)))
3077 return ret;
3078 IOCTL_OUT(arg, long, last);
3079 return 0;
3081 } /* switch */
3083 return -ENOTTY;
3086 static int cdrom_get_track_info(struct cdrom_device_info *cdi, __u16 track, __u8 type,
3087 track_information *ti)
3089 struct cdrom_device_ops *cdo = cdi->ops;
3090 struct packet_command cgc;
3091 int ret, buflen;
3093 init_cdrom_command(&cgc, ti, 8, CGC_DATA_READ);
3094 cgc.cmd[0] = GPCMD_READ_TRACK_RZONE_INFO;
3095 cgc.cmd[1] = type & 3;
3096 cgc.cmd[4] = (track & 0xff00) >> 8;
3097 cgc.cmd[5] = track & 0xff;
3098 cgc.cmd[8] = 8;
3099 cgc.quiet = 1;
3101 if ((ret = cdo->generic_packet(cdi, &cgc)))
3102 return ret;
3104 buflen = be16_to_cpu(ti->track_information_length) +
3105 sizeof(ti->track_information_length);
3107 if (buflen > sizeof(track_information))
3108 buflen = sizeof(track_information);
3110 cgc.cmd[8] = cgc.buflen = buflen;
3111 if ((ret = cdo->generic_packet(cdi, &cgc)))
3112 return ret;
3114 /* return actual fill size */
3115 return buflen;
3118 /* requires CD R/RW */
3119 static int cdrom_get_disc_info(struct cdrom_device_info *cdi, disc_information *di)
3121 struct cdrom_device_ops *cdo = cdi->ops;
3122 struct packet_command cgc;
3123 int ret, buflen;
3125 /* set up command and get the disc info */
3126 init_cdrom_command(&cgc, di, sizeof(*di), CGC_DATA_READ);
3127 cgc.cmd[0] = GPCMD_READ_DISC_INFO;
3128 cgc.cmd[8] = cgc.buflen = 2;
3129 cgc.quiet = 1;
3131 if ((ret = cdo->generic_packet(cdi, &cgc)))
3132 return ret;
3134 /* not all drives have the same disc_info length, so requeue
3135 * packet with the length the drive tells us it can supply
3137 buflen = be16_to_cpu(di->disc_information_length) +
3138 sizeof(di->disc_information_length);
3140 if (buflen > sizeof(disc_information))
3141 buflen = sizeof(disc_information);
3143 cgc.cmd[8] = cgc.buflen = buflen;
3144 if ((ret = cdo->generic_packet(cdi, &cgc)))
3145 return ret;
3147 /* return actual fill size */
3148 return buflen;
3151 /* return the last written block on the CD-R media. this is for the udf
3152 file system. */
3153 int cdrom_get_last_written(struct cdrom_device_info *cdi, long *last_written)
3155 struct cdrom_tocentry toc;
3156 disc_information di;
3157 track_information ti;
3158 __u32 last_track;
3159 int ret = -1, ti_size;
3161 if (!CDROM_CAN(CDC_GENERIC_PACKET))
3162 goto use_toc;
3164 ret = cdrom_get_disc_info(cdi, &di);
3165 if (ret < (int)(offsetof(typeof(di), last_track_lsb)
3166 + sizeof(di.last_track_lsb)))
3167 goto use_toc;
3169 /* if unit didn't return msb, it's zeroed by cdrom_get_disc_info */
3170 last_track = (di.last_track_msb << 8) | di.last_track_lsb;
3171 ti_size = cdrom_get_track_info(cdi, last_track, 1, &ti);
3172 if (ti_size < (int)offsetof(typeof(ti), track_start))
3173 goto use_toc;
3175 /* if this track is blank, try the previous. */
3176 if (ti.blank) {
3177 if (last_track==1)
3178 goto use_toc;
3179 last_track--;
3180 ti_size = cdrom_get_track_info(cdi, last_track, 1, &ti);
3183 if (ti_size < (int)(offsetof(typeof(ti), track_size)
3184 + sizeof(ti.track_size)))
3185 goto use_toc;
3187 /* if last recorded field is valid, return it. */
3188 if (ti.lra_v && ti_size >= (int)(offsetof(typeof(ti), last_rec_address)
3189 + sizeof(ti.last_rec_address))) {
3190 *last_written = be32_to_cpu(ti.last_rec_address);
3191 } else {
3192 /* make it up instead */
3193 *last_written = be32_to_cpu(ti.track_start) +
3194 be32_to_cpu(ti.track_size);
3195 if (ti.free_blocks)
3196 *last_written -= (be32_to_cpu(ti.free_blocks) + 7);
3198 return 0;
3200 /* this is where we end up if the drive either can't do a
3201 GPCMD_READ_DISC_INFO or GPCMD_READ_TRACK_RZONE_INFO or if
3202 it doesn't give enough information or fails. then we return
3203 the toc contents. */
3204 use_toc:
3205 toc.cdte_format = CDROM_MSF;
3206 toc.cdte_track = CDROM_LEADOUT;
3207 if ((ret = cdi->ops->audio_ioctl(cdi, CDROMREADTOCENTRY, &toc)))
3208 return ret;
3209 sanitize_format(&toc.cdte_addr, &toc.cdte_format, CDROM_LBA);
3210 *last_written = toc.cdte_addr.lba;
3211 return 0;
3214 /* return the next writable block. also for udf file system. */
3215 static int cdrom_get_next_writable(struct cdrom_device_info *cdi, long *next_writable)
3217 disc_information di;
3218 track_information ti;
3219 __u16 last_track;
3220 int ret, ti_size;
3222 if (!CDROM_CAN(CDC_GENERIC_PACKET))
3223 goto use_last_written;
3225 ret = cdrom_get_disc_info(cdi, &di);
3226 if (ret < 0 || ret < offsetof(typeof(di), last_track_lsb)
3227 + sizeof(di.last_track_lsb))
3228 goto use_last_written;
3230 /* if unit didn't return msb, it's zeroed by cdrom_get_disc_info */
3231 last_track = (di.last_track_msb << 8) | di.last_track_lsb;
3232 ti_size = cdrom_get_track_info(cdi, last_track, 1, &ti);
3233 if (ti_size < 0 || ti_size < offsetof(typeof(ti), track_start))
3234 goto use_last_written;
3236 /* if this track is blank, try the previous. */
3237 if (ti.blank) {
3238 if (last_track == 1)
3239 goto use_last_written;
3240 last_track--;
3241 ti_size = cdrom_get_track_info(cdi, last_track, 1, &ti);
3242 if (ti_size < 0)
3243 goto use_last_written;
3246 /* if next recordable address field is valid, use it. */
3247 if (ti.nwa_v && ti_size >= offsetof(typeof(ti), next_writable)
3248 + sizeof(ti.next_writable)) {
3249 *next_writable = be32_to_cpu(ti.next_writable);
3250 return 0;
3253 use_last_written:
3254 if ((ret = cdrom_get_last_written(cdi, next_writable))) {
3255 *next_writable = 0;
3256 return ret;
3257 } else {
3258 *next_writable += 7;
3259 return 0;
3263 EXPORT_SYMBOL(cdrom_get_last_written);
3264 EXPORT_SYMBOL(register_cdrom);
3265 EXPORT_SYMBOL(unregister_cdrom);
3266 EXPORT_SYMBOL(cdrom_open);
3267 EXPORT_SYMBOL(cdrom_release);
3268 EXPORT_SYMBOL(cdrom_ioctl);
3269 EXPORT_SYMBOL(cdrom_media_changed);
3270 EXPORT_SYMBOL(cdrom_number_of_slots);
3271 EXPORT_SYMBOL(cdrom_mode_select);
3272 EXPORT_SYMBOL(cdrom_mode_sense);
3273 EXPORT_SYMBOL(init_cdrom_command);
3274 EXPORT_SYMBOL(cdrom_get_media_event);
3276 #ifdef CONFIG_SYSCTL
3278 #define CDROM_STR_SIZE 1000
3280 static struct cdrom_sysctl_settings {
3281 char info[CDROM_STR_SIZE]; /* general info */
3282 int autoclose; /* close tray upon mount, etc */
3283 int autoeject; /* eject on umount */
3284 int debug; /* turn on debugging messages */
3285 int lock; /* lock the door on device open */
3286 int check; /* check media type */
3287 } cdrom_sysctl_settings;
3289 enum cdrom_print_option {
3290 CTL_NAME,
3291 CTL_SPEED,
3292 CTL_SLOTS,
3293 CTL_CAPABILITY
3296 static int cdrom_print_info(const char *header, int val, char *info,
3297 int *pos, enum cdrom_print_option option)
3299 const int max_size = sizeof(cdrom_sysctl_settings.info);
3300 struct cdrom_device_info *cdi;
3301 int ret;
3303 ret = scnprintf(info + *pos, max_size - *pos, header);
3304 if (!ret)
3305 return 1;
3307 *pos += ret;
3309 for (cdi = topCdromPtr; cdi; cdi = cdi->next) {
3310 switch (option) {
3311 case CTL_NAME:
3312 ret = scnprintf(info + *pos, max_size - *pos,
3313 "\t%s", cdi->name);
3314 break;
3315 case CTL_SPEED:
3316 ret = scnprintf(info + *pos, max_size - *pos,
3317 "\t%d", cdi->speed);
3318 break;
3319 case CTL_SLOTS:
3320 ret = scnprintf(info + *pos, max_size - *pos,
3321 "\t%d", cdi->capacity);
3322 break;
3323 case CTL_CAPABILITY:
3324 ret = scnprintf(info + *pos, max_size - *pos,
3325 "\t%d", CDROM_CAN(val) != 0);
3326 break;
3327 default:
3328 printk(KERN_INFO "cdrom: invalid option%d\n", option);
3329 return 1;
3331 if (!ret)
3332 return 1;
3333 *pos += ret;
3336 return 0;
3339 static int cdrom_sysctl_info(ctl_table *ctl, int write, struct file * filp,
3340 void __user *buffer, size_t *lenp, loff_t *ppos)
3342 int pos;
3343 char *info = cdrom_sysctl_settings.info;
3344 const int max_size = sizeof(cdrom_sysctl_settings.info);
3346 if (!*lenp || (*ppos && !write)) {
3347 *lenp = 0;
3348 return 0;
3351 mutex_lock(&cdrom_mutex);
3353 pos = sprintf(info, "CD-ROM information, " VERSION "\n");
3355 if (cdrom_print_info("\ndrive name:\t", 0, info, &pos, CTL_NAME))
3356 goto done;
3357 if (cdrom_print_info("\ndrive speed:\t", 0, info, &pos, CTL_SPEED))
3358 goto done;
3359 if (cdrom_print_info("\ndrive # of slots:", 0, info, &pos, CTL_SLOTS))
3360 goto done;
3361 if (cdrom_print_info("\nCan close tray:\t",
3362 CDC_CLOSE_TRAY, info, &pos, CTL_CAPABILITY))
3363 goto done;
3364 if (cdrom_print_info("\nCan open tray:\t",
3365 CDC_OPEN_TRAY, info, &pos, CTL_CAPABILITY))
3366 goto done;
3367 if (cdrom_print_info("\nCan lock tray:\t",
3368 CDC_LOCK, info, &pos, CTL_CAPABILITY))
3369 goto done;
3370 if (cdrom_print_info("\nCan change speed:",
3371 CDC_SELECT_SPEED, info, &pos, CTL_CAPABILITY))
3372 goto done;
3373 if (cdrom_print_info("\nCan select disk:",
3374 CDC_SELECT_DISC, info, &pos, CTL_CAPABILITY))
3375 goto done;
3376 if (cdrom_print_info("\nCan read multisession:",
3377 CDC_MULTI_SESSION, info, &pos, CTL_CAPABILITY))
3378 goto done;
3379 if (cdrom_print_info("\nCan read MCN:\t",
3380 CDC_MCN, info, &pos, CTL_CAPABILITY))
3381 goto done;
3382 if (cdrom_print_info("\nReports media changed:",
3383 CDC_MEDIA_CHANGED, info, &pos, CTL_CAPABILITY))
3384 goto done;
3385 if (cdrom_print_info("\nCan play audio:\t",
3386 CDC_PLAY_AUDIO, info, &pos, CTL_CAPABILITY))
3387 goto done;
3388 if (cdrom_print_info("\nCan write CD-R:\t",
3389 CDC_CD_R, info, &pos, CTL_CAPABILITY))
3390 goto done;
3391 if (cdrom_print_info("\nCan write CD-RW:",
3392 CDC_CD_RW, info, &pos, CTL_CAPABILITY))
3393 goto done;
3394 if (cdrom_print_info("\nCan read DVD:\t",
3395 CDC_DVD, info, &pos, CTL_CAPABILITY))
3396 goto done;
3397 if (cdrom_print_info("\nCan write DVD-R:",
3398 CDC_DVD_R, info, &pos, CTL_CAPABILITY))
3399 goto done;
3400 if (cdrom_print_info("\nCan write DVD-RAM:",
3401 CDC_DVD_RAM, info, &pos, CTL_CAPABILITY))
3402 goto done;
3403 if (cdrom_print_info("\nCan read MRW:\t",
3404 CDC_MRW, info, &pos, CTL_CAPABILITY))
3405 goto done;
3406 if (cdrom_print_info("\nCan write MRW:\t",
3407 CDC_MRW_W, info, &pos, CTL_CAPABILITY))
3408 goto done;
3409 if (cdrom_print_info("\nCan write RAM:\t",
3410 CDC_RAM, info, &pos, CTL_CAPABILITY))
3411 goto done;
3412 if (!scnprintf(info + pos, max_size - pos, "\n\n"))
3413 goto done;
3414 doit:
3415 mutex_unlock(&cdrom_mutex);
3416 return proc_dostring(ctl, write, filp, buffer, lenp, ppos);
3417 done:
3418 printk(KERN_INFO "cdrom: info buffer too small\n");
3419 goto doit;
3422 /* Unfortunately, per device settings are not implemented through
3423 procfs/sysctl yet. When they are, this will naturally disappear. For now
3424 just update all drives. Later this will become the template on which
3425 new registered drives will be based. */
3426 static void cdrom_update_settings(void)
3428 struct cdrom_device_info *cdi;
3430 for (cdi = topCdromPtr; cdi != NULL; cdi = cdi->next) {
3431 if (autoclose && CDROM_CAN(CDC_CLOSE_TRAY))
3432 cdi->options |= CDO_AUTO_CLOSE;
3433 else if (!autoclose)
3434 cdi->options &= ~CDO_AUTO_CLOSE;
3435 if (autoeject && CDROM_CAN(CDC_OPEN_TRAY))
3436 cdi->options |= CDO_AUTO_EJECT;
3437 else if (!autoeject)
3438 cdi->options &= ~CDO_AUTO_EJECT;
3439 if (lockdoor && CDROM_CAN(CDC_LOCK))
3440 cdi->options |= CDO_LOCK;
3441 else if (!lockdoor)
3442 cdi->options &= ~CDO_LOCK;
3443 if (check_media_type)
3444 cdi->options |= CDO_CHECK_TYPE;
3445 else
3446 cdi->options &= ~CDO_CHECK_TYPE;
3450 static int cdrom_sysctl_handler(ctl_table *ctl, int write, struct file * filp,
3451 void __user *buffer, size_t *lenp, loff_t *ppos)
3453 int ret;
3455 ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
3457 if (write) {
3459 /* we only care for 1 or 0. */
3460 autoclose = !!cdrom_sysctl_settings.autoclose;
3461 autoeject = !!cdrom_sysctl_settings.autoeject;
3462 debug = !!cdrom_sysctl_settings.debug;
3463 lockdoor = !!cdrom_sysctl_settings.lock;
3464 check_media_type = !!cdrom_sysctl_settings.check;
3466 /* update the option flags according to the changes. we
3467 don't have per device options through sysctl yet,
3468 but we will have and then this will disappear. */
3469 cdrom_update_settings();
3472 return ret;
3475 /* Place files in /proc/sys/dev/cdrom */
3476 static ctl_table cdrom_table[] = {
3478 .procname = "info",
3479 .data = &cdrom_sysctl_settings.info,
3480 .maxlen = CDROM_STR_SIZE,
3481 .mode = 0444,
3482 .proc_handler = &cdrom_sysctl_info,
3485 .procname = "autoclose",
3486 .data = &cdrom_sysctl_settings.autoclose,
3487 .maxlen = sizeof(int),
3488 .mode = 0644,
3489 .proc_handler = &cdrom_sysctl_handler,
3492 .procname = "autoeject",
3493 .data = &cdrom_sysctl_settings.autoeject,
3494 .maxlen = sizeof(int),
3495 .mode = 0644,
3496 .proc_handler = &cdrom_sysctl_handler,
3499 .procname = "debug",
3500 .data = &cdrom_sysctl_settings.debug,
3501 .maxlen = sizeof(int),
3502 .mode = 0644,
3503 .proc_handler = &cdrom_sysctl_handler,
3506 .procname = "lock",
3507 .data = &cdrom_sysctl_settings.lock,
3508 .maxlen = sizeof(int),
3509 .mode = 0644,
3510 .proc_handler = &cdrom_sysctl_handler,
3513 .procname = "check_media",
3514 .data = &cdrom_sysctl_settings.check,
3515 .maxlen = sizeof(int),
3516 .mode = 0644,
3517 .proc_handler = &cdrom_sysctl_handler
3519 { .ctl_name = 0 }
3522 static ctl_table cdrom_cdrom_table[] = {
3524 .ctl_name = DEV_CDROM,
3525 .procname = "cdrom",
3526 .maxlen = 0,
3527 .mode = 0555,
3528 .child = cdrom_table,
3530 { .ctl_name = 0 }
3533 /* Make sure that /proc/sys/dev is there */
3534 static ctl_table cdrom_root_table[] = {
3536 .ctl_name = CTL_DEV,
3537 .procname = "dev",
3538 .maxlen = 0,
3539 .mode = 0555,
3540 .child = cdrom_cdrom_table,
3542 { .ctl_name = 0 }
3544 static struct ctl_table_header *cdrom_sysctl_header;
3546 static void cdrom_sysctl_register(void)
3548 static int initialized;
3550 if (initialized == 1)
3551 return;
3553 cdrom_sysctl_header = register_sysctl_table(cdrom_root_table);
3555 /* set the defaults */
3556 cdrom_sysctl_settings.autoclose = autoclose;
3557 cdrom_sysctl_settings.autoeject = autoeject;
3558 cdrom_sysctl_settings.debug = debug;
3559 cdrom_sysctl_settings.lock = lockdoor;
3560 cdrom_sysctl_settings.check = check_media_type;
3562 initialized = 1;
3565 static void cdrom_sysctl_unregister(void)
3567 if (cdrom_sysctl_header)
3568 unregister_sysctl_table(cdrom_sysctl_header);
3571 #else /* CONFIG_SYSCTL */
3573 static void cdrom_sysctl_register(void)
3577 static void cdrom_sysctl_unregister(void)
3581 #endif /* CONFIG_SYSCTL */
3583 static int __init cdrom_init(void)
3585 cdrom_sysctl_register();
3587 return 0;
3590 static void __exit cdrom_exit(void)
3592 printk(KERN_INFO "Uniform CD-ROM driver unloaded\n");
3593 cdrom_sysctl_unregister();
3596 module_init(cdrom_init);
3597 module_exit(cdrom_exit);
3598 MODULE_LICENSE("GPL");