initial commit with v2.6.9
[linux-2.6.9-moxart.git] / drivers / cdrom / cdrom.c
blobe57d19031f8ea3d7b98e018b66fde36038d592c9
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 -------------------------------------------------------------------------*/
239 #define REVISION "Revision: 3.20"
240 #define VERSION "Id: cdrom.c 3.20 2003/12/17"
242 /* I use an error-log mask to give fine grain control over the type of
243 messages dumped to the system logs. The available masks include: */
244 #define CD_NOTHING 0x0
245 #define CD_WARNING 0x1
246 #define CD_REG_UNREG 0x2
247 #define CD_DO_IOCTL 0x4
248 #define CD_OPEN 0x8
249 #define CD_CLOSE 0x10
250 #define CD_COUNT_TRACKS 0x20
251 #define CD_CHANGER 0x40
252 #define CD_DVD 0x80
254 /* Define this to remove _all_ the debugging messages */
255 /* #define ERRLOGMASK CD_NOTHING */
256 #define ERRLOGMASK CD_WARNING
257 /* #define ERRLOGMASK (CD_WARNING|CD_OPEN|CD_COUNT_TRACKS|CD_CLOSE) */
258 /* #define ERRLOGMASK (CD_WARNING|CD_REG_UNREG|CD_DO_IOCTL|CD_OPEN|CD_CLOSE|CD_COUNT_TRACKS) */
260 #include <linux/config.h>
261 #include <linux/module.h>
262 #include <linux/fs.h>
263 #include <linux/buffer_head.h>
264 #include <linux/major.h>
265 #include <linux/types.h>
266 #include <linux/errno.h>
267 #include <linux/kernel.h>
268 #include <linux/mm.h>
269 #include <linux/slab.h>
270 #include <linux/cdrom.h>
271 #include <linux/sysctl.h>
272 #include <linux/proc_fs.h>
273 #include <linux/blkpg.h>
274 #include <linux/init.h>
275 #include <linux/fcntl.h>
276 #include <linux/blkdev.h>
277 #include <linux/times.h>
279 #include <asm/uaccess.h>
281 /* used to tell the module to turn on full debugging messages */
282 static int debug;
283 /* used to keep tray locked at all times */
284 static int keeplocked;
285 /* default compatibility mode */
286 static int autoclose=1;
287 static int autoeject;
288 static int lockdoor = 1;
289 /* will we ever get to use this... sigh. */
290 static int check_media_type;
291 /* automatically restart mrw format */
292 static int mrw_format_restart = 1;
293 MODULE_PARM(debug, "i");
294 MODULE_PARM(autoclose, "i");
295 MODULE_PARM(autoeject, "i");
296 MODULE_PARM(lockdoor, "i");
297 MODULE_PARM(check_media_type, "i");
298 MODULE_PARM(mrw_format_restart, "i");
300 static spinlock_t cdrom_lock = SPIN_LOCK_UNLOCKED;
302 static const char *mrw_format_status[] = {
303 "not mrw",
304 "bgformat inactive",
305 "bgformat active",
306 "mrw complete",
309 static const char *mrw_address_space[] = { "DMA", "GAA" };
311 #if (ERRLOGMASK!=CD_NOTHING)
312 #define cdinfo(type, fmt, args...) \
313 if ((ERRLOGMASK & type) || debug==1 ) \
314 printk(KERN_INFO "cdrom: " fmt, ## args)
315 #else
316 #define cdinfo(type, fmt, args...)
317 #endif
319 /* These are used to simplify getting data in from and back to user land */
320 #define IOCTL_IN(arg, type, in) \
321 if (copy_from_user(&(in), (type __user *) (arg), sizeof (in))) \
322 return -EFAULT;
324 #define IOCTL_OUT(arg, type, out) \
325 if (copy_to_user((type __user *) (arg), &(out), sizeof (out))) \
326 return -EFAULT;
328 /* The (cdo->capability & ~cdi->mask & CDC_XXX) construct was used in
329 a lot of places. This macro makes the code more clear. */
330 #define CDROM_CAN(type) (cdi->ops->capability & ~cdi->mask & (type))
332 /* used in the audio ioctls */
333 #define CHECKAUDIO if ((ret=check_for_audio_disc(cdi, cdo))) return ret
335 /* Not-exported routines. */
336 static int open_for_data(struct cdrom_device_info * cdi);
337 static int check_for_audio_disc(struct cdrom_device_info * cdi,
338 struct cdrom_device_ops * cdo);
339 static void sanitize_format(union cdrom_addr *addr,
340 u_char * curr, u_char requested);
341 static int mmc_ioctl(struct cdrom_device_info *cdi, unsigned int cmd,
342 unsigned long arg);
344 int cdrom_get_last_written(struct cdrom_device_info *, long *);
345 static int cdrom_get_next_writable(struct cdrom_device_info *, long *);
346 static void cdrom_count_tracks(struct cdrom_device_info *, tracktype*);
348 static int cdrom_mrw_exit(struct cdrom_device_info *cdi);
350 static int cdrom_get_disc_info(struct cdrom_device_info *cdi, disc_information *di);
352 #ifdef CONFIG_SYSCTL
353 static void cdrom_sysctl_register(void);
354 #endif /* CONFIG_SYSCTL */
355 static struct cdrom_device_info *topCdromPtr;
357 static int cdrom_dummy_generic_packet(struct cdrom_device_info *cdi,
358 struct packet_command *cgc)
360 if (cgc->sense) {
361 cgc->sense->sense_key = 0x05;
362 cgc->sense->asc = 0x20;
363 cgc->sense->ascq = 0x00;
366 cgc->stat = -EIO;
367 return -EIO;
370 /* This macro makes sure we don't have to check on cdrom_device_ops
371 * existence in the run-time routines below. Change_capability is a
372 * hack to have the capability flags defined const, while we can still
373 * change it here without gcc complaining at every line.
375 #define ENSURE(call, bits) if (cdo->call == NULL) *change_capability &= ~(bits)
377 int register_cdrom(struct cdrom_device_info *cdi)
379 static char banner_printed;
380 struct cdrom_device_ops *cdo = cdi->ops;
381 int *change_capability = (int *)&cdo->capability; /* hack */
383 cdinfo(CD_OPEN, "entering register_cdrom\n");
385 if (cdo->open == NULL || cdo->release == NULL)
386 return -2;
387 if (!banner_printed) {
388 printk(KERN_INFO "Uniform CD-ROM driver " REVISION "\n");
389 banner_printed = 1;
390 #ifdef CONFIG_SYSCTL
391 cdrom_sysctl_register();
392 #endif /* CONFIG_SYSCTL */
395 ENSURE(drive_status, CDC_DRIVE_STATUS );
396 ENSURE(media_changed, CDC_MEDIA_CHANGED);
397 ENSURE(tray_move, CDC_CLOSE_TRAY | CDC_OPEN_TRAY);
398 ENSURE(lock_door, CDC_LOCK);
399 ENSURE(select_speed, CDC_SELECT_SPEED);
400 ENSURE(get_last_session, CDC_MULTI_SESSION);
401 ENSURE(get_mcn, CDC_MCN);
402 ENSURE(reset, CDC_RESET);
403 ENSURE(audio_ioctl, CDC_PLAY_AUDIO);
404 ENSURE(dev_ioctl, CDC_IOCTLS);
405 ENSURE(generic_packet, CDC_GENERIC_PACKET);
406 cdi->mc_flags = 0;
407 cdo->n_minors = 0;
408 cdi->options = CDO_USE_FFLAGS;
410 if (autoclose==1 && CDROM_CAN(CDC_CLOSE_TRAY))
411 cdi->options |= (int) CDO_AUTO_CLOSE;
412 if (autoeject==1 && CDROM_CAN(CDC_OPEN_TRAY))
413 cdi->options |= (int) CDO_AUTO_EJECT;
414 if (lockdoor==1)
415 cdi->options |= (int) CDO_LOCK;
416 if (check_media_type==1)
417 cdi->options |= (int) CDO_CHECK_TYPE;
419 if (CDROM_CAN(CDC_MRW_W))
420 cdi->exit = cdrom_mrw_exit;
422 if (cdi->disk)
423 cdi->cdda_method = CDDA_BPC_FULL;
424 else
425 cdi->cdda_method = CDDA_OLD;
427 if (!cdo->generic_packet)
428 cdo->generic_packet = cdrom_dummy_generic_packet;
430 cdinfo(CD_REG_UNREG, "drive \"/dev/%s\" registered\n", cdi->name);
431 spin_lock(&cdrom_lock);
432 cdi->next = topCdromPtr;
433 topCdromPtr = cdi;
434 spin_unlock(&cdrom_lock);
435 return 0;
437 #undef ENSURE
439 int unregister_cdrom(struct cdrom_device_info *unreg)
441 struct cdrom_device_info *cdi, *prev;
442 cdinfo(CD_OPEN, "entering unregister_cdrom\n");
444 prev = NULL;
445 spin_lock(&cdrom_lock);
446 cdi = topCdromPtr;
447 while (cdi && cdi != unreg) {
448 prev = cdi;
449 cdi = cdi->next;
452 if (cdi == NULL) {
453 spin_unlock(&cdrom_lock);
454 return -2;
456 if (prev)
457 prev->next = cdi->next;
458 else
459 topCdromPtr = cdi->next;
461 spin_unlock(&cdrom_lock);
463 if (cdi->exit)
464 cdi->exit(cdi);
466 cdi->ops->n_minors--;
467 cdinfo(CD_REG_UNREG, "drive \"/dev/%s\" unregistered\n", cdi->name);
468 return 0;
471 int cdrom_get_media_event(struct cdrom_device_info *cdi,
472 struct media_event_desc *med)
474 struct packet_command cgc;
475 unsigned char buffer[8];
476 struct event_header *eh = (struct event_header *) buffer;
478 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_READ);
479 cgc.cmd[0] = GPCMD_GET_EVENT_STATUS_NOTIFICATION;
480 cgc.cmd[1] = 1; /* IMMED */
481 cgc.cmd[4] = 1 << 4; /* media event */
482 cgc.cmd[8] = sizeof(buffer);
483 cgc.quiet = 1;
485 if (cdi->ops->generic_packet(cdi, &cgc))
486 return 1;
488 if (be16_to_cpu(eh->data_len) < sizeof(*med))
489 return 1;
491 if (eh->nea || eh->notification_class != 0x4)
492 return 1;
494 memcpy(med, &buffer[sizeof(*eh)], sizeof(*med));
495 return 0;
499 * the first prototypes used 0x2c as the page code for the mrw mode page,
500 * subsequently this was changed to 0x03. probe the one used by this drive
502 int cdrom_mrw_probe_pc(struct cdrom_device_info *cdi)
504 struct packet_command cgc;
505 char buffer[16];
507 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_READ);
509 cgc.timeout = HZ;
510 cgc.quiet = 1;
512 if (!cdrom_mode_sense(cdi, &cgc, MRW_MODE_PC, 0)) {
513 cdi->mrw_mode_page = MRW_MODE_PC;
514 return 0;
515 } else if (!cdrom_mode_sense(cdi, &cgc, MRW_MODE_PC_PRE1, 0)) {
516 cdi->mrw_mode_page = MRW_MODE_PC_PRE1;
517 return 0;
520 return 1;
523 int cdrom_is_mrw(struct cdrom_device_info *cdi, int *write)
525 struct packet_command cgc;
526 struct mrw_feature_desc *mfd;
527 unsigned char buffer[16];
528 int ret;
530 *write = 0;
532 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_READ);
534 cgc.cmd[0] = GPCMD_GET_CONFIGURATION;
535 cgc.cmd[3] = CDF_MRW;
536 cgc.cmd[8] = sizeof(buffer);
537 cgc.quiet = 1;
539 if ((ret = cdi->ops->generic_packet(cdi, &cgc)))
540 return ret;
542 mfd = (struct mrw_feature_desc *)&buffer[sizeof(struct feature_header)];
543 *write = mfd->write;
545 if ((ret = cdrom_mrw_probe_pc(cdi))) {
546 *write = 0;
547 return ret;
550 return 0;
553 static int cdrom_mrw_bgformat(struct cdrom_device_info *cdi, int cont)
555 struct packet_command cgc;
556 unsigned char buffer[12];
557 int ret;
559 printk(KERN_INFO "cdrom: %sstarting format\n", cont ? "Re" : "");
562 * FmtData bit set (bit 4), format type is 1
564 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_WRITE);
565 cgc.cmd[0] = GPCMD_FORMAT_UNIT;
566 cgc.cmd[1] = (1 << 4) | 1;
568 cgc.timeout = 5 * 60 * HZ;
571 * 4 byte format list header, 8 byte format list descriptor
573 buffer[1] = 1 << 1;
574 buffer[3] = 8;
577 * nr_blocks field
579 buffer[4] = 0xff;
580 buffer[5] = 0xff;
581 buffer[6] = 0xff;
582 buffer[7] = 0xff;
584 buffer[8] = 0x24 << 2;
585 buffer[11] = cont;
587 ret = cdi->ops->generic_packet(cdi, &cgc);
588 if (ret)
589 printk(KERN_INFO "cdrom: bgformat failed\n");
591 return ret;
594 static int cdrom_mrw_bgformat_susp(struct cdrom_device_info *cdi, int immed)
596 struct packet_command cgc;
598 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
599 cgc.cmd[0] = GPCMD_CLOSE_TRACK;
602 * Session = 1, Track = 0
604 cgc.cmd[1] = !!immed;
605 cgc.cmd[2] = 1 << 1;
607 cgc.timeout = 5 * 60 * HZ;
609 return cdi->ops->generic_packet(cdi, &cgc);
612 static int cdrom_flush_cache(struct cdrom_device_info *cdi)
614 struct packet_command cgc;
616 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
617 cgc.cmd[0] = GPCMD_FLUSH_CACHE;
619 cgc.timeout = 5 * 60 * HZ;
621 return cdi->ops->generic_packet(cdi, &cgc);
624 static int cdrom_mrw_exit(struct cdrom_device_info *cdi)
626 disc_information di;
627 int ret;
629 ret = cdrom_get_disc_info(cdi, &di);
630 if (ret < 0 || ret < (int)offsetof(typeof(di),disc_type))
631 return 1;
633 ret = 0;
634 if (di.mrw_status == CDM_MRW_BGFORMAT_ACTIVE) {
635 printk(KERN_INFO "cdrom: issuing MRW back ground "
636 "format suspend\n");
637 ret = cdrom_mrw_bgformat_susp(cdi, 0);
640 if (!ret)
641 ret = cdrom_flush_cache(cdi);
643 return ret;
646 static int cdrom_mrw_set_lba_space(struct cdrom_device_info *cdi, int space)
648 struct packet_command cgc;
649 struct mode_page_header *mph;
650 char buffer[16];
651 int ret, offset, size;
653 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_READ);
655 cgc.buffer = buffer;
656 cgc.buflen = sizeof(buffer);
658 if ((ret = cdrom_mode_sense(cdi, &cgc, cdi->mrw_mode_page, 0)))
659 return ret;
661 mph = (struct mode_page_header *) buffer;
662 offset = be16_to_cpu(mph->desc_length);
663 size = be16_to_cpu(mph->mode_data_length) + 2;
665 buffer[offset + 3] = space;
666 cgc.buflen = size;
668 if ((ret = cdrom_mode_select(cdi, &cgc)))
669 return ret;
671 printk(KERN_INFO "cdrom: %s: mrw address space %s selected\n", cdi->name, mrw_address_space[space]);
672 return 0;
675 int cdrom_get_random_writable(struct cdrom_device_info *cdi,
676 struct rwrt_feature_desc *rfd)
678 struct packet_command cgc;
679 char buffer[24];
680 int ret;
682 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_READ);
684 cgc.cmd[0] = GPCMD_GET_CONFIGURATION; /* often 0x46 */
685 cgc.cmd[3] = CDF_RWRT; /* often 0x0020 */
686 cgc.cmd[8] = sizeof(buffer); /* often 0x18 */
687 cgc.quiet = 1;
689 if ((ret = cdi->ops->generic_packet(cdi, &cgc)))
690 return ret;
692 memcpy(rfd, &buffer[sizeof(struct feature_header)], sizeof (*rfd));
693 return 0;
696 int cdrom_has_defect_mgt(struct cdrom_device_info *cdi)
698 struct packet_command cgc;
699 char buffer[16];
700 __u16 *feature_code;
701 int ret;
703 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_READ);
705 cgc.cmd[0] = GPCMD_GET_CONFIGURATION;
706 cgc.cmd[3] = CDF_HWDM;
707 cgc.cmd[8] = sizeof(buffer);
708 cgc.quiet = 1;
710 if ((ret = cdi->ops->generic_packet(cdi, &cgc)))
711 return ret;
713 feature_code = (__u16 *) &buffer[sizeof(struct feature_header)];
714 if (be16_to_cpu(*feature_code) == CDF_HWDM)
715 return 0;
717 return 1;
721 int cdrom_is_random_writable(struct cdrom_device_info *cdi, int *write)
723 struct rwrt_feature_desc rfd;
724 int ret;
726 *write = 0;
728 if ((ret = cdrom_get_random_writable(cdi, &rfd)))
729 return ret;
731 if (CDF_RWRT == be16_to_cpu(rfd.feature_code))
732 *write = 1;
734 return 0;
737 static int cdrom_media_erasable(struct cdrom_device_info *cdi)
739 disc_information di;
740 int ret;
742 ret = cdrom_get_disc_info(cdi, &di);
743 if (ret < 0 || ret < offsetof(typeof(di), n_first_track))
744 return -1;
746 return di.erasable;
750 * FIXME: check RO bit
752 static int cdrom_dvdram_open_write(struct cdrom_device_info *cdi)
754 int ret = cdrom_media_erasable(cdi);
757 * allow writable open if media info read worked and media is
758 * erasable, _or_ if it fails since not all drives support it
760 if (!ret)
761 return 1;
763 return 0;
766 static int cdrom_mrw_open_write(struct cdrom_device_info *cdi)
768 disc_information di;
769 int ret;
772 * always reset to DMA lba space on open
774 if (cdrom_mrw_set_lba_space(cdi, MRW_LBA_DMA)) {
775 printk(KERN_ERR "cdrom: failed setting lba address space\n");
776 return 1;
779 ret = cdrom_get_disc_info(cdi, &di);
780 if (ret < 0 || ret < offsetof(typeof(di),disc_type))
781 return 1;
783 if (!di.erasable)
784 return 1;
787 * mrw_status
788 * 0 - not MRW formatted
789 * 1 - MRW bgformat started, but not running or complete
790 * 2 - MRW bgformat in progress
791 * 3 - MRW formatting complete
793 ret = 0;
794 printk(KERN_INFO "cdrom open: mrw_status '%s'\n",
795 mrw_format_status[di.mrw_status]);
796 if (!di.mrw_status)
797 ret = 1;
798 else if (di.mrw_status == CDM_MRW_BGFORMAT_INACTIVE &&
799 mrw_format_restart)
800 ret = cdrom_mrw_bgformat(cdi, 1);
802 return ret;
805 static int mo_open_write(struct cdrom_device_info *cdi)
807 struct packet_command cgc;
808 char buffer[255];
809 int ret;
811 init_cdrom_command(&cgc, &buffer, 4, CGC_DATA_READ);
812 cgc.quiet = 1;
815 * obtain write protect information as per
816 * drivers/scsi/sd.c:sd_read_write_protect_flag
819 ret = cdrom_mode_sense(cdi, &cgc, GPMODE_ALL_PAGES, 0);
820 if (ret)
821 ret = cdrom_mode_sense(cdi, &cgc, GPMODE_VENDOR_PAGE, 0);
822 if (ret) {
823 cgc.buflen = 255;
824 ret = cdrom_mode_sense(cdi, &cgc, GPMODE_ALL_PAGES, 0);
827 /* drive gave us no info, let the user go ahead */
828 if (ret)
829 return 0;
831 return buffer[3] & 0x80;
834 static int cdrom_ram_open_write(struct cdrom_device_info *cdi)
836 struct rwrt_feature_desc rfd;
837 int ret;
839 if ((ret = cdrom_has_defect_mgt(cdi)))
840 return ret;
842 if ((ret = cdrom_get_random_writable(cdi, &rfd)))
843 return ret;
844 else if (CDF_RWRT == be16_to_cpu(rfd.feature_code))
845 ret = !rfd.curr;
847 cdinfo(CD_OPEN, "can open for random write\n");
848 return ret;
852 * returns 0 for ok to open write, non-0 to disallow
854 static int cdrom_open_write(struct cdrom_device_info *cdi)
856 int mrw, mrw_write, ram_write;
857 int ret = 1;
859 mrw = 0;
860 if (!cdrom_is_mrw(cdi, &mrw_write))
861 mrw = 1;
863 if (CDROM_CAN(CDC_MO_DRIVE))
864 ram_write = 1;
865 else
866 (void) cdrom_is_random_writable(cdi, &ram_write);
868 if (mrw)
869 cdi->mask &= ~CDC_MRW;
870 else
871 cdi->mask |= CDC_MRW;
873 if (mrw_write)
874 cdi->mask &= ~CDC_MRW_W;
875 else
876 cdi->mask |= CDC_MRW_W;
878 if (ram_write)
879 cdi->mask &= ~CDC_RAM;
880 else
881 cdi->mask |= CDC_RAM;
883 if (CDROM_CAN(CDC_MRW_W))
884 ret = cdrom_mrw_open_write(cdi);
885 else if (CDROM_CAN(CDC_DVD_RAM))
886 ret = cdrom_dvdram_open_write(cdi);
887 else if (CDROM_CAN(CDC_RAM) &&
888 !CDROM_CAN(CDC_CD_R|CDC_CD_RW|CDC_DVD|CDC_DVD_R|CDC_MRW|CDC_MO_DRIVE))
889 ret = cdrom_ram_open_write(cdi);
890 else if (CDROM_CAN(CDC_MO_DRIVE))
891 ret = mo_open_write(cdi);
893 return ret;
896 static int cdrom_close_write(struct cdrom_device_info *cdi)
898 #if 0
899 return cdrom_flush_cache(cdi);
900 #else
901 return 0;
902 #endif
905 /* We use the open-option O_NONBLOCK to indicate that the
906 * purpose of opening is only for subsequent ioctl() calls; no device
907 * integrity checks are performed.
909 * We hope that all cd-player programs will adopt this convention. It
910 * is in their own interest: device control becomes a lot easier
911 * this way.
913 int cdrom_open(struct cdrom_device_info *cdi, struct inode *ip, struct file *fp)
915 int ret;
917 cdinfo(CD_OPEN, "entering cdrom_open\n");
919 /* if this was a O_NONBLOCK open and we should honor the flags,
920 * do a quick open without drive/disc integrity checks. */
921 cdi->use_count++;
922 if ((fp->f_flags & O_NONBLOCK) && (cdi->options & CDO_USE_FFLAGS)) {
923 ret = cdi->ops->open(cdi, 1);
924 } else {
925 ret = open_for_data(cdi);
926 if (ret)
927 goto err;
928 if (fp->f_mode & FMODE_WRITE) {
929 ret = -EROFS;
930 if (cdrom_open_write(cdi))
931 goto err;
932 if (!CDROM_CAN(CDC_RAM))
933 goto err;
934 ret = 0;
938 if (ret)
939 goto err;
941 cdinfo(CD_OPEN, "Use count for \"/dev/%s\" now %d\n",
942 cdi->name, cdi->use_count);
943 /* Do this on open. Don't wait for mount, because they might
944 not be mounting, but opening with O_NONBLOCK */
945 check_disk_change(ip->i_bdev);
946 return 0;
947 err:
948 cdi->use_count--;
949 return ret;
952 static
953 int open_for_data(struct cdrom_device_info * cdi)
955 int ret;
956 struct cdrom_device_ops *cdo = cdi->ops;
957 tracktype tracks;
958 cdinfo(CD_OPEN, "entering open_for_data\n");
959 /* Check if the driver can report drive status. If it can, we
960 can do clever things. If it can't, well, we at least tried! */
961 if (cdo->drive_status != NULL) {
962 ret = cdo->drive_status(cdi, CDSL_CURRENT);
963 cdinfo(CD_OPEN, "drive_status=%d\n", ret);
964 if (ret == CDS_TRAY_OPEN) {
965 cdinfo(CD_OPEN, "the tray is open...\n");
966 /* can/may i close it? */
967 if (CDROM_CAN(CDC_CLOSE_TRAY) &&
968 cdi->options & CDO_AUTO_CLOSE) {
969 cdinfo(CD_OPEN, "trying to close the tray.\n");
970 ret=cdo->tray_move(cdi,0);
971 if (ret) {
972 cdinfo(CD_OPEN, "bummer. tried to close the tray but failed.\n");
973 /* Ignore the error from the low
974 level driver. We don't care why it
975 couldn't close the tray. We only care
976 that there is no disc in the drive,
977 since that is the _REAL_ problem here.*/
978 ret=-ENOMEDIUM;
979 goto clean_up_and_return;
981 } else {
982 cdinfo(CD_OPEN, "bummer. this drive can't close the tray.\n");
983 ret=-ENOMEDIUM;
984 goto clean_up_and_return;
986 /* Ok, the door should be closed now.. Check again */
987 ret = cdo->drive_status(cdi, CDSL_CURRENT);
988 if ((ret == CDS_NO_DISC) || (ret==CDS_TRAY_OPEN)) {
989 cdinfo(CD_OPEN, "bummer. the tray is still not closed.\n");
990 cdinfo(CD_OPEN, "tray might not contain a medium.\n");
991 ret=-ENOMEDIUM;
992 goto clean_up_and_return;
994 cdinfo(CD_OPEN, "the tray is now closed.\n");
996 if (ret!=CDS_DISC_OK) {
997 ret = -ENOMEDIUM;
998 goto clean_up_and_return;
1001 cdrom_count_tracks(cdi, &tracks);
1002 if (tracks.error == CDS_NO_DISC) {
1003 cdinfo(CD_OPEN, "bummer. no disc.\n");
1004 ret=-ENOMEDIUM;
1005 goto clean_up_and_return;
1007 /* CD-Players which don't use O_NONBLOCK, workman
1008 * for example, need bit CDO_CHECK_TYPE cleared! */
1009 if (tracks.data==0) {
1010 if (cdi->options & CDO_CHECK_TYPE) {
1011 /* give people a warning shot, now that CDO_CHECK_TYPE
1012 is the default case! */
1013 cdinfo(CD_OPEN, "bummer. wrong media type.\n");
1014 cdinfo(CD_WARNING, "pid %d must open device O_NONBLOCK!\n",
1015 (unsigned int)current->pid);
1016 ret=-EMEDIUMTYPE;
1017 goto clean_up_and_return;
1019 else {
1020 cdinfo(CD_OPEN, "wrong media type, but CDO_CHECK_TYPE not set.\n");
1024 cdinfo(CD_OPEN, "all seems well, opening the device.\n");
1026 /* all seems well, we can open the device */
1027 ret = cdo->open(cdi, 0); /* open for data */
1028 cdinfo(CD_OPEN, "opening the device gave me %d.\n", ret);
1029 /* After all this careful checking, we shouldn't have problems
1030 opening the device, but we don't want the device locked if
1031 this somehow fails... */
1032 if (ret) {
1033 cdinfo(CD_OPEN, "open device failed.\n");
1034 goto clean_up_and_return;
1036 if (CDROM_CAN(CDC_LOCK) && (cdi->options & CDO_LOCK)) {
1037 cdo->lock_door(cdi, 1);
1038 cdinfo(CD_OPEN, "door locked.\n");
1040 cdinfo(CD_OPEN, "device opened successfully.\n");
1041 return ret;
1043 /* Something failed. Try to unlock the drive, because some drivers
1044 (notably ide-cd) lock the drive after every command. This produced
1045 a nasty bug where after mount failed, the drive would remain locked!
1046 This ensures that the drive gets unlocked after a mount fails. This
1047 is a goto to avoid bloating the driver with redundant code. */
1048 clean_up_and_return:
1049 cdinfo(CD_WARNING, "open failed.\n");
1050 if (CDROM_CAN(CDC_LOCK) && cdi->options & CDO_LOCK) {
1051 cdo->lock_door(cdi, 0);
1052 cdinfo(CD_OPEN, "door unlocked.\n");
1054 return ret;
1057 /* This code is similar to that in open_for_data. The routine is called
1058 whenever an audio play operation is requested.
1060 int check_for_audio_disc(struct cdrom_device_info * cdi,
1061 struct cdrom_device_ops * cdo)
1063 int ret;
1064 tracktype tracks;
1065 cdinfo(CD_OPEN, "entering check_for_audio_disc\n");
1066 if (!(cdi->options & CDO_CHECK_TYPE))
1067 return 0;
1068 if (cdo->drive_status != NULL) {
1069 ret = cdo->drive_status(cdi, CDSL_CURRENT);
1070 cdinfo(CD_OPEN, "drive_status=%d\n", ret);
1071 if (ret == CDS_TRAY_OPEN) {
1072 cdinfo(CD_OPEN, "the tray is open...\n");
1073 /* can/may i close it? */
1074 if (CDROM_CAN(CDC_CLOSE_TRAY) &&
1075 cdi->options & CDO_AUTO_CLOSE) {
1076 cdinfo(CD_OPEN, "trying to close the tray.\n");
1077 ret=cdo->tray_move(cdi,0);
1078 if (ret) {
1079 cdinfo(CD_OPEN, "bummer. tried to close tray but failed.\n");
1080 /* Ignore the error from the low
1081 level driver. We don't care why it
1082 couldn't close the tray. We only care
1083 that there is no disc in the drive,
1084 since that is the _REAL_ problem here.*/
1085 return -ENOMEDIUM;
1087 } else {
1088 cdinfo(CD_OPEN, "bummer. this driver can't close the tray.\n");
1089 return -ENOMEDIUM;
1091 /* Ok, the door should be closed now.. Check again */
1092 ret = cdo->drive_status(cdi, CDSL_CURRENT);
1093 if ((ret == CDS_NO_DISC) || (ret==CDS_TRAY_OPEN)) {
1094 cdinfo(CD_OPEN, "bummer. the tray is still not closed.\n");
1095 return -ENOMEDIUM;
1097 if (ret!=CDS_DISC_OK) {
1098 cdinfo(CD_OPEN, "bummer. disc isn't ready.\n");
1099 return -EIO;
1101 cdinfo(CD_OPEN, "the tray is now closed.\n");
1104 cdrom_count_tracks(cdi, &tracks);
1105 if (tracks.error)
1106 return(tracks.error);
1108 if (tracks.audio==0)
1109 return -EMEDIUMTYPE;
1111 return 0;
1114 /* Admittedly, the logic below could be performed in a nicer way. */
1115 int cdrom_release(struct cdrom_device_info *cdi, struct file *fp)
1117 struct cdrom_device_ops *cdo = cdi->ops;
1118 int opened_for_data;
1120 cdinfo(CD_CLOSE, "entering cdrom_release\n");
1122 if (cdi->use_count > 0)
1123 cdi->use_count--;
1124 if (cdi->use_count == 0)
1125 cdinfo(CD_CLOSE, "Use count for \"/dev/%s\" now zero\n", cdi->name);
1126 if (cdi->use_count == 0 &&
1127 (cdo->capability & CDC_LOCK) && !keeplocked) {
1128 cdinfo(CD_CLOSE, "Unlocking door!\n");
1129 cdo->lock_door(cdi, 0);
1131 opened_for_data = !(cdi->options & CDO_USE_FFLAGS) ||
1132 !(fp && fp->f_flags & O_NONBLOCK);
1135 * flush cache on last write release
1137 if (CDROM_CAN(CDC_RAM) && !cdi->use_count && cdi->for_data)
1138 cdrom_close_write(cdi);
1140 cdo->release(cdi);
1141 if (cdi->use_count == 0) { /* last process that closes dev*/
1142 if (opened_for_data &&
1143 cdi->options & CDO_AUTO_EJECT && CDROM_CAN(CDC_OPEN_TRAY))
1144 cdo->tray_move(cdi, 1);
1146 return 0;
1149 static int cdrom_read_mech_status(struct cdrom_device_info *cdi,
1150 struct cdrom_changer_info *buf)
1152 struct packet_command cgc;
1153 struct cdrom_device_ops *cdo = cdi->ops;
1154 int length;
1157 * Sanyo changer isn't spec compliant (doesn't use regular change
1158 * LOAD_UNLOAD command, and it doesn't implement the mech status
1159 * command below
1161 if (cdi->sanyo_slot) {
1162 buf->hdr.nslots = 3;
1163 buf->hdr.curslot = cdi->sanyo_slot == 3 ? 0 : cdi->sanyo_slot;
1164 for (length = 0; length < 3; length++) {
1165 buf->slots[length].disc_present = 1;
1166 buf->slots[length].change = 0;
1168 return 0;
1171 length = sizeof(struct cdrom_mechstat_header) +
1172 cdi->capacity * sizeof(struct cdrom_slot);
1174 init_cdrom_command(&cgc, buf, length, CGC_DATA_READ);
1175 cgc.cmd[0] = GPCMD_MECHANISM_STATUS;
1176 cgc.cmd[8] = (length >> 8) & 0xff;
1177 cgc.cmd[9] = length & 0xff;
1178 return cdo->generic_packet(cdi, &cgc);
1181 static int cdrom_slot_status(struct cdrom_device_info *cdi, int slot)
1183 struct cdrom_changer_info *info;
1184 int ret;
1186 cdinfo(CD_CHANGER, "entering cdrom_slot_status()\n");
1187 if (cdi->sanyo_slot)
1188 return CDS_NO_INFO;
1190 info = kmalloc(sizeof(*info), GFP_KERNEL);
1191 if (!info)
1192 return -ENOMEM;
1194 if ((ret = cdrom_read_mech_status(cdi, info)))
1195 goto out_free;
1197 if (info->slots[slot].disc_present)
1198 ret = CDS_DISC_OK;
1199 else
1200 ret = CDS_NO_DISC;
1202 out_free:
1203 kfree(info);
1204 return ret;
1207 /* Return the number of slots for an ATAPI/SCSI cdrom,
1208 * return 1 if not a changer.
1210 int cdrom_number_of_slots(struct cdrom_device_info *cdi)
1212 int status;
1213 int nslots = 1;
1214 struct cdrom_changer_info *info;
1216 cdinfo(CD_CHANGER, "entering cdrom_number_of_slots()\n");
1217 /* cdrom_read_mech_status requires a valid value for capacity: */
1218 cdi->capacity = 0;
1220 info = kmalloc(sizeof(*info), GFP_KERNEL);
1221 if (!info)
1222 return -ENOMEM;
1224 if ((status = cdrom_read_mech_status(cdi, info)) == 0)
1225 nslots = info->hdr.nslots;
1227 kfree(info);
1228 return nslots;
1232 /* If SLOT < 0, unload the current slot. Otherwise, try to load SLOT. */
1233 static int cdrom_load_unload(struct cdrom_device_info *cdi, int slot)
1235 struct packet_command cgc;
1237 cdinfo(CD_CHANGER, "entering cdrom_load_unload()\n");
1238 if (cdi->sanyo_slot && slot < 0)
1239 return 0;
1241 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
1242 cgc.cmd[0] = GPCMD_LOAD_UNLOAD;
1243 cgc.cmd[4] = 2 + (slot >= 0);
1244 cgc.cmd[8] = slot;
1245 cgc.timeout = 60 * HZ;
1247 /* The Sanyo 3 CD changer uses byte 7 of the
1248 GPCMD_TEST_UNIT_READY to command to switch CDs instead of
1249 using the GPCMD_LOAD_UNLOAD opcode. */
1250 if (cdi->sanyo_slot && -1 < slot) {
1251 cgc.cmd[0] = GPCMD_TEST_UNIT_READY;
1252 cgc.cmd[7] = slot;
1253 cgc.cmd[4] = cgc.cmd[8] = 0;
1254 cdi->sanyo_slot = slot ? slot : 3;
1257 return cdi->ops->generic_packet(cdi, &cgc);
1260 static int cdrom_select_disc(struct cdrom_device_info *cdi, int slot)
1262 struct cdrom_changer_info *info;
1263 int curslot;
1264 int ret;
1266 cdinfo(CD_CHANGER, "entering cdrom_select_disc()\n");
1267 if (!CDROM_CAN(CDC_SELECT_DISC))
1268 return -EDRIVE_CANT_DO_THIS;
1270 (void) cdi->ops->media_changed(cdi, slot);
1272 if (slot == CDSL_NONE) {
1273 /* set media changed bits, on both queues */
1274 cdi->mc_flags = 0x3;
1275 return cdrom_load_unload(cdi, -1);
1278 info = kmalloc(sizeof(*info), GFP_KERNEL);
1279 if (!info)
1280 return -ENOMEM;
1282 if ((ret = cdrom_read_mech_status(cdi, info))) {
1283 kfree(info);
1284 return ret;
1287 curslot = info->hdr.curslot;
1288 kfree(info);
1290 if (cdi->use_count > 1 || keeplocked) {
1291 if (slot == CDSL_CURRENT) {
1292 return curslot;
1293 } else {
1294 return -EBUSY;
1298 /* Specifying CDSL_CURRENT will attempt to load the currnet slot,
1299 which is useful if it had been previously unloaded.
1300 Whether it can or not, it returns the current slot.
1301 Similarly, if slot happens to be the current one, we still
1302 try and load it. */
1303 if (slot == CDSL_CURRENT)
1304 slot = curslot;
1306 /* set media changed bits on both queues */
1307 cdi->mc_flags = 0x3;
1308 if ((ret = cdrom_load_unload(cdi, slot)))
1309 return ret;
1311 return slot;
1314 /* We want to make media_changed accessible to the user through an
1315 * ioctl. The main problem now is that we must double-buffer the
1316 * low-level implementation, to assure that the VFS and the user both
1317 * see a medium change once.
1320 static
1321 int media_changed(struct cdrom_device_info *cdi, int queue)
1323 unsigned int mask = (1 << (queue & 1));
1324 int ret = !!(cdi->mc_flags & mask);
1326 if (!CDROM_CAN(CDC_MEDIA_CHANGED))
1327 return ret;
1328 /* changed since last call? */
1329 if (cdi->ops->media_changed(cdi, CDSL_CURRENT)) {
1330 cdi->mc_flags = 0x3; /* set bit on both queues */
1331 ret |= 1;
1333 cdi->mc_flags &= ~mask; /* clear bit */
1334 return ret;
1337 int cdrom_media_changed(struct cdrom_device_info *cdi)
1339 /* This talks to the VFS, which doesn't like errors - just 1 or 0.
1340 * Returning "0" is always safe (media hasn't been changed). Do that
1341 * if the low-level cdrom driver dosn't support media changed. */
1342 if (cdi == NULL || cdi->ops->media_changed == NULL)
1343 return 0;
1344 if (!CDROM_CAN(CDC_MEDIA_CHANGED))
1345 return 0;
1346 return media_changed(cdi, 0);
1349 /* badly broken, I know. Is due for a fixup anytime. */
1350 static void cdrom_count_tracks(struct cdrom_device_info *cdi, tracktype* tracks)
1352 struct cdrom_tochdr header;
1353 struct cdrom_tocentry entry;
1354 int ret, i;
1355 tracks->data=0;
1356 tracks->audio=0;
1357 tracks->cdi=0;
1358 tracks->xa=0;
1359 tracks->error=0;
1360 cdinfo(CD_COUNT_TRACKS, "entering cdrom_count_tracks\n");
1361 if (!CDROM_CAN(CDC_PLAY_AUDIO)) {
1362 tracks->error=CDS_NO_INFO;
1363 return;
1365 /* Grab the TOC header so we can see how many tracks there are */
1366 if ((ret = cdi->ops->audio_ioctl(cdi, CDROMREADTOCHDR, &header))) {
1367 if (ret == -ENOMEDIUM)
1368 tracks->error = CDS_NO_DISC;
1369 else
1370 tracks->error = CDS_NO_INFO;
1371 return;
1373 /* check what type of tracks are on this disc */
1374 entry.cdte_format = CDROM_MSF;
1375 for (i = header.cdth_trk0; i <= header.cdth_trk1; i++) {
1376 entry.cdte_track = i;
1377 if (cdi->ops->audio_ioctl(cdi, CDROMREADTOCENTRY, &entry)) {
1378 tracks->error=CDS_NO_INFO;
1379 return;
1381 if (entry.cdte_ctrl & CDROM_DATA_TRACK) {
1382 if (entry.cdte_format == 0x10)
1383 tracks->cdi++;
1384 else if (entry.cdte_format == 0x20)
1385 tracks->xa++;
1386 else
1387 tracks->data++;
1388 } else
1389 tracks->audio++;
1390 cdinfo(CD_COUNT_TRACKS, "track %d: format=%d, ctrl=%d\n",
1391 i, entry.cdte_format, entry.cdte_ctrl);
1393 cdinfo(CD_COUNT_TRACKS, "disc has %d tracks: %d=audio %d=data %d=Cd-I %d=XA\n",
1394 header.cdth_trk1, tracks->audio, tracks->data,
1395 tracks->cdi, tracks->xa);
1398 /* Requests to the low-level drivers will /always/ be done in the
1399 following format convention:
1401 CDROM_LBA: all data-related requests.
1402 CDROM_MSF: all audio-related requests.
1404 However, a low-level implementation is allowed to refuse this
1405 request, and return information in its own favorite format.
1407 It doesn't make sense /at all/ to ask for a play_audio in LBA
1408 format, or ask for multi-session info in MSF format. However, for
1409 backward compatibility these format requests will be satisfied, but
1410 the requests to the low-level drivers will be sanitized in the more
1411 meaningful format indicated above.
1414 static
1415 void sanitize_format(union cdrom_addr *addr,
1416 u_char * curr, u_char requested)
1418 if (*curr == requested)
1419 return; /* nothing to be done! */
1420 if (requested == CDROM_LBA) {
1421 addr->lba = (int) addr->msf.frame +
1422 75 * (addr->msf.second - 2 + 60 * addr->msf.minute);
1423 } else { /* CDROM_MSF */
1424 int lba = addr->lba;
1425 addr->msf.frame = lba % 75;
1426 lba /= 75;
1427 lba += 2;
1428 addr->msf.second = lba % 60;
1429 addr->msf.minute = lba / 60;
1431 *curr = requested;
1434 void init_cdrom_command(struct packet_command *cgc, void *buf, int len,
1435 int type)
1437 memset(cgc, 0, sizeof(struct packet_command));
1438 if (buf)
1439 memset(buf, 0, len);
1440 cgc->buffer = (char *) buf;
1441 cgc->buflen = len;
1442 cgc->data_direction = type;
1443 cgc->timeout = 5*HZ;
1446 /* DVD handling */
1448 #define copy_key(dest,src) memcpy((dest), (src), sizeof(dvd_key))
1449 #define copy_chal(dest,src) memcpy((dest), (src), sizeof(dvd_challenge))
1451 static void setup_report_key(struct packet_command *cgc, unsigned agid, unsigned type)
1453 cgc->cmd[0] = GPCMD_REPORT_KEY;
1454 cgc->cmd[10] = type | (agid << 6);
1455 switch (type) {
1456 case 0: case 8: case 5: {
1457 cgc->buflen = 8;
1458 break;
1460 case 1: {
1461 cgc->buflen = 16;
1462 break;
1464 case 2: case 4: {
1465 cgc->buflen = 12;
1466 break;
1469 cgc->cmd[9] = cgc->buflen;
1470 cgc->data_direction = CGC_DATA_READ;
1473 static void setup_send_key(struct packet_command *cgc, unsigned agid, unsigned type)
1475 cgc->cmd[0] = GPCMD_SEND_KEY;
1476 cgc->cmd[10] = type | (agid << 6);
1477 switch (type) {
1478 case 1: {
1479 cgc->buflen = 16;
1480 break;
1482 case 3: {
1483 cgc->buflen = 12;
1484 break;
1486 case 6: {
1487 cgc->buflen = 8;
1488 break;
1491 cgc->cmd[9] = cgc->buflen;
1492 cgc->data_direction = CGC_DATA_WRITE;
1495 static int dvd_do_auth(struct cdrom_device_info *cdi, dvd_authinfo *ai)
1497 int ret;
1498 u_char buf[20];
1499 struct packet_command cgc;
1500 struct cdrom_device_ops *cdo = cdi->ops;
1501 rpc_state_t rpc_state;
1503 memset(buf, 0, sizeof(buf));
1504 init_cdrom_command(&cgc, buf, 0, CGC_DATA_READ);
1506 switch (ai->type) {
1507 /* LU data send */
1508 case DVD_LU_SEND_AGID:
1509 cdinfo(CD_DVD, "entering DVD_LU_SEND_AGID\n");
1510 cgc.quiet = 1;
1511 setup_report_key(&cgc, ai->lsa.agid, 0);
1513 if ((ret = cdo->generic_packet(cdi, &cgc)))
1514 return ret;
1516 ai->lsa.agid = buf[7] >> 6;
1517 /* Returning data, let host change state */
1518 break;
1520 case DVD_LU_SEND_KEY1:
1521 cdinfo(CD_DVD, "entering DVD_LU_SEND_KEY1\n");
1522 setup_report_key(&cgc, ai->lsk.agid, 2);
1524 if ((ret = cdo->generic_packet(cdi, &cgc)))
1525 return ret;
1527 copy_key(ai->lsk.key, &buf[4]);
1528 /* Returning data, let host change state */
1529 break;
1531 case DVD_LU_SEND_CHALLENGE:
1532 cdinfo(CD_DVD, "entering DVD_LU_SEND_CHALLENGE\n");
1533 setup_report_key(&cgc, ai->lsc.agid, 1);
1535 if ((ret = cdo->generic_packet(cdi, &cgc)))
1536 return ret;
1538 copy_chal(ai->lsc.chal, &buf[4]);
1539 /* Returning data, let host change state */
1540 break;
1542 /* Post-auth key */
1543 case DVD_LU_SEND_TITLE_KEY:
1544 cdinfo(CD_DVD, "entering DVD_LU_SEND_TITLE_KEY\n");
1545 cgc.quiet = 1;
1546 setup_report_key(&cgc, ai->lstk.agid, 4);
1547 cgc.cmd[5] = ai->lstk.lba;
1548 cgc.cmd[4] = ai->lstk.lba >> 8;
1549 cgc.cmd[3] = ai->lstk.lba >> 16;
1550 cgc.cmd[2] = ai->lstk.lba >> 24;
1552 if ((ret = cdo->generic_packet(cdi, &cgc)))
1553 return ret;
1555 ai->lstk.cpm = (buf[4] >> 7) & 1;
1556 ai->lstk.cp_sec = (buf[4] >> 6) & 1;
1557 ai->lstk.cgms = (buf[4] >> 4) & 3;
1558 copy_key(ai->lstk.title_key, &buf[5]);
1559 /* Returning data, let host change state */
1560 break;
1562 case DVD_LU_SEND_ASF:
1563 cdinfo(CD_DVD, "entering DVD_LU_SEND_ASF\n");
1564 setup_report_key(&cgc, ai->lsasf.agid, 5);
1566 if ((ret = cdo->generic_packet(cdi, &cgc)))
1567 return ret;
1569 ai->lsasf.asf = buf[7] & 1;
1570 break;
1572 /* LU data receive (LU changes state) */
1573 case DVD_HOST_SEND_CHALLENGE:
1574 cdinfo(CD_DVD, "entering DVD_HOST_SEND_CHALLENGE\n");
1575 setup_send_key(&cgc, ai->hsc.agid, 1);
1576 buf[1] = 0xe;
1577 copy_chal(&buf[4], ai->hsc.chal);
1579 if ((ret = cdo->generic_packet(cdi, &cgc)))
1580 return ret;
1582 ai->type = DVD_LU_SEND_KEY1;
1583 break;
1585 case DVD_HOST_SEND_KEY2:
1586 cdinfo(CD_DVD, "entering DVD_HOST_SEND_KEY2\n");
1587 setup_send_key(&cgc, ai->hsk.agid, 3);
1588 buf[1] = 0xa;
1589 copy_key(&buf[4], ai->hsk.key);
1591 if ((ret = cdo->generic_packet(cdi, &cgc))) {
1592 ai->type = DVD_AUTH_FAILURE;
1593 return ret;
1595 ai->type = DVD_AUTH_ESTABLISHED;
1596 break;
1598 /* Misc */
1599 case DVD_INVALIDATE_AGID:
1600 cgc.quiet = 1;
1601 cdinfo(CD_DVD, "entering DVD_INVALIDATE_AGID\n");
1602 setup_report_key(&cgc, ai->lsa.agid, 0x3f);
1603 if ((ret = cdo->generic_packet(cdi, &cgc)))
1604 return ret;
1605 break;
1607 /* Get region settings */
1608 case DVD_LU_SEND_RPC_STATE:
1609 cdinfo(CD_DVD, "entering DVD_LU_SEND_RPC_STATE\n");
1610 setup_report_key(&cgc, 0, 8);
1611 memset(&rpc_state, 0, sizeof(rpc_state_t));
1612 cgc.buffer = (char *) &rpc_state;
1614 if ((ret = cdo->generic_packet(cdi, &cgc)))
1615 return ret;
1617 ai->lrpcs.type = rpc_state.type_code;
1618 ai->lrpcs.vra = rpc_state.vra;
1619 ai->lrpcs.ucca = rpc_state.ucca;
1620 ai->lrpcs.region_mask = rpc_state.region_mask;
1621 ai->lrpcs.rpc_scheme = rpc_state.rpc_scheme;
1622 break;
1624 /* Set region settings */
1625 case DVD_HOST_SEND_RPC_STATE:
1626 cdinfo(CD_DVD, "entering DVD_HOST_SEND_RPC_STATE\n");
1627 setup_send_key(&cgc, 0, 6);
1628 buf[1] = 6;
1629 buf[4] = ai->hrpcs.pdrc;
1631 if ((ret = cdo->generic_packet(cdi, &cgc)))
1632 return ret;
1633 break;
1635 default:
1636 cdinfo(CD_WARNING, "Invalid DVD key ioctl (%d)\n", ai->type);
1637 return -ENOTTY;
1640 return 0;
1643 static int dvd_read_physical(struct cdrom_device_info *cdi, dvd_struct *s)
1645 unsigned char buf[21], *base;
1646 struct dvd_layer *layer;
1647 struct packet_command cgc;
1648 struct cdrom_device_ops *cdo = cdi->ops;
1649 int ret, layer_num = s->physical.layer_num;
1651 if (layer_num >= DVD_LAYERS)
1652 return -EINVAL;
1654 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_READ);
1655 cgc.cmd[0] = GPCMD_READ_DVD_STRUCTURE;
1656 cgc.cmd[6] = layer_num;
1657 cgc.cmd[7] = s->type;
1658 cgc.cmd[9] = cgc.buflen & 0xff;
1661 * refrain from reporting errors on non-existing layers (mainly)
1663 cgc.quiet = 1;
1665 if ((ret = cdo->generic_packet(cdi, &cgc)))
1666 return ret;
1668 base = &buf[4];
1669 layer = &s->physical.layer[layer_num];
1672 * place the data... really ugly, but at least we won't have to
1673 * worry about endianess in userspace.
1675 memset(layer, 0, sizeof(*layer));
1676 layer->book_version = base[0] & 0xf;
1677 layer->book_type = base[0] >> 4;
1678 layer->min_rate = base[1] & 0xf;
1679 layer->disc_size = base[1] >> 4;
1680 layer->layer_type = base[2] & 0xf;
1681 layer->track_path = (base[2] >> 4) & 1;
1682 layer->nlayers = (base[2] >> 5) & 3;
1683 layer->track_density = base[3] & 0xf;
1684 layer->linear_density = base[3] >> 4;
1685 layer->start_sector = base[5] << 16 | base[6] << 8 | base[7];
1686 layer->end_sector = base[9] << 16 | base[10] << 8 | base[11];
1687 layer->end_sector_l0 = base[13] << 16 | base[14] << 8 | base[15];
1688 layer->bca = base[16] >> 7;
1690 return 0;
1693 static int dvd_read_copyright(struct cdrom_device_info *cdi, dvd_struct *s)
1695 int ret;
1696 u_char buf[8];
1697 struct packet_command cgc;
1698 struct cdrom_device_ops *cdo = cdi->ops;
1700 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_READ);
1701 cgc.cmd[0] = GPCMD_READ_DVD_STRUCTURE;
1702 cgc.cmd[6] = s->copyright.layer_num;
1703 cgc.cmd[7] = s->type;
1704 cgc.cmd[8] = cgc.buflen >> 8;
1705 cgc.cmd[9] = cgc.buflen & 0xff;
1707 if ((ret = cdo->generic_packet(cdi, &cgc)))
1708 return ret;
1710 s->copyright.cpst = buf[4];
1711 s->copyright.rmi = buf[5];
1713 return 0;
1716 static int dvd_read_disckey(struct cdrom_device_info *cdi, dvd_struct *s)
1718 int ret, size;
1719 u_char *buf;
1720 struct packet_command cgc;
1721 struct cdrom_device_ops *cdo = cdi->ops;
1723 size = sizeof(s->disckey.value) + 4;
1725 if ((buf = (u_char *) kmalloc(size, GFP_KERNEL)) == NULL)
1726 return -ENOMEM;
1728 init_cdrom_command(&cgc, buf, size, CGC_DATA_READ);
1729 cgc.cmd[0] = GPCMD_READ_DVD_STRUCTURE;
1730 cgc.cmd[7] = s->type;
1731 cgc.cmd[8] = size >> 8;
1732 cgc.cmd[9] = size & 0xff;
1733 cgc.cmd[10] = s->disckey.agid << 6;
1735 if (!(ret = cdo->generic_packet(cdi, &cgc)))
1736 memcpy(s->disckey.value, &buf[4], sizeof(s->disckey.value));
1738 kfree(buf);
1739 return ret;
1742 static int dvd_read_bca(struct cdrom_device_info *cdi, dvd_struct *s)
1744 int ret;
1745 u_char buf[4 + 188];
1746 struct packet_command cgc;
1747 struct cdrom_device_ops *cdo = cdi->ops;
1749 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_READ);
1750 cgc.cmd[0] = GPCMD_READ_DVD_STRUCTURE;
1751 cgc.cmd[7] = s->type;
1752 cgc.cmd[9] = cgc.buflen = 0xff;
1754 if ((ret = cdo->generic_packet(cdi, &cgc)))
1755 return ret;
1757 s->bca.len = buf[0] << 8 | buf[1];
1758 if (s->bca.len < 12 || s->bca.len > 188) {
1759 cdinfo(CD_WARNING, "Received invalid BCA length (%d)\n", s->bca.len);
1760 return -EIO;
1762 memcpy(s->bca.value, &buf[4], s->bca.len);
1764 return 0;
1767 static int dvd_read_manufact(struct cdrom_device_info *cdi, dvd_struct *s)
1769 int ret = 0, size;
1770 u_char *buf;
1771 struct packet_command cgc;
1772 struct cdrom_device_ops *cdo = cdi->ops;
1774 size = sizeof(s->manufact.value) + 4;
1776 if ((buf = (u_char *) kmalloc(size, GFP_KERNEL)) == NULL)
1777 return -ENOMEM;
1779 init_cdrom_command(&cgc, buf, size, CGC_DATA_READ);
1780 cgc.cmd[0] = GPCMD_READ_DVD_STRUCTURE;
1781 cgc.cmd[7] = s->type;
1782 cgc.cmd[8] = size >> 8;
1783 cgc.cmd[9] = size & 0xff;
1785 if ((ret = cdo->generic_packet(cdi, &cgc))) {
1786 kfree(buf);
1787 return ret;
1790 s->manufact.len = buf[0] << 8 | buf[1];
1791 if (s->manufact.len < 0 || s->manufact.len > 2048) {
1792 cdinfo(CD_WARNING, "Received invalid manufacture info length"
1793 " (%d)\n", s->bca.len);
1794 ret = -EIO;
1795 } else {
1796 memcpy(s->manufact.value, &buf[4], s->manufact.len);
1799 kfree(buf);
1800 return ret;
1803 static int dvd_read_struct(struct cdrom_device_info *cdi, dvd_struct *s)
1805 switch (s->type) {
1806 case DVD_STRUCT_PHYSICAL:
1807 return dvd_read_physical(cdi, s);
1809 case DVD_STRUCT_COPYRIGHT:
1810 return dvd_read_copyright(cdi, s);
1812 case DVD_STRUCT_DISCKEY:
1813 return dvd_read_disckey(cdi, s);
1815 case DVD_STRUCT_BCA:
1816 return dvd_read_bca(cdi, s);
1818 case DVD_STRUCT_MANUFACT:
1819 return dvd_read_manufact(cdi, s);
1821 default:
1822 cdinfo(CD_WARNING, ": Invalid DVD structure read requested (%d)\n",
1823 s->type);
1824 return -EINVAL;
1828 int cdrom_mode_sense(struct cdrom_device_info *cdi,
1829 struct packet_command *cgc,
1830 int page_code, int page_control)
1832 struct cdrom_device_ops *cdo = cdi->ops;
1834 memset(cgc->cmd, 0, sizeof(cgc->cmd));
1836 cgc->cmd[0] = GPCMD_MODE_SENSE_10;
1837 cgc->cmd[2] = page_code | (page_control << 6);
1838 cgc->cmd[7] = cgc->buflen >> 8;
1839 cgc->cmd[8] = cgc->buflen & 0xff;
1840 cgc->data_direction = CGC_DATA_READ;
1841 return cdo->generic_packet(cdi, cgc);
1844 int cdrom_mode_select(struct cdrom_device_info *cdi,
1845 struct packet_command *cgc)
1847 struct cdrom_device_ops *cdo = cdi->ops;
1849 memset(cgc->cmd, 0, sizeof(cgc->cmd));
1850 memset(cgc->buffer, 0, 2);
1851 cgc->cmd[0] = GPCMD_MODE_SELECT_10;
1852 cgc->cmd[1] = 0x10; /* PF */
1853 cgc->cmd[7] = cgc->buflen >> 8;
1854 cgc->cmd[8] = cgc->buflen & 0xff;
1855 cgc->data_direction = CGC_DATA_WRITE;
1856 return cdo->generic_packet(cdi, cgc);
1859 static int cdrom_read_subchannel(struct cdrom_device_info *cdi,
1860 struct cdrom_subchnl *subchnl, int mcn)
1862 struct cdrom_device_ops *cdo = cdi->ops;
1863 struct packet_command cgc;
1864 char buffer[32];
1865 int ret;
1867 init_cdrom_command(&cgc, buffer, 16, CGC_DATA_READ);
1868 cgc.cmd[0] = GPCMD_READ_SUBCHANNEL;
1869 cgc.cmd[1] = 2; /* MSF addressing */
1870 cgc.cmd[2] = 0x40; /* request subQ data */
1871 cgc.cmd[3] = mcn ? 2 : 1;
1872 cgc.cmd[8] = 16;
1874 if ((ret = cdo->generic_packet(cdi, &cgc)))
1875 return ret;
1877 subchnl->cdsc_audiostatus = cgc.buffer[1];
1878 subchnl->cdsc_format = CDROM_MSF;
1879 subchnl->cdsc_ctrl = cgc.buffer[5] & 0xf;
1880 subchnl->cdsc_trk = cgc.buffer[6];
1881 subchnl->cdsc_ind = cgc.buffer[7];
1883 subchnl->cdsc_reladdr.msf.minute = cgc.buffer[13];
1884 subchnl->cdsc_reladdr.msf.second = cgc.buffer[14];
1885 subchnl->cdsc_reladdr.msf.frame = cgc.buffer[15];
1886 subchnl->cdsc_absaddr.msf.minute = cgc.buffer[9];
1887 subchnl->cdsc_absaddr.msf.second = cgc.buffer[10];
1888 subchnl->cdsc_absaddr.msf.frame = cgc.buffer[11];
1890 return 0;
1894 * Specific READ_10 interface
1896 static int cdrom_read_cd(struct cdrom_device_info *cdi,
1897 struct packet_command *cgc, int lba,
1898 int blocksize, int nblocks)
1900 struct cdrom_device_ops *cdo = cdi->ops;
1902 memset(&cgc->cmd, 0, sizeof(cgc->cmd));
1903 cgc->cmd[0] = GPCMD_READ_10;
1904 cgc->cmd[2] = (lba >> 24) & 0xff;
1905 cgc->cmd[3] = (lba >> 16) & 0xff;
1906 cgc->cmd[4] = (lba >> 8) & 0xff;
1907 cgc->cmd[5] = lba & 0xff;
1908 cgc->cmd[6] = (nblocks >> 16) & 0xff;
1909 cgc->cmd[7] = (nblocks >> 8) & 0xff;
1910 cgc->cmd[8] = nblocks & 0xff;
1911 cgc->buflen = blocksize * nblocks;
1912 return cdo->generic_packet(cdi, cgc);
1915 /* very generic interface for reading the various types of blocks */
1916 static int cdrom_read_block(struct cdrom_device_info *cdi,
1917 struct packet_command *cgc,
1918 int lba, int nblocks, int format, int blksize)
1920 struct cdrom_device_ops *cdo = cdi->ops;
1922 memset(&cgc->cmd, 0, sizeof(cgc->cmd));
1923 cgc->cmd[0] = GPCMD_READ_CD;
1924 /* expected sector size - cdda,mode1,etc. */
1925 cgc->cmd[1] = format << 2;
1926 /* starting address */
1927 cgc->cmd[2] = (lba >> 24) & 0xff;
1928 cgc->cmd[3] = (lba >> 16) & 0xff;
1929 cgc->cmd[4] = (lba >> 8) & 0xff;
1930 cgc->cmd[5] = lba & 0xff;
1931 /* number of blocks */
1932 cgc->cmd[6] = (nblocks >> 16) & 0xff;
1933 cgc->cmd[7] = (nblocks >> 8) & 0xff;
1934 cgc->cmd[8] = nblocks & 0xff;
1935 cgc->buflen = blksize * nblocks;
1937 /* set the header info returned */
1938 switch (blksize) {
1939 case CD_FRAMESIZE_RAW0 : cgc->cmd[9] = 0x58; break;
1940 case CD_FRAMESIZE_RAW1 : cgc->cmd[9] = 0x78; break;
1941 case CD_FRAMESIZE_RAW : cgc->cmd[9] = 0xf8; break;
1942 default : cgc->cmd[9] = 0x10;
1945 return cdo->generic_packet(cdi, cgc);
1948 static int cdrom_read_cdda_old(struct cdrom_device_info *cdi, __u8 __user *ubuf,
1949 int lba, int nframes)
1951 struct packet_command cgc;
1952 int ret = 0;
1953 int nr;
1955 cdi->last_sense = 0;
1957 memset(&cgc, 0, sizeof(cgc));
1960 * start with will ra.nframes size, back down if alloc fails
1962 nr = nframes;
1963 do {
1964 cgc.buffer = kmalloc(CD_FRAMESIZE_RAW * nr, GFP_KERNEL);
1965 if (cgc.buffer)
1966 break;
1968 nr >>= 1;
1969 } while (nr);
1971 if (!nr)
1972 return -ENOMEM;
1974 if (!access_ok(VERIFY_WRITE, ubuf, nframes * CD_FRAMESIZE_RAW)) {
1975 ret = -EFAULT;
1976 goto out;
1979 cgc.data_direction = CGC_DATA_READ;
1980 while (nframes > 0) {
1981 if (nr > nframes)
1982 nr = nframes;
1984 ret = cdrom_read_block(cdi, &cgc, lba, nr, 1, CD_FRAMESIZE_RAW);
1985 if (ret)
1986 break;
1987 if (__copy_to_user(ubuf, cgc.buffer, CD_FRAMESIZE_RAW * nr)) {
1988 ret = -EFAULT;
1989 break;
1991 ubuf += CD_FRAMESIZE_RAW * nr;
1992 nframes -= nr;
1993 lba += nr;
1995 out:
1996 kfree(cgc.buffer);
1997 return ret;
2000 static int cdrom_read_cdda_bpc(struct cdrom_device_info *cdi, __u8 __user *ubuf,
2001 int lba, int nframes)
2003 request_queue_t *q = cdi->disk->queue;
2004 struct request *rq;
2005 struct bio *bio;
2006 unsigned int len;
2007 int nr, ret = 0;
2009 if (!q)
2010 return -ENXIO;
2012 cdi->last_sense = 0;
2014 while (nframes) {
2015 nr = nframes;
2016 if (cdi->cdda_method == CDDA_BPC_SINGLE)
2017 nr = 1;
2018 if (nr * CD_FRAMESIZE_RAW > (q->max_sectors << 9))
2019 nr = (q->max_sectors << 9) / CD_FRAMESIZE_RAW;
2021 len = nr * CD_FRAMESIZE_RAW;
2023 rq = blk_rq_map_user(q, READ, ubuf, len);
2024 if (IS_ERR(rq))
2025 return PTR_ERR(rq);
2027 memset(rq->cmd, 0, sizeof(rq->cmd));
2028 rq->cmd[0] = GPCMD_READ_CD;
2029 rq->cmd[1] = 1 << 2;
2030 rq->cmd[2] = (lba >> 24) & 0xff;
2031 rq->cmd[3] = (lba >> 16) & 0xff;
2032 rq->cmd[4] = (lba >> 8) & 0xff;
2033 rq->cmd[5] = lba & 0xff;
2034 rq->cmd[6] = (nr >> 16) & 0xff;
2035 rq->cmd[7] = (nr >> 8) & 0xff;
2036 rq->cmd[8] = nr & 0xff;
2037 rq->cmd[9] = 0xf8;
2039 rq->cmd_len = 12;
2040 rq->flags |= REQ_BLOCK_PC;
2041 rq->timeout = 60 * HZ;
2042 bio = rq->bio;
2044 if (rq->bio)
2045 blk_queue_bounce(q, &rq->bio);
2047 if (blk_execute_rq(q, cdi->disk, rq)) {
2048 struct request_sense *s = rq->sense;
2049 ret = -EIO;
2050 cdi->last_sense = s->sense_key;
2053 if (blk_rq_unmap_user(rq, bio, len))
2054 ret = -EFAULT;
2056 if (ret)
2057 break;
2059 nframes -= nr;
2060 lba += nr;
2061 ubuf += len;
2064 return ret;
2067 static int cdrom_read_cdda(struct cdrom_device_info *cdi, __u8 __user *ubuf,
2068 int lba, int nframes)
2070 int ret;
2072 if (cdi->cdda_method == CDDA_OLD)
2073 return cdrom_read_cdda_old(cdi, ubuf, lba, nframes);
2075 retry:
2077 * for anything else than success and io error, we need to retry
2079 ret = cdrom_read_cdda_bpc(cdi, ubuf, lba, nframes);
2080 if (!ret || ret != -EIO)
2081 return ret;
2084 * I've seen drives get sense 4/8/3 udma crc errors on multi
2085 * frame dma, so drop to single frame dma if we need to
2087 if (cdi->cdda_method == CDDA_BPC_FULL && nframes > 1) {
2088 printk("cdrom: dropping to single frame dma\n");
2089 cdi->cdda_method = CDDA_BPC_SINGLE;
2090 goto retry;
2094 * so we have an io error of some sort with multi frame dma. if the
2095 * condition wasn't a hardware error
2096 * problems, not for any error
2098 if (cdi->last_sense != 0x04 && cdi->last_sense != 0x0b)
2099 return ret;
2101 printk("cdrom: dropping to old style cdda (sense=%x)\n", cdi->last_sense);
2102 cdi->cdda_method = CDDA_OLD;
2103 return cdrom_read_cdda_old(cdi, ubuf, lba, nframes);
2106 /* Just about every imaginable ioctl is supported in the Uniform layer
2107 * these days. ATAPI / SCSI specific code now mainly resides in
2108 * mmc_ioct().
2110 int cdrom_ioctl(struct file * file, struct cdrom_device_info *cdi,
2111 struct inode *ip, unsigned int cmd, unsigned long arg)
2113 struct cdrom_device_ops *cdo = cdi->ops;
2114 int ret;
2116 /* Try the generic SCSI command ioctl's first.. */
2117 ret = scsi_cmd_ioctl(file, ip->i_bdev->bd_disk, cmd, (void __user *)arg);
2118 if (ret != -ENOTTY)
2119 return ret;
2121 /* the first few commands do not deal with audio drive_info, but
2122 only with routines in cdrom device operations. */
2123 switch (cmd) {
2124 case CDROMMULTISESSION: {
2125 struct cdrom_multisession ms_info;
2126 u_char requested_format;
2127 cdinfo(CD_DO_IOCTL, "entering CDROMMULTISESSION\n");
2128 if (!(cdo->capability & CDC_MULTI_SESSION))
2129 return -ENOSYS;
2130 IOCTL_IN(arg, struct cdrom_multisession, ms_info);
2131 requested_format = ms_info.addr_format;
2132 if (!((requested_format == CDROM_MSF) ||
2133 (requested_format == CDROM_LBA)))
2134 return -EINVAL;
2135 ms_info.addr_format = CDROM_LBA;
2136 if ((ret=cdo->get_last_session(cdi, &ms_info)))
2137 return ret;
2138 sanitize_format(&ms_info.addr, &ms_info.addr_format,
2139 requested_format);
2140 IOCTL_OUT(arg, struct cdrom_multisession, ms_info);
2141 cdinfo(CD_DO_IOCTL, "CDROMMULTISESSION successful\n");
2142 return 0;
2145 case CDROMEJECT: {
2146 cdinfo(CD_DO_IOCTL, "entering CDROMEJECT\n");
2147 if (!CDROM_CAN(CDC_OPEN_TRAY))
2148 return -ENOSYS;
2149 if (cdi->use_count != 1 || keeplocked)
2150 return -EBUSY;
2151 if (CDROM_CAN(CDC_LOCK))
2152 if ((ret=cdo->lock_door(cdi, 0)))
2153 return ret;
2155 return cdo->tray_move(cdi, 1);
2158 case CDROMCLOSETRAY: {
2159 cdinfo(CD_DO_IOCTL, "entering CDROMCLOSETRAY\n");
2160 if (!CDROM_CAN(CDC_CLOSE_TRAY))
2161 return -ENOSYS;
2162 return cdo->tray_move(cdi, 0);
2165 case CDROMEJECT_SW: {
2166 cdinfo(CD_DO_IOCTL, "entering CDROMEJECT_SW\n");
2167 if (!CDROM_CAN(CDC_OPEN_TRAY))
2168 return -ENOSYS;
2169 if (keeplocked)
2170 return -EBUSY;
2171 cdi->options &= ~(CDO_AUTO_CLOSE | CDO_AUTO_EJECT);
2172 if (arg)
2173 cdi->options |= CDO_AUTO_CLOSE | CDO_AUTO_EJECT;
2174 return 0;
2177 case CDROM_MEDIA_CHANGED: {
2178 struct cdrom_changer_info *info;
2179 int changed;
2181 cdinfo(CD_DO_IOCTL, "entering CDROM_MEDIA_CHANGED\n");
2182 if (!CDROM_CAN(CDC_MEDIA_CHANGED))
2183 return -ENOSYS;
2185 /* cannot select disc or select current disc */
2186 if (!CDROM_CAN(CDC_SELECT_DISC) || arg == CDSL_CURRENT)
2187 return media_changed(cdi, 1);
2189 if ((unsigned int)arg >= cdi->capacity)
2190 return -EINVAL;
2192 info = kmalloc(sizeof(*info), GFP_KERNEL);
2193 if (!info)
2194 return -ENOMEM;
2196 if ((ret = cdrom_read_mech_status(cdi, info))) {
2197 kfree(info);
2198 return ret;
2201 changed = info->slots[arg].change;
2202 kfree(info);
2203 return changed;
2206 case CDROM_SET_OPTIONS: {
2207 cdinfo(CD_DO_IOCTL, "entering CDROM_SET_OPTIONS\n");
2208 /* options need to be in sync with capability. too late for
2209 that, so we have to check each one separately... */
2210 switch (arg) {
2211 case CDO_USE_FFLAGS:
2212 case CDO_CHECK_TYPE:
2213 break;
2214 case CDO_LOCK:
2215 if (!CDROM_CAN(CDC_LOCK))
2216 return -ENOSYS;
2217 break;
2218 case 0:
2219 return cdi->options;
2220 /* default is basically CDO_[AUTO_CLOSE|AUTO_EJECT] */
2221 default:
2222 if (!CDROM_CAN(arg))
2223 return -ENOSYS;
2225 cdi->options |= (int) arg;
2226 return cdi->options;
2229 case CDROM_CLEAR_OPTIONS: {
2230 cdinfo(CD_DO_IOCTL, "entering CDROM_CLEAR_OPTIONS\n");
2231 cdi->options &= ~(int) arg;
2232 return cdi->options;
2235 case CDROM_SELECT_SPEED: {
2236 cdinfo(CD_DO_IOCTL, "entering CDROM_SELECT_SPEED\n");
2237 if (!CDROM_CAN(CDC_SELECT_SPEED))
2238 return -ENOSYS;
2239 return cdo->select_speed(cdi, arg);
2242 case CDROM_SELECT_DISC: {
2243 cdinfo(CD_DO_IOCTL, "entering CDROM_SELECT_DISC\n");
2244 if (!CDROM_CAN(CDC_SELECT_DISC))
2245 return -ENOSYS;
2247 if ((arg != CDSL_CURRENT) && (arg != CDSL_NONE))
2248 if ((int)arg >= cdi->capacity)
2249 return -EINVAL;
2251 /* cdo->select_disc is a hook to allow a driver-specific
2252 * way of seleting disc. However, since there is no
2253 * equiv hook for cdrom_slot_status this may not
2254 * actually be useful...
2256 if (cdo->select_disc != NULL)
2257 return cdo->select_disc(cdi, arg);
2259 /* no driver specific select_disc(), call our own */
2260 cdinfo(CD_CHANGER, "Using generic cdrom_select_disc()\n");
2261 return cdrom_select_disc(cdi, arg);
2264 case CDROMRESET: {
2265 if (!capable(CAP_SYS_ADMIN))
2266 return -EACCES;
2267 cdinfo(CD_DO_IOCTL, "entering CDROM_RESET\n");
2268 if (!CDROM_CAN(CDC_RESET))
2269 return -ENOSYS;
2270 invalidate_bdev(ip->i_bdev, 0);
2271 return cdo->reset(cdi);
2274 case CDROM_LOCKDOOR: {
2275 cdinfo(CD_DO_IOCTL, "%socking door.\n", arg ? "L" : "Unl");
2276 if (!CDROM_CAN(CDC_LOCK))
2277 return -EDRIVE_CANT_DO_THIS;
2278 keeplocked = arg ? 1 : 0;
2279 /* don't unlock the door on multiple opens,but allow root
2280 * to do so */
2281 if ((cdi->use_count != 1) && !arg && !capable(CAP_SYS_ADMIN))
2282 return -EBUSY;
2283 return cdo->lock_door(cdi, arg);
2286 case CDROM_DEBUG: {
2287 if (!capable(CAP_SYS_ADMIN))
2288 return -EACCES;
2289 cdinfo(CD_DO_IOCTL, "%sabling debug.\n", arg ? "En" : "Dis");
2290 debug = arg ? 1 : 0;
2291 return debug;
2294 case CDROM_GET_CAPABILITY: {
2295 cdinfo(CD_DO_IOCTL, "entering CDROM_GET_CAPABILITY\n");
2296 return (cdo->capability & ~cdi->mask);
2299 /* The following function is implemented, although very few audio
2300 * discs give Universal Product Code information, which should just be
2301 * the Medium Catalog Number on the box. Note, that the way the code
2302 * is written on the CD is /not/ uniform across all discs!
2304 case CDROM_GET_MCN: {
2305 struct cdrom_mcn mcn;
2306 cdinfo(CD_DO_IOCTL, "entering CDROM_GET_MCN\n");
2307 if (!(cdo->capability & CDC_MCN))
2308 return -ENOSYS;
2309 if ((ret=cdo->get_mcn(cdi, &mcn)))
2310 return ret;
2311 IOCTL_OUT(arg, struct cdrom_mcn, mcn);
2312 cdinfo(CD_DO_IOCTL, "CDROM_GET_MCN successful\n");
2313 return 0;
2316 case CDROM_DRIVE_STATUS: {
2317 cdinfo(CD_DO_IOCTL, "entering CDROM_DRIVE_STATUS\n");
2318 if (!(cdo->capability & CDC_DRIVE_STATUS))
2319 return -ENOSYS;
2320 if (!CDROM_CAN(CDC_SELECT_DISC))
2321 return cdo->drive_status(cdi, CDSL_CURRENT);
2322 if ((arg == CDSL_CURRENT) || (arg == CDSL_NONE))
2323 return cdo->drive_status(cdi, CDSL_CURRENT);
2324 if (((int)arg >= cdi->capacity))
2325 return -EINVAL;
2326 return cdrom_slot_status(cdi, arg);
2329 /* Ok, this is where problems start. The current interface for the
2330 CDROM_DISC_STATUS ioctl is flawed. It makes the false assumption
2331 that CDs are all CDS_DATA_1 or all CDS_AUDIO, etc. Unfortunatly,
2332 while this is often the case, it is also very common for CDs to
2333 have some tracks with data, and some tracks with audio. Just
2334 because I feel like it, I declare the following to be the best
2335 way to cope. If the CD has ANY data tracks on it, it will be
2336 returned as a data CD. If it has any XA tracks, I will return
2337 it as that. Now I could simplify this interface by combining these
2338 returns with the above, but this more clearly demonstrates
2339 the problem with the current interface. Too bad this wasn't
2340 designed to use bitmasks... -Erik
2342 Well, now we have the option CDS_MIXED: a mixed-type CD.
2343 User level programmers might feel the ioctl is not very useful.
2344 ---david
2346 case CDROM_DISC_STATUS: {
2347 tracktype tracks;
2348 cdinfo(CD_DO_IOCTL, "entering CDROM_DISC_STATUS\n");
2349 cdrom_count_tracks(cdi, &tracks);
2350 if (tracks.error)
2351 return(tracks.error);
2353 /* Policy mode on */
2354 if (tracks.audio > 0) {
2355 if (tracks.data==0 && tracks.cdi==0 && tracks.xa==0)
2356 return CDS_AUDIO;
2357 else
2358 return CDS_MIXED;
2360 if (tracks.cdi > 0) return CDS_XA_2_2;
2361 if (tracks.xa > 0) return CDS_XA_2_1;
2362 if (tracks.data > 0) return CDS_DATA_1;
2363 /* Policy mode off */
2365 cdinfo(CD_WARNING,"This disc doesn't have any tracks I recognize!\n");
2366 return CDS_NO_INFO;
2369 case CDROM_CHANGER_NSLOTS: {
2370 cdinfo(CD_DO_IOCTL, "entering CDROM_CHANGER_NSLOTS\n");
2371 return cdi->capacity;
2375 /* use the ioctls that are implemented through the generic_packet()
2376 interface. this may look at bit funny, but if -ENOTTY is
2377 returned that particular ioctl is not implemented and we
2378 let it go through the device specific ones. */
2379 if (CDROM_CAN(CDC_GENERIC_PACKET)) {
2380 ret = mmc_ioctl(cdi, cmd, arg);
2381 if (ret != -ENOTTY) {
2382 return ret;
2386 /* note: most of the cdinfo() calls are commented out here,
2387 because they fill up the sys log when CD players poll
2388 the drive. */
2389 switch (cmd) {
2390 case CDROMSUBCHNL: {
2391 struct cdrom_subchnl q;
2392 u_char requested, back;
2393 if (!CDROM_CAN(CDC_PLAY_AUDIO))
2394 return -ENOSYS;
2395 /* cdinfo(CD_DO_IOCTL,"entering CDROMSUBCHNL\n");*/
2396 IOCTL_IN(arg, struct cdrom_subchnl, q);
2397 requested = q.cdsc_format;
2398 if (!((requested == CDROM_MSF) ||
2399 (requested == CDROM_LBA)))
2400 return -EINVAL;
2401 q.cdsc_format = CDROM_MSF;
2402 if ((ret=cdo->audio_ioctl(cdi, cmd, &q)))
2403 return ret;
2404 back = q.cdsc_format; /* local copy */
2405 sanitize_format(&q.cdsc_absaddr, &back, requested);
2406 sanitize_format(&q.cdsc_reladdr, &q.cdsc_format, requested);
2407 IOCTL_OUT(arg, struct cdrom_subchnl, q);
2408 /* cdinfo(CD_DO_IOCTL, "CDROMSUBCHNL successful\n"); */
2409 return 0;
2411 case CDROMREADTOCHDR: {
2412 struct cdrom_tochdr header;
2413 if (!CDROM_CAN(CDC_PLAY_AUDIO))
2414 return -ENOSYS;
2415 /* cdinfo(CD_DO_IOCTL, "entering CDROMREADTOCHDR\n"); */
2416 IOCTL_IN(arg, struct cdrom_tochdr, header);
2417 if ((ret=cdo->audio_ioctl(cdi, cmd, &header)))
2418 return ret;
2419 IOCTL_OUT(arg, struct cdrom_tochdr, header);
2420 /* cdinfo(CD_DO_IOCTL, "CDROMREADTOCHDR successful\n"); */
2421 return 0;
2423 case CDROMREADTOCENTRY: {
2424 struct cdrom_tocentry entry;
2425 u_char requested_format;
2426 if (!CDROM_CAN(CDC_PLAY_AUDIO))
2427 return -ENOSYS;
2428 /* cdinfo(CD_DO_IOCTL, "entering CDROMREADTOCENTRY\n"); */
2429 IOCTL_IN(arg, struct cdrom_tocentry, entry);
2430 requested_format = entry.cdte_format;
2431 if (!((requested_format == CDROM_MSF) ||
2432 (requested_format == CDROM_LBA)))
2433 return -EINVAL;
2434 /* make interface to low-level uniform */
2435 entry.cdte_format = CDROM_MSF;
2436 if ((ret=cdo->audio_ioctl(cdi, cmd, &entry)))
2437 return ret;
2438 sanitize_format(&entry.cdte_addr,
2439 &entry.cdte_format, requested_format);
2440 IOCTL_OUT(arg, struct cdrom_tocentry, entry);
2441 /* cdinfo(CD_DO_IOCTL, "CDROMREADTOCENTRY successful\n"); */
2442 return 0;
2444 case CDROMPLAYMSF: {
2445 struct cdrom_msf msf;
2446 if (!CDROM_CAN(CDC_PLAY_AUDIO))
2447 return -ENOSYS;
2448 cdinfo(CD_DO_IOCTL, "entering CDROMPLAYMSF\n");
2449 IOCTL_IN(arg, struct cdrom_msf, msf);
2450 return cdo->audio_ioctl(cdi, cmd, &msf);
2452 case CDROMPLAYTRKIND: {
2453 struct cdrom_ti ti;
2454 if (!CDROM_CAN(CDC_PLAY_AUDIO))
2455 return -ENOSYS;
2456 cdinfo(CD_DO_IOCTL, "entering CDROMPLAYTRKIND\n");
2457 IOCTL_IN(arg, struct cdrom_ti, ti);
2458 CHECKAUDIO;
2459 return cdo->audio_ioctl(cdi, cmd, &ti);
2461 case CDROMVOLCTRL: {
2462 struct cdrom_volctrl volume;
2463 if (!CDROM_CAN(CDC_PLAY_AUDIO))
2464 return -ENOSYS;
2465 cdinfo(CD_DO_IOCTL, "entering CDROMVOLCTRL\n");
2466 IOCTL_IN(arg, struct cdrom_volctrl, volume);
2467 return cdo->audio_ioctl(cdi, cmd, &volume);
2469 case CDROMVOLREAD: {
2470 struct cdrom_volctrl volume;
2471 if (!CDROM_CAN(CDC_PLAY_AUDIO))
2472 return -ENOSYS;
2473 cdinfo(CD_DO_IOCTL, "entering CDROMVOLREAD\n");
2474 if ((ret=cdo->audio_ioctl(cdi, cmd, &volume)))
2475 return ret;
2476 IOCTL_OUT(arg, struct cdrom_volctrl, volume);
2477 return 0;
2479 case CDROMSTART:
2480 case CDROMSTOP:
2481 case CDROMPAUSE:
2482 case CDROMRESUME: {
2483 if (!CDROM_CAN(CDC_PLAY_AUDIO))
2484 return -ENOSYS;
2485 cdinfo(CD_DO_IOCTL, "doing audio ioctl (start/stop/pause/resume)\n");
2486 CHECKAUDIO;
2487 return cdo->audio_ioctl(cdi, cmd, NULL);
2489 } /* switch */
2491 /* do the device specific ioctls */
2492 if (CDROM_CAN(CDC_IOCTLS))
2493 return cdo->dev_ioctl(cdi, cmd, arg);
2495 return -ENOSYS;
2498 static inline
2499 int msf_to_lba(char m, char s, char f)
2501 return (((m * CD_SECS) + s) * CD_FRAMES + f) - CD_MSF_OFFSET;
2505 * Required when we need to use READ_10 to issue other than 2048 block
2506 * reads
2508 static int cdrom_switch_blocksize(struct cdrom_device_info *cdi, int size)
2510 struct cdrom_device_ops *cdo = cdi->ops;
2511 struct packet_command cgc;
2512 struct modesel_head mh;
2514 memset(&mh, 0, sizeof(mh));
2515 mh.block_desc_length = 0x08;
2516 mh.block_length_med = (size >> 8) & 0xff;
2517 mh.block_length_lo = size & 0xff;
2519 memset(&cgc, 0, sizeof(cgc));
2520 cgc.cmd[0] = 0x15;
2521 cgc.cmd[1] = 1 << 4;
2522 cgc.cmd[4] = 12;
2523 cgc.buflen = sizeof(mh);
2524 cgc.buffer = (char *) &mh;
2525 cgc.data_direction = CGC_DATA_WRITE;
2526 mh.block_desc_length = 0x08;
2527 mh.block_length_med = (size >> 8) & 0xff;
2528 mh.block_length_lo = size & 0xff;
2530 return cdo->generic_packet(cdi, &cgc);
2533 static int mmc_ioctl(struct cdrom_device_info *cdi, unsigned int cmd,
2534 unsigned long arg)
2536 struct cdrom_device_ops *cdo = cdi->ops;
2537 struct packet_command cgc;
2538 struct request_sense sense;
2539 unsigned char buffer[32];
2540 int ret = 0;
2542 memset(&cgc, 0, sizeof(cgc));
2544 /* build a unified command and queue it through
2545 cdo->generic_packet() */
2546 switch (cmd) {
2547 case CDROMREADRAW:
2548 case CDROMREADMODE1:
2549 case CDROMREADMODE2: {
2550 struct cdrom_msf msf;
2551 int blocksize = 0, format = 0, lba;
2553 switch (cmd) {
2554 case CDROMREADRAW:
2555 blocksize = CD_FRAMESIZE_RAW;
2556 break;
2557 case CDROMREADMODE1:
2558 blocksize = CD_FRAMESIZE;
2559 format = 2;
2560 break;
2561 case CDROMREADMODE2:
2562 blocksize = CD_FRAMESIZE_RAW0;
2563 break;
2565 IOCTL_IN(arg, struct cdrom_msf, msf);
2566 lba = msf_to_lba(msf.cdmsf_min0,msf.cdmsf_sec0,msf.cdmsf_frame0);
2567 /* FIXME: we need upper bound checking, too!! */
2568 if (lba < 0)
2569 return -EINVAL;
2570 cgc.buffer = (char *) kmalloc(blocksize, GFP_KERNEL);
2571 if (cgc.buffer == NULL)
2572 return -ENOMEM;
2573 memset(&sense, 0, sizeof(sense));
2574 cgc.sense = &sense;
2575 cgc.data_direction = CGC_DATA_READ;
2576 ret = cdrom_read_block(cdi, &cgc, lba, 1, format, blocksize);
2577 if (ret && sense.sense_key==0x05 && sense.asc==0x20 && sense.ascq==0x00) {
2579 * SCSI-II devices are not required to support
2580 * READ_CD, so let's try switching block size
2582 /* FIXME: switch back again... */
2583 if ((ret = cdrom_switch_blocksize(cdi, blocksize))) {
2584 kfree(cgc.buffer);
2585 return ret;
2587 cgc.sense = NULL;
2588 ret = cdrom_read_cd(cdi, &cgc, lba, blocksize, 1);
2589 ret |= cdrom_switch_blocksize(cdi, blocksize);
2591 if (!ret && copy_to_user((char __user *)arg, cgc.buffer, blocksize))
2592 ret = -EFAULT;
2593 kfree(cgc.buffer);
2594 return ret;
2596 case CDROMREADAUDIO: {
2597 struct cdrom_read_audio ra;
2598 int lba;
2600 IOCTL_IN(arg, struct cdrom_read_audio, ra);
2602 if (ra.addr_format == CDROM_MSF)
2603 lba = msf_to_lba(ra.addr.msf.minute,
2604 ra.addr.msf.second,
2605 ra.addr.msf.frame);
2606 else if (ra.addr_format == CDROM_LBA)
2607 lba = ra.addr.lba;
2608 else
2609 return -EINVAL;
2611 /* FIXME: we need upper bound checking, too!! */
2612 if (lba < 0 || ra.nframes <= 0 || ra.nframes > CD_FRAMES)
2613 return -EINVAL;
2615 return cdrom_read_cdda(cdi, ra.buf, lba, ra.nframes);
2617 case CDROMSUBCHNL: {
2618 struct cdrom_subchnl q;
2619 u_char requested, back;
2620 IOCTL_IN(arg, struct cdrom_subchnl, q);
2621 requested = q.cdsc_format;
2622 if (!((requested == CDROM_MSF) ||
2623 (requested == CDROM_LBA)))
2624 return -EINVAL;
2625 q.cdsc_format = CDROM_MSF;
2626 if ((ret = cdrom_read_subchannel(cdi, &q, 0)))
2627 return ret;
2628 back = q.cdsc_format; /* local copy */
2629 sanitize_format(&q.cdsc_absaddr, &back, requested);
2630 sanitize_format(&q.cdsc_reladdr, &q.cdsc_format, requested);
2631 IOCTL_OUT(arg, struct cdrom_subchnl, q);
2632 /* cdinfo(CD_DO_IOCTL, "CDROMSUBCHNL successful\n"); */
2633 return 0;
2635 case CDROMPLAYMSF: {
2636 struct cdrom_msf msf;
2637 cdinfo(CD_DO_IOCTL, "entering CDROMPLAYMSF\n");
2638 IOCTL_IN(arg, struct cdrom_msf, msf);
2639 cgc.cmd[0] = GPCMD_PLAY_AUDIO_MSF;
2640 cgc.cmd[3] = msf.cdmsf_min0;
2641 cgc.cmd[4] = msf.cdmsf_sec0;
2642 cgc.cmd[5] = msf.cdmsf_frame0;
2643 cgc.cmd[6] = msf.cdmsf_min1;
2644 cgc.cmd[7] = msf.cdmsf_sec1;
2645 cgc.cmd[8] = msf.cdmsf_frame1;
2646 cgc.data_direction = CGC_DATA_NONE;
2647 return cdo->generic_packet(cdi, &cgc);
2649 case CDROMPLAYBLK: {
2650 struct cdrom_blk blk;
2651 cdinfo(CD_DO_IOCTL, "entering CDROMPLAYBLK\n");
2652 IOCTL_IN(arg, struct cdrom_blk, blk);
2653 cgc.cmd[0] = GPCMD_PLAY_AUDIO_10;
2654 cgc.cmd[2] = (blk.from >> 24) & 0xff;
2655 cgc.cmd[3] = (blk.from >> 16) & 0xff;
2656 cgc.cmd[4] = (blk.from >> 8) & 0xff;
2657 cgc.cmd[5] = blk.from & 0xff;
2658 cgc.cmd[7] = (blk.len >> 8) & 0xff;
2659 cgc.cmd[8] = blk.len & 0xff;
2660 cgc.data_direction = CGC_DATA_NONE;
2661 return cdo->generic_packet(cdi, &cgc);
2663 case CDROMVOLCTRL:
2664 case CDROMVOLREAD: {
2665 struct cdrom_volctrl volctrl;
2666 char mask[sizeof(buffer)];
2667 unsigned short offset;
2669 cdinfo(CD_DO_IOCTL, "entering CDROMVOLUME\n");
2671 IOCTL_IN(arg, struct cdrom_volctrl, volctrl);
2673 cgc.buffer = buffer;
2674 cgc.buflen = 24;
2675 if ((ret = cdrom_mode_sense(cdi, &cgc, GPMODE_AUDIO_CTL_PAGE, 0)))
2676 return ret;
2678 /* originally the code depended on buffer[1] to determine
2679 how much data is available for transfer. buffer[1] is
2680 unfortunately ambigious and the only reliable way seem
2681 to be to simply skip over the block descriptor... */
2682 offset = 8 + be16_to_cpu(*(unsigned short *)(buffer+6));
2684 if (offset + 16 > sizeof(buffer))
2685 return -E2BIG;
2687 if (offset + 16 > cgc.buflen) {
2688 cgc.buflen = offset+16;
2689 ret = cdrom_mode_sense(cdi, &cgc,
2690 GPMODE_AUDIO_CTL_PAGE, 0);
2691 if (ret)
2692 return ret;
2695 /* sanity check */
2696 if ((buffer[offset] & 0x3f) != GPMODE_AUDIO_CTL_PAGE ||
2697 buffer[offset+1] < 14)
2698 return -EINVAL;
2700 /* now we have the current volume settings. if it was only
2701 a CDROMVOLREAD, return these values */
2702 if (cmd == CDROMVOLREAD) {
2703 volctrl.channel0 = buffer[offset+9];
2704 volctrl.channel1 = buffer[offset+11];
2705 volctrl.channel2 = buffer[offset+13];
2706 volctrl.channel3 = buffer[offset+15];
2707 IOCTL_OUT(arg, struct cdrom_volctrl, volctrl);
2708 return 0;
2711 /* get the volume mask */
2712 cgc.buffer = mask;
2713 if ((ret = cdrom_mode_sense(cdi, &cgc,
2714 GPMODE_AUDIO_CTL_PAGE, 1)))
2715 return ret;
2717 buffer[offset+9] = volctrl.channel0 & mask[offset+9];
2718 buffer[offset+11] = volctrl.channel1 & mask[offset+11];
2719 buffer[offset+13] = volctrl.channel2 & mask[offset+13];
2720 buffer[offset+15] = volctrl.channel3 & mask[offset+15];
2722 /* set volume */
2723 cgc.buffer = buffer + offset - 8;
2724 memset(cgc.buffer, 0, 8);
2725 return cdrom_mode_select(cdi, &cgc);
2728 case CDROMSTART:
2729 case CDROMSTOP: {
2730 cdinfo(CD_DO_IOCTL, "entering CDROMSTART/CDROMSTOP\n");
2731 cgc.cmd[0] = GPCMD_START_STOP_UNIT;
2732 cgc.cmd[1] = 1;
2733 cgc.cmd[4] = (cmd == CDROMSTART) ? 1 : 0;
2734 cgc.data_direction = CGC_DATA_NONE;
2735 return cdo->generic_packet(cdi, &cgc);
2738 case CDROMPAUSE:
2739 case CDROMRESUME: {
2740 cdinfo(CD_DO_IOCTL, "entering CDROMPAUSE/CDROMRESUME\n");
2741 cgc.cmd[0] = GPCMD_PAUSE_RESUME;
2742 cgc.cmd[8] = (cmd == CDROMRESUME) ? 1 : 0;
2743 cgc.data_direction = CGC_DATA_NONE;
2744 return cdo->generic_packet(cdi, &cgc);
2747 case DVD_READ_STRUCT: {
2748 dvd_struct *s;
2749 int size = sizeof(dvd_struct);
2750 if (!CDROM_CAN(CDC_DVD))
2751 return -ENOSYS;
2752 if ((s = (dvd_struct *) kmalloc(size, GFP_KERNEL)) == NULL)
2753 return -ENOMEM;
2754 cdinfo(CD_DO_IOCTL, "entering DVD_READ_STRUCT\n");
2755 if (copy_from_user(s, (dvd_struct __user *)arg, size)) {
2756 kfree(s);
2757 return -EFAULT;
2759 if ((ret = dvd_read_struct(cdi, s))) {
2760 kfree(s);
2761 return ret;
2763 if (copy_to_user((dvd_struct __user *)arg, s, size))
2764 ret = -EFAULT;
2765 kfree(s);
2766 return ret;
2769 case DVD_AUTH: {
2770 dvd_authinfo ai;
2771 if (!CDROM_CAN(CDC_DVD))
2772 return -ENOSYS;
2773 cdinfo(CD_DO_IOCTL, "entering DVD_AUTH\n");
2774 IOCTL_IN(arg, dvd_authinfo, ai);
2775 if ((ret = dvd_do_auth (cdi, &ai)))
2776 return ret;
2777 IOCTL_OUT(arg, dvd_authinfo, ai);
2778 return 0;
2781 case CDROM_NEXT_WRITABLE: {
2782 long next = 0;
2783 cdinfo(CD_DO_IOCTL, "entering CDROM_NEXT_WRITABLE\n");
2784 if ((ret = cdrom_get_next_writable(cdi, &next)))
2785 return ret;
2786 IOCTL_OUT(arg, long, next);
2787 return 0;
2789 case CDROM_LAST_WRITTEN: {
2790 long last = 0;
2791 cdinfo(CD_DO_IOCTL, "entering CDROM_LAST_WRITTEN\n");
2792 if ((ret = cdrom_get_last_written(cdi, &last)))
2793 return ret;
2794 IOCTL_OUT(arg, long, last);
2795 return 0;
2797 } /* switch */
2799 return -ENOTTY;
2802 static int cdrom_get_track_info(struct cdrom_device_info *cdi, __u16 track, __u8 type,
2803 track_information *ti)
2805 struct cdrom_device_ops *cdo = cdi->ops;
2806 struct packet_command cgc;
2807 int ret, buflen;
2809 init_cdrom_command(&cgc, ti, 8, CGC_DATA_READ);
2810 cgc.cmd[0] = GPCMD_READ_TRACK_RZONE_INFO;
2811 cgc.cmd[1] = type & 3;
2812 cgc.cmd[4] = (track & 0xff00) >> 8;
2813 cgc.cmd[5] = track & 0xff;
2814 cgc.cmd[8] = 8;
2815 cgc.quiet = 1;
2817 if ((ret = cdo->generic_packet(cdi, &cgc)))
2818 return ret;
2820 buflen = be16_to_cpu(ti->track_information_length) +
2821 sizeof(ti->track_information_length);
2823 if (buflen > sizeof(track_information))
2824 buflen = sizeof(track_information);
2826 cgc.cmd[8] = cgc.buflen = buflen;
2827 if ((ret = cdo->generic_packet(cdi, &cgc)))
2828 return ret;
2830 /* return actual fill size */
2831 return buflen;
2834 /* requires CD R/RW */
2835 static int cdrom_get_disc_info(struct cdrom_device_info *cdi, disc_information *di)
2837 struct cdrom_device_ops *cdo = cdi->ops;
2838 struct packet_command cgc;
2839 int ret, buflen;
2841 /* set up command and get the disc info */
2842 init_cdrom_command(&cgc, di, sizeof(*di), CGC_DATA_READ);
2843 cgc.cmd[0] = GPCMD_READ_DISC_INFO;
2844 cgc.cmd[8] = cgc.buflen = 2;
2845 cgc.quiet = 1;
2847 if ((ret = cdo->generic_packet(cdi, &cgc)))
2848 return ret;
2850 /* not all drives have the same disc_info length, so requeue
2851 * packet with the length the drive tells us it can supply
2853 buflen = be16_to_cpu(di->disc_information_length) +
2854 sizeof(di->disc_information_length);
2856 if (buflen > sizeof(disc_information))
2857 buflen = sizeof(disc_information);
2859 cgc.cmd[8] = cgc.buflen = buflen;
2860 if ((ret = cdo->generic_packet(cdi, &cgc)))
2861 return ret;
2863 /* return actual fill size */
2864 return buflen;
2867 /* return the last written block on the CD-R media. this is for the udf
2868 file system. */
2869 int cdrom_get_last_written(struct cdrom_device_info *cdi, long *last_written)
2871 struct cdrom_tocentry toc;
2872 disc_information di;
2873 track_information ti;
2874 __u32 last_track;
2875 int ret = -1, ti_size;
2877 if (!CDROM_CAN(CDC_GENERIC_PACKET))
2878 goto use_toc;
2880 ret = cdrom_get_disc_info(cdi, &di);
2881 if (ret < (int)(offsetof(typeof(di), last_track_lsb)
2882 + sizeof(di.last_track_lsb)))
2883 goto use_toc;
2885 /* if unit didn't return msb, it's zeroed by cdrom_get_disc_info */
2886 last_track = (di.last_track_msb << 8) | di.last_track_lsb;
2887 ti_size = cdrom_get_track_info(cdi, last_track, 1, &ti);
2888 if (ti_size < (int)offsetof(typeof(ti), track_start))
2889 goto use_toc;
2891 /* if this track is blank, try the previous. */
2892 if (ti.blank) {
2893 if (last_track==1)
2894 goto use_toc;
2895 last_track--;
2896 ti_size = cdrom_get_track_info(cdi, last_track, 1, &ti);
2899 if (ti_size < (int)(offsetof(typeof(ti), track_size)
2900 + sizeof(ti.track_size)))
2901 goto use_toc;
2903 /* if last recorded field is valid, return it. */
2904 if (ti.lra_v && ti_size >= (int)(offsetof(typeof(ti), last_rec_address)
2905 + sizeof(ti.last_rec_address))) {
2906 *last_written = be32_to_cpu(ti.last_rec_address);
2907 } else {
2908 /* make it up instead */
2909 *last_written = be32_to_cpu(ti.track_start) +
2910 be32_to_cpu(ti.track_size);
2911 if (ti.free_blocks)
2912 *last_written -= (be32_to_cpu(ti.free_blocks) + 7);
2914 return 0;
2916 /* this is where we end up if the drive either can't do a
2917 GPCMD_READ_DISC_INFO or GPCMD_READ_TRACK_RZONE_INFO or if
2918 it doesn't give enough information or fails. then we return
2919 the toc contents. */
2920 use_toc:
2921 toc.cdte_format = CDROM_MSF;
2922 toc.cdte_track = CDROM_LEADOUT;
2923 if ((ret = cdi->ops->audio_ioctl(cdi, CDROMREADTOCENTRY, &toc)))
2924 return ret;
2925 sanitize_format(&toc.cdte_addr, &toc.cdte_format, CDROM_LBA);
2926 *last_written = toc.cdte_addr.lba;
2927 return 0;
2930 /* return the next writable block. also for udf file system. */
2931 static int cdrom_get_next_writable(struct cdrom_device_info *cdi, long *next_writable)
2933 disc_information di;
2934 track_information ti;
2935 __u16 last_track;
2936 int ret, ti_size;
2938 if (!CDROM_CAN(CDC_GENERIC_PACKET))
2939 goto use_last_written;
2941 ret = cdrom_get_disc_info(cdi, &di);
2942 if (ret < 0 || ret < offsetof(typeof(di), last_track_lsb)
2943 + sizeof(di.last_track_lsb))
2944 goto use_last_written;
2946 /* if unit didn't return msb, it's zeroed by cdrom_get_disc_info */
2947 last_track = (di.last_track_msb << 8) | di.last_track_lsb;
2948 ti_size = cdrom_get_track_info(cdi, last_track, 1, &ti);
2949 if (ti_size < 0 || ti_size < offsetof(typeof(ti), track_start))
2950 goto use_last_written;
2952 /* if this track is blank, try the previous. */
2953 if (ti.blank) {
2954 if (last_track == 1)
2955 goto use_last_written;
2956 last_track--;
2957 ti_size = cdrom_get_track_info(cdi, last_track, 1, &ti);
2958 if (ti_size < 0)
2959 goto use_last_written;
2962 /* if next recordable address field is valid, use it. */
2963 if (ti.nwa_v && ti_size >= offsetof(typeof(ti), next_writable)
2964 + sizeof(ti.next_writable)) {
2965 *next_writable = be32_to_cpu(ti.next_writable);
2966 return 0;
2969 use_last_written:
2970 if ((ret = cdrom_get_last_written(cdi, next_writable))) {
2971 *next_writable = 0;
2972 return ret;
2973 } else {
2974 *next_writable += 7;
2975 return 0;
2979 EXPORT_SYMBOL(cdrom_get_last_written);
2980 EXPORT_SYMBOL(register_cdrom);
2981 EXPORT_SYMBOL(unregister_cdrom);
2982 EXPORT_SYMBOL(cdrom_open);
2983 EXPORT_SYMBOL(cdrom_release);
2984 EXPORT_SYMBOL(cdrom_ioctl);
2985 EXPORT_SYMBOL(cdrom_media_changed);
2986 EXPORT_SYMBOL(cdrom_number_of_slots);
2987 EXPORT_SYMBOL(cdrom_mode_select);
2988 EXPORT_SYMBOL(cdrom_mode_sense);
2989 EXPORT_SYMBOL(init_cdrom_command);
2990 EXPORT_SYMBOL(cdrom_get_media_event);
2991 EXPORT_SYMBOL(cdrom_is_mrw);
2992 EXPORT_SYMBOL(cdrom_is_random_writable);
2994 #ifdef CONFIG_SYSCTL
2996 #define CDROM_STR_SIZE 1000
2998 struct cdrom_sysctl_settings {
2999 char info[CDROM_STR_SIZE]; /* general info */
3000 int autoclose; /* close tray upon mount, etc */
3001 int autoeject; /* eject on umount */
3002 int debug; /* turn on debugging messages */
3003 int lock; /* lock the door on device open */
3004 int check; /* check media type */
3005 } cdrom_sysctl_settings;
3007 int cdrom_sysctl_info(ctl_table *ctl, int write, struct file * filp,
3008 void __user *buffer, size_t *lenp, loff_t *ppos)
3010 int pos;
3011 struct cdrom_device_info *cdi;
3012 char *info = cdrom_sysctl_settings.info;
3014 if (!*lenp || (*ppos && !write)) {
3015 *lenp = 0;
3016 return 0;
3019 pos = sprintf(info, "CD-ROM information, " VERSION "\n");
3021 pos += sprintf(info+pos, "\ndrive name:\t");
3022 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
3023 pos += sprintf(info+pos, "\t%s", cdi->name);
3025 pos += sprintf(info+pos, "\ndrive speed:\t");
3026 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
3027 pos += sprintf(info+pos, "\t%d", cdi->speed);
3029 pos += sprintf(info+pos, "\ndrive # of slots:");
3030 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
3031 pos += sprintf(info+pos, "\t%d", cdi->capacity);
3033 pos += sprintf(info+pos, "\nCan close tray:\t");
3034 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
3035 pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_CLOSE_TRAY) != 0);
3037 pos += sprintf(info+pos, "\nCan open tray:\t");
3038 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
3039 pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_OPEN_TRAY) != 0);
3041 pos += sprintf(info+pos, "\nCan lock tray:\t");
3042 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
3043 pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_LOCK) != 0);
3045 pos += sprintf(info+pos, "\nCan change speed:");
3046 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
3047 pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_SELECT_SPEED) != 0);
3049 pos += sprintf(info+pos, "\nCan select disk:");
3050 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
3051 pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_SELECT_DISC) != 0);
3053 pos += sprintf(info+pos, "\nCan read multisession:");
3054 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
3055 pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_MULTI_SESSION) != 0);
3057 pos += sprintf(info+pos, "\nCan read MCN:\t");
3058 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
3059 pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_MCN) != 0);
3061 pos += sprintf(info+pos, "\nReports media changed:");
3062 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
3063 pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_MEDIA_CHANGED) != 0);
3065 pos += sprintf(info+pos, "\nCan play audio:\t");
3066 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
3067 pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_PLAY_AUDIO) != 0);
3069 pos += sprintf(info+pos, "\nCan write CD-R:\t");
3070 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
3071 pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_CD_R) != 0);
3073 pos += sprintf(info+pos, "\nCan write CD-RW:");
3074 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
3075 pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_CD_RW) != 0);
3077 pos += sprintf(info+pos, "\nCan read DVD:\t");
3078 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
3079 pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_DVD) != 0);
3081 pos += sprintf(info+pos, "\nCan write DVD-R:");
3082 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
3083 pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_DVD_R) != 0);
3085 pos += sprintf(info+pos, "\nCan write DVD-RAM:");
3086 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
3087 pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_DVD_RAM) != 0);
3089 pos += sprintf(info+pos, "\nCan read MRW:\t");
3090 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
3091 pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_MRW) != 0);
3093 pos += sprintf(info+pos, "\nCan write MRW:\t");
3094 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
3095 pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_MRW_W) != 0);
3097 pos += sprintf(info+pos, "\nCan write RAM:\t");
3098 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
3099 pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_RAM) != 0);
3101 strcpy(info+pos,"\n\n");
3103 return proc_dostring(ctl, write, filp, buffer, lenp, ppos);
3106 /* Unfortunately, per device settings are not implemented through
3107 procfs/sysctl yet. When they are, this will naturally disappear. For now
3108 just update all drives. Later this will become the template on which
3109 new registered drives will be based. */
3110 void cdrom_update_settings(void)
3112 struct cdrom_device_info *cdi;
3114 for (cdi = topCdromPtr; cdi != NULL; cdi = cdi->next) {
3115 if (autoclose && CDROM_CAN(CDC_CLOSE_TRAY))
3116 cdi->options |= CDO_AUTO_CLOSE;
3117 else if (!autoclose)
3118 cdi->options &= ~CDO_AUTO_CLOSE;
3119 if (autoeject && CDROM_CAN(CDC_OPEN_TRAY))
3120 cdi->options |= CDO_AUTO_EJECT;
3121 else if (!autoeject)
3122 cdi->options &= ~CDO_AUTO_EJECT;
3123 if (lockdoor && CDROM_CAN(CDC_LOCK))
3124 cdi->options |= CDO_LOCK;
3125 else if (!lockdoor)
3126 cdi->options &= ~CDO_LOCK;
3127 if (check_media_type)
3128 cdi->options |= CDO_CHECK_TYPE;
3129 else
3130 cdi->options &= ~CDO_CHECK_TYPE;
3134 static int cdrom_sysctl_handler(ctl_table *ctl, int write, struct file * filp,
3135 void __user *buffer, size_t *lenp, loff_t *ppos)
3137 int *valp = ctl->data;
3138 int val = *valp;
3139 int ret;
3141 ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
3143 if (write && *valp != val) {
3145 /* we only care for 1 or 0. */
3146 if (*valp)
3147 *valp = 1;
3148 else
3149 *valp = 0;
3151 switch (ctl->ctl_name) {
3152 case DEV_CDROM_AUTOCLOSE: {
3153 if (valp == &cdrom_sysctl_settings.autoclose)
3154 autoclose = cdrom_sysctl_settings.autoclose;
3155 break;
3157 case DEV_CDROM_AUTOEJECT: {
3158 if (valp == &cdrom_sysctl_settings.autoeject)
3159 autoeject = cdrom_sysctl_settings.autoeject;
3160 break;
3162 case DEV_CDROM_DEBUG: {
3163 if (valp == &cdrom_sysctl_settings.debug)
3164 debug = cdrom_sysctl_settings.debug;
3165 break;
3167 case DEV_CDROM_LOCK: {
3168 if (valp == &cdrom_sysctl_settings.lock)
3169 lockdoor = cdrom_sysctl_settings.lock;
3170 break;
3172 case DEV_CDROM_CHECK_MEDIA: {
3173 if (valp == &cdrom_sysctl_settings.check)
3174 check_media_type = cdrom_sysctl_settings.check;
3175 break;
3178 /* update the option flags according to the changes. we
3179 don't have per device options through sysctl yet,
3180 but we will have and then this will disappear. */
3181 cdrom_update_settings();
3184 return ret;
3187 /* Place files in /proc/sys/dev/cdrom */
3188 ctl_table cdrom_table[] = {
3190 .ctl_name = DEV_CDROM_INFO,
3191 .procname = "info",
3192 .data = &cdrom_sysctl_settings.info,
3193 .maxlen = CDROM_STR_SIZE,
3194 .mode = 0444,
3195 .proc_handler = &cdrom_sysctl_info,
3198 .ctl_name = DEV_CDROM_AUTOCLOSE,
3199 .procname = "autoclose",
3200 .data = &cdrom_sysctl_settings.autoclose,
3201 .maxlen = sizeof(int),
3202 .mode = 0644,
3203 .proc_handler = &cdrom_sysctl_handler,
3206 .ctl_name = DEV_CDROM_AUTOEJECT,
3207 .procname = "autoeject",
3208 .data = &cdrom_sysctl_settings.autoeject,
3209 .maxlen = sizeof(int),
3210 .mode = 0644,
3211 .proc_handler = &cdrom_sysctl_handler,
3214 .ctl_name = DEV_CDROM_DEBUG,
3215 .procname = "debug",
3216 .data = &cdrom_sysctl_settings.debug,
3217 .maxlen = sizeof(int),
3218 .mode = 0644,
3219 .proc_handler = &cdrom_sysctl_handler,
3222 .ctl_name = DEV_CDROM_LOCK,
3223 .procname = "lock",
3224 .data = &cdrom_sysctl_settings.lock,
3225 .maxlen = sizeof(int),
3226 .mode = 0644,
3227 .proc_handler = &cdrom_sysctl_handler,
3230 .ctl_name = DEV_CDROM_CHECK_MEDIA,
3231 .procname = "check_media",
3232 .data = &cdrom_sysctl_settings.check,
3233 .maxlen = sizeof(int),
3234 .mode = 0644,
3235 .proc_handler = &cdrom_sysctl_handler
3237 { .ctl_name = 0 }
3240 ctl_table cdrom_cdrom_table[] = {
3242 .ctl_name = DEV_CDROM,
3243 .procname = "cdrom",
3244 .maxlen = 0,
3245 .mode = 0555,
3246 .child = cdrom_table,
3248 { .ctl_name = 0 }
3251 /* Make sure that /proc/sys/dev is there */
3252 ctl_table cdrom_root_table[] = {
3253 #ifdef CONFIG_PROC_FS
3255 .ctl_name = CTL_DEV,
3256 .procname = "dev",
3257 .maxlen = 0,
3258 .mode = 0555,
3259 .child = cdrom_cdrom_table,
3261 #endif /* CONFIG_PROC_FS */
3262 { .ctl_name = 0 }
3264 static struct ctl_table_header *cdrom_sysctl_header;
3266 static void cdrom_sysctl_register(void)
3268 static int initialized;
3270 if (initialized == 1)
3271 return;
3273 cdrom_sysctl_header = register_sysctl_table(cdrom_root_table, 1);
3274 if (cdrom_root_table->ctl_name && cdrom_root_table->child->de)
3275 cdrom_root_table->child->de->owner = THIS_MODULE;
3277 /* set the defaults */
3278 cdrom_sysctl_settings.autoclose = autoclose;
3279 cdrom_sysctl_settings.autoeject = autoeject;
3280 cdrom_sysctl_settings.debug = debug;
3281 cdrom_sysctl_settings.lock = lockdoor;
3282 cdrom_sysctl_settings.check = check_media_type;
3284 initialized = 1;
3287 static void cdrom_sysctl_unregister(void)
3289 if (cdrom_sysctl_header)
3290 unregister_sysctl_table(cdrom_sysctl_header);
3293 #endif /* CONFIG_SYSCTL */
3295 static int __init cdrom_init(void)
3297 #ifdef CONFIG_SYSCTL
3298 cdrom_sysctl_register();
3299 #endif
3300 return 0;
3303 static void __exit cdrom_exit(void)
3305 printk(KERN_INFO "Uniform CD-ROM driver unloaded\n");
3306 #ifdef CONFIG_SYSCTL
3307 cdrom_sysctl_unregister();
3308 #endif
3311 module_init(cdrom_init);
3312 module_exit(cdrom_exit);
3313 MODULE_LICENSE("GPL");