1 /* linux/drivers/cdrom/cdrom.c.
2 Copyright (c) 1996, 1997 David A. van Leeuwen.
3 Copyright (c) 1997, 1998 Erik Andersen <andersee@debian.org>
4 Copyright (c) 1998, 1999 Jens Axboe <axboe@image.dk>
5
6 May be copied or modified under the terms of the GNU General Public
7 License. See linux/COPYING for more information.
8
9 Uniform CD-ROM driver for Linux.
10 See Documentation/cdrom/cdrom-standard.tex for usage information.
11
12 The routines in the file provide a uniform interface between the
13 software that uses CD-ROMs and the various low-level drivers that
14 actually talk to the hardware. Suggestions are welcome.
15 Patches that work are more welcome though. ;-)
16
17 To Do List:
18 ----------------------------------
19
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!
24
25
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?
31
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.
36
37 -- Added (rudimentary) sysctl interface. I realize this is really weak
38 right now, and is _very_ badly implemented. It will be improved...
39
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.
45
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.
49
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).
53
54 -- Made it so if a function isn't implemented in a low-level driver,
55 ENOSYS is now returned instead of EINVAL.
56
57 -- Simplified some complex logic so that the source code is easier to read.
58
59 -- Other stuff I probably forgot to mention (lots of changes).
60
61 2.01 to 2.11 Dec 1997-Jan 1998
62 -- TO-DO! Write changelogs for 2.01 to 2.12.
63
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.
69
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.
78
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.
84
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
89
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.
94
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.
103
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.
113
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>.
117
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.
122
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.
140
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 :).
146
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.
155
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.
166
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 loose the index part, however.
176 -- Small modifications to accomodate 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.
180
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.
189
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.
193
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.
202
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.
213
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.
218
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.
223
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
227
228 3.12 Oct 18, 2000 - Jens Axboe <axboe@suse.de>
229 -- Use quiet bit on packet commands not known to work
230
231 -------------------------------------------------------------------------*/
232
233 #define REVISION "Revision: 3.12"
234 #define VERSION "Id: cdrom.c 3.12 2000/10/18"
235
236 /* I use an error-log mask to give fine grain control over the type of
237 messages dumped to the system logs. The available masks include: */
238 #define CD_NOTHING 0x0
239 #define CD_WARNING 0x1
240 #define CD_REG_UNREG 0x2
241 #define CD_DO_IOCTL 0x4
242 #define CD_OPEN 0x8
243 #define CD_CLOSE 0x10
244 #define CD_COUNT_TRACKS 0x20
245 #define CD_CHANGER 0x40
246 #define CD_DVD 0x80
247
248 /* Define this to remove _all_ the debugging messages */
249 /* #define ERRLOGMASK CD_NOTHING */
250 #define ERRLOGMASK (CD_WARNING)
251 /* #define ERRLOGMASK (CD_WARNING|CD_OPEN|CD_COUNT_TRACKS|CD_CLOSE) */
252 /* #define ERRLOGMASK (CD_WARNING|CD_REG_UNREG|CD_DO_IOCTL|CD_OPEN|CD_CLOSE|CD_COUNT_TRACKS) */
253
254 #include <linux/config.h>
255 #include <linux/module.h>
256 #include <linux/fs.h>
257 #include <linux/major.h>
258 #include <linux/types.h>
259 #include <linux/errno.h>
260 #include <linux/kernel.h>
261 #include <linux/mm.h>
262 #include <linux/slab.h>
263 #include <linux/cdrom.h>
264 #include <linux/sysctl.h>
265 #include <linux/proc_fs.h>
266 #include <linux/init.h>
267
268 #include <asm/fcntl.h>
269 #include <asm/segment.h>
270 #include <asm/uaccess.h>
271
272 /* used to tell the module to turn on full debugging messages */
273 static int debug;
274 /* used to keep tray locked at all times */
275 static int keeplocked;
276 /* default compatibility mode */
277 static int autoclose=1;
278 static int autoeject;
279 static int lockdoor = 1;
280 /* will we ever get to use this... sigh. */
281 static int check_media_type;
282 MODULE_PARM(debug, "i");
283 MODULE_PARM(autoclose, "i");
284 MODULE_PARM(autoeject, "i");
285 MODULE_PARM(lockdoor, "i");
286 MODULE_PARM(check_media_type, "i");
287
288 #if (ERRLOGMASK!=CD_NOTHING)
289 #define cdinfo(type, fmt, args...) \
290 if ((ERRLOGMASK & type) || debug==1 ) \
291 printk(KERN_INFO "cdrom: " fmt, ## args)
292 #else
293 #define cdinfo(type, fmt, args...)
294 #endif
295
296 /* These are used to simplify getting data in from and back to user land */
297 #define IOCTL_IN(arg, type, in) \
298 if (copy_from_user(&(in), (type *) (arg), sizeof (in))) \
299 return -EFAULT;
300
301 #define IOCTL_OUT(arg, type, out) \
302 if (copy_to_user((type *) (arg), &(out), sizeof (out))) \
303 return -EFAULT;
304
305 /* The (cdo->capability & ~cdi->mask & CDC_XXX) construct was used in
306 a lot of places. This macro makes the code more clear. */
307 #define CDROM_CAN(type) (cdi->ops->capability & ~cdi->mask & (type))
308
309 /* used in the audio ioctls */
310 #define CHECKAUDIO if ((ret=check_for_audio_disc(cdi, cdo))) return ret
311
312 /* Not-exported routines. */
313 static int open_for_data(struct cdrom_device_info * cdi);
314 static int check_for_audio_disc(struct cdrom_device_info * cdi,
315 struct cdrom_device_ops * cdo);
316 static void sanitize_format(union cdrom_addr *addr,
317 u_char * curr, u_char requested);
318 static int mmc_ioctl(struct cdrom_device_info *cdi, unsigned int cmd,
319 unsigned long arg);
320
321 int cdrom_get_last_written(kdev_t dev, long *last_written);
322 int cdrom_get_next_writable(kdev_t dev, long *next_writable);
323
324 #ifdef CONFIG_SYSCTL
325 static void cdrom_sysctl_register(void);
326 #endif /* CONFIG_SYSCTL */
327 static struct cdrom_device_info *topCdromPtr;
328 static devfs_handle_t devfs_handle;
329 static struct unique_numspace cdrom_numspace = UNIQUE_NUMBERSPACE_INITIALISER;
330
331 /* This macro makes sure we don't have to check on cdrom_device_ops
332 * existence in the run-time routines below. Change_capability is a
333 * hack to have the capability flags defined const, while we can still
334 * change it here without gcc complaining at every line.
335 */
336 #define ENSURE(call, bits) if (cdo->call == NULL) *change_capability &= ~(bits)
337
register_cdrom(struct cdrom_device_info * cdi)338 int register_cdrom(struct cdrom_device_info *cdi)
339 {
340 static char banner_printed;
341 int major = MAJOR(cdi->dev);
342 struct cdrom_device_ops *cdo = cdi->ops;
343 int *change_capability = (int *)&cdo->capability; /* hack */
344
345 cdinfo(CD_OPEN, "entering register_cdrom\n");
346
347 if (major < 0 || major >= MAX_BLKDEV)
348 return -1;
349 if (cdo->open == NULL || cdo->release == NULL)
350 return -2;
351 if ( !banner_printed ) {
352 printk(KERN_INFO "Uniform CD-ROM driver " REVISION "\n");
353 banner_printed = 1;
354 #ifdef CONFIG_SYSCTL
355 cdrom_sysctl_register();
356 #endif /* CONFIG_SYSCTL */
357 }
358 ENSURE(drive_status, CDC_DRIVE_STATUS );
359 ENSURE(media_changed, CDC_MEDIA_CHANGED);
360 ENSURE(tray_move, CDC_CLOSE_TRAY | CDC_OPEN_TRAY);
361 ENSURE(lock_door, CDC_LOCK);
362 ENSURE(select_speed, CDC_SELECT_SPEED);
363 ENSURE(get_last_session, CDC_MULTI_SESSION);
364 ENSURE(get_mcn, CDC_MCN);
365 ENSURE(reset, CDC_RESET);
366 ENSURE(audio_ioctl, CDC_PLAY_AUDIO);
367 ENSURE(dev_ioctl, CDC_IOCTLS);
368 ENSURE(generic_packet, CDC_GENERIC_PACKET);
369 cdi->mc_flags = 0;
370 cdo->n_minors = 0;
371 cdi->options = CDO_USE_FFLAGS;
372
373 if (autoclose==1 && CDROM_CAN(CDC_CLOSE_TRAY))
374 cdi->options |= (int) CDO_AUTO_CLOSE;
375 if (autoeject==1 && CDROM_CAN(CDC_OPEN_TRAY))
376 cdi->options |= (int) CDO_AUTO_EJECT;
377 if (lockdoor==1)
378 cdi->options |= (int) CDO_LOCK;
379 if (check_media_type==1)
380 cdi->options |= (int) CDO_CHECK_TYPE;
381
382 if (!devfs_handle)
383 devfs_handle = devfs_mk_dir (NULL, "cdroms", NULL);
384 cdi->number = devfs_alloc_unique_number (&cdrom_numspace);
385 if (cdi->de) {
386 int pos;
387 devfs_handle_t slave;
388 char rname[64];
389
390 pos = devfs_generate_path (cdi->de, rname + 3,
391 sizeof rname - 3);
392 if (pos >= 0) {
393 char vname[16];
394 sprintf (vname, "cdrom%d", cdi->number);
395 strncpy (rname + pos, "../", 3);
396 devfs_mk_symlink (devfs_handle, vname,
397 DEVFS_FL_DEFAULT,
398 rname + pos, &slave, NULL);
399 devfs_auto_unregister (cdi->de, slave);
400 }
401 }
402 cdinfo(CD_REG_UNREG, "drive \"/dev/%s\" registered\n", cdi->name);
403 cdi->next = topCdromPtr;
404 topCdromPtr = cdi;
405 return 0;
406 }
407 #undef ENSURE
408
unregister_cdrom(struct cdrom_device_info * unreg)409 int unregister_cdrom(struct cdrom_device_info *unreg)
410 {
411 struct cdrom_device_info *cdi, *prev;
412 int major = MAJOR(unreg->dev);
413
414 cdinfo(CD_OPEN, "entering unregister_cdrom\n");
415
416 if (major < 0 || major >= MAX_BLKDEV)
417 return -1;
418
419 prev = NULL;
420 cdi = topCdromPtr;
421 while (cdi != NULL && cdi->dev != unreg->dev) {
422 prev = cdi;
423 cdi = cdi->next;
424 }
425
426 if (cdi == NULL)
427 return -2;
428 if (prev)
429 prev->next = cdi->next;
430 else
431 topCdromPtr = cdi->next;
432 cdi->ops->n_minors--;
433 devfs_unregister (cdi->de);
434 devfs_dealloc_unique_number (&cdrom_numspace, cdi->number);
435 cdinfo(CD_REG_UNREG, "drive \"/dev/%s\" unregistered\n", cdi->name);
436 return 0;
437 }
438
cdrom_find_device(kdev_t dev)439 struct cdrom_device_info *cdrom_find_device(kdev_t dev)
440 {
441 struct cdrom_device_info *cdi;
442
443 cdi = topCdromPtr;
444 while (cdi != NULL && cdi->dev != dev)
445 cdi = cdi->next;
446
447 return cdi;
448 }
449
450 /* We use the open-option O_NONBLOCK to indicate that the
451 * purpose of opening is only for subsequent ioctl() calls; no device
452 * integrity checks are performed.
453 *
454 * We hope that all cd-player programs will adopt this convention. It
455 * is in their own interest: device control becomes a lot easier
456 * this way.
457 */
cdrom_open(struct inode * ip,struct file * fp)458 int cdrom_open(struct inode *ip, struct file *fp)
459 {
460 struct cdrom_device_info *cdi;
461 kdev_t dev = ip->i_rdev;
462 int ret;
463
464 cdinfo(CD_OPEN, "entering cdrom_open\n");
465 if ((cdi = cdrom_find_device(dev)) == NULL)
466 return -ENODEV;
467
468 if ((fp->f_mode & FMODE_WRITE) && !CDROM_CAN(CDC_DVD_RAM))
469 return -EROFS;
470
471 /* if this was a O_NONBLOCK open and we should honor the flags,
472 * do a quick open without drive/disc integrity checks. */
473 if ((fp->f_flags & O_NONBLOCK) && (cdi->options & CDO_USE_FFLAGS))
474 ret = cdi->ops->open(cdi, 1);
475 else
476 ret = open_for_data(cdi);
477
478 if (!ret) cdi->use_count++;
479
480 cdinfo(CD_OPEN, "Use count for \"/dev/%s\" now %d\n", cdi->name, cdi->use_count);
481 /* Do this on open. Don't wait for mount, because they might
482 not be mounting, but opening with O_NONBLOCK */
483 check_disk_change(dev);
484 return ret;
485 }
486
487 static
open_for_data(struct cdrom_device_info * cdi)488 int open_for_data(struct cdrom_device_info * cdi)
489 {
490 int ret;
491 struct cdrom_device_ops *cdo = cdi->ops;
492 tracktype tracks;
493 cdinfo(CD_OPEN, "entering open_for_data\n");
494 /* Check if the driver can report drive status. If it can, we
495 can do clever things. If it can't, well, we at least tried! */
496 if (cdo->drive_status != NULL) {
497 ret = cdo->drive_status(cdi, CDSL_CURRENT);
498 cdinfo(CD_OPEN, "drive_status=%d\n", ret);
499 if (ret == CDS_TRAY_OPEN) {
500 cdinfo(CD_OPEN, "the tray is open...\n");
501 /* can/may i close it? */
502 if (CDROM_CAN(CDC_CLOSE_TRAY) &&
503 cdi->options & CDO_AUTO_CLOSE) {
504 cdinfo(CD_OPEN, "trying to close the tray.\n");
505 ret=cdo->tray_move(cdi,0);
506 if (ret) {
507 cdinfo(CD_OPEN, "bummer. tried to close the tray but failed.\n");
508 /* Ignore the error from the low
509 level driver. We don't care why it
510 couldn't close the tray. We only care
511 that there is no disc in the drive,
512 since that is the _REAL_ problem here.*/
513 ret=-ENOMEDIUM;
514 goto clean_up_and_return;
515 }
516 } else {
517 cdinfo(CD_OPEN, "bummer. this drive can't close the tray.\n");
518 ret=-ENOMEDIUM;
519 goto clean_up_and_return;
520 }
521 /* Ok, the door should be closed now.. Check again */
522 ret = cdo->drive_status(cdi, CDSL_CURRENT);
523 if ((ret == CDS_NO_DISC) || (ret==CDS_TRAY_OPEN)) {
524 cdinfo(CD_OPEN, "bummer. the tray is still not closed.\n");
525 cdinfo(CD_OPEN, "tray might not contain a medium.\n");
526 ret=-ENOMEDIUM;
527 goto clean_up_and_return;
528 }
529 cdinfo(CD_OPEN, "the tray is now closed.\n");
530 }
531 if (ret!=CDS_DISC_OK) {
532 ret = -ENOMEDIUM;
533 goto clean_up_and_return;
534 }
535 }
536 cdrom_count_tracks(cdi, &tracks);
537 if (tracks.error == CDS_NO_DISC) {
538 cdinfo(CD_OPEN, "bummer. no disc.\n");
539 ret=-ENOMEDIUM;
540 goto clean_up_and_return;
541 }
542 /* CD-Players which don't use O_NONBLOCK, workman
543 * for example, need bit CDO_CHECK_TYPE cleared! */
544 if (tracks.data==0) {
545 if (cdi->options & CDO_CHECK_TYPE) {
546 /* give people a warning shot, now that CDO_CHECK_TYPE
547 is the default case! */
548 cdinfo(CD_OPEN, "bummer. wrong media type.\n");
549 cdinfo(CD_WARNING, "pid %d must open device O_NONBLOCK!\n",
550 (unsigned int)current->pid);
551 ret=-EMEDIUMTYPE;
552 goto clean_up_and_return;
553 }
554 else {
555 cdinfo(CD_OPEN, "wrong media type, but CDO_CHECK_TYPE not set.\n");
556 }
557 }
558
559 cdinfo(CD_OPEN, "all seems well, opening the device.\n");
560
561 /* all seems well, we can open the device */
562 ret = cdo->open(cdi, 0); /* open for data */
563 cdinfo(CD_OPEN, "opening the device gave me %d.\n", ret);
564 /* After all this careful checking, we shouldn't have problems
565 opening the device, but we don't want the device locked if
566 this somehow fails... */
567 if (ret) {
568 cdinfo(CD_OPEN, "open device failed.\n");
569 goto clean_up_and_return;
570 }
571 if (CDROM_CAN(CDC_LOCK) && cdi->options & CDO_LOCK) {
572 cdo->lock_door(cdi, 1);
573 cdinfo(CD_OPEN, "door locked.\n");
574 }
575 cdinfo(CD_OPEN, "device opened successfully.\n");
576 return ret;
577
578 /* Something failed. Try to unlock the drive, because some drivers
579 (notably ide-cd) lock the drive after every command. This produced
580 a nasty bug where after mount failed, the drive would remain locked!
581 This ensures that the drive gets unlocked after a mount fails. This
582 is a goto to avoid bloating the driver with redundant code. */
583 clean_up_and_return:
584 cdinfo(CD_WARNING, "open failed.\n");
585 if (CDROM_CAN(CDC_LOCK) && cdi->options & CDO_LOCK) {
586 cdo->lock_door(cdi, 0);
587 cdinfo(CD_OPEN, "door unlocked.\n");
588 }
589 return ret;
590 }
591
592 /* This code is similar to that in open_for_data. The routine is called
593 whenever an audio play operation is requested.
594 */
check_for_audio_disc(struct cdrom_device_info * cdi,struct cdrom_device_ops * cdo)595 int check_for_audio_disc(struct cdrom_device_info * cdi,
596 struct cdrom_device_ops * cdo)
597 {
598 int ret;
599 tracktype tracks;
600 cdinfo(CD_OPEN, "entering check_for_audio_disc\n");
601 if (!(cdi->options & CDO_CHECK_TYPE))
602 return 0;
603 if (cdo->drive_status != NULL) {
604 ret = cdo->drive_status(cdi, CDSL_CURRENT);
605 cdinfo(CD_OPEN, "drive_status=%d\n", ret);
606 if (ret == CDS_TRAY_OPEN) {
607 cdinfo(CD_OPEN, "the tray is open...\n");
608 /* can/may i close it? */
609 if (CDROM_CAN(CDC_CLOSE_TRAY) &&
610 cdi->options & CDO_AUTO_CLOSE) {
611 cdinfo(CD_OPEN, "trying to close the tray.\n");
612 ret=cdo->tray_move(cdi,0);
613 if (ret) {
614 cdinfo(CD_OPEN, "bummer. tried to close tray but failed.\n");
615 /* Ignore the error from the low
616 level driver. We don't care why it
617 couldn't close the tray. We only care
618 that there is no disc in the drive,
619 since that is the _REAL_ problem here.*/
620 return -ENOMEDIUM;
621 }
622 } else {
623 cdinfo(CD_OPEN, "bummer. this driver can't close the tray.\n");
624 return -ENOMEDIUM;
625 }
626 /* Ok, the door should be closed now.. Check again */
627 ret = cdo->drive_status(cdi, CDSL_CURRENT);
628 if ((ret == CDS_NO_DISC) || (ret==CDS_TRAY_OPEN)) {
629 cdinfo(CD_OPEN, "bummer. the tray is still not closed.\n");
630 return -ENOMEDIUM;
631 }
632 if (ret!=CDS_DISC_OK) {
633 cdinfo(CD_OPEN, "bummer. disc isn't ready.\n");
634 return -EIO;
635 }
636 cdinfo(CD_OPEN, "the tray is now closed.\n");
637 }
638 }
639 cdrom_count_tracks(cdi, &tracks);
640 if (tracks.error)
641 return(tracks.error);
642
643 if (tracks.audio==0)
644 return -EMEDIUMTYPE;
645
646 return 0;
647 }
648
649
650 /* Admittedly, the logic below could be performed in a nicer way. */
cdrom_release(struct inode * ip,struct file * fp)651 int cdrom_release(struct inode *ip, struct file *fp)
652 {
653 kdev_t dev = ip->i_rdev;
654 struct cdrom_device_info *cdi = cdrom_find_device(dev);
655 struct cdrom_device_ops *cdo = cdi->ops;
656 int opened_for_data;
657
658 cdinfo(CD_CLOSE, "entering cdrom_release\n");
659
660 if (cdi->use_count > 0)
661 cdi->use_count--;
662 if (cdi->use_count == 0)
663 cdinfo(CD_CLOSE, "Use count for \"/dev/%s\" now zero\n", cdi->name);
664 if (cdi->use_count == 0 &&
665 cdo->capability & CDC_LOCK && !keeplocked) {
666 cdinfo(CD_CLOSE, "Unlocking door!\n");
667 cdo->lock_door(cdi, 0);
668 }
669 opened_for_data = !(cdi->options & CDO_USE_FFLAGS) ||
670 !(fp && fp->f_flags & O_NONBLOCK);
671 cdo->release(cdi);
672 if (cdi->use_count == 0) { /* last process that closes dev*/
673 if (opened_for_data &&
674 cdi->options & CDO_AUTO_EJECT && CDROM_CAN(CDC_OPEN_TRAY))
675 cdo->tray_move(cdi, 1);
676 }
677 return 0;
678 }
679
cdrom_read_mech_status(struct cdrom_device_info * cdi,struct cdrom_changer_info * buf)680 static int cdrom_read_mech_status(struct cdrom_device_info *cdi,
681 struct cdrom_changer_info *buf)
682 {
683 struct cdrom_generic_command cgc;
684 struct cdrom_device_ops *cdo = cdi->ops;
685 int length;
686
687 /*
688 * Sanyo changer isn't spec compliant (doesn't use regular change
689 * LOAD_UNLOAD command, and it doesn't implement the mech status
690 * command below
691 */
692 if (cdi->sanyo_slot) {
693 buf->hdr.nslots = 3;
694 buf->hdr.curslot = cdi->sanyo_slot == 3 ? 0 : cdi->sanyo_slot;
695 for (length = 0; length < 3; length++) {
696 buf->slots[length].disc_present = 1;
697 buf->slots[length].change = 0;
698 }
699 return 0;
700 }
701
702 length = sizeof(struct cdrom_mechstat_header) +
703 cdi->capacity * sizeof(struct cdrom_slot);
704
705 init_cdrom_command(&cgc, buf, length, CGC_DATA_READ);
706 cgc.cmd[0] = GPCMD_MECHANISM_STATUS;
707 cgc.cmd[8] = (length >> 8) & 0xff;
708 cgc.cmd[9] = length & 0xff;
709 return cdo->generic_packet(cdi, &cgc);
710 }
711
cdrom_slot_status(struct cdrom_device_info * cdi,int slot)712 static int cdrom_slot_status(struct cdrom_device_info *cdi, int slot)
713 {
714 struct cdrom_changer_info info;
715 int ret;
716
717 cdinfo(CD_CHANGER, "entering cdrom_slot_status()\n");
718 if (cdi->sanyo_slot)
719 return CDS_NO_INFO;
720
721 if ((ret = cdrom_read_mech_status(cdi, &info)))
722 return ret;
723
724 if (info.slots[slot].disc_present)
725 return CDS_DISC_OK;
726 else
727 return CDS_NO_DISC;
728
729 }
730
731 /* Return the number of slots for an ATAPI/SCSI cdrom,
732 * return 1 if not a changer.
733 */
cdrom_number_of_slots(struct cdrom_device_info * cdi)734 int cdrom_number_of_slots(struct cdrom_device_info *cdi)
735 {
736 int status;
737 int nslots = 1;
738 struct cdrom_changer_info info;
739
740 cdinfo(CD_CHANGER, "entering cdrom_number_of_slots()\n");
741 /* cdrom_read_mech_status requires a valid value for capacity: */
742 cdi->capacity = 0;
743
744 if ((status = cdrom_read_mech_status(cdi, &info)) == 0)
745 nslots = info.hdr.nslots;
746
747 return nslots;
748 }
749
750
751 /* If SLOT < 0, unload the current slot. Otherwise, try to load SLOT. */
cdrom_load_unload(struct cdrom_device_info * cdi,int slot)752 static int cdrom_load_unload(struct cdrom_device_info *cdi, int slot)
753 {
754 struct cdrom_generic_command cgc;
755
756 cdinfo(CD_CHANGER, "entering cdrom_load_unload()\n");
757 if (cdi->sanyo_slot && slot < 0)
758 return 0;
759
760 init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
761 cgc.cmd[0] = GPCMD_LOAD_UNLOAD;
762 cgc.cmd[4] = 2 + (slot >= 0);
763 cgc.cmd[8] = slot;
764 cgc.timeout = 60 * HZ;
765
766 /* The Sanyo 3 CD changer uses byte 7 of the
767 GPCMD_TEST_UNIT_READY to command to switch CDs instead of
768 using the GPCMD_LOAD_UNLOAD opcode. */
769 if (cdi->sanyo_slot && -1 < slot) {
770 cgc.cmd[0] = GPCMD_TEST_UNIT_READY;
771 cgc.cmd[7] = slot;
772 cgc.cmd[4] = cgc.cmd[8] = 0;
773 cdi->sanyo_slot = slot ? slot : 3;
774 }
775
776 return cdi->ops->generic_packet(cdi, &cgc);
777 }
778
cdrom_select_disc(struct cdrom_device_info * cdi,int slot)779 int cdrom_select_disc(struct cdrom_device_info *cdi, int slot)
780 {
781 struct cdrom_changer_info info;
782 int curslot;
783 int ret;
784
785 cdinfo(CD_CHANGER, "entering cdrom_select_disc()\n");
786 if (!CDROM_CAN(CDC_SELECT_DISC))
787 return -EDRIVE_CANT_DO_THIS;
788
789 (void) cdi->ops->media_changed(cdi, slot);
790
791 if (slot == CDSL_NONE) {
792 /* set media changed bits, on both queues */
793 cdi->mc_flags = 0x3;
794 return cdrom_load_unload(cdi, -1);
795 }
796
797 if ((ret = cdrom_read_mech_status(cdi, &info)))
798 return ret;
799
800 curslot = info.hdr.curslot;
801
802 if (cdi->use_count > 1 || keeplocked) {
803 if (slot == CDSL_CURRENT) {
804 return curslot;
805 } else {
806 return -EBUSY;
807 }
808 }
809
810 /* Specifying CDSL_CURRENT will attempt to load the currnet slot,
811 which is useful if it had been previously unloaded.
812 Whether it can or not, it returns the current slot.
813 Similarly, if slot happens to be the current one, we still
814 try and load it. */
815 if (slot == CDSL_CURRENT)
816 slot = curslot;
817
818 /* set media changed bits on both queues */
819 cdi->mc_flags = 0x3;
820 if ((ret = cdrom_load_unload(cdi, slot)))
821 return ret;
822
823 return slot;
824 }
825
826 /* We want to make media_changed accessible to the user through an
827 * ioctl. The main problem now is that we must double-buffer the
828 * low-level implementation, to assure that the VFS and the user both
829 * see a medium change once.
830 */
831
832 static
media_changed(struct cdrom_device_info * cdi,int queue)833 int media_changed(struct cdrom_device_info *cdi, int queue)
834 {
835 unsigned int mask = (1 << (queue & 1));
836 int ret = !!(cdi->mc_flags & mask);
837
838 if (!CDROM_CAN(CDC_MEDIA_CHANGED))
839 return ret;
840 /* changed since last call? */
841 if (cdi->ops->media_changed(cdi, CDSL_CURRENT)) {
842 cdi->mc_flags = 0x3; /* set bit on both queues */
843 ret |= 1;
844 }
845 cdi->mc_flags &= ~mask; /* clear bit */
846 return ret;
847 }
848
cdrom_media_changed(kdev_t dev)849 int cdrom_media_changed(kdev_t dev)
850 {
851 struct cdrom_device_info *cdi = cdrom_find_device(dev);
852 /* This talks to the VFS, which doesn't like errors - just 1 or 0.
853 * Returning "0" is always safe (media hasn't been changed). Do that
854 * if the low-level cdrom driver dosn't support media changed. */
855 if (cdi == NULL || cdi->ops->media_changed == NULL)
856 return 0;
857 if (!CDROM_CAN(CDC_MEDIA_CHANGED))
858 return 0;
859 return media_changed(cdi, 0);
860 }
861
862 /* badly broken, I know. Is due for a fixup anytime. */
cdrom_count_tracks(struct cdrom_device_info * cdi,tracktype * tracks)863 void cdrom_count_tracks(struct cdrom_device_info *cdi, tracktype* tracks)
864 {
865 struct cdrom_tochdr header;
866 struct cdrom_tocentry entry;
867 int ret, i;
868 tracks->data=0;
869 tracks->audio=0;
870 tracks->cdi=0;
871 tracks->xa=0;
872 tracks->error=0;
873 cdinfo(CD_COUNT_TRACKS, "entering cdrom_count_tracks\n");
874 if (!CDROM_CAN(CDC_PLAY_AUDIO)) {
875 tracks->error=CDS_NO_INFO;
876 return;
877 }
878 /* Grab the TOC header so we can see how many tracks there are */
879 if ((ret = cdi->ops->audio_ioctl(cdi, CDROMREADTOCHDR, &header))) {
880 if (ret == -ENOMEDIUM)
881 tracks->error = CDS_NO_DISC;
882 else
883 tracks->error = CDS_NO_INFO;
884 return;
885 }
886 /* check what type of tracks are on this disc */
887 entry.cdte_format = CDROM_MSF;
888 for (i = header.cdth_trk0; i <= header.cdth_trk1; i++) {
889 entry.cdte_track = i;
890 if (cdi->ops->audio_ioctl(cdi, CDROMREADTOCENTRY, &entry)) {
891 tracks->error=CDS_NO_INFO;
892 return;
893 }
894 if (entry.cdte_ctrl & CDROM_DATA_TRACK) {
895 if (entry.cdte_format == 0x10)
896 tracks->cdi++;
897 else if (entry.cdte_format == 0x20)
898 tracks->xa++;
899 else
900 tracks->data++;
901 } else
902 tracks->audio++;
903 cdinfo(CD_COUNT_TRACKS, "track %d: format=%d, ctrl=%d\n",
904 i, entry.cdte_format, entry.cdte_ctrl);
905 }
906 cdinfo(CD_COUNT_TRACKS, "disc has %d tracks: %d=audio %d=data %d=Cd-I %d=XA\n",
907 header.cdth_trk1, tracks->audio, tracks->data,
908 tracks->cdi, tracks->xa);
909 }
910
911 /* Requests to the low-level drivers will /always/ be done in the
912 following format convention:
913
914 CDROM_LBA: all data-related requests.
915 CDROM_MSF: all audio-related requests.
916
917 However, a low-level implementation is allowed to refuse this
918 request, and return information in its own favorite format.
919
920 It doesn't make sense /at all/ to ask for a play_audio in LBA
921 format, or ask for multi-session info in MSF format. However, for
922 backward compatibility these format requests will be satisfied, but
923 the requests to the low-level drivers will be sanitized in the more
924 meaningful format indicated above.
925 */
926
927 static
sanitize_format(union cdrom_addr * addr,u_char * curr,u_char requested)928 void sanitize_format(union cdrom_addr *addr,
929 u_char * curr, u_char requested)
930 {
931 if (*curr == requested)
932 return; /* nothing to be done! */
933 if (requested == CDROM_LBA) {
934 addr->lba = (int) addr->msf.frame +
935 75 * (addr->msf.second - 2 + 60 * addr->msf.minute);
936 } else { /* CDROM_MSF */
937 int lba = addr->lba;
938 addr->msf.frame = lba % 75;
939 lba /= 75;
940 lba += 2;
941 addr->msf.second = lba % 60;
942 addr->msf.minute = lba / 60;
943 }
944 *curr = requested;
945 }
946
init_cdrom_command(struct cdrom_generic_command * cgc,void * buf,int len,int type)947 void init_cdrom_command(struct cdrom_generic_command *cgc, void *buf, int len,
948 int type)
949 {
950 memset(cgc, 0, sizeof(struct cdrom_generic_command));
951 if (buf)
952 memset(buf, 0, len);
953 cgc->buffer = (char *) buf;
954 cgc->buflen = len;
955 cgc->data_direction = type;
956 cgc->timeout = 5*HZ;
957 }
958
959 /* DVD handling */
960
961 #define copy_key(dest,src) memcpy((dest), (src), sizeof(dvd_key))
962 #define copy_chal(dest,src) memcpy((dest), (src), sizeof(dvd_challenge))
963
setup_report_key(struct cdrom_generic_command * cgc,unsigned agid,unsigned type)964 static void setup_report_key(struct cdrom_generic_command *cgc, unsigned agid, unsigned type)
965 {
966 cgc->cmd[0] = GPCMD_REPORT_KEY;
967 cgc->cmd[10] = type | (agid << 6);
968 switch (type) {
969 case 0: case 8: case 5: {
970 cgc->buflen = 8;
971 break;
972 }
973 case 1: {
974 cgc->buflen = 16;
975 break;
976 }
977 case 2: case 4: {
978 cgc->buflen = 12;
979 break;
980 }
981 }
982 cgc->cmd[9] = cgc->buflen;
983 cgc->data_direction = CGC_DATA_READ;
984 }
985
setup_send_key(struct cdrom_generic_command * cgc,unsigned agid,unsigned type)986 static void setup_send_key(struct cdrom_generic_command *cgc, unsigned agid, unsigned type)
987 {
988 cgc->cmd[0] = GPCMD_SEND_KEY;
989 cgc->cmd[10] = type | (agid << 6);
990 switch (type) {
991 case 1: {
992 cgc->buflen = 16;
993 break;
994 }
995 case 3: {
996 cgc->buflen = 12;
997 break;
998 }
999 case 6: {
1000 cgc->buflen = 8;
1001 break;
1002 }
1003 }
1004 cgc->cmd[9] = cgc->buflen;
1005 cgc->data_direction = CGC_DATA_WRITE;
1006 }
1007
dvd_do_auth(struct cdrom_device_info * cdi,dvd_authinfo * ai)1008 static int dvd_do_auth(struct cdrom_device_info *cdi, dvd_authinfo *ai)
1009 {
1010 int ret;
1011 u_char buf[20];
1012 struct cdrom_generic_command cgc;
1013 struct cdrom_device_ops *cdo = cdi->ops;
1014 rpc_state_t rpc_state;
1015
1016 memset(buf, 0, sizeof(buf));
1017 init_cdrom_command(&cgc, buf, 0, CGC_DATA_READ);
1018
1019 switch (ai->type) {
1020 /* LU data send */
1021 case DVD_LU_SEND_AGID:
1022 cdinfo(CD_DVD, "entering DVD_LU_SEND_AGID\n");
1023 setup_report_key(&cgc, ai->lsa.agid, 0);
1024
1025 if ((ret = cdo->generic_packet(cdi, &cgc)))
1026 return ret;
1027
1028 ai->lsa.agid = buf[7] >> 6;
1029 /* Returning data, let host change state */
1030 break;
1031
1032 case DVD_LU_SEND_KEY1:
1033 cdinfo(CD_DVD, "entering DVD_LU_SEND_KEY1\n");
1034 setup_report_key(&cgc, ai->lsk.agid, 2);
1035
1036 if ((ret = cdo->generic_packet(cdi, &cgc)))
1037 return ret;
1038
1039 copy_key(ai->lsk.key, &buf[4]);
1040 /* Returning data, let host change state */
1041 break;
1042
1043 case DVD_LU_SEND_CHALLENGE:
1044 cdinfo(CD_DVD, "entering DVD_LU_SEND_CHALLENGE\n");
1045 setup_report_key(&cgc, ai->lsc.agid, 1);
1046
1047 if ((ret = cdo->generic_packet(cdi, &cgc)))
1048 return ret;
1049
1050 copy_chal(ai->lsc.chal, &buf[4]);
1051 /* Returning data, let host change state */
1052 break;
1053
1054 /* Post-auth key */
1055 case DVD_LU_SEND_TITLE_KEY:
1056 cdinfo(CD_DVD, "entering DVD_LU_SEND_TITLE_KEY\n");
1057 setup_report_key(&cgc, ai->lstk.agid, 4);
1058 cgc.cmd[5] = ai->lstk.lba;
1059 cgc.cmd[4] = ai->lstk.lba >> 8;
1060 cgc.cmd[3] = ai->lstk.lba >> 16;
1061 cgc.cmd[2] = ai->lstk.lba >> 24;
1062
1063 if ((ret = cdo->generic_packet(cdi, &cgc)))
1064 return ret;
1065
1066 ai->lstk.cpm = (buf[4] >> 7) & 1;
1067 ai->lstk.cp_sec = (buf[4] >> 6) & 1;
1068 ai->lstk.cgms = (buf[4] >> 4) & 3;
1069 copy_key(ai->lstk.title_key, &buf[5]);
1070 /* Returning data, let host change state */
1071 break;
1072
1073 case DVD_LU_SEND_ASF:
1074 cdinfo(CD_DVD, "entering DVD_LU_SEND_ASF\n");
1075 setup_report_key(&cgc, ai->lsasf.agid, 5);
1076
1077 if ((ret = cdo->generic_packet(cdi, &cgc)))
1078 return ret;
1079
1080 ai->lsasf.asf = buf[7] & 1;
1081 break;
1082
1083 /* LU data receive (LU changes state) */
1084 case DVD_HOST_SEND_CHALLENGE:
1085 cdinfo(CD_DVD, "entering DVD_HOST_SEND_CHALLENGE\n");
1086 setup_send_key(&cgc, ai->hsc.agid, 1);
1087 buf[1] = 0xe;
1088 copy_chal(&buf[4], ai->hsc.chal);
1089
1090 if ((ret = cdo->generic_packet(cdi, &cgc)))
1091 return ret;
1092
1093 ai->type = DVD_LU_SEND_KEY1;
1094 break;
1095
1096 case DVD_HOST_SEND_KEY2:
1097 cdinfo(CD_DVD, "entering DVD_HOST_SEND_KEY2\n");
1098 setup_send_key(&cgc, ai->hsk.agid, 3);
1099 buf[1] = 0xa;
1100 copy_key(&buf[4], ai->hsk.key);
1101
1102 if ((ret = cdo->generic_packet(cdi, &cgc))) {
1103 ai->type = DVD_AUTH_FAILURE;
1104 return ret;
1105 }
1106 ai->type = DVD_AUTH_ESTABLISHED;
1107 break;
1108
1109 /* Misc */
1110 case DVD_INVALIDATE_AGID:
1111 cdinfo(CD_DVD, "entering DVD_INVALIDATE_AGID\n");
1112 setup_report_key(&cgc, ai->lsa.agid, 0x3f);
1113 if ((ret = cdo->generic_packet(cdi, &cgc)))
1114 return ret;
1115 break;
1116
1117 /* Get region settings */
1118 case DVD_LU_SEND_RPC_STATE:
1119 cdinfo(CD_DVD, "entering DVD_LU_SEND_RPC_STATE\n");
1120 setup_report_key(&cgc, 0, 8);
1121 memset(&rpc_state, 0, sizeof(rpc_state_t));
1122 cgc.buffer = (char *) &rpc_state;
1123
1124 if ((ret = cdo->generic_packet(cdi, &cgc)))
1125 return ret;
1126
1127 ai->lrpcs.type = rpc_state.type_code;
1128 ai->lrpcs.vra = rpc_state.vra;
1129 ai->lrpcs.ucca = rpc_state.ucca;
1130 ai->lrpcs.region_mask = rpc_state.region_mask;
1131 ai->lrpcs.rpc_scheme = rpc_state.rpc_scheme;
1132 break;
1133
1134 /* Set region settings */
1135 case DVD_HOST_SEND_RPC_STATE:
1136 cdinfo(CD_DVD, "entering DVD_HOST_SEND_RPC_STATE\n");
1137 setup_send_key(&cgc, 0, 6);
1138 buf[1] = 6;
1139 buf[4] = ai->hrpcs.pdrc;
1140
1141 if ((ret = cdo->generic_packet(cdi, &cgc)))
1142 return ret;
1143 break;
1144
1145 default:
1146 cdinfo(CD_WARNING, "Invalid DVD key ioctl (%d)\n", ai->type);
1147 return -ENOTTY;
1148 }
1149
1150 return 0;
1151 }
1152
dvd_read_physical(struct cdrom_device_info * cdi,dvd_struct * s)1153 static int dvd_read_physical(struct cdrom_device_info *cdi, dvd_struct *s)
1154 {
1155 unsigned char buf[20], *base;
1156 struct dvd_layer *layer;
1157 struct cdrom_generic_command cgc;
1158 struct cdrom_device_ops *cdo = cdi->ops;
1159 int ret, layer_num = s->physical.layer_num;
1160
1161 if (layer_num >= DVD_LAYERS)
1162 return -EINVAL;
1163
1164 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_READ);
1165 cgc.cmd[0] = GPCMD_READ_DVD_STRUCTURE;
1166 cgc.cmd[6] = layer_num;
1167 cgc.cmd[7] = s->type;
1168 cgc.cmd[9] = cgc.buflen & 0xff;
1169
1170 /*
1171 * refrain from reporting errors on non-existing layers (mainly)
1172 */
1173 cgc.quiet = 1;
1174
1175 if ((ret = cdo->generic_packet(cdi, &cgc)))
1176 return ret;
1177
1178 base = &buf[4];
1179 layer = &s->physical.layer[layer_num];
1180
1181 /*
1182 * place the data... really ugly, but at least we won't have to
1183 * worry about endianess in userspace.
1184 */
1185 memset(layer, 0, sizeof(*layer));
1186 layer->book_version = base[0] & 0xf;
1187 layer->book_type = base[0] >> 4;
1188 layer->min_rate = base[1] & 0xf;
1189 layer->disc_size = base[1] >> 4;
1190 layer->layer_type = base[2] & 0xf;
1191 layer->track_path = (base[2] >> 4) & 1;
1192 layer->nlayers = (base[2] >> 5) & 3;
1193 layer->track_density = base[3] & 0xf;
1194 layer->linear_density = base[3] >> 4;
1195 layer->start_sector = base[5] << 16 | base[6] << 8 | base[7];
1196 layer->end_sector = base[9] << 16 | base[10] << 8 | base[11];
1197 layer->end_sector_l0 = base[13] << 16 | base[14] << 8 | base[15];
1198 layer->bca = base[16] >> 7;
1199
1200 return 0;
1201 }
1202
dvd_read_copyright(struct cdrom_device_info * cdi,dvd_struct * s)1203 static int dvd_read_copyright(struct cdrom_device_info *cdi, dvd_struct *s)
1204 {
1205 int ret;
1206 u_char buf[8];
1207 struct cdrom_generic_command cgc;
1208 struct cdrom_device_ops *cdo = cdi->ops;
1209
1210 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_READ);
1211 cgc.cmd[0] = GPCMD_READ_DVD_STRUCTURE;
1212 cgc.cmd[6] = s->copyright.layer_num;
1213 cgc.cmd[7] = s->type;
1214 cgc.cmd[8] = cgc.buflen >> 8;
1215 cgc.cmd[9] = cgc.buflen & 0xff;
1216
1217 if ((ret = cdo->generic_packet(cdi, &cgc)))
1218 return ret;
1219
1220 s->copyright.cpst = buf[4];
1221 s->copyright.rmi = buf[5];
1222
1223 return 0;
1224 }
1225
dvd_read_disckey(struct cdrom_device_info * cdi,dvd_struct * s)1226 static int dvd_read_disckey(struct cdrom_device_info *cdi, dvd_struct *s)
1227 {
1228 int ret, size;
1229 u_char *buf;
1230 struct cdrom_generic_command cgc;
1231 struct cdrom_device_ops *cdo = cdi->ops;
1232
1233 size = sizeof(s->disckey.value) + 4;
1234
1235 if ((buf = (u_char *) kmalloc(size, GFP_KERNEL)) == NULL)
1236 return -ENOMEM;
1237
1238 init_cdrom_command(&cgc, buf, size, CGC_DATA_READ);
1239 cgc.cmd[0] = GPCMD_READ_DVD_STRUCTURE;
1240 cgc.cmd[7] = s->type;
1241 cgc.cmd[8] = size >> 8;
1242 cgc.cmd[9] = size & 0xff;
1243 cgc.cmd[10] = s->disckey.agid << 6;
1244
1245 if (!(ret = cdo->generic_packet(cdi, &cgc)))
1246 memcpy(s->disckey.value, &buf[4], sizeof(s->disckey.value));
1247
1248 kfree(buf);
1249 return ret;
1250 }
1251
dvd_read_bca(struct cdrom_device_info * cdi,dvd_struct * s)1252 static int dvd_read_bca(struct cdrom_device_info *cdi, dvd_struct *s)
1253 {
1254 int ret;
1255 u_char buf[4 + 188];
1256 struct cdrom_generic_command cgc;
1257 struct cdrom_device_ops *cdo = cdi->ops;
1258
1259 init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_READ);
1260 cgc.cmd[0] = GPCMD_READ_DVD_STRUCTURE;
1261 cgc.cmd[7] = s->type;
1262 cgc.cmd[9] = cgc.buflen & 0xff;
1263
1264 if ((ret = cdo->generic_packet(cdi, &cgc)))
1265 return ret;
1266
1267 s->bca.len = buf[0] << 8 | buf[1];
1268 if (s->bca.len < 12 || s->bca.len > 188) {
1269 cdinfo(CD_WARNING, "Received invalid BCA length (%d)\n", s->bca.len);
1270 return -EIO;
1271 }
1272 memcpy(s->bca.value, &buf[4], s->bca.len);
1273
1274 return 0;
1275 }
1276
dvd_read_manufact(struct cdrom_device_info * cdi,dvd_struct * s)1277 static int dvd_read_manufact(struct cdrom_device_info *cdi, dvd_struct *s)
1278 {
1279 int ret = 0, size;
1280 u_char *buf;
1281 struct cdrom_generic_command cgc;
1282 struct cdrom_device_ops *cdo = cdi->ops;
1283
1284 size = sizeof(s->manufact.value) + 4;
1285
1286 if ((buf = (u_char *) kmalloc(size, GFP_KERNEL)) == NULL)
1287 return -ENOMEM;
1288
1289 init_cdrom_command(&cgc, buf, size, CGC_DATA_READ);
1290 cgc.cmd[0] = GPCMD_READ_DVD_STRUCTURE;
1291 cgc.cmd[7] = s->type;
1292 cgc.cmd[8] = size >> 8;
1293 cgc.cmd[9] = size & 0xff;
1294
1295 if ((ret = cdo->generic_packet(cdi, &cgc))) {
1296 kfree(buf);
1297 return ret;
1298 }
1299
1300 s->manufact.len = buf[0] << 8 | buf[1];
1301 if (s->manufact.len < 0 || s->manufact.len > 2048) {
1302 cdinfo(CD_WARNING, "Received invalid manufacture info length"
1303 " (%d)\n", s->bca.len);
1304 ret = -EIO;
1305 } else {
1306 memcpy(s->manufact.value, &buf[4], s->manufact.len);
1307 }
1308
1309 kfree(buf);
1310 return ret;
1311 }
1312
dvd_read_struct(struct cdrom_device_info * cdi,dvd_struct * s)1313 static int dvd_read_struct(struct cdrom_device_info *cdi, dvd_struct *s)
1314 {
1315 switch (s->type) {
1316 case DVD_STRUCT_PHYSICAL:
1317 return dvd_read_physical(cdi, s);
1318
1319 case DVD_STRUCT_COPYRIGHT:
1320 return dvd_read_copyright(cdi, s);
1321
1322 case DVD_STRUCT_DISCKEY:
1323 return dvd_read_disckey(cdi, s);
1324
1325 case DVD_STRUCT_BCA:
1326 return dvd_read_bca(cdi, s);
1327
1328 case DVD_STRUCT_MANUFACT:
1329 return dvd_read_manufact(cdi, s);
1330
1331 default:
1332 cdinfo(CD_WARNING, ": Invalid DVD structure read requested (%d)\n",
1333 s->type);
1334 return -EINVAL;
1335 }
1336 }
1337
cdrom_mode_sense(struct cdrom_device_info * cdi,struct cdrom_generic_command * cgc,int page_code,int page_control)1338 int cdrom_mode_sense(struct cdrom_device_info *cdi,
1339 struct cdrom_generic_command *cgc,
1340 int page_code, int page_control)
1341 {
1342 struct cdrom_device_ops *cdo = cdi->ops;
1343
1344 memset(cgc->cmd, 0, sizeof(cgc->cmd));
1345
1346 cgc->cmd[0] = GPCMD_MODE_SENSE_10;
1347 cgc->cmd[2] = page_code | (page_control << 6);
1348 cgc->cmd[7] = cgc->buflen >> 8;
1349 cgc->cmd[8] = cgc->buflen & 0xff;
1350 cgc->data_direction = CGC_DATA_READ;
1351 return cdo->generic_packet(cdi, cgc);
1352 }
1353
cdrom_mode_select(struct cdrom_device_info * cdi,struct cdrom_generic_command * cgc)1354 int cdrom_mode_select(struct cdrom_device_info *cdi,
1355 struct cdrom_generic_command *cgc)
1356 {
1357 struct cdrom_device_ops *cdo = cdi->ops;
1358
1359 memset(cgc->cmd, 0, sizeof(cgc->cmd));
1360 memset(cgc->buffer, 0, 2);
1361 cgc->cmd[0] = GPCMD_MODE_SELECT_10;
1362 cgc->cmd[1] = 0x10; /* PF */
1363 cgc->cmd[7] = cgc->buflen >> 8;
1364 cgc->cmd[8] = cgc->buflen & 0xff;
1365 cgc->data_direction = CGC_DATA_WRITE;
1366 return cdo->generic_packet(cdi, cgc);
1367 }
1368
cdrom_read_subchannel(struct cdrom_device_info * cdi,struct cdrom_subchnl * subchnl,int mcn)1369 static int cdrom_read_subchannel(struct cdrom_device_info *cdi,
1370 struct cdrom_subchnl *subchnl, int mcn)
1371 {
1372 struct cdrom_device_ops *cdo = cdi->ops;
1373 struct cdrom_generic_command cgc;
1374 char buffer[32];
1375 int ret;
1376
1377 init_cdrom_command(&cgc, buffer, 16, CGC_DATA_READ);
1378 cgc.cmd[0] = GPCMD_READ_SUBCHANNEL;
1379 cgc.cmd[1] = 2; /* MSF addressing */
1380 cgc.cmd[2] = 0x40; /* request subQ data */
1381 cgc.cmd[3] = mcn ? 2 : 1;
1382 cgc.cmd[8] = 16;
1383
1384 if ((ret = cdo->generic_packet(cdi, &cgc)))
1385 return ret;
1386
1387 subchnl->cdsc_audiostatus = cgc.buffer[1];
1388 subchnl->cdsc_format = CDROM_MSF;
1389 subchnl->cdsc_ctrl = cgc.buffer[5] & 0xf;
1390 subchnl->cdsc_trk = cgc.buffer[6];
1391 subchnl->cdsc_ind = cgc.buffer[7];
1392
1393 subchnl->cdsc_reladdr.msf.minute = cgc.buffer[13];
1394 subchnl->cdsc_reladdr.msf.second = cgc.buffer[14];
1395 subchnl->cdsc_reladdr.msf.frame = cgc.buffer[15];
1396 subchnl->cdsc_absaddr.msf.minute = cgc.buffer[9];
1397 subchnl->cdsc_absaddr.msf.second = cgc.buffer[10];
1398 subchnl->cdsc_absaddr.msf.frame = cgc.buffer[11];
1399
1400 return 0;
1401 }
1402
1403 /*
1404 * Specific READ_10 interface
1405 */
cdrom_read_cd(struct cdrom_device_info * cdi,struct cdrom_generic_command * cgc,int lba,int blocksize,int nblocks)1406 static int cdrom_read_cd(struct cdrom_device_info *cdi,
1407 struct cdrom_generic_command *cgc, int lba,
1408 int blocksize, int nblocks)
1409 {
1410 struct cdrom_device_ops *cdo = cdi->ops;
1411
1412 memset(&cgc->cmd, 0, sizeof(cgc->cmd));
1413 cgc->cmd[0] = GPCMD_READ_10;
1414 cgc->cmd[2] = (lba >> 24) & 0xff;
1415 cgc->cmd[3] = (lba >> 16) & 0xff;
1416 cgc->cmd[4] = (lba >> 8) & 0xff;
1417 cgc->cmd[5] = lba & 0xff;
1418 cgc->cmd[6] = (nblocks >> 16) & 0xff;
1419 cgc->cmd[7] = (nblocks >> 8) & 0xff;
1420 cgc->cmd[8] = nblocks & 0xff;
1421 cgc->buflen = blocksize * nblocks;
1422 return cdo->generic_packet(cdi, cgc);
1423 }
1424
1425 /* very generic interface for reading the various types of blocks */
cdrom_read_block(struct cdrom_device_info * cdi,struct cdrom_generic_command * cgc,int lba,int nblocks,int format,int blksize)1426 static int cdrom_read_block(struct cdrom_device_info *cdi,
1427 struct cdrom_generic_command *cgc,
1428 int lba, int nblocks, int format, int blksize)
1429 {
1430 struct cdrom_device_ops *cdo = cdi->ops;
1431
1432 memset(&cgc->cmd, 0, sizeof(cgc->cmd));
1433 cgc->cmd[0] = GPCMD_READ_CD;
1434 /* expected sector size - cdda,mode1,etc. */
1435 cgc->cmd[1] = format << 2;
1436 /* starting address */
1437 cgc->cmd[2] = (lba >> 24) & 0xff;
1438 cgc->cmd[3] = (lba >> 16) & 0xff;
1439 cgc->cmd[4] = (lba >> 8) & 0xff;
1440 cgc->cmd[5] = lba & 0xff;
1441 /* number of blocks */
1442 cgc->cmd[6] = (nblocks >> 16) & 0xff;
1443 cgc->cmd[7] = (nblocks >> 8) & 0xff;
1444 cgc->cmd[8] = nblocks & 0xff;
1445 cgc->buflen = blksize * nblocks;
1446
1447 /* set the header info returned */
1448 switch (blksize) {
1449 case CD_FRAMESIZE_RAW0 : cgc->cmd[9] = 0x58; break;
1450 case CD_FRAMESIZE_RAW1 : cgc->cmd[9] = 0x78; break;
1451 case CD_FRAMESIZE_RAW : cgc->cmd[9] = 0xf8; break;
1452 default : cgc->cmd[9] = 0x10;
1453 }
1454
1455 return cdo->generic_packet(cdi, cgc);
1456 }
1457
1458 /* Just about every imaginable ioctl is supported in the Uniform layer
1459 * these days. ATAPI / SCSI specific code now mainly resides in
1460 * mmc_ioct().
1461 */
cdrom_ioctl(struct inode * ip,struct file * fp,unsigned int cmd,unsigned long arg)1462 int cdrom_ioctl(struct inode *ip, struct file *fp, unsigned int cmd,
1463 unsigned long arg)
1464 {
1465 kdev_t dev = ip->i_rdev;
1466 struct cdrom_device_info *cdi = cdrom_find_device(dev);
1467 struct cdrom_device_ops *cdo = cdi->ops;
1468 int ret;
1469
1470 /* the first few commands do not deal with audio drive_info, but
1471 only with routines in cdrom device operations. */
1472 switch (cmd) {
1473 case CDROMMULTISESSION: {
1474 struct cdrom_multisession ms_info;
1475 u_char requested_format;
1476 cdinfo(CD_DO_IOCTL, "entering CDROMMULTISESSION\n");
1477 if (!(cdo->capability & CDC_MULTI_SESSION))
1478 return -ENOSYS;
1479 IOCTL_IN(arg, struct cdrom_multisession, ms_info);
1480 requested_format = ms_info.addr_format;
1481 if (!((requested_format == CDROM_MSF) ||
1482 (requested_format == CDROM_LBA)))
1483 return -EINVAL;
1484 ms_info.addr_format = CDROM_LBA;
1485 if ((ret=cdo->get_last_session(cdi, &ms_info)))
1486 return ret;
1487 sanitize_format(&ms_info.addr, &ms_info.addr_format,
1488 requested_format);
1489 IOCTL_OUT(arg, struct cdrom_multisession, ms_info);
1490 cdinfo(CD_DO_IOCTL, "CDROMMULTISESSION successful\n");
1491 return 0;
1492 }
1493
1494 case CDROMEJECT: {
1495 cdinfo(CD_DO_IOCTL, "entering CDROMEJECT\n");
1496 if (!CDROM_CAN(CDC_OPEN_TRAY))
1497 return -ENOSYS;
1498 if (cdi->use_count != 1 || keeplocked)
1499 return -EBUSY;
1500 if (CDROM_CAN(CDC_LOCK))
1501 if ((ret=cdo->lock_door(cdi, 0)))
1502 return ret;
1503
1504 return cdo->tray_move(cdi, 1);
1505 }
1506
1507 case CDROMCLOSETRAY: {
1508 cdinfo(CD_DO_IOCTL, "entering CDROMCLOSETRAY\n");
1509 if (!CDROM_CAN(CDC_CLOSE_TRAY))
1510 return -ENOSYS;
1511 return cdo->tray_move(cdi, 0);
1512 }
1513
1514 case CDROMEJECT_SW: {
1515 cdinfo(CD_DO_IOCTL, "entering CDROMEJECT_SW\n");
1516 if (!CDROM_CAN(CDC_OPEN_TRAY))
1517 return -ENOSYS;
1518 if (keeplocked)
1519 return -EBUSY;
1520 cdi->options &= ~(CDO_AUTO_CLOSE | CDO_AUTO_EJECT);
1521 if (arg)
1522 cdi->options |= CDO_AUTO_CLOSE | CDO_AUTO_EJECT;
1523 return 0;
1524 }
1525
1526 case CDROM_MEDIA_CHANGED: {
1527 struct cdrom_changer_info info;
1528
1529 cdinfo(CD_DO_IOCTL, "entering CDROM_MEDIA_CHANGED\n");
1530 if (!CDROM_CAN(CDC_MEDIA_CHANGED))
1531 return -ENOSYS;
1532
1533 /* cannot select disc or select current disc */
1534 if (!CDROM_CAN(CDC_SELECT_DISC) || arg == CDSL_CURRENT)
1535 return media_changed(cdi, 1);
1536
1537 if ((unsigned int)arg >= cdi->capacity)
1538 return -EINVAL;
1539
1540 if ((ret = cdrom_read_mech_status(cdi, &info)))
1541 return ret;
1542
1543 return info.slots[arg].change;
1544 }
1545
1546 case CDROM_SET_OPTIONS: {
1547 cdinfo(CD_DO_IOCTL, "entering CDROM_SET_OPTIONS\n");
1548 /* options need to be in sync with capability. too late for
1549 that, so we have to check each one separately... */
1550 switch (arg) {
1551 case CDO_USE_FFLAGS:
1552 case CDO_CHECK_TYPE:
1553 break;
1554 case CDO_LOCK:
1555 if (!CDROM_CAN(CDC_LOCK))
1556 return -ENOSYS;
1557 break;
1558 case 0:
1559 return cdi->options;
1560 /* default is basically CDO_[AUTO_CLOSE|AUTO_EJECT] */
1561 default:
1562 if (!CDROM_CAN(arg))
1563 return -ENOSYS;
1564 }
1565 cdi->options |= (int) arg;
1566 return cdi->options;
1567 }
1568
1569 case CDROM_CLEAR_OPTIONS: {
1570 cdinfo(CD_DO_IOCTL, "entering CDROM_CLEAR_OPTIONS\n");
1571 cdi->options &= ~(int) arg;
1572 return cdi->options;
1573 }
1574
1575 case CDROM_SELECT_SPEED: {
1576 cdinfo(CD_DO_IOCTL, "entering CDROM_SELECT_SPEED\n");
1577 if (!CDROM_CAN(CDC_SELECT_SPEED))
1578 return -ENOSYS;
1579 return cdo->select_speed(cdi, arg);
1580 }
1581
1582 case CDROM_SELECT_DISC: {
1583 cdinfo(CD_DO_IOCTL, "entering CDROM_SELECT_DISC\n");
1584 if (!CDROM_CAN(CDC_SELECT_DISC))
1585 return -ENOSYS;
1586
1587 if ((arg != CDSL_CURRENT) && (arg != CDSL_NONE))
1588 if ((int)arg >= cdi->capacity)
1589 return -EINVAL;
1590
1591 /* cdo->select_disc is a hook to allow a driver-specific
1592 * way of seleting disc. However, since there is no
1593 * equiv hook for cdrom_slot_status this may not
1594 * actually be useful...
1595 */
1596 if (cdo->select_disc != NULL)
1597 return cdo->select_disc(cdi, arg);
1598
1599 /* no driver specific select_disc(), call our own */
1600 cdinfo(CD_CHANGER, "Using generic cdrom_select_disc()\n");
1601 return cdrom_select_disc(cdi, arg);
1602 }
1603
1604 case CDROMRESET: {
1605 if (!capable(CAP_SYS_ADMIN))
1606 return -EACCES;
1607 cdinfo(CD_DO_IOCTL, "entering CDROM_RESET\n");
1608 if (!CDROM_CAN(CDC_RESET))
1609 return -ENOSYS;
1610 return cdo->reset(cdi);
1611 }
1612
1613 case CDROM_LOCKDOOR: {
1614 cdinfo(CD_DO_IOCTL, "%socking door.\n", arg ? "L" : "Unl");
1615 if (!CDROM_CAN(CDC_LOCK))
1616 return -EDRIVE_CANT_DO_THIS;
1617 keeplocked = arg ? 1 : 0;
1618 /* don't unlock the door on multiple opens,but allow root
1619 * to do so */
1620 if ((cdi->use_count != 1) && !arg && !capable(CAP_SYS_ADMIN))
1621 return -EBUSY;
1622 return cdo->lock_door(cdi, arg);
1623 }
1624
1625 case CDROM_DEBUG: {
1626 if (!capable(CAP_SYS_ADMIN))
1627 return -EACCES;
1628 cdinfo(CD_DO_IOCTL, "%sabling debug.\n", arg ? "En" : "Dis");
1629 debug = arg ? 1 : 0;
1630 return debug;
1631 }
1632
1633 case CDROM_GET_CAPABILITY: {
1634 cdinfo(CD_DO_IOCTL, "entering CDROM_GET_CAPABILITY\n");
1635 return (cdo->capability & ~cdi->mask);
1636 }
1637
1638 /* The following function is implemented, although very few audio
1639 * discs give Universal Product Code information, which should just be
1640 * the Medium Catalog Number on the box. Note, that the way the code
1641 * is written on the CD is /not/ uniform across all discs!
1642 */
1643 case CDROM_GET_MCN: {
1644 struct cdrom_mcn mcn;
1645 cdinfo(CD_DO_IOCTL, "entering CDROM_GET_MCN\n");
1646 if (!(cdo->capability & CDC_MCN))
1647 return -ENOSYS;
1648 if ((ret=cdo->get_mcn(cdi, &mcn)))
1649 return ret;
1650 IOCTL_OUT(arg, struct cdrom_mcn, mcn);
1651 cdinfo(CD_DO_IOCTL, "CDROM_GET_MCN successful\n");
1652 return 0;
1653 }
1654
1655 case CDROM_DRIVE_STATUS: {
1656 cdinfo(CD_DO_IOCTL, "entering CDROM_DRIVE_STATUS\n");
1657 if (!(cdo->capability & CDC_DRIVE_STATUS))
1658 return -ENOSYS;
1659 if (!CDROM_CAN(CDC_SELECT_DISC))
1660 return cdo->drive_status(cdi, CDSL_CURRENT);
1661 if ((arg == CDSL_CURRENT) || (arg == CDSL_NONE))
1662 return cdo->drive_status(cdi, CDSL_CURRENT);
1663 if (((int)arg >= cdi->capacity))
1664 return -EINVAL;
1665 return cdrom_slot_status(cdi, arg);
1666 }
1667
1668 /* Ok, this is where problems start. The current interface for the
1669 CDROM_DISC_STATUS ioctl is flawed. It makes the false assumption
1670 that CDs are all CDS_DATA_1 or all CDS_AUDIO, etc. Unfortunatly,
1671 while this is often the case, it is also very common for CDs to
1672 have some tracks with data, and some tracks with audio. Just
1673 because I feel like it, I declare the following to be the best
1674 way to cope. If the CD has ANY data tracks on it, it will be
1675 returned as a data CD. If it has any XA tracks, I will return
1676 it as that. Now I could simplify this interface by combining these
1677 returns with the above, but this more clearly demonstrates
1678 the problem with the current interface. Too bad this wasn't
1679 designed to use bitmasks... -Erik
1680
1681 Well, now we have the option CDS_MIXED: a mixed-type CD.
1682 User level programmers might feel the ioctl is not very useful.
1683 ---david
1684 */
1685 case CDROM_DISC_STATUS: {
1686 tracktype tracks;
1687 cdinfo(CD_DO_IOCTL, "entering CDROM_DISC_STATUS\n");
1688 cdrom_count_tracks(cdi, &tracks);
1689 if (tracks.error)
1690 return(tracks.error);
1691
1692 /* Policy mode on */
1693 if (tracks.audio > 0) {
1694 if (tracks.data==0 && tracks.cdi==0 && tracks.xa==0)
1695 return CDS_AUDIO;
1696 else
1697 return CDS_MIXED;
1698 }
1699 if (tracks.cdi > 0) return CDS_XA_2_2;
1700 if (tracks.xa > 0) return CDS_XA_2_1;
1701 if (tracks.data > 0) return CDS_DATA_1;
1702 /* Policy mode off */
1703
1704 cdinfo(CD_WARNING,"This disc doesn't have any tracks I recognize!\n");
1705 return CDS_NO_INFO;
1706 }
1707
1708 case CDROM_CHANGER_NSLOTS: {
1709 cdinfo(CD_DO_IOCTL, "entering CDROM_CHANGER_NSLOTS\n");
1710 return cdi->capacity;
1711 }
1712 }
1713
1714 /* use the ioctls that are implemented through the generic_packet()
1715 interface. this may look at bit funny, but if -ENOTTY is
1716 returned that particular ioctl is not implemented and we
1717 let it go through the device specific ones. */
1718 if (CDROM_CAN(CDC_GENERIC_PACKET)) {
1719 ret = mmc_ioctl(cdi, cmd, arg);
1720 if (ret != -ENOTTY) {
1721 return ret;
1722 }
1723 }
1724
1725 /* note: most of the cdinfo() calls are commented out here,
1726 because they fill up the sys log when CD players poll
1727 the drive. */
1728 switch (cmd) {
1729 case CDROMSUBCHNL: {
1730 struct cdrom_subchnl q;
1731 u_char requested, back;
1732 if (!CDROM_CAN(CDC_PLAY_AUDIO))
1733 return -ENOSYS;
1734 /* cdinfo(CD_DO_IOCTL,"entering CDROMSUBCHNL\n");*/
1735 IOCTL_IN(arg, struct cdrom_subchnl, q);
1736 requested = q.cdsc_format;
1737 if (!((requested == CDROM_MSF) ||
1738 (requested == CDROM_LBA)))
1739 return -EINVAL;
1740 q.cdsc_format = CDROM_MSF;
1741 if ((ret=cdo->audio_ioctl(cdi, cmd, &q)))
1742 return ret;
1743 back = q.cdsc_format; /* local copy */
1744 sanitize_format(&q.cdsc_absaddr, &back, requested);
1745 sanitize_format(&q.cdsc_reladdr, &q.cdsc_format, requested);
1746 IOCTL_OUT(arg, struct cdrom_subchnl, q);
1747 /* cdinfo(CD_DO_IOCTL, "CDROMSUBCHNL successful\n"); */
1748 return 0;
1749 }
1750 case CDROMREADTOCHDR: {
1751 struct cdrom_tochdr header;
1752 if (!CDROM_CAN(CDC_PLAY_AUDIO))
1753 return -ENOSYS;
1754 /* cdinfo(CD_DO_IOCTL, "entering CDROMREADTOCHDR\n"); */
1755 IOCTL_IN(arg, struct cdrom_tochdr, header);
1756 if ((ret=cdo->audio_ioctl(cdi, cmd, &header)))
1757 return ret;
1758 IOCTL_OUT(arg, struct cdrom_tochdr, header);
1759 /* cdinfo(CD_DO_IOCTL, "CDROMREADTOCHDR successful\n"); */
1760 return 0;
1761 }
1762 case CDROMREADTOCENTRY: {
1763 struct cdrom_tocentry entry;
1764 u_char requested_format;
1765 if (!CDROM_CAN(CDC_PLAY_AUDIO))
1766 return -ENOSYS;
1767 /* cdinfo(CD_DO_IOCTL, "entering CDROMREADTOCENTRY\n"); */
1768 IOCTL_IN(arg, struct cdrom_tocentry, entry);
1769 requested_format = entry.cdte_format;
1770 if (!((requested_format == CDROM_MSF) ||
1771 (requested_format == CDROM_LBA)))
1772 return -EINVAL;
1773 /* make interface to low-level uniform */
1774 entry.cdte_format = CDROM_MSF;
1775 if ((ret=cdo->audio_ioctl(cdi, cmd, &entry)))
1776 return ret;
1777 sanitize_format(&entry.cdte_addr,
1778 &entry.cdte_format, requested_format);
1779 IOCTL_OUT(arg, struct cdrom_tocentry, entry);
1780 /* cdinfo(CD_DO_IOCTL, "CDROMREADTOCENTRY successful\n"); */
1781 return 0;
1782 }
1783 case CDROMPLAYMSF: {
1784 struct cdrom_msf msf;
1785 if (!CDROM_CAN(CDC_PLAY_AUDIO))
1786 return -ENOSYS;
1787 cdinfo(CD_DO_IOCTL, "entering CDROMPLAYMSF\n");
1788 IOCTL_IN(arg, struct cdrom_msf, msf);
1789 return cdo->audio_ioctl(cdi, cmd, &msf);
1790 }
1791 case CDROMPLAYTRKIND: {
1792 struct cdrom_ti ti;
1793 if (!CDROM_CAN(CDC_PLAY_AUDIO))
1794 return -ENOSYS;
1795 cdinfo(CD_DO_IOCTL, "entering CDROMPLAYTRKIND\n");
1796 IOCTL_IN(arg, struct cdrom_ti, ti);
1797 CHECKAUDIO;
1798 return cdo->audio_ioctl(cdi, cmd, &ti);
1799 }
1800 case CDROMVOLCTRL: {
1801 struct cdrom_volctrl volume;
1802 if (!CDROM_CAN(CDC_PLAY_AUDIO))
1803 return -ENOSYS;
1804 cdinfo(CD_DO_IOCTL, "entering CDROMVOLCTRL\n");
1805 IOCTL_IN(arg, struct cdrom_volctrl, volume);
1806 return cdo->audio_ioctl(cdi, cmd, &volume);
1807 }
1808 case CDROMVOLREAD: {
1809 struct cdrom_volctrl volume;
1810 if (!CDROM_CAN(CDC_PLAY_AUDIO))
1811 return -ENOSYS;
1812 cdinfo(CD_DO_IOCTL, "entering CDROMVOLREAD\n");
1813 if ((ret=cdo->audio_ioctl(cdi, cmd, &volume)))
1814 return ret;
1815 IOCTL_OUT(arg, struct cdrom_volctrl, volume);
1816 return 0;
1817 }
1818 case CDROMSTART:
1819 case CDROMSTOP:
1820 case CDROMPAUSE:
1821 case CDROMRESUME: {
1822 if (!CDROM_CAN(CDC_PLAY_AUDIO))
1823 return -ENOSYS;
1824 cdinfo(CD_DO_IOCTL, "doing audio ioctl (start/stop/pause/resume)\n");
1825 CHECKAUDIO;
1826 return cdo->audio_ioctl(cdi, cmd, NULL);
1827 }
1828 } /* switch */
1829
1830 /* do the device specific ioctls */
1831 if (CDROM_CAN(CDC_IOCTLS))
1832 return cdo->dev_ioctl(cdi, cmd, arg);
1833
1834 return -ENOSYS;
1835 }
1836
1837 static inline
msf_to_lba(char m,char s,char f)1838 int msf_to_lba(char m, char s, char f)
1839 {
1840 return (((m * CD_SECS) + s) * CD_FRAMES + f) - CD_MSF_OFFSET;
1841 }
1842
1843 /*
1844 * Required when we need to use READ_10 to issue other than 2048 block
1845 * reads
1846 */
cdrom_switch_blocksize(struct cdrom_device_info * cdi,int size)1847 static int cdrom_switch_blocksize(struct cdrom_device_info *cdi, int size)
1848 {
1849 struct cdrom_device_ops *cdo = cdi->ops;
1850 struct cdrom_generic_command cgc;
1851 struct modesel_head mh;
1852
1853 memset(&mh, 0, sizeof(mh));
1854 mh.block_desc_length = 0x08;
1855 mh.block_length_med = (size >> 8) & 0xff;
1856 mh.block_length_lo = size & 0xff;
1857
1858 memset(&cgc, 0, sizeof(cgc));
1859 cgc.cmd[0] = 0x15;
1860 cgc.cmd[1] = 1 << 4;
1861 cgc.cmd[4] = 12;
1862 cgc.buflen = sizeof(mh);
1863 cgc.buffer = (char *) &mh;
1864 cgc.data_direction = CGC_DATA_WRITE;
1865 mh.block_desc_length = 0x08;
1866 mh.block_length_med = (size >> 8) & 0xff;
1867 mh.block_length_lo = size & 0xff;
1868
1869 return cdo->generic_packet(cdi, &cgc);
1870 }
1871
cdrom_do_cmd(struct cdrom_device_info * cdi,struct cdrom_generic_command * cgc)1872 static int cdrom_do_cmd(struct cdrom_device_info *cdi,
1873 struct cdrom_generic_command *cgc)
1874 {
1875 struct request_sense *usense, sense;
1876 unsigned char *ubuf;
1877 int ret;
1878
1879 if (cgc->data_direction == CGC_DATA_UNKNOWN)
1880 return -EINVAL;
1881
1882 if (cgc->buflen < 0 || cgc->buflen >= 131072)
1883 return -EINVAL;
1884
1885 usense = cgc->sense;
1886 cgc->sense = &sense;
1887 if (usense && !access_ok(VERIFY_WRITE, usense, sizeof(*usense))) {
1888 return -EFAULT;
1889 }
1890
1891 ubuf = cgc->buffer;
1892 if (cgc->data_direction == CGC_DATA_READ ||
1893 cgc->data_direction == CGC_DATA_WRITE) {
1894 cgc->buffer = kmalloc(cgc->buflen, GFP_KERNEL);
1895 if (cgc->buffer == NULL)
1896 return -ENOMEM;
1897 }
1898
1899
1900 if (cgc->data_direction == CGC_DATA_READ) {
1901 if (!access_ok(VERIFY_READ, ubuf, cgc->buflen)) {
1902 kfree(cgc->buffer);
1903 return -EFAULT;
1904 }
1905 } else if (cgc->data_direction == CGC_DATA_WRITE) {
1906 if (copy_from_user(cgc->buffer, ubuf, cgc->buflen)) {
1907 kfree(cgc->buffer);
1908 return -EFAULT;
1909 }
1910 }
1911
1912 ret = cdi->ops->generic_packet(cdi, cgc);
1913 __copy_to_user(usense, cgc->sense, sizeof(*usense));
1914 if (!ret && cgc->data_direction == CGC_DATA_READ)
1915 __copy_to_user(ubuf, cgc->buffer, cgc->buflen);
1916 if (cgc->data_direction == CGC_DATA_READ ||
1917 cgc->data_direction == CGC_DATA_WRITE) {
1918 kfree(cgc->buffer);
1919 }
1920 return ret;
1921 }
1922
mmc_ioctl(struct cdrom_device_info * cdi,unsigned int cmd,unsigned long arg)1923 static int mmc_ioctl(struct cdrom_device_info *cdi, unsigned int cmd,
1924 unsigned long arg)
1925 {
1926 struct cdrom_device_ops *cdo = cdi->ops;
1927 struct cdrom_generic_command cgc;
1928 struct request_sense sense;
1929 kdev_t dev = cdi->dev;
1930 char buffer[32];
1931 int ret = 0;
1932
1933 memset(&cgc, 0, sizeof(cgc));
1934
1935 /* build a unified command and queue it through
1936 cdo->generic_packet() */
1937 switch (cmd) {
1938 case CDROMREADRAW:
1939 case CDROMREADMODE1:
1940 case CDROMREADMODE2: {
1941 struct cdrom_msf msf;
1942 int blocksize = 0, format = 0, lba;
1943
1944 switch (cmd) {
1945 case CDROMREADRAW:
1946 blocksize = CD_FRAMESIZE_RAW;
1947 break;
1948 case CDROMREADMODE1:
1949 blocksize = CD_FRAMESIZE;
1950 format = 2;
1951 break;
1952 case CDROMREADMODE2:
1953 blocksize = CD_FRAMESIZE_RAW0;
1954 break;
1955 }
1956 IOCTL_IN(arg, struct cdrom_msf, msf);
1957 lba = msf_to_lba(msf.cdmsf_min0,msf.cdmsf_sec0,msf.cdmsf_frame0);
1958 /* FIXME: we need upper bound checking, too!! */
1959 if (lba < 0)
1960 return -EINVAL;
1961 cgc.buffer = (char *) kmalloc(blocksize, GFP_KERNEL);
1962 if (cgc.buffer == NULL)
1963 return -ENOMEM;
1964 memset(&sense, 0, sizeof(sense));
1965 cgc.sense = &sense;
1966 cgc.data_direction = CGC_DATA_READ;
1967 ret = cdrom_read_block(cdi, &cgc, lba, 1, format, blocksize);
1968 if (ret && sense.sense_key==0x05 && sense.asc==0x20 && sense.ascq==0x00) {
1969 /*
1970 * SCSI-II devices are not required to support
1971 * READ_CD, so let's try switching block size
1972 */
1973 /* FIXME: switch back again... */
1974 if ((ret = cdrom_switch_blocksize(cdi, blocksize))) {
1975 kfree(cgc.buffer);
1976 return ret;
1977 }
1978 cgc.sense = NULL;
1979 ret = cdrom_read_cd(cdi, &cgc, lba, blocksize, 1);
1980 ret |= cdrom_switch_blocksize(cdi, blocksize);
1981 }
1982 if (!ret && copy_to_user((char *)arg, cgc.buffer, blocksize))
1983 ret = -EFAULT;
1984 kfree(cgc.buffer);
1985 return ret;
1986 }
1987 case CDROMREADAUDIO: {
1988 struct cdrom_read_audio ra;
1989 int lba, nr;
1990
1991 IOCTL_IN(arg, struct cdrom_read_audio, ra);
1992
1993 if (ra.addr_format == CDROM_MSF)
1994 lba = msf_to_lba(ra.addr.msf.minute,
1995 ra.addr.msf.second,
1996 ra.addr.msf.frame);
1997 else if (ra.addr_format == CDROM_LBA)
1998 lba = ra.addr.lba;
1999 else
2000 return -EINVAL;
2001
2002 /* FIXME: we need upper bound checking, too!! */
2003 if (lba < 0 || ra.nframes <= 0)
2004 return -EINVAL;
2005
2006 /*
2007 * start with will ra.nframes size, back down if alloc fails
2008 */
2009 nr = ra.nframes;
2010 do {
2011 cgc.buffer = kmalloc(CD_FRAMESIZE_RAW * nr, GFP_KERNEL);
2012 if (cgc.buffer)
2013 break;
2014
2015 nr >>= 1;
2016 } while (nr);
2017
2018 if (!nr)
2019 return -ENOMEM;
2020
2021 if (!access_ok(VERIFY_WRITE, ra.buf, ra.nframes*CD_FRAMESIZE_RAW)) {
2022 kfree(cgc.buffer);
2023 return -EFAULT;
2024 }
2025 cgc.data_direction = CGC_DATA_READ;
2026 while (ra.nframes > 0) {
2027 if (nr > ra.nframes)
2028 nr = ra.nframes;
2029
2030 ret = cdrom_read_block(cdi, &cgc, lba, nr, 1, CD_FRAMESIZE_RAW);
2031 if (ret)
2032 break;
2033 __copy_to_user(ra.buf, cgc.buffer, CD_FRAMESIZE_RAW*nr);
2034 ra.buf += CD_FRAMESIZE_RAW * nr;
2035 ra.nframes -= nr;
2036 lba += nr;
2037 }
2038 kfree(cgc.buffer);
2039 return ret;
2040 }
2041 case CDROMSUBCHNL: {
2042 struct cdrom_subchnl q;
2043 u_char requested, back;
2044 IOCTL_IN(arg, struct cdrom_subchnl, q);
2045 requested = q.cdsc_format;
2046 if (!((requested == CDROM_MSF) ||
2047 (requested == CDROM_LBA)))
2048 return -EINVAL;
2049 q.cdsc_format = CDROM_MSF;
2050 if ((ret = cdrom_read_subchannel(cdi, &q, 0)))
2051 return ret;
2052 back = q.cdsc_format; /* local copy */
2053 sanitize_format(&q.cdsc_absaddr, &back, requested);
2054 sanitize_format(&q.cdsc_reladdr, &q.cdsc_format, requested);
2055 IOCTL_OUT(arg, struct cdrom_subchnl, q);
2056 /* cdinfo(CD_DO_IOCTL, "CDROMSUBCHNL successful\n"); */
2057 return 0;
2058 }
2059 case CDROMPLAYMSF: {
2060 struct cdrom_msf msf;
2061 cdinfo(CD_DO_IOCTL, "entering CDROMPLAYMSF\n");
2062 IOCTL_IN(arg, struct cdrom_msf, msf);
2063 cgc.cmd[0] = GPCMD_PLAY_AUDIO_MSF;
2064 cgc.cmd[3] = msf.cdmsf_min0;
2065 cgc.cmd[4] = msf.cdmsf_sec0;
2066 cgc.cmd[5] = msf.cdmsf_frame0;
2067 cgc.cmd[6] = msf.cdmsf_min1;
2068 cgc.cmd[7] = msf.cdmsf_sec1;
2069 cgc.cmd[8] = msf.cdmsf_frame1;
2070 cgc.data_direction = CGC_DATA_NONE;
2071 return cdo->generic_packet(cdi, &cgc);
2072 }
2073 case CDROMPLAYBLK: {
2074 struct cdrom_blk blk;
2075 cdinfo(CD_DO_IOCTL, "entering CDROMPLAYBLK\n");
2076 IOCTL_IN(arg, struct cdrom_blk, blk);
2077 cgc.cmd[0] = GPCMD_PLAY_AUDIO_10;
2078 cgc.cmd[2] = (blk.from >> 24) & 0xff;
2079 cgc.cmd[3] = (blk.from >> 16) & 0xff;
2080 cgc.cmd[4] = (blk.from >> 8) & 0xff;
2081 cgc.cmd[5] = blk.from & 0xff;
2082 cgc.cmd[7] = (blk.len >> 8) & 0xff;
2083 cgc.cmd[8] = blk.len & 0xff;
2084 cgc.data_direction = CGC_DATA_NONE;
2085 return cdo->generic_packet(cdi, &cgc);
2086 }
2087 case CDROMVOLCTRL:
2088 case CDROMVOLREAD: {
2089 struct cdrom_volctrl volctrl;
2090 char mask[32];
2091 unsigned short offset;
2092 cdinfo(CD_DO_IOCTL, "entering CDROMVOLUME\n");
2093
2094 IOCTL_IN(arg, struct cdrom_volctrl, volctrl);
2095
2096 cgc.buffer = buffer;
2097 cgc.buflen = 24;
2098 if ((ret = cdrom_mode_sense(cdi, &cgc, GPMODE_AUDIO_CTL_PAGE, 0)))
2099 return ret;
2100
2101 /* some drives have longer pages, adjust and reread. */
2102 if (buffer[1] > cgc.buflen) {
2103 cgc.buflen = buffer[1] + 2;
2104 if ((ret = cdrom_mode_sense(cdi, &cgc,
2105 GPMODE_AUDIO_CTL_PAGE, 0)))
2106 return ret;
2107 }
2108
2109 /* get the offset from the length of the page. length
2110 is measure from byte 2 an on, thus the 14. */
2111 offset = buffer[1] - 14;
2112
2113 /* now we have the current volume settings. if it was only
2114 a CDROMVOLREAD, return these values */
2115 if (cmd == CDROMVOLREAD) {
2116 volctrl.channel0 = buffer[offset+9];
2117 volctrl.channel1 = buffer[offset+11];
2118 volctrl.channel2 = buffer[offset+13];
2119 volctrl.channel3 = buffer[offset+15];
2120 IOCTL_OUT(arg, struct cdrom_volctrl, volctrl);
2121 return 0;
2122 }
2123
2124 /* get the volume mask */
2125 cgc.buffer = mask;
2126 if ((ret = cdrom_mode_sense(cdi, &cgc,
2127 GPMODE_AUDIO_CTL_PAGE, 1)))
2128 return ret;
2129
2130 buffer[offset+9] = volctrl.channel0 & mask[offset+9];
2131 buffer[offset+11] = volctrl.channel1 & mask[offset+11];
2132 buffer[offset+13] = volctrl.channel2 & mask[offset+13];
2133 buffer[offset+15] = volctrl.channel3 & mask[offset+15];
2134
2135 /* set volume */
2136 cgc.buffer = buffer;
2137 return cdrom_mode_select(cdi, &cgc);
2138 }
2139
2140 case CDROMSTART:
2141 case CDROMSTOP: {
2142 cdinfo(CD_DO_IOCTL, "entering CDROMSTART/CDROMSTOP\n");
2143 cgc.cmd[0] = GPCMD_START_STOP_UNIT;
2144 cgc.cmd[1] = 1;
2145 cgc.cmd[4] = (cmd == CDROMSTART) ? 1 : 0;
2146 cgc.data_direction = CGC_DATA_NONE;
2147 return cdo->generic_packet(cdi, &cgc);
2148 }
2149
2150 case CDROMPAUSE:
2151 case CDROMRESUME: {
2152 cdinfo(CD_DO_IOCTL, "entering CDROMPAUSE/CDROMRESUME\n");
2153 cgc.cmd[0] = GPCMD_PAUSE_RESUME;
2154 cgc.cmd[8] = (cmd == CDROMRESUME) ? 1 : 0;
2155 cgc.data_direction = CGC_DATA_NONE;
2156 return cdo->generic_packet(cdi, &cgc);
2157 }
2158
2159 case DVD_READ_STRUCT: {
2160 dvd_struct *s;
2161 int size = sizeof(dvd_struct);
2162 if (!CDROM_CAN(CDC_DVD))
2163 return -ENOSYS;
2164 if ((s = (dvd_struct *) kmalloc(size, GFP_KERNEL)) == NULL)
2165 return -ENOMEM;
2166 cdinfo(CD_DO_IOCTL, "entering DVD_READ_STRUCT\n");
2167 if (copy_from_user(s, (dvd_struct *)arg, size)) {
2168 kfree(s);
2169 return -EFAULT;
2170 }
2171 if ((ret = dvd_read_struct(cdi, s))) {
2172 kfree(s);
2173 return ret;
2174 }
2175 if (copy_to_user((dvd_struct *)arg, s, size))
2176 ret = -EFAULT;
2177 kfree(s);
2178 return ret;
2179 }
2180
2181 case DVD_AUTH: {
2182 dvd_authinfo ai;
2183 if (!CDROM_CAN(CDC_DVD))
2184 return -ENOSYS;
2185 cdinfo(CD_DO_IOCTL, "entering DVD_AUTH\n");
2186 IOCTL_IN(arg, dvd_authinfo, ai);
2187 if ((ret = dvd_do_auth (cdi, &ai)))
2188 return ret;
2189 IOCTL_OUT(arg, dvd_authinfo, ai);
2190 return 0;
2191 }
2192
2193 case CDROM_SEND_PACKET: {
2194 if (!CDROM_CAN(CDC_GENERIC_PACKET))
2195 return -ENOSYS;
2196 cdinfo(CD_DO_IOCTL, "entering CDROM_SEND_PACKET\n");
2197 IOCTL_IN(arg, struct cdrom_generic_command, cgc);
2198 return cdrom_do_cmd(cdi, &cgc);
2199 }
2200 case CDROM_NEXT_WRITABLE: {
2201 long next = 0;
2202 cdinfo(CD_DO_IOCTL, "entering CDROM_NEXT_WRITABLE\n");
2203 if ((ret = cdrom_get_next_writable(dev, &next)))
2204 return ret;
2205 IOCTL_OUT(arg, long, next);
2206 return 0;
2207 }
2208 case CDROM_LAST_WRITTEN: {
2209 long last = 0;
2210 cdinfo(CD_DO_IOCTL, "entering CDROM_LAST_WRITTEN\n");
2211 if ((ret = cdrom_get_last_written(dev, &last)))
2212 return ret;
2213 IOCTL_OUT(arg, long, last);
2214 return 0;
2215 }
2216 } /* switch */
2217
2218 return -ENOTTY;
2219 }
2220
cdrom_get_track_info(kdev_t dev,__u16 track,__u8 type,track_information * ti)2221 int cdrom_get_track_info(kdev_t dev, __u16 track, __u8 type,
2222 track_information *ti)
2223 {
2224 struct cdrom_device_info *cdi = cdrom_find_device(dev);
2225 struct cdrom_device_ops *cdo = cdi->ops;
2226 struct cdrom_generic_command cgc;
2227 int ret;
2228
2229 init_cdrom_command(&cgc, ti, 8, CGC_DATA_READ);
2230 cgc.cmd[0] = GPCMD_READ_TRACK_RZONE_INFO;
2231 cgc.cmd[1] = type & 3;
2232 cgc.cmd[4] = (track & 0xff00) >> 8;
2233 cgc.cmd[5] = track & 0xff;
2234 cgc.cmd[8] = 8;
2235 cgc.quiet = 1;
2236
2237 if ((ret = cdo->generic_packet(cdi, &cgc)))
2238 return ret;
2239
2240 cgc.buflen = be16_to_cpu(ti->track_information_length) +
2241 sizeof(ti->track_information_length);
2242
2243 if (cgc.buflen > sizeof(track_information))
2244 cgc.buflen = sizeof(track_information);
2245
2246 cgc.cmd[8] = cgc.buflen;
2247 return cdo->generic_packet(cdi, &cgc);
2248 }
2249
2250 /* requires CD R/RW */
cdrom_get_disc_info(kdev_t dev,disc_information * di)2251 int cdrom_get_disc_info(kdev_t dev, disc_information *di)
2252 {
2253 struct cdrom_device_info *cdi = cdrom_find_device(dev);
2254 struct cdrom_device_ops *cdo = cdi->ops;
2255 struct cdrom_generic_command cgc;
2256 int ret;
2257
2258 /* set up command and get the disc info */
2259 init_cdrom_command(&cgc, di, sizeof(*di), CGC_DATA_READ);
2260 cgc.cmd[0] = GPCMD_READ_DISC_INFO;
2261 cgc.cmd[8] = cgc.buflen = 2;
2262 cgc.quiet = 1;
2263
2264 if ((ret = cdo->generic_packet(cdi, &cgc)))
2265 return ret;
2266
2267 /* not all drives have the same disc_info length, so requeue
2268 * packet with the length the drive tells us it can supply
2269 */
2270 cgc.buflen = be16_to_cpu(di->disc_information_length) +
2271 sizeof(di->disc_information_length);
2272
2273 if (cgc.buflen > sizeof(disc_information))
2274 cgc.buflen = sizeof(disc_information);
2275
2276 cgc.cmd[8] = cgc.buflen;
2277 return cdo->generic_packet(cdi, &cgc);
2278 }
2279
2280
2281 /* return the last written block on the CD-R media. this is for the udf
2282 file system. */
cdrom_get_last_written(kdev_t dev,long * last_written)2283 int cdrom_get_last_written(kdev_t dev, long *last_written)
2284 {
2285 struct cdrom_device_info *cdi = cdrom_find_device(dev);
2286 struct cdrom_tocentry toc;
2287 disc_information di;
2288 track_information ti;
2289 __u32 last_track;
2290 int ret = -1;
2291
2292 if (!CDROM_CAN(CDC_GENERIC_PACKET))
2293 goto use_toc;
2294
2295 if ((ret = cdrom_get_disc_info(dev, &di)))
2296 goto use_toc;
2297
2298 last_track = (di.last_track_msb << 8) | di.last_track_lsb;
2299 if ((ret = cdrom_get_track_info(dev, last_track, 1, &ti)))
2300 goto use_toc;
2301
2302 /* if this track is blank, try the previous. */
2303 if (ti.blank) {
2304 last_track--;
2305 if ((ret = cdrom_get_track_info(dev, last_track, 1, &ti)))
2306 goto use_toc;
2307 }
2308
2309 /* if last recorded field is valid, return it. */
2310 if (ti.lra_v) {
2311 *last_written = be32_to_cpu(ti.last_rec_address);
2312 } else {
2313 /* make it up instead */
2314 *last_written = be32_to_cpu(ti.track_start) +
2315 be32_to_cpu(ti.track_size);
2316 if (ti.free_blocks)
2317 *last_written -= (be32_to_cpu(ti.free_blocks) + 7);
2318 }
2319 return 0;
2320
2321 /* this is where we end up if the drive either can't do a
2322 GPCMD_READ_DISC_INFO or GPCMD_READ_TRACK_RZONE_INFO or if
2323 it fails. then we return the toc contents. */
2324 use_toc:
2325 toc.cdte_format = CDROM_MSF;
2326 toc.cdte_track = CDROM_LEADOUT;
2327 if (cdi->ops->audio_ioctl(cdi, CDROMREADTOCENTRY, &toc))
2328 return ret;
2329 sanitize_format(&toc.cdte_addr, &toc.cdte_format, CDROM_LBA);
2330 *last_written = toc.cdte_addr.lba;
2331 return 0;
2332 }
2333
2334 /* return the next writable block. also for udf file system. */
cdrom_get_next_writable(kdev_t dev,long * next_writable)2335 int cdrom_get_next_writable(kdev_t dev, long *next_writable)
2336 {
2337 struct cdrom_device_info *cdi = cdrom_find_device(dev);
2338 disc_information di;
2339 track_information ti;
2340 __u16 last_track;
2341 int ret = -1;
2342
2343 if (!CDROM_CAN(CDC_GENERIC_PACKET))
2344 goto use_last_written;
2345
2346 if ((ret = cdrom_get_disc_info(dev, &di)))
2347 goto use_last_written;
2348
2349 last_track = (di.last_track_msb << 8) | di.last_track_lsb;
2350 if ((ret = cdrom_get_track_info(dev, last_track, 1, &ti)))
2351 goto use_last_written;
2352
2353 /* if this track is blank, try the previous. */
2354 if (ti.blank) {
2355 last_track--;
2356 if ((ret = cdrom_get_track_info(dev, last_track, 1, &ti)))
2357 goto use_last_written;
2358 }
2359
2360 /* if next recordable address field is valid, use it. */
2361 if (ti.nwa_v)
2362 *next_writable = be32_to_cpu(ti.next_writable);
2363 else
2364 goto use_last_written;
2365
2366 return 0;
2367
2368 use_last_written:
2369 if ((ret = cdrom_get_last_written(dev, next_writable))) {
2370 *next_writable = 0;
2371 return ret;
2372 } else {
2373 *next_writable += 7;
2374 return 0;
2375 }
2376 }
2377
2378 EXPORT_SYMBOL(cdrom_get_disc_info);
2379 EXPORT_SYMBOL(cdrom_get_track_info);
2380 EXPORT_SYMBOL(cdrom_get_next_writable);
2381 EXPORT_SYMBOL(cdrom_get_last_written);
2382 EXPORT_SYMBOL(cdrom_count_tracks);
2383 EXPORT_SYMBOL(register_cdrom);
2384 EXPORT_SYMBOL(unregister_cdrom);
2385 EXPORT_SYMBOL(cdrom_open);
2386 EXPORT_SYMBOL(cdrom_release);
2387 EXPORT_SYMBOL(cdrom_ioctl);
2388 EXPORT_SYMBOL(cdrom_media_changed);
2389 EXPORT_SYMBOL(cdrom_number_of_slots);
2390 EXPORT_SYMBOL(cdrom_select_disc);
2391 EXPORT_SYMBOL(cdrom_mode_select);
2392 EXPORT_SYMBOL(cdrom_mode_sense);
2393 EXPORT_SYMBOL(init_cdrom_command);
2394 EXPORT_SYMBOL(cdrom_find_device);
2395
2396 #ifdef CONFIG_SYSCTL
2397
2398 #define CDROM_STR_SIZE 1000
2399
2400 struct cdrom_sysctl_settings {
2401 char info[CDROM_STR_SIZE]; /* general info */
2402 int autoclose; /* close tray upon mount, etc */
2403 int autoeject; /* eject on umount */
2404 int debug; /* turn on debugging messages */
2405 int lock; /* lock the door on device open */
2406 int check; /* check media type */
2407 } cdrom_sysctl_settings;
2408
cdrom_sysctl_info(ctl_table * ctl,int write,struct file * filp,void * buffer,size_t * lenp)2409 int cdrom_sysctl_info(ctl_table *ctl, int write, struct file * filp,
2410 void *buffer, size_t *lenp)
2411 {
2412 int pos;
2413 struct cdrom_device_info *cdi;
2414 char *info = cdrom_sysctl_settings.info;
2415
2416 if (!*lenp || (filp->f_pos && !write)) {
2417 *lenp = 0;
2418 return 0;
2419 }
2420
2421 pos = sprintf(info, "CD-ROM information, " VERSION "\n");
2422
2423 pos += sprintf(info+pos, "\ndrive name:\t");
2424 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
2425 pos += sprintf(info+pos, "\t%s", cdi->name);
2426
2427 pos += sprintf(info+pos, "\ndrive speed:\t");
2428 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
2429 pos += sprintf(info+pos, "\t%d", cdi->speed);
2430
2431 pos += sprintf(info+pos, "\ndrive # of slots:");
2432 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
2433 pos += sprintf(info+pos, "\t%d", cdi->capacity);
2434
2435 pos += sprintf(info+pos, "\nCan close tray:\t");
2436 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
2437 pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_CLOSE_TRAY) != 0);
2438
2439 pos += sprintf(info+pos, "\nCan open tray:\t");
2440 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
2441 pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_OPEN_TRAY) != 0);
2442
2443 pos += sprintf(info+pos, "\nCan lock tray:\t");
2444 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
2445 pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_LOCK) != 0);
2446
2447 pos += sprintf(info+pos, "\nCan change speed:");
2448 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
2449 pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_SELECT_SPEED) != 0);
2450
2451 pos += sprintf(info+pos, "\nCan select disk:");
2452 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
2453 pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_SELECT_DISC) != 0);
2454
2455 pos += sprintf(info+pos, "\nCan read multisession:");
2456 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
2457 pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_MULTI_SESSION) != 0);
2458
2459 pos += sprintf(info+pos, "\nCan read MCN:\t");
2460 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
2461 pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_MCN) != 0);
2462
2463 pos += sprintf(info+pos, "\nReports media changed:");
2464 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
2465 pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_MEDIA_CHANGED) != 0);
2466
2467 pos += sprintf(info+pos, "\nCan play audio:\t");
2468 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
2469 pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_PLAY_AUDIO) != 0);
2470
2471 pos += sprintf(info+pos, "\nCan write CD-R:\t");
2472 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
2473 pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_CD_R) != 0);
2474
2475 pos += sprintf(info+pos, "\nCan write CD-RW:");
2476 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
2477 pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_CD_RW) != 0);
2478
2479 pos += sprintf(info+pos, "\nCan read DVD:\t");
2480 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
2481 pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_DVD) != 0);
2482
2483 pos += sprintf(info+pos, "\nCan write DVD-R:");
2484 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
2485 pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_DVD_R) != 0);
2486
2487 pos += sprintf(info+pos, "\nCan write DVD-RAM:");
2488 for (cdi=topCdromPtr;cdi!=NULL;cdi=cdi->next)
2489 pos += sprintf(info+pos, "\t%d", CDROM_CAN(CDC_DVD_RAM) != 0);
2490
2491 strcpy(info+pos,"\n\n");
2492
2493 return proc_dostring(ctl, write, filp, buffer, lenp);
2494 }
2495
2496 /* Unfortunately, per device settings are not implemented through
2497 procfs/sysctl yet. When they are, this will naturally disappear. For now
2498 just update all drives. Later this will become the template on which
2499 new registered drives will be based. */
cdrom_update_settings(void)2500 void cdrom_update_settings(void)
2501 {
2502 struct cdrom_device_info *cdi;
2503
2504 for (cdi = topCdromPtr; cdi != NULL; cdi = cdi->next) {
2505 if (autoclose && CDROM_CAN(CDC_CLOSE_TRAY))
2506 cdi->options |= CDO_AUTO_CLOSE;
2507 else if (!autoclose)
2508 cdi->options &= ~CDO_AUTO_CLOSE;
2509 if (autoeject && CDROM_CAN(CDC_OPEN_TRAY))
2510 cdi->options |= CDO_AUTO_EJECT;
2511 else if (!autoeject)
2512 cdi->options &= ~CDO_AUTO_EJECT;
2513 if (lockdoor && CDROM_CAN(CDC_LOCK))
2514 cdi->options |= CDO_LOCK;
2515 else if (!lockdoor)
2516 cdi->options &= ~CDO_LOCK;
2517 if (check_media_type)
2518 cdi->options |= CDO_CHECK_TYPE;
2519 else
2520 cdi->options &= ~CDO_CHECK_TYPE;
2521 }
2522 }
2523
cdrom_sysctl_handler(ctl_table * ctl,int write,struct file * filp,void * buffer,size_t * lenp)2524 static int cdrom_sysctl_handler(ctl_table *ctl, int write, struct file * filp,
2525 void *buffer, size_t *lenp)
2526 {
2527 int *valp = ctl->data;
2528 int val = *valp;
2529 int ret;
2530
2531 ret = proc_dointvec(ctl, write, filp, buffer, lenp);
2532
2533 if (write && *valp != val) {
2534
2535 /* we only care for 1 or 0. */
2536 if (*valp)
2537 *valp = 1;
2538 else
2539 *valp = 0;
2540
2541 switch (ctl->ctl_name) {
2542 case DEV_CDROM_AUTOCLOSE: {
2543 if (valp == &cdrom_sysctl_settings.autoclose)
2544 autoclose = cdrom_sysctl_settings.autoclose;
2545 break;
2546 }
2547 case DEV_CDROM_AUTOEJECT: {
2548 if (valp == &cdrom_sysctl_settings.autoeject)
2549 autoeject = cdrom_sysctl_settings.autoeject;
2550 break;
2551 }
2552 case DEV_CDROM_DEBUG: {
2553 if (valp == &cdrom_sysctl_settings.debug)
2554 debug = cdrom_sysctl_settings.debug;
2555 break;
2556 }
2557 case DEV_CDROM_LOCK: {
2558 if (valp == &cdrom_sysctl_settings.lock)
2559 lockdoor = cdrom_sysctl_settings.lock;
2560 break;
2561 }
2562 case DEV_CDROM_CHECK_MEDIA: {
2563 if (valp == &cdrom_sysctl_settings.check)
2564 check_media_type = cdrom_sysctl_settings.check;
2565 break;
2566 }
2567 }
2568 /* update the option flags according to the changes. we
2569 don't have per device options through sysctl yet,
2570 but we will have and then this will disappear. */
2571 cdrom_update_settings();
2572 }
2573
2574 return ret;
2575 }
2576
2577 /* Place files in /proc/sys/dev/cdrom */
2578 ctl_table cdrom_table[] = {
2579 {DEV_CDROM_INFO, "info", &cdrom_sysctl_settings.info,
2580 CDROM_STR_SIZE, 0444, NULL, &cdrom_sysctl_info},
2581 {DEV_CDROM_AUTOCLOSE, "autoclose", &cdrom_sysctl_settings.autoclose,
2582 sizeof(int), 0644, NULL, &cdrom_sysctl_handler },
2583 {DEV_CDROM_AUTOEJECT, "autoeject", &cdrom_sysctl_settings.autoeject,
2584 sizeof(int), 0644, NULL, &cdrom_sysctl_handler },
2585 {DEV_CDROM_DEBUG, "debug", &cdrom_sysctl_settings.debug,
2586 sizeof(int), 0644, NULL, &cdrom_sysctl_handler },
2587 {DEV_CDROM_LOCK, "lock", &cdrom_sysctl_settings.lock,
2588 sizeof(int), 0644, NULL, &cdrom_sysctl_handler },
2589 {DEV_CDROM_CHECK_MEDIA, "check_media", &cdrom_sysctl_settings.check,
2590 sizeof(int), 0644, NULL, &cdrom_sysctl_handler },
2591 {0}
2592 };
2593
2594 ctl_table cdrom_cdrom_table[] = {
2595 {DEV_CDROM, "cdrom", NULL, 0, 0555, cdrom_table},
2596 {0}
2597 };
2598
2599 /* Make sure that /proc/sys/dev is there */
2600 ctl_table cdrom_root_table[] = {
2601 {CTL_DEV, "dev", NULL, 0, 0555, cdrom_cdrom_table},
2602 {0}
2603 };
2604 static struct ctl_table_header *cdrom_sysctl_header;
2605
cdrom_sysctl_register(void)2606 static void cdrom_sysctl_register(void)
2607 {
2608 static int initialized;
2609
2610 if (initialized == 1)
2611 return;
2612
2613 cdrom_sysctl_header = register_sysctl_table(cdrom_root_table, 1);
2614 cdrom_root_table->child->de->owner = THIS_MODULE;
2615
2616 /* set the defaults */
2617 cdrom_sysctl_settings.autoclose = autoclose;
2618 cdrom_sysctl_settings.autoeject = autoeject;
2619 cdrom_sysctl_settings.debug = debug;
2620 cdrom_sysctl_settings.lock = lockdoor;
2621 cdrom_sysctl_settings.check = check_media_type;
2622
2623 initialized = 1;
2624 }
2625
cdrom_sysctl_unregister(void)2626 static void cdrom_sysctl_unregister(void)
2627 {
2628 if (cdrom_sysctl_header)
2629 unregister_sysctl_table(cdrom_sysctl_header);
2630 }
2631
2632 #endif /* CONFIG_SYSCTL */
2633
cdrom_init(void)2634 static int __init cdrom_init(void)
2635 {
2636 #ifdef CONFIG_SYSCTL
2637 cdrom_sysctl_register();
2638 #endif
2639 if (!devfs_handle)
2640 devfs_handle = devfs_mk_dir(NULL, "cdroms", NULL);
2641 return 0;
2642 }
2643
cdrom_exit(void)2644 static void __exit cdrom_exit(void)
2645 {
2646 printk(KERN_INFO "Uniform CD-ROM driver unloaded\n");
2647 #ifdef CONFIG_SYSCTL
2648 cdrom_sysctl_unregister();
2649 #endif
2650 devfs_unregister(devfs_handle);
2651 }
2652
2653 module_init(cdrom_init);
2654 module_exit(cdrom_exit);
2655 MODULE_LICENSE("GPL");
2656