c4213b7d0b20c1ddf0a213e073c991d3ef917bb8
[linux-2.6.git] / drivers / cdrom / cdrom.c
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 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.
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    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
236
237   Nov 5 2001, Aug 8 2002. Modified by Andy Polyakov
238   <appro@fy.chalmers.se> to support MMC-3 compliant DVD+RW units.
239
240   Modified by Nigel Kukard <nkukard@lbsd.net> - support DVD+RW
241   2.4.x patch by Andy Polyakov <appro@fy.chalmers.se>
242
243 -------------------------------------------------------------------------*/
244
245 #define REVISION "Revision: 3.20"
246 #define VERSION "Id: cdrom.c 3.20 2003/12/17"
247
248 /* I use an error-log mask to give fine grain control over the type of
249    messages dumped to the system logs.  The available masks include: */
250 #define CD_NOTHING      0x0
251 #define CD_WARNING      0x1
252 #define CD_REG_UNREG    0x2
253 #define CD_DO_IOCTL     0x4
254 #define CD_OPEN         0x8
255 #define CD_CLOSE        0x10
256 #define CD_COUNT_TRACKS 0x20
257 #define CD_CHANGER      0x40
258 #define CD_DVD          0x80
259
260 /* Define this to remove _all_ the debugging messages */
261 /* #define ERRLOGMASK CD_NOTHING */
262 #define ERRLOGMASK CD_WARNING
263 /* #define ERRLOGMASK (CD_WARNING|CD_OPEN|CD_COUNT_TRACKS|CD_CLOSE) */
264 /* #define ERRLOGMASK (CD_WARNING|CD_REG_UNREG|CD_DO_IOCTL|CD_OPEN|CD_CLOSE|CD_COUNT_TRACKS) */
265
266 #include <linux/module.h>
267 #include <linux/fs.h>
268 #include <linux/buffer_head.h>
269 #include <linux/major.h>
270 #include <linux/types.h>
271 #include <linux/errno.h>
272 #include <linux/kernel.h>
273 #include <linux/mm.h>
274 #include <linux/slab.h> 
275 #include <linux/cdrom.h>
276 #include <linux/sysctl.h>
277 #include <linux/proc_fs.h>
278 #include <linux/blkpg.h>
279 #include <linux/init.h>
280 #include <linux/fcntl.h>
281 #include <linux/blkdev.h>
282 #include <linux/times.h>
283
284 #include <asm/uaccess.h>
285
286 /* used to tell the module to turn on full debugging messages */
287 static int debug;
288 /* used to keep tray locked at all times */
289 static int keeplocked;
290 /* default compatibility mode */
291 static int autoclose=1;
292 static int autoeject;
293 static int lockdoor = 1;
294 /* will we ever get to use this... sigh. */
295 static int check_media_type;
296 /* automatically restart mrw format */
297 static int mrw_format_restart = 1;
298 module_param(debug, bool, 0);
299 module_param(autoclose, bool, 0);
300 module_param(autoeject, bool, 0);
301 module_param(lockdoor, bool, 0);
302 module_param(check_media_type, bool, 0);
303 module_param(mrw_format_restart, bool, 0);
304
305 static DEFINE_MUTEX(cdrom_mutex);
306
307 static const char *mrw_format_status[] = {
308         "not mrw",
309         "bgformat inactive",
310         "bgformat active",
311         "mrw complete",
312 };
313
314 static const char *mrw_address_space[] = { "DMA", "GAA" };
315
316 #if (ERRLOGMASK!=CD_NOTHING)
317 #define cdinfo(type, fmt, args...) \
318         if ((ERRLOGMASK & type) || debug==1 ) \
319             printk(KERN_INFO "cdrom: " fmt, ## args)
320 #else
321 #define cdinfo(type, fmt, args...) 
322 #endif
323
324 /* These are used to simplify getting data in from and back to user land */
325 #define IOCTL_IN(arg, type, in)                                 \
326         if (copy_from_user(&(in), (type __user *) (arg), sizeof (in)))  \
327                 return -EFAULT;
328
329 #define IOCTL_OUT(arg, type, out) \
330         if (copy_to_user((type __user *) (arg), &(out), sizeof (out)))  \
331                 return -EFAULT;
332
333 /* The (cdo->capability & ~cdi->mask & CDC_XXX) construct was used in
334    a lot of places. This macro makes the code more clear. */
335 #define CDROM_CAN(type) (cdi->ops->capability & ~cdi->mask & (type))
336
337 /* used in the audio ioctls */
338 #define CHECKAUDIO if ((ret=check_for_audio_disc(cdi, cdo))) return ret
339
340 /*
341  * Another popular OS uses 7 seconds as the hard timeout for default
342  * commands, so it is a good choice for us as well.
343  */
344 #define CDROM_DEF_TIMEOUT       (7 * HZ)
345
346 /* Not-exported routines. */
347 static int open_for_data(struct cdrom_device_info * cdi);
348 static int check_for_audio_disc(struct cdrom_device_info * cdi,
349                          struct cdrom_device_ops * cdo);
350 static void sanitize_format(union cdrom_addr *addr, 
351                 u_char * curr, u_char requested);
352 static int mmc_ioctl(struct cdrom_device_info *cdi, unsigned int cmd,
353                      unsigned long arg);
354
355 int cdrom_get_last_written(struct cdrom_device_info *, long *);
356 static int cdrom_get_next_writable(struct cdrom_device_info *, long *);
357 static void cdrom_count_tracks(struct cdrom_device_info *, tracktype*);
358
359 static int cdrom_mrw_exit(struct cdrom_device_info *cdi);
360
361 static int cdrom_get_disc_info(struct cdrom_device_info *cdi, disc_information *di);
362
363 static void cdrom_sysctl_register(void);
364
365 static LIST_HEAD(cdrom_list);
366
367 static int cdrom_dummy_generic_packet(struct cdrom_device_info *cdi,
368                                       struct packet_command *cgc)
369 {
370         if (cgc->sense) {
371                 cgc->sense->sense_key = 0x05;
372                 cgc->sense->asc = 0x20;
373                 cgc->sense->ascq = 0x00;
374         }
375
376         cgc->stat = -EIO;
377         return -EIO;
378 }
379
380 /* This macro makes sure we don't have to check on cdrom_device_ops
381  * existence in the run-time routines below. Change_capability is a
382  * hack to have the capability flags defined const, while we can still
383  * change it here without gcc complaining at every line.
384  */
385 #define ENSURE(call, bits) if (cdo->call == NULL) *change_capability &= ~(bits)
386
387 int register_cdrom(struct cdrom_device_info *cdi)
388 {
389         static char banner_printed;
390         struct cdrom_device_ops *cdo = cdi->ops;
391         int *change_capability = (int *)&cdo->capability; /* hack */
392
393         cdinfo(CD_OPEN, "entering register_cdrom\n"); 
394
395         if (cdo->open == NULL || cdo->release == NULL)
396                 return -EINVAL;
397         if (!banner_printed) {
398                 printk(KERN_INFO "Uniform CD-ROM driver " REVISION "\n");
399                 banner_printed = 1;
400                 cdrom_sysctl_register();
401         }
402
403         ENSURE(drive_status, CDC_DRIVE_STATUS );
404         ENSURE(media_changed, CDC_MEDIA_CHANGED);
405         ENSURE(tray_move, CDC_CLOSE_TRAY | CDC_OPEN_TRAY);
406         ENSURE(lock_door, CDC_LOCK);
407         ENSURE(select_speed, CDC_SELECT_SPEED);
408         ENSURE(get_last_session, CDC_MULTI_SESSION);
409         ENSURE(get_mcn, CDC_MCN);
410         ENSURE(reset, CDC_RESET);
411         ENSURE(audio_ioctl, CDC_PLAY_AUDIO);
412         ENSURE(generic_packet, CDC_GENERIC_PACKET);
413         cdi->mc_flags = 0;
414         cdo->n_minors = 0;
415         cdi->options = CDO_USE_FFLAGS;
416         
417         if (autoclose==1 && CDROM_CAN(CDC_CLOSE_TRAY))
418                 cdi->options |= (int) CDO_AUTO_CLOSE;
419         if (autoeject==1 && CDROM_CAN(CDC_OPEN_TRAY))
420                 cdi->options |= (int) CDO_AUTO_EJECT;
421         if (lockdoor==1)
422                 cdi->options |= (int) CDO_LOCK;
423         if (check_media_type==1)
424                 cdi->options |= (int) CDO_CHECK_TYPE;
425
426         if (CDROM_CAN(CDC_MRW_W))
427                 cdi->exit = cdrom_mrw_exit;
428
429         if (cdi->disk)
430                 cdi->cdda_method = CDDA_BPC_FULL;
431         else
432                 cdi->cdda_method = CDDA_OLD;
433
434         if (!cdo->generic_packet)
435                 cdo->generic_packet = cdrom_dummy_generic_packet;
436
437         cdinfo(CD_REG_UNREG, "drive \"/dev/%s\" registered\n", cdi->name);
438         mutex_lock(&cdrom_mutex);
439         list_add(&cdi->list, &cdrom_list);
440         mutex_unlock(&cdrom_mutex);
441         return 0;
442 }
443 #undef ENSURE
444
445 int unregister_cdrom(struct cdrom_device_info *cdi)
446 {
447         cdinfo(CD_OPEN, "entering unregister_cdrom\n"); 
448
449         mutex_lock(&cdrom_mutex);
450         list_del(&cdi->list);
451         mutex_unlock(&cdrom_mutex);
452
453         if (cdi->exit)
454                 cdi->exit(cdi);
455
456         cdi->ops->n_minors--;
457         cdinfo(CD_REG_UNREG, "drive \"/dev/%s\" unregistered\n", cdi->name);
458         return 0;
459 }
460
461 int cdrom_get_media_event(struct cdrom_device_info *cdi,
462                           struct media_event_desc *med)
463 {
464         struct packet_command cgc;
465         unsigned char buffer[8];
466         struct event_header *eh = (struct event_header *) buffer;
467
468         init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_READ);
469         cgc.cmd[0] = GPCMD_GET_EVENT_STATUS_NOTIFICATION;
470         cgc.cmd[1] = 1;         /* IMMED */
471         cgc.cmd[4] = 1 << 4;    /* media event */
472         cgc.cmd[8] = sizeof(buffer);
473         cgc.quiet = 1;
474
475         if (cdi->ops->generic_packet(cdi, &cgc))
476                 return 1;
477
478         if (be16_to_cpu(eh->data_len) < sizeof(*med))
479                 return 1;
480
481         if (eh->nea || eh->notification_class != 0x4)
482                 return 1;
483
484         memcpy(med, &buffer[sizeof(*eh)], sizeof(*med));
485         return 0;
486 }
487
488 /*
489  * the first prototypes used 0x2c as the page code for the mrw mode page,
490  * subsequently this was changed to 0x03. probe the one used by this drive
491  */
492 static int cdrom_mrw_probe_pc(struct cdrom_device_info *cdi)
493 {
494         struct packet_command cgc;
495         char buffer[16];
496
497         init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_READ);
498
499         cgc.timeout = HZ;
500         cgc.quiet = 1;
501
502         if (!cdrom_mode_sense(cdi, &cgc, MRW_MODE_PC, 0)) {
503                 cdi->mrw_mode_page = MRW_MODE_PC;
504                 return 0;
505         } else if (!cdrom_mode_sense(cdi, &cgc, MRW_MODE_PC_PRE1, 0)) {
506                 cdi->mrw_mode_page = MRW_MODE_PC_PRE1;
507                 return 0;
508         }
509
510         return 1;
511 }
512
513 static int cdrom_is_mrw(struct cdrom_device_info *cdi, int *write)
514 {
515         struct packet_command cgc;
516         struct mrw_feature_desc *mfd;
517         unsigned char buffer[16];
518         int ret;
519
520         *write = 0;
521
522         init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_READ);
523
524         cgc.cmd[0] = GPCMD_GET_CONFIGURATION;
525         cgc.cmd[3] = CDF_MRW;
526         cgc.cmd[8] = sizeof(buffer);
527         cgc.quiet = 1;
528
529         if ((ret = cdi->ops->generic_packet(cdi, &cgc)))
530                 return ret;
531
532         mfd = (struct mrw_feature_desc *)&buffer[sizeof(struct feature_header)];
533         if (be16_to_cpu(mfd->feature_code) != CDF_MRW)
534                 return 1;
535         *write = mfd->write;
536
537         if ((ret = cdrom_mrw_probe_pc(cdi))) {
538                 *write = 0;
539                 return ret;
540         }
541
542         return 0;
543 }
544
545 static int cdrom_mrw_bgformat(struct cdrom_device_info *cdi, int cont)
546 {
547         struct packet_command cgc;
548         unsigned char buffer[12];
549         int ret;
550
551         printk(KERN_INFO "cdrom: %sstarting format\n", cont ? "Re" : "");
552
553         /*
554          * FmtData bit set (bit 4), format type is 1
555          */
556         init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_WRITE);
557         cgc.cmd[0] = GPCMD_FORMAT_UNIT;
558         cgc.cmd[1] = (1 << 4) | 1;
559
560         cgc.timeout = 5 * 60 * HZ;
561
562         /*
563          * 4 byte format list header, 8 byte format list descriptor
564          */
565         buffer[1] = 1 << 1;
566         buffer[3] = 8;
567
568         /*
569          * nr_blocks field
570          */
571         buffer[4] = 0xff;
572         buffer[5] = 0xff;
573         buffer[6] = 0xff;
574         buffer[7] = 0xff;
575
576         buffer[8] = 0x24 << 2;
577         buffer[11] = cont;
578
579         ret = cdi->ops->generic_packet(cdi, &cgc);
580         if (ret)
581                 printk(KERN_INFO "cdrom: bgformat failed\n");
582
583         return ret;
584 }
585
586 static int cdrom_mrw_bgformat_susp(struct cdrom_device_info *cdi, int immed)
587 {
588         struct packet_command cgc;
589
590         init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
591         cgc.cmd[0] = GPCMD_CLOSE_TRACK;
592
593         /*
594          * Session = 1, Track = 0
595          */
596         cgc.cmd[1] = !!immed;
597         cgc.cmd[2] = 1 << 1;
598
599         cgc.timeout = 5 * 60 * HZ;
600
601         return cdi->ops->generic_packet(cdi, &cgc);
602 }
603
604 static int cdrom_flush_cache(struct cdrom_device_info *cdi)
605 {
606         struct packet_command cgc;
607
608         init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
609         cgc.cmd[0] = GPCMD_FLUSH_CACHE;
610
611         cgc.timeout = 5 * 60 * HZ;
612
613         return cdi->ops->generic_packet(cdi, &cgc);
614 }
615
616 static int cdrom_mrw_exit(struct cdrom_device_info *cdi)
617 {
618         disc_information di;
619         int ret;
620
621         ret = cdrom_get_disc_info(cdi, &di);
622         if (ret < 0 || ret < (int)offsetof(typeof(di),disc_type))
623                 return 1;
624
625         ret = 0;
626         if (di.mrw_status == CDM_MRW_BGFORMAT_ACTIVE) {
627                 printk(KERN_INFO "cdrom: issuing MRW back ground "
628                                 "format suspend\n");
629                 ret = cdrom_mrw_bgformat_susp(cdi, 0);
630         }
631
632         if (!ret && cdi->media_written)
633                 ret = cdrom_flush_cache(cdi);
634
635         return ret;
636 }
637
638 static int cdrom_mrw_set_lba_space(struct cdrom_device_info *cdi, int space)
639 {
640         struct packet_command cgc;
641         struct mode_page_header *mph;
642         char buffer[16];
643         int ret, offset, size;
644
645         init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_READ);
646
647         cgc.buffer = buffer;
648         cgc.buflen = sizeof(buffer);
649
650         if ((ret = cdrom_mode_sense(cdi, &cgc, cdi->mrw_mode_page, 0)))
651                 return ret;
652
653         mph = (struct mode_page_header *) buffer;
654         offset = be16_to_cpu(mph->desc_length);
655         size = be16_to_cpu(mph->mode_data_length) + 2;
656
657         buffer[offset + 3] = space;
658         cgc.buflen = size;
659
660         if ((ret = cdrom_mode_select(cdi, &cgc)))
661                 return ret;
662
663         printk(KERN_INFO "cdrom: %s: mrw address space %s selected\n", cdi->name, mrw_address_space[space]);
664         return 0;
665 }
666
667 static int cdrom_get_random_writable(struct cdrom_device_info *cdi,
668                               struct rwrt_feature_desc *rfd)
669 {
670         struct packet_command cgc;
671         char buffer[24];
672         int ret;
673
674         init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_READ);
675
676         cgc.cmd[0] = GPCMD_GET_CONFIGURATION;   /* often 0x46 */
677         cgc.cmd[3] = CDF_RWRT;                  /* often 0x0020 */
678         cgc.cmd[8] = sizeof(buffer);            /* often 0x18 */
679         cgc.quiet = 1;
680
681         if ((ret = cdi->ops->generic_packet(cdi, &cgc)))
682                 return ret;
683
684         memcpy(rfd, &buffer[sizeof(struct feature_header)], sizeof (*rfd));
685         return 0;
686 }
687
688 static int cdrom_has_defect_mgt(struct cdrom_device_info *cdi)
689 {
690         struct packet_command cgc;
691         char buffer[16];
692         __be16 *feature_code;
693         int ret;
694
695         init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_READ);
696
697         cgc.cmd[0] = GPCMD_GET_CONFIGURATION;
698         cgc.cmd[3] = CDF_HWDM;
699         cgc.cmd[8] = sizeof(buffer);
700         cgc.quiet = 1;
701
702         if ((ret = cdi->ops->generic_packet(cdi, &cgc)))
703                 return ret;
704
705         feature_code = (__be16 *) &buffer[sizeof(struct feature_header)];
706         if (be16_to_cpu(*feature_code) == CDF_HWDM)
707                 return 0;
708
709         return 1;
710 }
711
712
713 static int cdrom_is_random_writable(struct cdrom_device_info *cdi, int *write)
714 {
715         struct rwrt_feature_desc rfd;
716         int ret;
717
718         *write = 0;
719
720         if ((ret = cdrom_get_random_writable(cdi, &rfd)))
721                 return ret;
722
723         if (CDF_RWRT == be16_to_cpu(rfd.feature_code))
724                 *write = 1;
725
726         return 0;
727 }
728
729 static int cdrom_media_erasable(struct cdrom_device_info *cdi)
730 {
731         disc_information di;
732         int ret;
733
734         ret = cdrom_get_disc_info(cdi, &di);
735         if (ret < 0 || ret < offsetof(typeof(di), n_first_track))
736                 return -1;
737
738         return di.erasable;
739 }
740
741 /*
742  * FIXME: check RO bit
743  */
744 static int cdrom_dvdram_open_write(struct cdrom_device_info *cdi)
745 {
746         int ret = cdrom_media_erasable(cdi);
747
748         /*
749          * allow writable open if media info read worked and media is
750          * erasable, _or_ if it fails since not all drives support it
751          */
752         if (!ret)
753                 return 1;
754
755         return 0;
756 }
757
758 static int cdrom_mrw_open_write(struct cdrom_device_info *cdi)
759 {
760         disc_information di;
761         int ret;
762
763         /*
764          * always reset to DMA lba space on open
765          */
766         if (cdrom_mrw_set_lba_space(cdi, MRW_LBA_DMA)) {
767                 printk(KERN_ERR "cdrom: failed setting lba address space\n");
768                 return 1;
769         }
770
771         ret = cdrom_get_disc_info(cdi, &di);
772         if (ret < 0 || ret < offsetof(typeof(di),disc_type))
773                 return 1;
774
775         if (!di.erasable)
776                 return 1;
777
778         /*
779          * mrw_status
780          * 0    -       not MRW formatted
781          * 1    -       MRW bgformat started, but not running or complete
782          * 2    -       MRW bgformat in progress
783          * 3    -       MRW formatting complete
784          */
785         ret = 0;
786         printk(KERN_INFO "cdrom open: mrw_status '%s'\n",
787                         mrw_format_status[di.mrw_status]);
788         if (!di.mrw_status)
789                 ret = 1;
790         else if (di.mrw_status == CDM_MRW_BGFORMAT_INACTIVE &&
791                         mrw_format_restart)
792                 ret = cdrom_mrw_bgformat(cdi, 1);
793
794         return ret;
795 }
796
797 static int mo_open_write(struct cdrom_device_info *cdi)
798 {
799         struct packet_command cgc;
800         char buffer[255];
801         int ret;
802
803         init_cdrom_command(&cgc, &buffer, 4, CGC_DATA_READ);
804         cgc.quiet = 1;
805
806         /*
807          * obtain write protect information as per
808          * drivers/scsi/sd.c:sd_read_write_protect_flag
809          */
810
811         ret = cdrom_mode_sense(cdi, &cgc, GPMODE_ALL_PAGES, 0);
812         if (ret)
813                 ret = cdrom_mode_sense(cdi, &cgc, GPMODE_VENDOR_PAGE, 0);
814         if (ret) {
815                 cgc.buflen = 255;
816                 ret = cdrom_mode_sense(cdi, &cgc, GPMODE_ALL_PAGES, 0);
817         }
818
819         /* drive gave us no info, let the user go ahead */
820         if (ret)
821                 return 0;
822
823         return buffer[3] & 0x80;
824 }
825
826 static int cdrom_ram_open_write(struct cdrom_device_info *cdi)
827 {
828         struct rwrt_feature_desc rfd;
829         int ret;
830
831         if ((ret = cdrom_has_defect_mgt(cdi)))
832                 return ret;
833
834         if ((ret = cdrom_get_random_writable(cdi, &rfd)))
835                 return ret;
836         else if (CDF_RWRT == be16_to_cpu(rfd.feature_code))
837                 ret = !rfd.curr;
838
839         cdinfo(CD_OPEN, "can open for random write\n");
840         return ret;
841 }
842
843 static void cdrom_mmc3_profile(struct cdrom_device_info *cdi)
844 {
845         struct packet_command cgc;
846         char buffer[32];
847         int ret, mmc3_profile;
848
849         init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_READ);
850
851         cgc.cmd[0] = GPCMD_GET_CONFIGURATION;
852         cgc.cmd[1] = 0;
853         cgc.cmd[2] = cgc.cmd[3] = 0;            /* Starting Feature Number */
854         cgc.cmd[8] = sizeof(buffer);            /* Allocation Length */
855         cgc.quiet = 1;
856
857         if ((ret = cdi->ops->generic_packet(cdi, &cgc)))
858                 mmc3_profile = 0xffff;
859         else
860                 mmc3_profile = (buffer[6] << 8) | buffer[7];
861
862         cdi->mmc3_profile = mmc3_profile;
863 }
864
865 static int cdrom_is_dvd_rw(struct cdrom_device_info *cdi)
866 {
867         switch (cdi->mmc3_profile) {
868         case 0x12:      /* DVD-RAM      */
869         case 0x1A:      /* DVD+RW       */
870                 return 0;
871         default:
872                 return 1;
873         }
874 }
875
876 /*
877  * returns 0 for ok to open write, non-0 to disallow
878  */
879 static int cdrom_open_write(struct cdrom_device_info *cdi)
880 {
881         int mrw, mrw_write, ram_write;
882         int ret = 1;
883
884         mrw = 0;
885         if (!cdrom_is_mrw(cdi, &mrw_write))
886                 mrw = 1;
887
888         if (CDROM_CAN(CDC_MO_DRIVE))
889                 ram_write = 1;
890         else
891                 (void) cdrom_is_random_writable(cdi, &ram_write);
892         
893         if (mrw)
894                 cdi->mask &= ~CDC_MRW;
895         else
896                 cdi->mask |= CDC_MRW;
897
898         if (mrw_write)
899                 cdi->mask &= ~CDC_MRW_W;
900         else
901                 cdi->mask |= CDC_MRW_W;
902
903         if (ram_write)
904                 cdi->mask &= ~CDC_RAM;
905         else
906                 cdi->mask |= CDC_RAM;
907
908         if (CDROM_CAN(CDC_MRW_W))
909                 ret = cdrom_mrw_open_write(cdi);
910         else if (CDROM_CAN(CDC_DVD_RAM))
911                 ret = cdrom_dvdram_open_write(cdi);
912         else if (CDROM_CAN(CDC_RAM) &&
913                  !CDROM_CAN(CDC_CD_R|CDC_CD_RW|CDC_DVD|CDC_DVD_R|CDC_MRW|CDC_MO_DRIVE))
914                 ret = cdrom_ram_open_write(cdi);
915         else if (CDROM_CAN(CDC_MO_DRIVE))
916                 ret = mo_open_write(cdi);
917         else if (!cdrom_is_dvd_rw(cdi))
918                 ret = 0;
919
920         return ret;
921 }
922
923 static void cdrom_dvd_rw_close_write(struct cdrom_device_info *cdi)
924 {
925         struct packet_command cgc;
926
927         if (cdi->mmc3_profile != 0x1a) {
928                 cdinfo(CD_CLOSE, "%s: No DVD+RW\n", cdi->name);
929                 return;
930         }
931
932         if (!cdi->media_written) {
933                 cdinfo(CD_CLOSE, "%s: DVD+RW media clean\n", cdi->name);
934                 return;
935         }
936
937         printk(KERN_INFO "cdrom: %s: dirty DVD+RW media, \"finalizing\"\n",
938                cdi->name);
939
940         init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
941         cgc.cmd[0] = GPCMD_FLUSH_CACHE;
942         cgc.timeout = 30*HZ;
943         cdi->ops->generic_packet(cdi, &cgc);
944
945         init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
946         cgc.cmd[0] = GPCMD_CLOSE_TRACK;
947         cgc.timeout = 3000*HZ;
948         cgc.quiet = 1;
949         cdi->ops->generic_packet(cdi, &cgc);
950
951         init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
952         cgc.cmd[0] = GPCMD_CLOSE_TRACK;
953         cgc.cmd[2] = 2;  /* Close session */
954         cgc.quiet = 1;
955         cgc.timeout = 3000*HZ;
956         cdi->ops->generic_packet(cdi, &cgc);
957
958         cdi->media_written = 0;
959 }
960
961 static int cdrom_close_write(struct cdrom_device_info *cdi)
962 {
963 #if 0
964         return cdrom_flush_cache(cdi);
965 #else
966         return 0;
967 #endif
968 }
969
970 /* We use the open-option O_NONBLOCK to indicate that the
971  * purpose of opening is only for subsequent ioctl() calls; no device
972  * integrity checks are performed.
973  *
974  * We hope that all cd-player programs will adopt this convention. It
975  * is in their own interest: device control becomes a lot easier
976  * this way.
977  */
978 int cdrom_open(struct cdrom_device_info *cdi, struct inode *ip, struct file *fp)
979 {
980         int ret;
981
982         cdinfo(CD_OPEN, "entering cdrom_open\n"); 
983
984         /* if this was a O_NONBLOCK open and we should honor the flags,
985          * do a quick open without drive/disc integrity checks. */
986         cdi->use_count++;
987         if ((fp->f_flags & O_NONBLOCK) && (cdi->options & CDO_USE_FFLAGS)) {
988                 ret = cdi->ops->open(cdi, 1);
989         } else {
990                 ret = open_for_data(cdi);
991                 if (ret)
992                         goto err;
993                 cdrom_mmc3_profile(cdi);
994                 if (fp->f_mode & FMODE_WRITE) {
995                         ret = -EROFS;
996                         if (cdrom_open_write(cdi))
997                                 goto err_release;
998                         if (!CDROM_CAN(CDC_RAM))
999                                 goto err_release;
1000                         ret = 0;
1001                         cdi->media_written = 0;
1002                 }
1003         }
1004
1005         if (ret)
1006                 goto err;
1007
1008         cdinfo(CD_OPEN, "Use count for \"/dev/%s\" now %d\n",
1009                         cdi->name, cdi->use_count);
1010         /* Do this on open.  Don't wait for mount, because they might
1011             not be mounting, but opening with O_NONBLOCK */
1012         check_disk_change(ip->i_bdev);
1013         return 0;
1014 err_release:
1015         if (CDROM_CAN(CDC_LOCK) && cdi->options & CDO_LOCK) {
1016                 cdi->ops->lock_door(cdi, 0);
1017                 cdinfo(CD_OPEN, "door unlocked.\n");
1018         }
1019         cdi->ops->release(cdi);
1020 err:
1021         cdi->use_count--;
1022         return ret;
1023 }
1024
1025 static
1026 int open_for_data(struct cdrom_device_info * cdi)
1027 {
1028         int ret;
1029         struct cdrom_device_ops *cdo = cdi->ops;
1030         tracktype tracks;
1031         cdinfo(CD_OPEN, "entering open_for_data\n");
1032         /* Check if the driver can report drive status.  If it can, we
1033            can do clever things.  If it can't, well, we at least tried! */
1034         if (cdo->drive_status != NULL) {
1035                 ret = cdo->drive_status(cdi, CDSL_CURRENT);
1036                 cdinfo(CD_OPEN, "drive_status=%d\n", ret); 
1037                 if (ret == CDS_TRAY_OPEN) {
1038                         cdinfo(CD_OPEN, "the tray is open...\n"); 
1039                         /* can/may i close it? */
1040                         if (CDROM_CAN(CDC_CLOSE_TRAY) &&
1041                             cdi->options & CDO_AUTO_CLOSE) {
1042                                 cdinfo(CD_OPEN, "trying to close the tray.\n"); 
1043                                 ret=cdo->tray_move(cdi,0);
1044                                 if (ret) {
1045                                         cdinfo(CD_OPEN, "bummer. tried to close the tray but failed.\n"); 
1046                                         /* Ignore the error from the low
1047                                         level driver.  We don't care why it
1048                                         couldn't close the tray.  We only care 
1049                                         that there is no disc in the drive, 
1050                                         since that is the _REAL_ problem here.*/
1051                                         ret=-ENOMEDIUM;
1052                                         goto clean_up_and_return;
1053                                 }
1054                         } else {
1055                                 cdinfo(CD_OPEN, "bummer. this drive can't close the tray.\n"); 
1056                                 ret=-ENOMEDIUM;
1057                                 goto clean_up_and_return;
1058                         }
1059                         /* Ok, the door should be closed now.. Check again */
1060                         ret = cdo->drive_status(cdi, CDSL_CURRENT);
1061                         if ((ret == CDS_NO_DISC) || (ret==CDS_TRAY_OPEN)) {
1062                                 cdinfo(CD_OPEN, "bummer. the tray is still not closed.\n"); 
1063                                 cdinfo(CD_OPEN, "tray might not contain a medium.\n");
1064                                 ret=-ENOMEDIUM;
1065                                 goto clean_up_and_return;
1066                         }
1067                         cdinfo(CD_OPEN, "the tray is now closed.\n"); 
1068                 }
1069                 /* the door should be closed now, check for the disc */
1070                 ret = cdo->drive_status(cdi, CDSL_CURRENT);
1071                 if (ret!=CDS_DISC_OK) {
1072                         ret = -ENOMEDIUM;
1073                         goto clean_up_and_return;
1074                 }
1075         }
1076         cdrom_count_tracks(cdi, &tracks);
1077         if (tracks.error == CDS_NO_DISC) {
1078                 cdinfo(CD_OPEN, "bummer. no disc.\n");
1079                 ret=-ENOMEDIUM;
1080                 goto clean_up_and_return;
1081         }
1082         /* CD-Players which don't use O_NONBLOCK, workman
1083          * for example, need bit CDO_CHECK_TYPE cleared! */
1084         if (tracks.data==0) {
1085                 if (cdi->options & CDO_CHECK_TYPE) {
1086                     /* give people a warning shot, now that CDO_CHECK_TYPE
1087                        is the default case! */
1088                     cdinfo(CD_OPEN, "bummer. wrong media type.\n"); 
1089                     cdinfo(CD_WARNING, "pid %d must open device O_NONBLOCK!\n",
1090                                         (unsigned int)task_pid_nr(current));
1091                     ret=-EMEDIUMTYPE;
1092                     goto clean_up_and_return;
1093                 }
1094                 else {
1095                     cdinfo(CD_OPEN, "wrong media type, but CDO_CHECK_TYPE not set.\n");
1096                 }
1097         }
1098
1099         cdinfo(CD_OPEN, "all seems well, opening the device.\n"); 
1100
1101         /* all seems well, we can open the device */
1102         ret = cdo->open(cdi, 0); /* open for data */
1103         cdinfo(CD_OPEN, "opening the device gave me %d.\n", ret); 
1104         /* After all this careful checking, we shouldn't have problems
1105            opening the device, but we don't want the device locked if 
1106            this somehow fails... */
1107         if (ret) {
1108                 cdinfo(CD_OPEN, "open device failed.\n"); 
1109                 goto clean_up_and_return;
1110         }
1111         if (CDROM_CAN(CDC_LOCK) && (cdi->options & CDO_LOCK)) {
1112                         cdo->lock_door(cdi, 1);
1113                         cdinfo(CD_OPEN, "door locked.\n");
1114         }
1115         cdinfo(CD_OPEN, "device opened successfully.\n"); 
1116         return ret;
1117
1118         /* Something failed.  Try to unlock the drive, because some drivers
1119         (notably ide-cd) lock the drive after every command.  This produced
1120         a nasty bug where after mount failed, the drive would remain locked!  
1121         This ensures that the drive gets unlocked after a mount fails.  This 
1122         is a goto to avoid bloating the driver with redundant code. */ 
1123 clean_up_and_return:
1124         cdinfo(CD_OPEN, "open failed.\n"); 
1125         if (CDROM_CAN(CDC_LOCK) && cdi->options & CDO_LOCK) {
1126                         cdo->lock_door(cdi, 0);
1127                         cdinfo(CD_OPEN, "door unlocked.\n");
1128         }
1129         return ret;
1130 }
1131
1132 /* This code is similar to that in open_for_data. The routine is called
1133    whenever an audio play operation is requested.
1134 */
1135 static int check_for_audio_disc(struct cdrom_device_info * cdi,
1136                                 struct cdrom_device_ops * cdo)
1137 {
1138         int ret;
1139         tracktype tracks;
1140         cdinfo(CD_OPEN, "entering check_for_audio_disc\n");
1141         if (!(cdi->options & CDO_CHECK_TYPE))
1142                 return 0;
1143         if (cdo->drive_status != NULL) {
1144                 ret = cdo->drive_status(cdi, CDSL_CURRENT);
1145                 cdinfo(CD_OPEN, "drive_status=%d\n", ret); 
1146                 if (ret == CDS_TRAY_OPEN) {
1147                         cdinfo(CD_OPEN, "the tray is open...\n"); 
1148                         /* can/may i close it? */
1149                         if (CDROM_CAN(CDC_CLOSE_TRAY) &&
1150                             cdi->options & CDO_AUTO_CLOSE) {
1151                                 cdinfo(CD_OPEN, "trying to close the tray.\n"); 
1152                                 ret=cdo->tray_move(cdi,0);
1153                                 if (ret) {
1154                                         cdinfo(CD_OPEN, "bummer. tried to close tray but failed.\n"); 
1155                                         /* Ignore the error from the low
1156                                         level driver.  We don't care why it
1157                                         couldn't close the tray.  We only care 
1158                                         that there is no disc in the drive, 
1159                                         since that is the _REAL_ problem here.*/
1160                                         return -ENOMEDIUM;
1161                                 }
1162                         } else {
1163                                 cdinfo(CD_OPEN, "bummer. this driver can't close the tray.\n"); 
1164                                 return -ENOMEDIUM;
1165                         }
1166                         /* Ok, the door should be closed now.. Check again */
1167                         ret = cdo->drive_status(cdi, CDSL_CURRENT);
1168                         if ((ret == CDS_NO_DISC) || (ret==CDS_TRAY_OPEN)) {
1169                                 cdinfo(CD_OPEN, "bummer. the tray is still not closed.\n"); 
1170                                 return -ENOMEDIUM;
1171                         }       
1172                         if (ret!=CDS_DISC_OK) {
1173                                 cdinfo(CD_OPEN, "bummer. disc isn't ready.\n"); 
1174                                 return -EIO;
1175                         }       
1176                         cdinfo(CD_OPEN, "the tray is now closed.\n"); 
1177                 }       
1178         }
1179         cdrom_count_tracks(cdi, &tracks);
1180         if (tracks.error) 
1181                 return(tracks.error);
1182
1183         if (tracks.audio==0)
1184                 return -EMEDIUMTYPE;
1185
1186         return 0;
1187 }
1188
1189 int cdrom_release(struct cdrom_device_info *cdi, struct file *fp)
1190 {
1191         struct cdrom_device_ops *cdo = cdi->ops;
1192         int opened_for_data;
1193
1194         cdinfo(CD_CLOSE, "entering cdrom_release\n");
1195
1196         if (cdi->use_count > 0)
1197                 cdi->use_count--;
1198
1199         if (cdi->use_count == 0) {
1200                 cdinfo(CD_CLOSE, "Use count for \"/dev/%s\" now zero\n", cdi->name);
1201                 cdrom_dvd_rw_close_write(cdi);
1202
1203                 if ((cdo->capability & CDC_LOCK) && !keeplocked) {
1204                         cdinfo(CD_CLOSE, "Unlocking door!\n");
1205                         cdo->lock_door(cdi, 0);
1206                 }
1207         }
1208
1209         opened_for_data = !(cdi->options & CDO_USE_FFLAGS) ||
1210                 !(fp && fp->f_flags & O_NONBLOCK);
1211
1212         /*
1213          * flush cache on last write release
1214          */
1215         if (CDROM_CAN(CDC_RAM) && !cdi->use_count && cdi->for_data)
1216                 cdrom_close_write(cdi);
1217
1218         cdo->release(cdi);
1219         if (cdi->use_count == 0) {      /* last process that closes dev*/
1220                 if (opened_for_data &&
1221                     cdi->options & CDO_AUTO_EJECT && CDROM_CAN(CDC_OPEN_TRAY))
1222                         cdo->tray_move(cdi, 1);
1223         }
1224         return 0;
1225 }
1226
1227 static int cdrom_read_mech_status(struct cdrom_device_info *cdi, 
1228                                   struct cdrom_changer_info *buf)
1229 {
1230         struct packet_command cgc;
1231         struct cdrom_device_ops *cdo = cdi->ops;
1232         int length;
1233
1234         /*
1235          * Sanyo changer isn't spec compliant (doesn't use regular change
1236          * LOAD_UNLOAD command, and it doesn't implement the mech status
1237          * command below
1238          */
1239         if (cdi->sanyo_slot) {
1240                 buf->hdr.nslots = 3;
1241                 buf->hdr.curslot = cdi->sanyo_slot == 3 ? 0 : cdi->sanyo_slot;
1242                 for (length = 0; length < 3; length++) {
1243                         buf->slots[length].disc_present = 1;
1244                         buf->slots[length].change = 0;
1245                 }
1246                 return 0;
1247         }
1248
1249         length = sizeof(struct cdrom_mechstat_header) +
1250                  cdi->capacity * sizeof(struct cdrom_slot);
1251
1252         init_cdrom_command(&cgc, buf, length, CGC_DATA_READ);
1253         cgc.cmd[0] = GPCMD_MECHANISM_STATUS;
1254         cgc.cmd[8] = (length >> 8) & 0xff;
1255         cgc.cmd[9] = length & 0xff;
1256         return cdo->generic_packet(cdi, &cgc);
1257 }
1258
1259 static int cdrom_slot_status(struct cdrom_device_info *cdi, int slot)
1260 {
1261         struct cdrom_changer_info *info;
1262         int ret;
1263
1264         cdinfo(CD_CHANGER, "entering cdrom_slot_status()\n"); 
1265         if (cdi->sanyo_slot)
1266                 return CDS_NO_INFO;
1267         
1268         info = kmalloc(sizeof(*info), GFP_KERNEL);
1269         if (!info)
1270                 return -ENOMEM;
1271
1272         if ((ret = cdrom_read_mech_status(cdi, info)))
1273                 goto out_free;
1274
1275         if (info->slots[slot].disc_present)
1276                 ret = CDS_DISC_OK;
1277         else
1278                 ret = CDS_NO_DISC;
1279
1280 out_free:
1281         kfree(info);
1282         return ret;
1283 }
1284
1285 /* Return the number of slots for an ATAPI/SCSI cdrom, 
1286  * return 1 if not a changer. 
1287  */
1288 int cdrom_number_of_slots(struct cdrom_device_info *cdi) 
1289 {
1290         int status;
1291         int nslots = 1;
1292         struct cdrom_changer_info *info;
1293
1294         cdinfo(CD_CHANGER, "entering cdrom_number_of_slots()\n"); 
1295         /* cdrom_read_mech_status requires a valid value for capacity: */
1296         cdi->capacity = 0; 
1297
1298         info = kmalloc(sizeof(*info), GFP_KERNEL);
1299         if (!info)
1300                 return -ENOMEM;
1301
1302         if ((status = cdrom_read_mech_status(cdi, info)) == 0)
1303                 nslots = info->hdr.nslots;
1304
1305         kfree(info);
1306         return nslots;
1307 }
1308
1309
1310 /* If SLOT < 0, unload the current slot.  Otherwise, try to load SLOT. */
1311 static int cdrom_load_unload(struct cdrom_device_info *cdi, int slot) 
1312 {
1313         struct packet_command cgc;
1314
1315         cdinfo(CD_CHANGER, "entering cdrom_load_unload()\n"); 
1316         if (cdi->sanyo_slot && slot < 0)
1317                 return 0;
1318
1319         init_cdrom_command(&cgc, NULL, 0, CGC_DATA_NONE);
1320         cgc.cmd[0] = GPCMD_LOAD_UNLOAD;
1321         cgc.cmd[4] = 2 + (slot >= 0);
1322         cgc.cmd[8] = slot;
1323         cgc.timeout = 60 * HZ;
1324
1325         /* The Sanyo 3 CD changer uses byte 7 of the 
1326         GPCMD_TEST_UNIT_READY to command to switch CDs instead of
1327         using the GPCMD_LOAD_UNLOAD opcode. */
1328         if (cdi->sanyo_slot && -1 < slot) {
1329                 cgc.cmd[0] = GPCMD_TEST_UNIT_READY;
1330                 cgc.cmd[7] = slot;
1331                 cgc.cmd[4] = cgc.cmd[8] = 0;
1332                 cdi->sanyo_slot = slot ? slot : 3;
1333         }
1334
1335         return cdi->ops->generic_packet(cdi, &cgc);
1336 }
1337
1338 static int cdrom_select_disc(struct cdrom_device_info *cdi, int slot)
1339 {
1340         struct cdrom_changer_info *info;
1341         int curslot;
1342         int ret;
1343
1344         cdinfo(CD_CHANGER, "entering cdrom_select_disc()\n"); 
1345         if (!CDROM_CAN(CDC_SELECT_DISC))
1346                 return -EDRIVE_CANT_DO_THIS;
1347
1348         (void) cdi->ops->media_changed(cdi, slot);
1349
1350         if (slot == CDSL_NONE) {
1351                 /* set media changed bits, on both queues */
1352                 cdi->mc_flags = 0x3;
1353                 return cdrom_load_unload(cdi, -1);
1354         }
1355
1356         info = kmalloc(sizeof(*info), GFP_KERNEL);
1357         if (!info)
1358                 return -ENOMEM;
1359
1360         if ((ret = cdrom_read_mech_status(cdi, info))) {
1361                 kfree(info);
1362                 return ret;
1363         }
1364
1365         curslot = info->hdr.curslot;
1366         kfree(info);
1367
1368         if (cdi->use_count > 1 || keeplocked) {
1369                 if (slot == CDSL_CURRENT) {
1370                         return curslot;
1371                 } else {
1372                         return -EBUSY;
1373                 }
1374         }
1375
1376         /* Specifying CDSL_CURRENT will attempt to load the currnet slot,
1377         which is useful if it had been previously unloaded.
1378         Whether it can or not, it returns the current slot. 
1379         Similarly,  if slot happens to be the current one, we still
1380         try and load it. */
1381         if (slot == CDSL_CURRENT)
1382                 slot = curslot;
1383
1384         /* set media changed bits on both queues */
1385         cdi->mc_flags = 0x3;
1386         if ((ret = cdrom_load_unload(cdi, slot)))
1387                 return ret;
1388
1389         return slot;
1390 }
1391
1392 /* We want to make media_changed accessible to the user through an
1393  * ioctl. The main problem now is that we must double-buffer the
1394  * low-level implementation, to assure that the VFS and the user both
1395  * see a medium change once.
1396  */
1397
1398 static
1399 int media_changed(struct cdrom_device_info *cdi, int queue)
1400 {
1401         unsigned int mask = (1 << (queue & 1));
1402         int ret = !!(cdi->mc_flags & mask);
1403
1404         if (!CDROM_CAN(CDC_MEDIA_CHANGED))
1405             return ret;
1406         /* changed since last call? */
1407         if (cdi->ops->media_changed(cdi, CDSL_CURRENT)) {
1408                 cdi->mc_flags = 0x3;    /* set bit on both queues */
1409                 ret |= 1;
1410                 cdi->media_written = 0;
1411         }
1412         cdi->mc_flags &= ~mask;         /* clear bit */
1413         return ret;
1414 }
1415
1416 int cdrom_media_changed(struct cdrom_device_info *cdi)
1417 {
1418         /* This talks to the VFS, which doesn't like errors - just 1 or 0.  
1419          * Returning "0" is always safe (media hasn't been changed). Do that 
1420          * if the low-level cdrom driver dosn't support media changed. */ 
1421         if (cdi == NULL || cdi->ops->media_changed == NULL)
1422                 return 0;
1423         if (!CDROM_CAN(CDC_MEDIA_CHANGED))
1424                 return 0;
1425         return media_changed(cdi, 0);
1426 }
1427
1428 /* badly broken, I know. Is due for a fixup anytime. */
1429 static void cdrom_count_tracks(struct cdrom_device_info *cdi, tracktype* tracks)
1430 {
1431         struct cdrom_tochdr header;
1432         struct cdrom_tocentry entry;
1433         int ret, i;
1434         tracks->data=0;
1435         tracks->audio=0;
1436         tracks->cdi=0;
1437         tracks->xa=0;
1438         tracks->error=0;
1439         cdinfo(CD_COUNT_TRACKS, "entering cdrom_count_tracks\n"); 
1440         if (!CDROM_CAN(CDC_PLAY_AUDIO)) { 
1441                 tracks->error=CDS_NO_INFO;
1442                 return;
1443         }        
1444         /* Grab the TOC header so we can see how many tracks there are */
1445         if ((ret = cdi->ops->audio_ioctl(cdi, CDROMREADTOCHDR, &header))) {
1446                 if (ret == -ENOMEDIUM)
1447                         tracks->error = CDS_NO_DISC;
1448                 else
1449                         tracks->error = CDS_NO_INFO;
1450                 return;
1451         }       
1452         /* check what type of tracks are on this disc */
1453         entry.cdte_format = CDROM_MSF;
1454         for (i = header.cdth_trk0; i <= header.cdth_trk1; i++) {
1455                 entry.cdte_track  = i;
1456                 if (cdi->ops->audio_ioctl(cdi, CDROMREADTOCENTRY, &entry)) {
1457                         tracks->error=CDS_NO_INFO;
1458                         return;
1459                 }       
1460                 if (entry.cdte_ctrl & CDROM_DATA_TRACK) {
1461                     if (entry.cdte_format == 0x10)
1462                         tracks->cdi++;
1463                     else if (entry.cdte_format == 0x20) 
1464                         tracks->xa++;
1465                     else
1466                         tracks->data++;
1467                 } else
1468                     tracks->audio++;
1469                 cdinfo(CD_COUNT_TRACKS, "track %d: format=%d, ctrl=%d\n",
1470                        i, entry.cdte_format, entry.cdte_ctrl);
1471         }       
1472         cdinfo(CD_COUNT_TRACKS, "disc has %d tracks: %d=audio %d=data %d=Cd-I %d=XA\n", 
1473                 header.cdth_trk1, tracks->audio, tracks->data, 
1474                 tracks->cdi, tracks->xa);
1475 }       
1476
1477 /* Requests to the low-level drivers will /always/ be done in the
1478    following format convention:
1479
1480    CDROM_LBA: all data-related requests.
1481    CDROM_MSF: all audio-related requests.
1482
1483    However, a low-level implementation is allowed to refuse this
1484    request, and return information in its own favorite format.
1485
1486    It doesn't make sense /at all/ to ask for a play_audio in LBA
1487    format, or ask for multi-session info in MSF format. However, for
1488    backward compatibility these format requests will be satisfied, but
1489    the requests to the low-level drivers will be sanitized in the more
1490    meaningful format indicated above.
1491  */
1492
1493 static
1494 void sanitize_format(union cdrom_addr *addr,
1495                      u_char * curr, u_char requested)
1496 {
1497         if (*curr == requested)
1498                 return;                 /* nothing to be done! */
1499         if (requested == CDROM_LBA) {
1500                 addr->lba = (int) addr->msf.frame +
1501                         75 * (addr->msf.second - 2 + 60 * addr->msf.minute);
1502         } else {                        /* CDROM_MSF */
1503                 int lba = addr->lba;
1504                 addr->msf.frame = lba % 75;
1505                 lba /= 75;
1506                 lba += 2;
1507                 addr->msf.second = lba % 60;
1508                 addr->msf.minute = lba / 60;
1509         }
1510         *curr = requested;
1511 }
1512
1513 void init_cdrom_command(struct packet_command *cgc, void *buf, int len,
1514                         int type)
1515 {
1516         memset(cgc, 0, sizeof(struct packet_command));
1517         if (buf)
1518                 memset(buf, 0, len);
1519         cgc->buffer = (char *) buf;
1520         cgc->buflen = len;
1521         cgc->data_direction = type;
1522         cgc->timeout = CDROM_DEF_TIMEOUT;
1523 }
1524
1525 /* DVD handling */
1526
1527 #define copy_key(dest,src)      memcpy((dest), (src), sizeof(dvd_key))
1528 #define copy_chal(dest,src)     memcpy((dest), (src), sizeof(dvd_challenge))
1529
1530 static void setup_report_key(struct packet_command *cgc, unsigned agid, unsigned type)
1531 {
1532         cgc->cmd[0] = GPCMD_REPORT_KEY;
1533         cgc->cmd[10] = type | (agid << 6);
1534         switch (type) {
1535                 case 0: case 8: case 5: {
1536                         cgc->buflen = 8;
1537                         break;
1538                 }
1539                 case 1: {
1540                         cgc->buflen = 16;
1541                         break;
1542                 }
1543                 case 2: case 4: {
1544                         cgc->buflen = 12;
1545                         break;
1546                 }
1547         }
1548         cgc->cmd[9] = cgc->buflen;
1549         cgc->data_direction = CGC_DATA_READ;
1550 }
1551
1552 static void setup_send_key(struct packet_command *cgc, unsigned agid, unsigned type)
1553 {
1554         cgc->cmd[0] = GPCMD_SEND_KEY;
1555         cgc->cmd[10] = type | (agid << 6);
1556         switch (type) {
1557                 case 1: {
1558                         cgc->buflen = 16;
1559                         break;
1560                 }
1561                 case 3: {
1562                         cgc->buflen = 12;
1563                         break;
1564                 }
1565                 case 6: {
1566                         cgc->buflen = 8;
1567                         break;
1568                 }
1569         }
1570         cgc->cmd[9] = cgc->buflen;
1571         cgc->data_direction = CGC_DATA_WRITE;
1572 }
1573
1574 static int dvd_do_auth(struct cdrom_device_info *cdi, dvd_authinfo *ai)
1575 {
1576         int ret;
1577         u_char buf[20];
1578         struct packet_command cgc;
1579         struct cdrom_device_ops *cdo = cdi->ops;
1580         rpc_state_t rpc_state;
1581
1582         memset(buf, 0, sizeof(buf));
1583         init_cdrom_command(&cgc, buf, 0, CGC_DATA_READ);
1584
1585         switch (ai->type) {
1586         /* LU data send */
1587         case DVD_LU_SEND_AGID:
1588                 cdinfo(CD_DVD, "entering DVD_LU_SEND_AGID\n"); 
1589                 cgc.quiet = 1;
1590                 setup_report_key(&cgc, ai->lsa.agid, 0);
1591
1592                 if ((ret = cdo->generic_packet(cdi, &cgc)))
1593                         return ret;
1594
1595                 ai->lsa.agid = buf[7] >> 6;
1596                 /* Returning data, let host change state */
1597                 break;
1598
1599         case DVD_LU_SEND_KEY1:
1600                 cdinfo(CD_DVD, "entering DVD_LU_SEND_KEY1\n"); 
1601                 setup_report_key(&cgc, ai->lsk.agid, 2);
1602
1603                 if ((ret = cdo->generic_packet(cdi, &cgc)))
1604                         return ret;
1605
1606                 copy_key(ai->lsk.key, &buf[4]);
1607                 /* Returning data, let host change state */
1608                 break;
1609
1610         case DVD_LU_SEND_CHALLENGE:
1611                 cdinfo(CD_DVD, "entering DVD_LU_SEND_CHALLENGE\n"); 
1612                 setup_report_key(&cgc, ai->lsc.agid, 1);
1613
1614                 if ((ret = cdo->generic_packet(cdi, &cgc)))
1615                         return ret;
1616
1617                 copy_chal(ai->lsc.chal, &buf[4]);
1618                 /* Returning data, let host change state */
1619                 break;
1620
1621         /* Post-auth key */
1622         case DVD_LU_SEND_TITLE_KEY:
1623                 cdinfo(CD_DVD, "entering DVD_LU_SEND_TITLE_KEY\n"); 
1624                 cgc.quiet = 1;
1625                 setup_report_key(&cgc, ai->lstk.agid, 4);
1626                 cgc.cmd[5] = ai->lstk.lba;
1627                 cgc.cmd[4] = ai->lstk.lba >> 8;
1628                 cgc.cmd[3] = ai->lstk.lba >> 16;
1629                 cgc.cmd[2] = ai->lstk.lba >> 24;
1630
1631                 if ((ret = cdo->generic_packet(cdi, &cgc)))
1632                         return ret;
1633
1634                 ai->lstk.cpm = (buf[4] >> 7) & 1;
1635                 ai->lstk.cp_sec = (buf[4] >> 6) & 1;
1636                 ai->lstk.cgms = (buf[4] >> 4) & 3;
1637                 copy_key(ai->lstk.title_key, &buf[5]);
1638                 /* Returning data, let host change state */
1639                 break;
1640
1641         case DVD_LU_SEND_ASF:
1642                 cdinfo(CD_DVD, "entering DVD_LU_SEND_ASF\n"); 
1643                 setup_report_key(&cgc, ai->lsasf.agid, 5);
1644                 
1645                 if ((ret = cdo->generic_packet(cdi, &cgc)))
1646                         return ret;
1647
1648                 ai->lsasf.asf = buf[7] & 1;
1649                 break;
1650
1651         /* LU data receive (LU changes state) */
1652         case DVD_HOST_SEND_CHALLENGE:
1653                 cdinfo(CD_DVD, "entering DVD_HOST_SEND_CHALLENGE\n"); 
1654                 setup_send_key(&cgc, ai->hsc.agid, 1);
1655                 buf[1] = 0xe;
1656                 copy_chal(&buf[4], ai->hsc.chal);
1657
1658                 if ((ret = cdo->generic_packet(cdi, &cgc)))
1659                         return ret;
1660
1661                 ai->type = DVD_LU_SEND_KEY1;
1662                 break;
1663
1664         case DVD_HOST_SEND_KEY2:
1665                 cdinfo(CD_DVD, "entering DVD_HOST_SEND_KEY2\n"); 
1666                 setup_send_key(&cgc, ai->hsk.agid, 3);
1667                 buf[1] = 0xa;
1668                 copy_key(&buf[4], ai->hsk.key);
1669
1670                 if ((ret = cdo->generic_packet(cdi, &cgc))) {
1671                         ai->type = DVD_AUTH_FAILURE;
1672                         return ret;
1673                 }
1674                 ai->type = DVD_AUTH_ESTABLISHED;
1675                 break;
1676
1677         /* Misc */
1678         case DVD_INVALIDATE_AGID:
1679                 cgc.quiet = 1;
1680                 cdinfo(CD_DVD, "entering DVD_INVALIDATE_AGID\n"); 
1681                 setup_report_key(&cgc, ai->lsa.agid, 0x3f);
1682                 if ((ret = cdo->generic_packet(cdi, &cgc)))
1683                         return ret;
1684                 break;
1685
1686         /* Get region settings */
1687         case DVD_LU_SEND_RPC_STATE:
1688                 cdinfo(CD_DVD, "entering DVD_LU_SEND_RPC_STATE\n");
1689                 setup_report_key(&cgc, 0, 8);
1690                 memset(&rpc_state, 0, sizeof(rpc_state_t));
1691                 cgc.buffer = (char *) &rpc_state;
1692
1693                 if ((ret = cdo->generic_packet(cdi, &cgc)))
1694                         return ret;
1695
1696                 ai->lrpcs.type = rpc_state.type_code;
1697                 ai->lrpcs.vra = rpc_state.vra;
1698                 ai->lrpcs.ucca = rpc_state.ucca;
1699                 ai->lrpcs.region_mask = rpc_state.region_mask;
1700                 ai->lrpcs.rpc_scheme = rpc_state.rpc_scheme;
1701                 break;
1702
1703         /* Set region settings */
1704         case DVD_HOST_SEND_RPC_STATE:
1705                 cdinfo(CD_DVD, "entering DVD_HOST_SEND_RPC_STATE\n");
1706                 setup_send_key(&cgc, 0, 6);
1707                 buf[1] = 6;
1708                 buf[4] = ai->hrpcs.pdrc;
1709
1710                 if ((ret = cdo->generic_packet(cdi, &cgc)))
1711                         return ret;
1712                 break;
1713
1714         default:
1715                 cdinfo(CD_WARNING, "Invalid DVD key ioctl (%d)\n", ai->type);
1716                 return -ENOTTY;
1717         }
1718
1719         return 0;
1720 }
1721
1722 static int dvd_read_physical(struct cdrom_device_info *cdi, dvd_struct *s)
1723 {
1724         unsigned char buf[21], *base;
1725         struct dvd_layer *layer;
1726         struct packet_command cgc;
1727         struct cdrom_device_ops *cdo = cdi->ops;
1728         int ret, layer_num = s->physical.layer_num;
1729
1730         if (layer_num >= DVD_LAYERS)
1731                 return -EINVAL;
1732
1733         init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_READ);
1734         cgc.cmd[0] = GPCMD_READ_DVD_STRUCTURE;
1735         cgc.cmd[6] = layer_num;
1736         cgc.cmd[7] = s->type;
1737         cgc.cmd[9] = cgc.buflen & 0xff;
1738
1739         /*
1740          * refrain from reporting errors on non-existing layers (mainly)
1741          */
1742         cgc.quiet = 1;
1743
1744         if ((ret = cdo->generic_packet(cdi, &cgc)))
1745                 return ret;
1746
1747         base = &buf[4];
1748         layer = &s->physical.layer[layer_num];
1749
1750         /*
1751          * place the data... really ugly, but at least we won't have to
1752          * worry about endianess in userspace.
1753          */
1754         memset(layer, 0, sizeof(*layer));
1755         layer->book_version = base[0] & 0xf;
1756         layer->book_type = base[0] >> 4;
1757         layer->min_rate = base[1] & 0xf;
1758         layer->disc_size = base[1] >> 4;
1759         layer->layer_type = base[2] & 0xf;
1760         layer->track_path = (base[2] >> 4) & 1;
1761         layer->nlayers = (base[2] >> 5) & 3;
1762         layer->track_density = base[3] & 0xf;
1763         layer->linear_density = base[3] >> 4;
1764         layer->start_sector = base[5] << 16 | base[6] << 8 | base[7];
1765         layer->end_sector = base[9] << 16 | base[10] << 8 | base[11];
1766         layer->end_sector_l0 = base[13] << 16 | base[14] << 8 | base[15];
1767         layer->bca = base[16] >> 7;
1768
1769         return 0;
1770 }
1771
1772 static int dvd_read_copyright(struct cdrom_device_info *cdi, dvd_struct *s)
1773 {
1774         int ret;
1775         u_char buf[8];
1776         struct packet_command cgc;
1777         struct cdrom_device_ops *cdo = cdi->ops;
1778
1779         init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_READ);
1780         cgc.cmd[0] = GPCMD_READ_DVD_STRUCTURE;
1781         cgc.cmd[6] = s->copyright.layer_num;
1782         cgc.cmd[7] = s->type;
1783         cgc.cmd[8] = cgc.buflen >> 8;
1784         cgc.cmd[9] = cgc.buflen & 0xff;
1785
1786         if ((ret = cdo->generic_packet(cdi, &cgc)))
1787                 return ret;
1788
1789         s->copyright.cpst = buf[4];
1790         s->copyright.rmi = buf[5];
1791
1792         return 0;
1793 }
1794
1795 static int dvd_read_disckey(struct cdrom_device_info *cdi, dvd_struct *s)
1796 {
1797         int ret, size;
1798         u_char *buf;
1799         struct packet_command cgc;
1800         struct cdrom_device_ops *cdo = cdi->ops;
1801
1802         size = sizeof(s->disckey.value) + 4;
1803
1804         if ((buf = kmalloc(size, GFP_KERNEL)) == NULL)
1805                 return -ENOMEM;
1806
1807         init_cdrom_command(&cgc, buf, size, CGC_DATA_READ);
1808         cgc.cmd[0] = GPCMD_READ_DVD_STRUCTURE;
1809         cgc.cmd[7] = s->type;
1810         cgc.cmd[8] = size >> 8;
1811         cgc.cmd[9] = size & 0xff;
1812         cgc.cmd[10] = s->disckey.agid << 6;
1813
1814         if (!(ret = cdo->generic_packet(cdi, &cgc)))
1815                 memcpy(s->disckey.value, &buf[4], sizeof(s->disckey.value));
1816
1817         kfree(buf);
1818         return ret;
1819 }
1820
1821 static int dvd_read_bca(struct cdrom_device_info *cdi, dvd_struct *s)
1822 {
1823         int ret;
1824         u_char buf[4 + 188];
1825         struct packet_command cgc;
1826         struct cdrom_device_ops *cdo = cdi->ops;
1827
1828         init_cdrom_command(&cgc, buf, sizeof(buf), CGC_DATA_READ);
1829         cgc.cmd[0] = GPCMD_READ_DVD_STRUCTURE;
1830         cgc.cmd[7] = s->type;
1831         cgc.cmd[9] = cgc.buflen & 0xff;
1832
1833         if ((ret = cdo->generic_packet(cdi, &cgc)))
1834                 return ret;
1835
1836         s->bca.len = buf[0] << 8 | buf[1];
1837         if (s->bca.len < 12 || s->bca.len > 188) {
1838                 cdinfo(CD_WARNING, "Received invalid BCA length (%d)\n", s->bca.len);
1839                 return -EIO;
1840         }
1841         memcpy(s->bca.value, &buf[4], s->bca.len);
1842
1843         return 0;
1844 }
1845
1846 static int dvd_read_manufact(struct cdrom_device_info *cdi, dvd_struct *s)
1847 {
1848         int ret = 0, size;
1849         u_char *buf;
1850         struct packet_command cgc;
1851         struct cdrom_device_ops *cdo = cdi->ops;
1852
1853         size = sizeof(s->manufact.value) + 4;
1854
1855         if ((buf = kmalloc(size, GFP_KERNEL)) == NULL)
1856                 return -ENOMEM;
1857
1858         init_cdrom_command(&cgc, buf, size, CGC_DATA_READ);
1859         cgc.cmd[0] = GPCMD_READ_DVD_STRUCTURE;
1860         cgc.cmd[7] = s->type;
1861         cgc.cmd[8] = size >> 8;
1862         cgc.cmd[9] = size & 0xff;
1863
1864         if ((ret = cdo->generic_packet(cdi, &cgc))) {
1865                 kfree(buf);
1866                 return ret;
1867         }
1868
1869         s->manufact.len = buf[0] << 8 | buf[1];
1870         if (s->manufact.len < 0 || s->manufact.len > 2048) {
1871                 cdinfo(CD_WARNING, "Received invalid manufacture info length"
1872                                    " (%d)\n", s->manufact.len);
1873                 ret = -EIO;
1874         } else {
1875                 memcpy(s->manufact.value, &buf[4], s->manufact.len);
1876         }
1877
1878         kfree(buf);
1879         return ret;
1880 }
1881
1882 static int dvd_read_struct(struct cdrom_device_info *cdi, dvd_struct *s)
1883 {
1884         switch (s->type) {
1885         case DVD_STRUCT_PHYSICAL:
1886                 return dvd_read_physical(cdi, s);
1887
1888         case DVD_STRUCT_COPYRIGHT:
1889                 return dvd_read_copyright(cdi, s);
1890
1891         case DVD_STRUCT_DISCKEY:
1892                 return dvd_read_disckey(cdi, s);
1893
1894         case DVD_STRUCT_BCA:
1895                 return dvd_read_bca(cdi, s);
1896
1897         case DVD_STRUCT_MANUFACT:
1898                 return dvd_read_manufact(cdi, s);
1899                 
1900         default:
1901                 cdinfo(CD_WARNING, ": Invalid DVD structure read requested (%d)\n",
1902                                         s->type);
1903                 return -EINVAL;
1904         }
1905 }
1906
1907 int cdrom_mode_sense(struct cdrom_device_info *cdi,
1908                      struct packet_command *cgc,
1909                      int page_code, int page_control)
1910 {
1911         struct cdrom_device_ops *cdo = cdi->ops;
1912
1913         memset(cgc->cmd, 0, sizeof(cgc->cmd));
1914
1915         cgc->cmd[0] = GPCMD_MODE_SENSE_10;
1916         cgc->cmd[2] = page_code | (page_control << 6);
1917         cgc->cmd[7] = cgc->buflen >> 8;
1918         cgc->cmd[8] = cgc->buflen & 0xff;
1919         cgc->data_direction = CGC_DATA_READ;
1920         return cdo->generic_packet(cdi, cgc);
1921 }
1922
1923 int cdrom_mode_select(struct cdrom_device_info *cdi,
1924                       struct packet_command *cgc)
1925 {
1926         struct cdrom_device_ops *cdo = cdi->ops;
1927
1928         memset(cgc->cmd, 0, sizeof(cgc->cmd));
1929         memset(cgc->buffer, 0, 2);
1930         cgc->cmd[0] = GPCMD_MODE_SELECT_10;
1931         cgc->cmd[1] = 0x10;             /* PF */
1932         cgc->cmd[7] = cgc->buflen >> 8;
1933         cgc->cmd[8] = cgc->buflen & 0xff;
1934         cgc->data_direction = CGC_DATA_WRITE;
1935         return cdo->generic_packet(cdi, cgc);
1936 }
1937
1938 static int cdrom_read_subchannel(struct cdrom_device_info *cdi,
1939                                  struct cdrom_subchnl *subchnl, int mcn)
1940 {
1941         struct cdrom_device_ops *cdo = cdi->ops;
1942         struct packet_command cgc;
1943         char buffer[32];
1944         int ret;
1945
1946         init_cdrom_command(&cgc, buffer, 16, CGC_DATA_READ);
1947         cgc.cmd[0] = GPCMD_READ_SUBCHANNEL;
1948         cgc.cmd[1] = 2;     /* MSF addressing */
1949         cgc.cmd[2] = 0x40;  /* request subQ data */
1950         cgc.cmd[3] = mcn ? 2 : 1;
1951         cgc.cmd[8] = 16;
1952
1953         if ((ret = cdo->generic_packet(cdi, &cgc)))
1954                 return ret;
1955
1956         subchnl->cdsc_audiostatus = cgc.buffer[1];
1957         subchnl->cdsc_format = CDROM_MSF;
1958         subchnl->cdsc_ctrl = cgc.buffer[5] & 0xf;
1959         subchnl->cdsc_trk = cgc.buffer[6];
1960         subchnl->cdsc_ind = cgc.buffer[7];
1961
1962         subchnl->cdsc_reladdr.msf.minute = cgc.buffer[13];
1963         subchnl->cdsc_reladdr.msf.second = cgc.buffer[14];
1964         subchnl->cdsc_reladdr.msf.frame = cgc.buffer[15];
1965         subchnl->cdsc_absaddr.msf.minute = cgc.buffer[9];
1966         subchnl->cdsc_absaddr.msf.second = cgc.buffer[10];
1967         subchnl->cdsc_absaddr.msf.frame = cgc.buffer[11];
1968
1969         return 0;
1970 }
1971
1972 /*
1973  * Specific READ_10 interface
1974  */
1975 static int cdrom_read_cd(struct cdrom_device_info *cdi,
1976                          struct packet_command *cgc, int lba,
1977                          int blocksize, int nblocks)
1978 {
1979         struct cdrom_device_ops *cdo = cdi->ops;
1980
1981         memset(&cgc->cmd, 0, sizeof(cgc->cmd));
1982         cgc->cmd[0] = GPCMD_READ_10;
1983         cgc->cmd[2] = (lba >> 24) & 0xff;
1984         cgc->cmd[3] = (lba >> 16) & 0xff;
1985         cgc->cmd[4] = (lba >>  8) & 0xff;
1986         cgc->cmd[5] = lba & 0xff;
1987         cgc->cmd[6] = (nblocks >> 16) & 0xff;
1988         cgc->cmd[7] = (nblocks >>  8) & 0xff;
1989         cgc->cmd[8] = nblocks & 0xff;
1990         cgc->buflen = blocksize * nblocks;
1991         return cdo->generic_packet(cdi, cgc);
1992 }
1993
1994 /* very generic interface for reading the various types of blocks */
1995 static int cdrom_read_block(struct cdrom_device_info *cdi,
1996                             struct packet_command *cgc,
1997                             int lba, int nblocks, int format, int blksize)
1998 {
1999         struct cdrom_device_ops *cdo = cdi->ops;
2000
2001         memset(&cgc->cmd, 0, sizeof(cgc->cmd));
2002         cgc->cmd[0] = GPCMD_READ_CD;
2003         /* expected sector size - cdda,mode1,etc. */
2004         cgc->cmd[1] = format << 2;
2005         /* starting address */
2006         cgc->cmd[2] = (lba >> 24) & 0xff;
2007         cgc->cmd[3] = (lba >> 16) & 0xff;
2008         cgc->cmd[4] = (lba >>  8) & 0xff;
2009         cgc->cmd[5] = lba & 0xff;
2010         /* number of blocks */
2011         cgc->cmd[6] = (nblocks >> 16) & 0xff;
2012         cgc->cmd[7] = (nblocks >>  8) & 0xff;
2013         cgc->cmd[8] = nblocks & 0xff;
2014         cgc->buflen = blksize * nblocks;
2015         
2016         /* set the header info returned */
2017         switch (blksize) {
2018         case CD_FRAMESIZE_RAW0  : cgc->cmd[9] = 0x58; break;
2019         case CD_FRAMESIZE_RAW1  : cgc->cmd[9] = 0x78; break;
2020         case CD_FRAMESIZE_RAW   : cgc->cmd[9] = 0xf8; break;
2021         default                 : cgc->cmd[9] = 0x10;
2022         }
2023         
2024         return cdo->generic_packet(cdi, cgc);
2025 }
2026
2027 static int cdrom_read_cdda_old(struct cdrom_device_info *cdi, __u8 __user *ubuf,
2028                                int lba, int nframes)
2029 {
2030         struct packet_command cgc;
2031         int ret = 0;
2032         int nr;
2033
2034         cdi->last_sense = 0;
2035
2036         memset(&cgc, 0, sizeof(cgc));
2037
2038         /*
2039          * start with will ra.nframes size, back down if alloc fails
2040          */
2041         nr = nframes;
2042         do {
2043                 cgc.buffer = kmalloc(CD_FRAMESIZE_RAW * nr, GFP_KERNEL);
2044                 if (cgc.buffer)
2045                         break;
2046
2047                 nr >>= 1;
2048         } while (nr);
2049
2050         if (!nr)
2051                 return -ENOMEM;
2052
2053         if (!access_ok(VERIFY_WRITE, ubuf, nframes * CD_FRAMESIZE_RAW)) {
2054                 ret = -EFAULT;
2055                 goto out;
2056         }
2057
2058         cgc.data_direction = CGC_DATA_READ;
2059         while (nframes > 0) {
2060                 if (nr > nframes)
2061                         nr = nframes;
2062
2063                 ret = cdrom_read_block(cdi, &cgc, lba, nr, 1, CD_FRAMESIZE_RAW);
2064                 if (ret)
2065                         break;
2066                 if (__copy_to_user(ubuf, cgc.buffer, CD_FRAMESIZE_RAW * nr)) {
2067                         ret = -EFAULT;
2068                         break;
2069                 }
2070                 ubuf += CD_FRAMESIZE_RAW * nr;
2071                 nframes -= nr;
2072                 lba += nr;
2073         }
2074 out:
2075         kfree(cgc.buffer);
2076         return ret;
2077 }
2078
2079 static int cdrom_read_cdda_bpc(struct cdrom_device_info *cdi, __u8 __user *ubuf,
2080                                int lba, int nframes)
2081 {
2082         struct request_queue *q = cdi->disk->queue;
2083         struct request *rq;
2084         struct bio *bio;
2085         unsigned int len;
2086         int nr, ret = 0;
2087
2088         if (!q)
2089                 return -ENXIO;
2090
2091         rq = blk_get_request(q, READ, GFP_KERNEL);
2092         if (!rq)
2093                 return -ENOMEM;
2094
2095         cdi->last_sense = 0;
2096
2097         while (nframes) {
2098                 nr = nframes;
2099                 if (cdi->cdda_method == CDDA_BPC_SINGLE)
2100                         nr = 1;
2101                 if (nr * CD_FRAMESIZE_RAW > (q->max_sectors << 9))
2102                         nr = (q->max_sectors << 9) / CD_FRAMESIZE_RAW;
2103
2104                 len = nr * CD_FRAMESIZE_RAW;
2105
2106                 ret = blk_rq_map_user(q, rq, ubuf, len);
2107                 if (ret)
2108                         break;
2109
2110                 memset(rq->cmd, 0, sizeof(rq->cmd));
2111                 rq->cmd[0] = GPCMD_READ_CD;
2112                 rq->cmd[1] = 1 << 2;
2113                 rq->cmd[2] = (lba >> 24) & 0xff;
2114                 rq->cmd[3] = (lba >> 16) & 0xff;
2115                 rq->cmd[4] = (lba >>  8) & 0xff;
2116                 rq->cmd[5] = lba & 0xff;
2117                 rq->cmd[6] = (nr >> 16) & 0xff;
2118                 rq->cmd[7] = (nr >>  8) & 0xff;
2119                 rq->cmd[8] = nr & 0xff;
2120                 rq->cmd[9] = 0xf8;
2121
2122                 rq->cmd_len = 12;
2123                 rq->cmd_type = REQ_TYPE_BLOCK_PC;
2124                 rq->timeout = 60 * HZ;
2125                 bio = rq->bio;
2126
2127                 if (blk_execute_rq(q, cdi->disk, rq, 0)) {
2128                         struct request_sense *s = rq->sense;
2129                         ret = -EIO;
2130                         cdi->last_sense = s->sense_key;
2131                 }
2132
2133                 if (blk_rq_unmap_user(bio))
2134                         ret = -EFAULT;
2135
2136                 if (ret)
2137                         break;
2138
2139                 nframes -= nr;
2140                 lba += nr;
2141                 ubuf += len;
2142         }
2143
2144         blk_put_request(rq);
2145         return ret;
2146 }
2147
2148 static int cdrom_read_cdda(struct cdrom_device_info *cdi, __u8 __user *ubuf,
2149                            int lba, int nframes)
2150 {
2151         int ret;
2152
2153         if (cdi->cdda_method == CDDA_OLD)
2154                 return cdrom_read_cdda_old(cdi, ubuf, lba, nframes);
2155
2156 retry:
2157         /*
2158          * for anything else than success and io error, we need to retry
2159          */
2160         ret = cdrom_read_cdda_bpc(cdi, ubuf, lba, nframes);
2161         if (!ret || ret != -EIO)
2162                 return ret;
2163
2164         /*
2165          * I've seen drives get sense 4/8/3 udma crc errors on multi
2166          * frame dma, so drop to single frame dma if we need to
2167          */
2168         if (cdi->cdda_method == CDDA_BPC_FULL && nframes > 1) {
2169                 printk("cdrom: dropping to single frame dma\n");
2170                 cdi->cdda_method = CDDA_BPC_SINGLE;
2171                 goto retry;
2172         }
2173
2174         /*
2175          * so we have an io error of some sort with multi frame dma. if the
2176          * condition wasn't a hardware error
2177          * problems, not for any error
2178          */
2179         if (cdi->last_sense != 0x04 && cdi->last_sense != 0x0b)
2180                 return ret;
2181
2182         printk("cdrom: dropping to old style cdda (sense=%x)\n", cdi->last_sense);
2183         cdi->cdda_method = CDDA_OLD;
2184         return cdrom_read_cdda_old(cdi, ubuf, lba, nframes);    
2185 }
2186
2187 static int cdrom_ioctl_multisession(struct cdrom_device_info *cdi,
2188                 void __user *argp)
2189 {
2190         struct cdrom_multisession ms_info;
2191         u8 requested_format;
2192         int ret;
2193
2194         cdinfo(CD_DO_IOCTL, "entering CDROMMULTISESSION\n");
2195
2196         if (!(cdi->ops->capability & CDC_MULTI_SESSION))
2197                 return -ENOSYS;
2198
2199         if (copy_from_user(&ms_info, argp, sizeof(ms_info)))
2200                 return -EFAULT;
2201
2202         requested_format = ms_info.addr_format;
2203         if (requested_format != CDROM_MSF && requested_format != CDROM_LBA)
2204                 return -EINVAL;
2205         ms_info.addr_format = CDROM_LBA;
2206
2207         ret = cdi->ops->get_last_session(cdi, &ms_info);
2208         if (ret)
2209                 return ret;
2210
2211         sanitize_format(&ms_info.addr, &ms_info.addr_format, requested_format);
2212
2213         if (copy_to_user(argp, &ms_info, sizeof(ms_info)))
2214                 return -EFAULT;
2215
2216         cdinfo(CD_DO_IOCTL, "CDROMMULTISESSION successful\n");
2217         return 0;
2218 }
2219
2220 static int cdrom_ioctl_eject(struct cdrom_device_info *cdi)
2221 {
2222         cdinfo(CD_DO_IOCTL, "entering CDROMEJECT\n");
2223
2224         if (!CDROM_CAN(CDC_OPEN_TRAY))
2225                 return -ENOSYS;
2226         if (cdi->use_count != 1 || keeplocked)
2227                 return -EBUSY;
2228         if (CDROM_CAN(CDC_LOCK)) {
2229                 int ret = cdi->ops->lock_door(cdi, 0);
2230                 if (ret)
2231                         return ret;
2232         }
2233
2234         return cdi->ops->tray_move(cdi, 1);
2235 }
2236
2237 static int cdrom_ioctl_closetray(struct cdrom_device_info *cdi)
2238 {
2239         cdinfo(CD_DO_IOCTL, "entering CDROMCLOSETRAY\n");
2240
2241         if (!CDROM_CAN(CDC_CLOSE_TRAY))
2242                 return -ENOSYS;
2243         return cdi->ops->tray_move(cdi, 0);
2244 }
2245
2246 static int cdrom_ioctl_eject_sw(struct cdrom_device_info *cdi,
2247                 unsigned long arg)
2248 {
2249         cdinfo(CD_DO_IOCTL, "entering CDROMEJECT_SW\n");
2250
2251         if (!CDROM_CAN(CDC_OPEN_TRAY))
2252                 return -ENOSYS;
2253         if (keeplocked)
2254                 return -EBUSY;
2255
2256         cdi->options &= ~(CDO_AUTO_CLOSE | CDO_AUTO_EJECT);
2257         if (arg)
2258                 cdi->options |= CDO_AUTO_CLOSE | CDO_AUTO_EJECT;
2259         return 0;
2260 }
2261
2262 static int cdrom_ioctl_media_changed(struct cdrom_device_info *cdi,
2263                 unsigned long arg)
2264 {
2265         struct cdrom_changer_info *info;
2266         int ret;
2267
2268         cdinfo(CD_DO_IOCTL, "entering CDROM_MEDIA_CHANGED\n");
2269
2270         if (!CDROM_CAN(CDC_MEDIA_CHANGED))
2271                 return -ENOSYS;
2272
2273         /* cannot select disc or select current disc */
2274         if (!CDROM_CAN(CDC_SELECT_DISC) || arg == CDSL_CURRENT)
2275                 return media_changed(cdi, 1);
2276
2277         if ((unsigned int)arg >= cdi->capacity)
2278                 return -EINVAL;
2279
2280         info = kmalloc(sizeof(*info), GFP_KERNEL);
2281         if (!info)
2282                 return -ENOMEM;
2283
2284         ret = cdrom_read_mech_status(cdi, info);
2285         if (!ret)
2286                 ret = info->slots[arg].change;
2287         kfree(info);
2288         return ret;
2289 }
2290
2291 static int cdrom_ioctl_set_options(struct cdrom_device_info *cdi,
2292                 unsigned long arg)
2293 {
2294         cdinfo(CD_DO_IOCTL, "entering CDROM_SET_OPTIONS\n");
2295
2296         /*
2297          * Options need to be in sync with capability.
2298          * Too late for that, so we have to check each one separately.
2299          */
2300         switch (arg) {
2301         case CDO_USE_FFLAGS:
2302         case CDO_CHECK_TYPE:
2303                 break;
2304         case CDO_LOCK:
2305                 if (!CDROM_CAN(CDC_LOCK))
2306                         return -ENOSYS;
2307                 break;
2308         case 0:
2309                 return cdi->options;
2310         /* default is basically CDO_[AUTO_CLOSE|AUTO_EJECT] */
2311         default:
2312                 if (!CDROM_CAN(arg))
2313                         return -ENOSYS;
2314         }
2315         cdi->options |= (int) arg;
2316         return cdi->options;
2317 }
2318
2319 static int cdrom_ioctl_clear_options(struct cdrom_device_info *cdi,
2320                 unsigned long arg)
2321 {
2322         cdinfo(CD_DO_IOCTL, "entering CDROM_CLEAR_OPTIONS\n");
2323
2324         cdi->options &= ~(int) arg;
2325         return cdi->options;
2326 }
2327
2328 static int cdrom_ioctl_select_speed(struct cdrom_device_info *cdi,
2329                 unsigned long arg)
2330 {
2331         cdinfo(CD_DO_IOCTL, "entering CDROM_SELECT_SPEED\n");
2332
2333         if (!CDROM_CAN(CDC_SELECT_SPEED))
2334                 return -ENOSYS;
2335         return cdi->ops->select_speed(cdi, arg);
2336 }
2337
2338 static int cdrom_ioctl_select_disc(struct cdrom_device_info *cdi,
2339                 unsigned long arg)
2340 {
2341         cdinfo(CD_DO_IOCTL, "entering CDROM_SELECT_DISC\n");
2342
2343         if (!CDROM_CAN(CDC_SELECT_DISC))
2344                 return -ENOSYS;
2345
2346         if (arg != CDSL_CURRENT && arg != CDSL_NONE) {
2347                 if ((int)arg >= cdi->capacity)
2348                         return -EINVAL;
2349         }
2350
2351         /*
2352          * ->select_disc is a hook to allow a driver-specific way of
2353          * seleting disc.  However, since there is no equivalent hook for
2354          * cdrom_slot_status this may not actually be useful...
2355          */
2356         if (cdi->ops->select_disc)
2357                 return cdi->ops->select_disc(cdi, arg);
2358
2359         cdinfo(CD_CHANGER, "Using generic cdrom_select_disc()\n");
2360         return cdrom_select_disc(cdi, arg);
2361 }
2362
2363 static int cdrom_ioctl_reset(struct cdrom_device_info *cdi,
2364                 struct block_device *bdev)
2365 {
2366         cdinfo(CD_DO_IOCTL, "entering CDROM_RESET\n");
2367
2368         if (!capable(CAP_SYS_ADMIN))
2369                 return -EACCES;
2370         if (!CDROM_CAN(CDC_RESET))
2371                 return -ENOSYS;
2372         invalidate_bdev(bdev);
2373         return cdi->ops->reset(cdi);
2374 }
2375
2376 static int cdrom_ioctl_lock_door(struct cdrom_device_info *cdi,
2377                 unsigned long arg)
2378 {
2379         cdinfo(CD_DO_IOCTL, "%socking door.\n", arg ? "L" : "Unl");
2380
2381         if (!CDROM_CAN(CDC_LOCK))
2382                 return -EDRIVE_CANT_DO_THIS;
2383
2384         keeplocked = arg ? 1 : 0;
2385
2386         /*
2387          * Don't unlock the door on multiple opens by default, but allow
2388          * root to do so.
2389          */
2390         if (cdi->use_count != 1 && !arg && !capable(CAP_SYS_ADMIN))
2391                 return -EBUSY;
2392         return cdi->ops->lock_door(cdi, arg);
2393 }
2394
2395 static int cdrom_ioctl_debug(struct cdrom_device_info *cdi,
2396                 unsigned long arg)
2397 {
2398         cdinfo(CD_DO_IOCTL, "%sabling debug.\n", arg ? "En" : "Dis");
2399
2400         if (!capable(CAP_SYS_ADMIN))
2401                 return -EACCES;
2402         debug = arg ? 1 : 0;
2403         return debug;
2404 }
2405
2406 static int cdrom_ioctl_get_capability(struct cdrom_device_info *cdi)
2407 {
2408         cdinfo(CD_DO_IOCTL, "entering CDROM_GET_CAPABILITY\n");
2409         return (cdi->ops->capability & ~cdi->mask);
2410 }
2411
2412 /*
2413  * The following function is implemented, although very few audio
2414  * discs give Universal Product Code information, which should just be
2415  * the Medium Catalog Number on the box.  Note, that the way the code
2416  * is written on the CD is /not/ uniform across all discs!
2417  */
2418 static int cdrom_ioctl_get_mcn(struct cdrom_device_info *cdi,
2419                 void __user *argp)
2420 {
2421         struct cdrom_mcn mcn;
2422         int ret;
2423
2424         cdinfo(CD_DO_IOCTL, "entering CDROM_GET_MCN\n");
2425
2426         if (!(cdi->ops->capability & CDC_MCN))
2427                 return -ENOSYS;
2428         ret = cdi->ops->get_mcn(cdi, &mcn);
2429         if (ret)
2430                 return ret;
2431
2432         if (copy_to_user(argp, &mcn, sizeof(mcn)))
2433                 return -EFAULT;
2434         cdinfo(CD_DO_IOCTL, "CDROM_GET_MCN successful\n");
2435         return 0;
2436 }
2437
2438 static int cdrom_ioctl_drive_status(struct cdrom_device_info *cdi,
2439                 unsigned long arg)
2440 {
2441         cdinfo(CD_DO_IOCTL, "entering CDROM_DRIVE_STATUS\n");
2442
2443         if (!(cdi->ops->capability & CDC_DRIVE_STATUS))
2444                 return -ENOSYS;
2445         if (!CDROM_CAN(CDC_SELECT_DISC) ||
2446             (arg == CDSL_CURRENT || arg == CDSL_NONE))
2447                 return cdi->ops->drive_status(cdi, CDSL_CURRENT);
2448         if (((int)arg >= cdi->capacity))
2449                 return -EINVAL;
2450         return cdrom_slot_status(cdi, arg);
2451 }
2452
2453 /*
2454  * Ok, this is where problems start.  The current interface for the
2455  * CDROM_DISC_STATUS ioctl is flawed.  It makes the false assumption that
2456  * CDs are all CDS_DATA_1 or all CDS_AUDIO, etc.  Unfortunatly, while this
2457  * is often the case, it is also very common for CDs to have some tracks
2458  * with data, and some tracks with audio.  Just because I feel like it,
2459  * I declare the following to be the best way to cope.  If the CD has ANY
2460  * data tracks on it, it will be returned as a data CD.  If it has any XA
2461  * tracks, I will return it as that.  Now I could simplify this interface
2462  * by combining these  returns with the above, but this more clearly
2463  * demonstrates the problem with the current interface.  Too bad this
2464  * wasn't designed to use bitmasks...         -Erik
2465  *
2466  * Well, now we have the option CDS_MIXED: a mixed-type CD.
2467  * User level programmers might feel the ioctl is not very useful.
2468  *                                      ---david
2469  */
2470 static int cdrom_ioctl_disc_status(struct cdrom_device_info *cdi)
2471 {
2472         tracktype tracks;
2473
2474         cdinfo(CD_DO_IOCTL, "entering CDROM_DISC_STATUS\n");
2475
2476         cdrom_count_tracks(cdi, &tracks);
2477         if (tracks.error)
2478                 return tracks.error;
2479
2480         /* Policy mode on */
2481         if (tracks.audio > 0) {
2482                 if (!tracks.data && !tracks.cdi && !tracks.xa)
2483                         return CDS_AUDIO;
2484                 else
2485                         return CDS_MIXED;
2486         }
2487
2488         if (tracks.cdi > 0)
2489                 return CDS_XA_2_2;
2490         if (tracks.xa > 0)
2491                 return CDS_XA_2_1;
2492         if (tracks.data > 0)
2493                 return CDS_DATA_1;
2494         /* Policy mode off */
2495
2496         cdinfo(CD_WARNING,"This disc doesn't have any tracks I recognize!\n");
2497         return CDS_NO_INFO;
2498 }
2499
2500 static int cdrom_ioctl_changer_nslots(struct cdrom_device_info *cdi)
2501 {
2502         cdinfo(CD_DO_IOCTL, "entering CDROM_CHANGER_NSLOTS\n");
2503         return cdi->capacity;
2504 }
2505
2506 static int cdrom_ioctl_get_subchnl(struct cdrom_device_info *cdi,
2507                 void __user *argp)
2508 {
2509         struct cdrom_subchnl q;
2510         u8 requested, back;
2511         int ret;
2512
2513         /* cdinfo(CD_DO_IOCTL,"entering CDROMSUBCHNL\n");*/
2514
2515         if (!CDROM_CAN(CDC_PLAY_AUDIO))
2516                 return -ENOSYS;
2517         if (copy_from_user(&q, argp, sizeof(q)))
2518                 return -EFAULT;
2519
2520         requested = q.cdsc_format;
2521         if (requested != CDROM_MSF && requested != CDROM_LBA)
2522                 return -EINVAL;
2523         q.cdsc_format = CDROM_MSF;
2524
2525         ret = cdi->ops->audio_ioctl(cdi, CDROMSUBCHNL, &q);
2526         if (ret)
2527                 return ret;
2528
2529         back = q.cdsc_format; /* local copy */
2530         sanitize_format(&q.cdsc_absaddr, &back, requested);
2531         sanitize_format(&q.cdsc_reladdr, &q.cdsc_format, requested);
2532
2533         if (copy_to_user(argp, &q, sizeof(q)))
2534                 return -EFAULT;
2535         /* cdinfo(CD_DO_IOCTL, "CDROMSUBCHNL successful\n"); */
2536         return 0;
2537 }
2538
2539 static int cdrom_ioctl_read_tochdr(struct cdrom_device_info *cdi,
2540                 void __user *argp)
2541 {
2542         struct cdrom_tochdr header;
2543         int ret;
2544
2545         /* cdinfo(CD_DO_IOCTL, "entering CDROMREADTOCHDR\n"); */
2546
2547         if (!CDROM_CAN(CDC_PLAY_AUDIO))
2548                 return -ENOSYS;
2549         if (copy_from_user(&header, argp, sizeof(header)))
2550                 return -EFAULT;
2551
2552         ret = cdi->ops->audio_ioctl(cdi, CDROMREADTOCHDR, &header);
2553         if (ret)
2554                 return ret;
2555
2556         if (copy_to_user(argp, &header, sizeof(header)))
2557                 return -EFAULT;
2558         /* cdinfo(CD_DO_IOCTL, "CDROMREADTOCHDR successful\n"); */
2559         return 0;
2560 }
2561
2562 static int cdrom_ioctl_read_tocentry(struct cdrom_device_info *cdi,
2563                 void __user *argp)
2564 {
2565         struct cdrom_tocentry entry;
2566         u8 requested_format;
2567         int ret;
2568
2569         /* cdinfo(CD_DO_IOCTL, "entering CDROMREADTOCENTRY\n"); */
2570
2571         if (!CDROM_CAN(CDC_PLAY_AUDIO))
2572                 return -ENOSYS;
2573         if (copy_from_user(&entry, argp, sizeof(entry)))
2574                 return -EFAULT;
2575
2576         requested_format = entry.cdte_format;
2577         if (requested_format != CDROM_MSF && requested_format != CDROM_LBA)
2578                 return -EINVAL;
2579         /* make interface to low-level uniform */
2580         entry.cdte_format = CDROM_MSF;
2581         ret = cdi->ops->audio_ioctl(cdi, CDROMREADTOCENTRY, &entry);
2582         if (ret)
2583                 return ret;
2584         sanitize_format(&entry.cdte_addr, &entry.cdte_format, requested_format);
2585
2586         if (copy_to_user(argp, &entry, sizeof(entry)))
2587                 return -EFAULT;
2588         /* cdinfo(CD_DO_IOCTL, "CDROMREADTOCENTRY successful\n"); */
2589         return 0;
2590 }
2591
2592 static int cdrom_ioctl_play_msf(struct cdrom_device_info *cdi,
2593                 void __user *argp)
2594 {
2595         struct cdrom_msf msf;
2596
2597         cdinfo(CD_DO_IOCTL, "entering CDROMPLAYMSF\n");
2598
2599         if (!CDROM_CAN(CDC_PLAY_AUDIO))
2600                 return -ENOSYS;
2601         if (copy_from_user(&msf, argp, sizeof(msf)))
2602                 return -EFAULT;
2603         return cdi->ops->audio_ioctl(cdi, CDROMPLAYMSF, &msf);
2604 }
2605
2606 static int cdrom_ioctl_play_trkind(struct cdrom_device_info *cdi,
2607                 void __user *argp)
2608 {
2609         struct cdrom_ti ti;
2610         int ret;
2611
2612         cdinfo(CD_DO_IOCTL, "entering CDROMPLAYTRKIND\n");
2613
2614         if (!CDROM_CAN(CDC_PLAY_AUDIO))
2615                 return -ENOSYS;
2616         if (copy_from_user(&ti, argp, sizeof(ti)))
2617                 return -EFAULT;
2618
2619         ret = check_for_audio_disc(cdi, cdi->ops);
2620         if (ret)
2621                 return ret;
2622         return cdi->ops->audio_ioctl(cdi, CDROMPLAYTRKIND, &ti);
2623 }
2624 static int cdrom_ioctl_volctrl(struct cdrom_device_info *cdi,
2625                 void __user *argp)
2626 {
2627         struct cdrom_volctrl volume;
2628
2629         cdinfo(CD_DO_IOCTL, "entering CDROMVOLCTRL\n");
2630
2631         if (!CDROM_CAN(CDC_PLAY_AUDIO))
2632                 return -ENOSYS;
2633         if (copy_from_user(&volume, argp, sizeof(volume)))
2634                 return -EFAULT;
2635         return cdi->ops->audio_ioctl(cdi, CDROMVOLCTRL, &volume);
2636 }
2637
2638 static int cdrom_ioctl_volread(struct cdrom_device_info *cdi,
2639                 void __user *argp)
2640 {
2641         struct cdrom_volctrl volume;
2642         int ret;
2643
2644         cdinfo(CD_DO_IOCTL, "entering CDROMVOLREAD\n");
2645
2646         if (!CDROM_CAN(CDC_PLAY_AUDIO))
2647                 return -ENOSYS;
2648
2649         ret = cdi->ops->audio_ioctl(cdi, CDROMVOLREAD, &volume);
2650         if (ret)
2651                 return ret;
2652
2653         if (copy_to_user(argp, &volume, sizeof(volume)))
2654                 return -EFAULT;
2655         return 0;
2656 }
2657
2658 static int cdrom_ioctl_audioctl(struct cdrom_device_info *cdi,
2659                 unsigned int cmd)
2660 {
2661         int ret;
2662
2663         cdinfo(CD_DO_IOCTL, "doing audio ioctl (start/stop/pause/resume)\n");
2664
2665         if (!CDROM_CAN(CDC_PLAY_AUDIO))
2666                 return -ENOSYS;
2667         ret = check_for_audio_disc(cdi, cdi->ops);
2668         if (ret)
2669                 return ret;
2670         return cdi->ops->audio_ioctl(cdi, cmd, NULL);
2671 }
2672
2673 /*
2674  * Just about every imaginable ioctl is supported in the Uniform layer
2675  * these days.
2676  * ATAPI / SCSI specific code now mainly resides in mmc_ioctl().
2677  */
2678 int cdrom_ioctl(struct file * file, struct cdrom_device_info *cdi,
2679                 struct inode *ip, unsigned int cmd, unsigned long arg)
2680 {
2681         void __user *argp = (void __user *)arg;
2682         int ret;
2683         struct gendisk *disk = ip->i_bdev->bd_disk;
2684
2685         /*
2686          * Try the generic SCSI command ioctl's first.
2687          */
2688         ret = scsi_cmd_ioctl(file, disk->queue, disk, cmd, argp);
2689         if (ret != -ENOTTY)
2690                 return ret;
2691
2692         switch (cmd) {
2693         case CDROMMULTISESSION:
2694                 return cdrom_ioctl_multisession(cdi, argp);
2695         case CDROMEJECT:
2696                 return cdrom_ioctl_eject(cdi);
2697         case CDROMCLOSETRAY:
2698                 return cdrom_ioctl_closetray(cdi);
2699         case CDROMEJECT_SW:
2700                 return cdrom_ioctl_eject_sw(cdi, arg);
2701         case CDROM_MEDIA_CHANGED:
2702                 return cdrom_ioctl_media_changed(cdi, arg);
2703         case CDROM_SET_OPTIONS:
2704                 return cdrom_ioctl_set_options(cdi, arg);
2705         case CDROM_CLEAR_OPTIONS:
2706                 return cdrom_ioctl_clear_options(cdi, arg);
2707         case CDROM_SELECT_SPEED:
2708                 return cdrom_ioctl_select_speed(cdi, arg);
2709         case CDROM_SELECT_DISC:
2710                 return cdrom_ioctl_select_disc(cdi, arg);
2711         case CDROMRESET:
2712                 return cdrom_ioctl_reset(cdi, ip->i_bdev);
2713         case CDROM_LOCKDOOR:
2714                 return cdrom_ioctl_lock_door(cdi, arg);
2715         case CDROM_DEBUG:
2716                 return cdrom_ioctl_debug(cdi, arg);
2717         case CDROM_GET_CAPABILITY:
2718                 return cdrom_ioctl_get_capability(cdi);
2719         case CDROM_GET_MCN:
2720                 return cdrom_ioctl_get_mcn(cdi, argp);
2721         case CDROM_DRIVE_STATUS:
2722                 return cdrom_ioctl_drive_status(cdi, arg);
2723         case CDROM_DISC_STATUS:
2724                 return cdrom_ioctl_disc_status(cdi);
2725         case CDROM_CHANGER_NSLOTS:
2726                 return cdrom_ioctl_changer_nslots(cdi);
2727         }
2728
2729         /*
2730          * Use the ioctls that are implemented through the generic_packet()
2731          * interface. this may look at bit funny, but if -ENOTTY is
2732          * returned that particular ioctl is not implemented and we
2733          * let it go through the device specific ones.
2734          */
2735         if (CDROM_CAN(CDC_GENERIC_PACKET)) {
2736                 ret = mmc_ioctl(cdi, cmd, arg);
2737                 if (ret != -ENOTTY)
2738                         return ret;
2739         }
2740
2741         /*
2742          * Note: most of the cdinfo() calls are commented out here,
2743          * because they fill up the sys log when CD players poll
2744          * the drive.
2745          */
2746         switch (cmd) {
2747         case CDROMSUBCHNL:
2748                 return cdrom_ioctl_get_subchnl(cdi, argp);
2749         case CDROMREADTOCHDR:
2750                 return cdrom_ioctl_read_tochdr(cdi, argp);
2751         case CDROMREADTOCENTRY:
2752                 return cdrom_ioctl_read_tocentry(cdi, argp);
2753         case CDROMPLAYMSF:
2754                 return cdrom_ioctl_play_msf(cdi, argp);
2755         case CDROMPLAYTRKIND:
2756                 return cdrom_ioctl_play_trkind(cdi, argp);
2757         case CDROMVOLCTRL:
2758                 return cdrom_ioctl_volctrl(cdi, argp);
2759         case CDROMVOLREAD:
2760                 return cdrom_ioctl_volread(cdi, argp);
2761         case CDROMSTART:
2762         case CDROMSTOP:
2763         case CDROMPAUSE:
2764         case CDROMRESUME:
2765                 return cdrom_ioctl_audioctl(cdi, cmd);
2766         }
2767
2768         return -ENOSYS;
2769 }
2770
2771 /*
2772  * Required when we need to use READ_10 to issue other than 2048 block
2773  * reads
2774  */
2775 static int cdrom_switch_blocksize(struct cdrom_device_info *cdi, int size)
2776 {
2777         struct cdrom_device_ops *cdo = cdi->ops;
2778         struct packet_command cgc;
2779         struct modesel_head mh;
2780
2781         memset(&mh, 0, sizeof(mh));
2782         mh.block_desc_length = 0x08;
2783         mh.block_length_med = (size >> 8) & 0xff;
2784         mh.block_length_lo = size & 0xff;
2785
2786         memset(&cgc, 0, sizeof(cgc));
2787         cgc.cmd[0] = 0x15;
2788         cgc.cmd[1] = 1 << 4;
2789         cgc.cmd[4] = 12;
2790         cgc.buflen = sizeof(mh);
2791         cgc.buffer = (char *) &mh;
2792         cgc.data_direction = CGC_DATA_WRITE;
2793         mh.block_desc_length = 0x08;
2794         mh.block_length_med = (size >> 8) & 0xff;
2795         mh.block_length_lo = size & 0xff;
2796
2797         return cdo->generic_packet(cdi, &cgc);
2798 }
2799
2800 static int mmc_ioctl(struct cdrom_device_info *cdi, unsigned int cmd,
2801                      unsigned long arg)
2802 {               
2803         struct cdrom_device_ops *cdo = cdi->ops;
2804         struct packet_command cgc;
2805         struct request_sense sense;
2806         unsigned char buffer[32];
2807         int ret = 0;
2808
2809         memset(&cgc, 0, sizeof(cgc));
2810
2811         /* build a unified command and queue it through
2812            cdo->generic_packet() */
2813         switch (cmd) {
2814         case CDROMREADRAW:
2815         case CDROMREADMODE1:
2816         case CDROMREADMODE2: {
2817                 struct cdrom_msf msf;
2818                 int blocksize = 0, format = 0, lba;
2819                 
2820                 switch (cmd) {
2821                 case CDROMREADRAW:
2822                         blocksize = CD_FRAMESIZE_RAW;
2823                         break;
2824                 case CDROMREADMODE1:
2825                         blocksize = CD_FRAMESIZE;
2826                         format = 2;
2827                         break;
2828                 case CDROMREADMODE2:
2829                         blocksize = CD_FRAMESIZE_RAW0;
2830                         break;
2831                 }
2832                 IOCTL_IN(arg, struct cdrom_msf, msf);
2833                 lba = msf_to_lba(msf.cdmsf_min0,msf.cdmsf_sec0,msf.cdmsf_frame0);
2834                 /* FIXME: we need upper bound checking, too!! */
2835                 if (lba < 0)
2836                         return -EINVAL;
2837                 cgc.buffer = kmalloc(blocksize, GFP_KERNEL);
2838                 if (cgc.buffer == NULL)
2839                         return -ENOMEM;
2840                 memset(&sense, 0, sizeof(sense));
2841                 cgc.sense = &sense;
2842                 cgc.data_direction = CGC_DATA_READ;
2843                 ret = cdrom_read_block(cdi, &cgc, lba, 1, format, blocksize);
2844                 if (ret && sense.sense_key==0x05 && sense.asc==0x20 && sense.ascq==0x00) {
2845                         /*
2846                          * SCSI-II devices are not required to support
2847                          * READ_CD, so let's try switching block size
2848                          */
2849                         /* FIXME: switch back again... */
2850                         if ((ret = cdrom_switch_blocksize(cdi, blocksize))) {
2851                                 kfree(cgc.buffer);
2852                                 return ret;
2853                         }
2854                         cgc.sense = NULL;
2855                         ret = cdrom_read_cd(cdi, &cgc, lba, blocksize, 1);
2856                         ret |= cdrom_switch_blocksize(cdi, blocksize);
2857                 }
2858                 if (!ret && copy_to_user((char __user *)arg, cgc.buffer, blocksize))
2859                         ret = -EFAULT;
2860                 kfree(cgc.buffer);
2861                 return ret;
2862                 }
2863         case CDROMREADAUDIO: {
2864                 struct cdrom_read_audio ra;
2865                 int lba;
2866
2867                 IOCTL_IN(arg, struct cdrom_read_audio, ra);
2868
2869                 if (ra.addr_format == CDROM_MSF)
2870                         lba = msf_to_lba(ra.addr.msf.minute,
2871                                          ra.addr.msf.second,
2872                                          ra.addr.msf.frame);
2873                 else if (ra.addr_format == CDROM_LBA)
2874                         lba = ra.addr.lba;
2875                 else
2876                         return -EINVAL;
2877
2878                 /* FIXME: we need upper bound checking, too!! */
2879                 if (lba < 0 || ra.nframes <= 0 || ra.nframes > CD_FRAMES)
2880                         return -EINVAL;
2881
2882                 return cdrom_read_cdda(cdi, ra.buf, lba, ra.nframes);
2883                 }
2884         case CDROMSUBCHNL: {
2885                 struct cdrom_subchnl q;
2886                 u_char requested, back;
2887                 IOCTL_IN(arg, struct cdrom_subchnl, q);
2888                 requested = q.cdsc_format;
2889                 if (!((requested == CDROM_MSF) ||
2890                       (requested == CDROM_LBA)))
2891                         return -EINVAL;
2892                 q.cdsc_format = CDROM_MSF;
2893                 if ((ret = cdrom_read_subchannel(cdi, &q, 0)))
2894                         return ret;
2895                 back = q.cdsc_format; /* local copy */
2896                 sanitize_format(&q.cdsc_absaddr, &back, requested);
2897                 sanitize_format(&q.cdsc_reladdr, &q.cdsc_format, requested);
2898                 IOCTL_OUT(arg, struct cdrom_subchnl, q);
2899                 /* cdinfo(CD_DO_IOCTL, "CDROMSUBCHNL successful\n"); */ 
2900                 return 0;
2901                 }
2902         case CDROMPLAYMSF: {
2903                 struct cdrom_msf msf;
2904                 cdinfo(CD_DO_IOCTL, "entering CDROMPLAYMSF\n");
2905                 IOCTL_IN(arg, struct cdrom_msf, msf);
2906                 cgc.cmd[0] = GPCMD_PLAY_AUDIO_MSF;
2907                 cgc.cmd[3] = msf.cdmsf_min0;
2908                 cgc.cmd[4] = msf.cdmsf_sec0;
2909                 cgc.cmd[5] = msf.cdmsf_frame0;
2910                 cgc.cmd[6] = msf.cdmsf_min1;
2911                 cgc.cmd[7] = msf.cdmsf_sec1;
2912                 cgc.cmd[8] = msf.cdmsf_frame1;
2913                 cgc.data_direction = CGC_DATA_NONE;
2914                 return cdo->generic_packet(cdi, &cgc);
2915                 }
2916         case CDROMPLAYBLK: {
2917                 struct cdrom_blk blk;
2918                 cdinfo(CD_DO_IOCTL, "entering CDROMPLAYBLK\n");
2919                 IOCTL_IN(arg, struct cdrom_blk, blk);
2920                 cgc.cmd[0] = GPCMD_PLAY_AUDIO_10;
2921                 cgc.cmd[2] = (blk.from >> 24) & 0xff;
2922                 cgc.cmd[3] = (blk.from >> 16) & 0xff;
2923                 cgc.cmd[4] = (blk.from >>  8) & 0xff;
2924                 cgc.cmd[5] = blk.from & 0xff;
2925                 cgc.cmd[7] = (blk.len >> 8) & 0xff;
2926                 cgc.cmd[8] = blk.len & 0xff;
2927                 cgc.data_direction = CGC_DATA_NONE;
2928                 return cdo->generic_packet(cdi, &cgc);
2929                 }
2930         case CDROMVOLCTRL:
2931         case CDROMVOLREAD: {
2932                 struct cdrom_volctrl volctrl;
2933                 char mask[sizeof(buffer)];
2934                 unsigned short offset;
2935
2936                 cdinfo(CD_DO_IOCTL, "entering CDROMVOLUME\n");
2937
2938                 IOCTL_IN(arg, struct cdrom_volctrl, volctrl);
2939
2940                 cgc.buffer = buffer;
2941                 cgc.buflen = 24;
2942                 if ((ret = cdrom_mode_sense(cdi, &cgc, GPMODE_AUDIO_CTL_PAGE, 0)))
2943                     return ret;
2944                 
2945                 /* originally the code depended on buffer[1] to determine
2946                    how much data is available for transfer. buffer[1] is
2947                    unfortunately ambigious and the only reliable way seem
2948                    to be to simply skip over the block descriptor... */
2949                 offset = 8 + be16_to_cpu(*(__be16 *)(buffer+6));
2950
2951                 if (offset + 16 > sizeof(buffer))
2952                         return -E2BIG;
2953
2954                 if (offset + 16 > cgc.buflen) {
2955                         cgc.buflen = offset+16;
2956                         ret = cdrom_mode_sense(cdi, &cgc,
2957                                                 GPMODE_AUDIO_CTL_PAGE, 0);
2958                         if (ret)
2959                                 return ret;
2960                 }
2961
2962                 /* sanity check */
2963                 if ((buffer[offset] & 0x3f) != GPMODE_AUDIO_CTL_PAGE ||
2964                                 buffer[offset+1] < 14)
2965                         return -EINVAL;
2966
2967                 /* now we have the current volume settings. if it was only
2968                    a CDROMVOLREAD, return these values */
2969                 if (cmd == CDROMVOLREAD) {
2970                         volctrl.channel0 = buffer[offset+9];
2971                         volctrl.channel1 = buffer[offset+11];
2972                         volctrl.channel2 = buffer[offset+13];
2973                         volctrl.channel3 = buffer[offset+15];
2974                         IOCTL_OUT(arg, struct cdrom_volctrl, volctrl);
2975                         return 0;
2976                 }
2977                 
2978                 /* get the volume mask */
2979                 cgc.buffer = mask;
2980                 if ((ret = cdrom_mode_sense(cdi, &cgc, 
2981                                 GPMODE_AUDIO_CTL_PAGE, 1)))
2982                         return ret;
2983
2984                 buffer[offset+9] = volctrl.channel0 & mask[offset+9];
2985                 buffer[offset+11] = volctrl.channel1 & mask[offset+11];
2986                 buffer[offset+13] = volctrl.channel2 & mask[offset+13];
2987                 buffer[offset+15] = volctrl.channel3 & mask[offset+15];
2988
2989                 /* set volume */
2990                 cgc.buffer = buffer + offset - 8;
2991                 memset(cgc.buffer, 0, 8);
2992                 return cdrom_mode_select(cdi, &cgc);
2993                 }
2994
2995         case CDROMSTART:
2996         case CDROMSTOP: {
2997                 cdinfo(CD_DO_IOCTL, "entering CDROMSTART/CDROMSTOP\n"); 
2998                 cgc.cmd[0] = GPCMD_START_STOP_UNIT;
2999                 cgc.cmd[1] = 1;
3000                 cgc.cmd[4] = (cmd == CDROMSTART) ? 1 : 0;
3001                 cgc.data_direction = CGC_DATA_NONE;
3002                 return cdo->generic_packet(cdi, &cgc);
3003                 }
3004
3005         case CDROMPAUSE:
3006         case CDROMRESUME: {
3007                 cdinfo(CD_DO_IOCTL, "entering CDROMPAUSE/CDROMRESUME\n"); 
3008                 cgc.cmd[0] = GPCMD_PAUSE_RESUME;
3009                 cgc.cmd[8] = (cmd == CDROMRESUME) ? 1 : 0;
3010                 cgc.data_direction = CGC_DATA_NONE;
3011                 return cdo->generic_packet(cdi, &cgc);
3012                 }
3013
3014         case DVD_READ_STRUCT: {
3015                 dvd_struct *s;
3016                 int size = sizeof(dvd_struct);
3017                 if (!CDROM_CAN(CDC_DVD))
3018                         return -ENOSYS;
3019                 if ((s = kmalloc(size, GFP_KERNEL)) == NULL)
3020                         return -ENOMEM;
3021                 cdinfo(CD_DO_IOCTL, "entering DVD_READ_STRUCT\n"); 
3022                 if (copy_from_user(s, (dvd_struct __user *)arg, size)) {
3023                         kfree(s);
3024                         return -EFAULT;
3025                 }
3026                 if ((ret = dvd_read_struct(cdi, s))) {
3027                         kfree(s);
3028                         return ret;
3029                 }
3030                 if (copy_to_user((dvd_struct __user *)arg, s, size))
3031                         ret = -EFAULT;
3032                 kfree(s);
3033                 return ret;
3034                 }
3035
3036         case DVD_AUTH: {
3037                 dvd_authinfo ai;
3038                 if (!CDROM_CAN(CDC_DVD))
3039                         return -ENOSYS;
3040                 cdinfo(CD_DO_IOCTL, "entering DVD_AUTH\n"); 
3041                 IOCTL_IN(arg, dvd_authinfo, ai);
3042                 if ((ret = dvd_do_auth (cdi, &ai)))
3043                         return ret;
3044                 IOCTL_OUT(arg, dvd_authinfo, ai);
3045                 return 0;
3046                 }
3047
3048         case CDROM_NEXT_WRITABLE: {
3049                 long next = 0;
3050                 cdinfo(CD_DO_IOCTL, "entering CDROM_NEXT_WRITABLE\n"); 
3051                 if ((ret = cdrom_get_next_writable(cdi, &next)))
3052                         return ret;
3053                 IOCTL_OUT(arg, long, next);
3054                 return 0;
3055                 }
3056         case CDROM_LAST_WRITTEN: {
3057                 long last = 0;
3058                 cdinfo(CD_DO_IOCTL, "entering CDROM_LAST_WRITTEN\n"); 
3059                 if ((ret = cdrom_get_last_written(cdi, &last)))
3060                         return ret;
3061                 IOCTL_OUT(arg, long, last);
3062                 return 0;
3063                 }
3064         } /* switch */
3065
3066         return -ENOTTY;
3067 }
3068
3069 static int cdrom_get_track_info(struct cdrom_device_info *cdi, __u16 track, __u8 type,
3070                          track_information *ti)
3071 {
3072         struct cdrom_device_ops *cdo = cdi->ops;
3073         struct packet_command cgc;
3074         int ret, buflen;
3075
3076         init_cdrom_command(&cgc, ti, 8, CGC_DATA_READ);
3077         cgc.cmd[0] = GPCMD_READ_TRACK_RZONE_INFO;
3078         cgc.cmd[1] = type & 3;
3079         cgc.cmd[4] = (track & 0xff00) >> 8;
3080         cgc.cmd[5] = track & 0xff;
3081         cgc.cmd[8] = 8;
3082         cgc.quiet = 1;
3083
3084         if ((ret = cdo->generic_packet(cdi, &cgc)))
3085                 return ret;
3086         
3087         buflen = be16_to_cpu(ti->track_information_length) +
3088                      sizeof(ti->track_information_length);
3089
3090         if (buflen > sizeof(track_information))
3091                 buflen = sizeof(track_information);
3092
3093         cgc.cmd[8] = cgc.buflen = buflen;
3094         if ((ret = cdo->generic_packet(cdi, &cgc)))
3095                 return ret;
3096
3097         /* return actual fill size */
3098         return buflen;
3099 }
3100
3101 /* requires CD R/RW */
3102 static int cdrom_get_disc_info(struct cdrom_device_info *cdi, disc_information *di)
3103 {
3104         struct cdrom_device_ops *cdo = cdi->ops;
3105         struct packet_command cgc;
3106         int ret, buflen;
3107
3108         /* set up command and get the disc info */
3109         init_cdrom_command(&cgc, di, sizeof(*di), CGC_DATA_READ);
3110         cgc.cmd[0] = GPCMD_READ_DISC_INFO;
3111         cgc.cmd[8] = cgc.buflen = 2;
3112         cgc.quiet = 1;
3113
3114         if ((ret = cdo->generic_packet(cdi, &cgc)))
3115                 return ret;
3116
3117         /* not all drives have the same disc_info length, so requeue
3118          * packet with the length the drive tells us it can supply
3119          */
3120         buflen = be16_to_cpu(di->disc_information_length) +
3121                      sizeof(di->disc_information_length);
3122
3123         if (buflen > sizeof(disc_information))
3124                 buflen = sizeof(disc_information);
3125
3126         cgc.cmd[8] = cgc.buflen = buflen;
3127         if ((ret = cdo->generic_packet(cdi, &cgc)))
3128                 return ret;
3129
3130         /* return actual fill size */
3131         return buflen;
3132 }
3133
3134 /* return the last written block on the CD-R media. this is for the udf
3135    file system. */
3136 int cdrom_get_last_written(struct cdrom_device_info *cdi, long *last_written)
3137 {
3138         struct cdrom_tocentry toc;
3139         disc_information di;
3140         track_information ti;
3141         __u32 last_track;
3142         int ret = -1, ti_size;
3143
3144         if (!CDROM_CAN(CDC_GENERIC_PACKET))
3145                 goto use_toc;
3146
3147         ret = cdrom_get_disc_info(cdi, &di);
3148         if (ret < (int)(offsetof(typeof(di), last_track_lsb)
3149                         + sizeof(di.last_track_lsb)))
3150                 goto use_toc;
3151
3152         /* if unit didn't return msb, it's zeroed by cdrom_get_disc_info */
3153         last_track = (di.last_track_msb << 8) | di.last_track_lsb;
3154         ti_size = cdrom_get_track_info(cdi, last_track, 1, &ti);
3155         if (ti_size < (int)offsetof(typeof(ti), track_start))
3156                 goto use_toc;
3157
3158         /* if this track is blank, try the previous. */
3159         if (ti.blank) {
3160                 if (last_track==1)
3161                         goto use_toc;
3162                 last_track--;
3163                 ti_size = cdrom_get_track_info(cdi, last_track, 1, &ti);
3164         }
3165
3166         if (ti_size < (int)(offsetof(typeof(ti), track_size)
3167                                 + sizeof(ti.track_size)))
3168                 goto use_toc;
3169
3170         /* if last recorded field is valid, return it. */
3171         if (ti.lra_v && ti_size >= (int)(offsetof(typeof(ti), last_rec_address)
3172                                 + sizeof(ti.last_rec_address))) {
3173                 *last_written = be32_to_cpu(ti.last_rec_address);
3174         } else {
3175                 /* make it up instead */
3176                 *last_written = be32_to_cpu(ti.track_start) +
3177                                 be32_to_cpu(ti.track_size);
3178                 if (ti.free_blocks)
3179                         *last_written -= (be32_to_cpu(ti.free_blocks) + 7);
3180         }
3181         return 0;
3182
3183         /* this is where we end up if the drive either can't do a
3184            GPCMD_READ_DISC_INFO or GPCMD_READ_TRACK_RZONE_INFO or if
3185            it doesn't give enough information or fails. then we return
3186            the toc contents. */
3187 use_toc:
3188         toc.cdte_format = CDROM_MSF;
3189         toc.cdte_track = CDROM_LEADOUT;
3190         if ((ret = cdi->ops->audio_ioctl(cdi, CDROMREADTOCENTRY, &toc)))
3191                 return ret;
3192         sanitize_format(&toc.cdte_addr, &toc.cdte_format, CDROM_LBA);
3193         *last_written = toc.cdte_addr.lba;
3194         return 0;
3195 }
3196
3197 /* return the next writable block. also for udf file system. */
3198 static int cdrom_get_next_writable(struct cdrom_device_info *cdi, long *next_writable)
3199 {
3200         disc_information di;
3201         track_information ti;
3202         __u16 last_track;
3203         int ret, ti_size;
3204
3205         if (!CDROM_CAN(CDC_GENERIC_PACKET))
3206                 goto use_last_written;
3207
3208         ret = cdrom_get_disc_info(cdi, &di);
3209         if (ret < 0 || ret < offsetof(typeof(di), last_track_lsb)
3210                                 + sizeof(di.last_track_lsb))
3211                 goto use_last_written;
3212
3213         /* if unit didn't return msb, it's zeroed by cdrom_get_disc_info */
3214         last_track = (di.last_track_msb << 8) | di.last_track_lsb;
3215         ti_size = cdrom_get_track_info(cdi, last_track, 1, &ti);
3216         if (ti_size < 0 || ti_size < offsetof(typeof(ti), track_start))
3217                 goto use_last_written;
3218
3219         /* if this track is blank, try the previous. */
3220         if (ti.blank) {
3221                 if (last_track == 1)
3222                         goto use_last_written;
3223                 last_track--;
3224                 ti_size = cdrom_get_track_info(cdi, last_track, 1, &ti);
3225                 if (ti_size < 0)
3226                         goto use_last_written;
3227         }
3228
3229         /* if next recordable address field is valid, use it. */
3230         if (ti.nwa_v && ti_size >= offsetof(typeof(ti), next_writable)
3231                                 + sizeof(ti.next_writable)) {
3232                 *next_writable = be32_to_cpu(ti.next_writable);
3233                 return 0;
3234         }
3235
3236 use_last_written:
3237         if ((ret = cdrom_get_last_written(cdi, next_writable))) {
3238                 *next_writable = 0;
3239                 return ret;
3240         } else {
3241                 *next_writable += 7;
3242                 return 0;
3243         }
3244 }
3245
3246 EXPORT_SYMBOL(cdrom_get_last_written);
3247 EXPORT_SYMBOL(register_cdrom);
3248 EXPORT_SYMBOL(unregister_cdrom);
3249 EXPORT_SYMBOL(cdrom_open);
3250 EXPORT_SYMBOL(cdrom_release);
3251 EXPORT_SYMBOL(cdrom_ioctl);
3252 EXPORT_SYMBOL(cdrom_media_changed);
3253 EXPORT_SYMBOL(cdrom_number_of_slots);
3254 EXPORT_SYMBOL(cdrom_mode_select);
3255 EXPORT_SYMBOL(cdrom_mode_sense);
3256 EXPORT_SYMBOL(init_cdrom_command);
3257 EXPORT_SYMBOL(cdrom_get_media_event);
3258
3259 #ifdef CONFIG_SYSCTL
3260
3261 #define CDROM_STR_SIZE 1000
3262
3263 static struct cdrom_sysctl_settings {
3264         char    info[CDROM_STR_SIZE];   /* general info */
3265         int     autoclose;              /* close tray upon mount, etc */
3266         int     autoeject;              /* eject on umount */
3267         int     debug;                  /* turn on debugging messages */
3268         int     lock;                   /* lock the door on device open */
3269         int     check;                  /* check media type */
3270 } cdrom_sysctl_settings;
3271
3272 enum cdrom_print_option {
3273         CTL_NAME,
3274         CTL_SPEED,
3275         CTL_SLOTS,
3276         CTL_CAPABILITY
3277 };
3278
3279 static int cdrom_print_info(const char *header, int val, char *info,
3280                                 int *pos, enum cdrom_print_option option)
3281 {
3282         const int max_size = sizeof(cdrom_sysctl_settings.info);
3283         struct cdrom_device_info *cdi;
3284         int ret;
3285
3286         ret = scnprintf(info + *pos, max_size - *pos, header);
3287         if (!ret)
3288                 return 1;
3289
3290         *pos += ret;
3291
3292         list_for_each_entry(cdi, &cdrom_list, list) {
3293                 switch (option) {
3294                 case CTL_NAME:
3295                         ret = scnprintf(info + *pos, max_size - *pos,
3296                                         "\t%s", cdi->name);
3297                         break;
3298                 case CTL_SPEED:
3299                         ret = scnprintf(info + *pos, max_size - *pos,
3300                                         "\t%d", cdi->speed);
3301                         break;
3302                 case CTL_SLOTS:
3303                         ret = scnprintf(info + *pos, max_size - *pos,
3304                                         "\t%d", cdi->capacity);
3305                         break;
3306                 case CTL_CAPABILITY:
3307                         ret = scnprintf(info + *pos, max_size - *pos,
3308                                         "\t%d", CDROM_CAN(val) != 0);
3309                         break;
3310                 default:
3311                         printk(KERN_INFO "cdrom: invalid option%d\n", option);
3312                         return 1;
3313                 }
3314                 if (!ret)
3315                         return 1;
3316                 *pos += ret;
3317         }
3318
3319         return 0;
3320 }
3321
3322 static int cdrom_sysctl_info(ctl_table *ctl, int write, struct file * filp,
3323                            void __user *buffer, size_t *lenp, loff_t *ppos)
3324 {
3325         int pos;
3326         char *info = cdrom_sysctl_settings.info;
3327         const int max_size = sizeof(cdrom_sysctl_settings.info);
3328         
3329         if (!*lenp || (*ppos && !write)) {
3330                 *lenp = 0;
3331                 return 0;
3332         }
3333
3334         mutex_lock(&cdrom_mutex);
3335
3336         pos = sprintf(info, "CD-ROM information, " VERSION "\n");
3337         
3338         if (cdrom_print_info("\ndrive name:\t", 0, info, &pos, CTL_NAME))
3339                 goto done;
3340         if (cdrom_print_info("\ndrive speed:\t", 0, info, &pos, CTL_SPEED))
3341                 goto done;
3342         if (cdrom_print_info("\ndrive # of slots:", 0, info, &pos, CTL_SLOTS))
3343                 goto done;
3344         if (cdrom_print_info("\nCan close tray:\t",
3345                                 CDC_CLOSE_TRAY, info, &pos, CTL_CAPABILITY))
3346                 goto done;
3347         if (cdrom_print_info("\nCan open tray:\t",
3348                                 CDC_OPEN_TRAY, info, &pos, CTL_CAPABILITY))
3349                 goto done;
3350         if (cdrom_print_info("\nCan lock tray:\t",
3351                                 CDC_LOCK, info, &pos, CTL_CAPABILITY))
3352                 goto done;
3353         if (cdrom_print_info("\nCan change speed:",
3354                                 CDC_SELECT_SPEED, info, &pos, CTL_CAPABILITY))
3355                 goto done;
3356         if (cdrom_print_info("\nCan select disk:",
3357                                 CDC_SELECT_DISC, info, &pos, CTL_CAPABILITY))
3358                 goto done;
3359         if (cdrom_print_info("\nCan read multisession:",
3360                                 CDC_MULTI_SESSION, info, &pos, CTL_CAPABILITY))
3361                 goto done;
3362         if (cdrom_print_info("\nCan read MCN:\t",
3363                                 CDC_MCN, info, &pos, CTL_CAPABILITY))
3364                 goto done;
3365         if (cdrom_print_info("\nReports media changed:",
3366                                 CDC_MEDIA_CHANGED, info, &pos, CTL_CAPABILITY))
3367                 goto done;
3368         if (cdrom_print_info("\nCan play audio:\t",
3369                                 CDC_PLAY_AUDIO, info, &pos, CTL_CAPABILITY))
3370                 goto done;
3371         if (cdrom_print_info("\nCan write CD-R:\t",
3372                                 CDC_CD_R, info, &pos, CTL_CAPABILITY))
3373                 goto done;
3374         if (cdrom_print_info("\nCan write CD-RW:",
3375                                 CDC_CD_RW, info, &pos, CTL_CAPABILITY))
3376                 goto done;
3377         if (cdrom_print_info("\nCan read DVD:\t",
3378                                 CDC_DVD, info, &pos, CTL_CAPABILITY))
3379                 goto done;
3380         if (cdrom_print_info("\nCan write DVD-R:",
3381                                 CDC_DVD_R, info, &pos, CTL_CAPABILITY))
3382                 goto done;
3383         if (cdrom_print_info("\nCan write DVD-RAM:",
3384                                 CDC_DVD_RAM, info, &pos, CTL_CAPABILITY))
3385                 goto done;
3386         if (cdrom_print_info("\nCan read MRW:\t",
3387                                 CDC_MRW, info, &pos, CTL_CAPABILITY))
3388                 goto done;
3389         if (cdrom_print_info("\nCan write MRW:\t",
3390                                 CDC_MRW_W, info, &pos, CTL_CAPABILITY))
3391                 goto done;
3392         if (cdrom_print_info("\nCan write RAM:\t",
3393                                 CDC_RAM, info, &pos, CTL_CAPABILITY))
3394                 goto done;
3395         if (!scnprintf(info + pos, max_size - pos, "\n\n"))
3396                 goto done;
3397 doit:
3398         mutex_unlock(&cdrom_mutex);
3399         return proc_dostring(ctl, write, filp, buffer, lenp, ppos);
3400 done:
3401         printk(KERN_INFO "cdrom: info buffer too small\n");
3402         goto doit;
3403 }
3404
3405 /* Unfortunately, per device settings are not implemented through
3406    procfs/sysctl yet. When they are, this will naturally disappear. For now
3407    just update all drives. Later this will become the template on which
3408    new registered drives will be based. */
3409 static void cdrom_update_settings(void)
3410 {
3411         struct cdrom_device_info *cdi;
3412
3413         mutex_lock(&cdrom_mutex);
3414         list_for_each_entry(cdi, &cdrom_list, list) {
3415                 if (autoclose && CDROM_CAN(CDC_CLOSE_TRAY))
3416                         cdi->options |= CDO_AUTO_CLOSE;
3417                 else if (!autoclose)
3418                         cdi->options &= ~CDO_AUTO_CLOSE;
3419                 if (autoeject && CDROM_CAN(CDC_OPEN_TRAY))
3420                         cdi->options |= CDO_AUTO_EJECT;
3421                 else if (!autoeject)
3422                         cdi->options &= ~CDO_AUTO_EJECT;
3423                 if (lockdoor && CDROM_CAN(CDC_LOCK))
3424                         cdi->options |= CDO_LOCK;
3425                 else if (!lockdoor)
3426                         cdi->options &= ~CDO_LOCK;
3427                 if (check_media_type)
3428                         cdi->options |= CDO_CHECK_TYPE;
3429                 else
3430                         cdi->options &= ~CDO_CHECK_TYPE;
3431         }
3432         mutex_unlock(&cdrom_mutex);
3433 }
3434
3435 static int cdrom_sysctl_handler(ctl_table *ctl, int write, struct file * filp,
3436                                 void __user *buffer, size_t *lenp, loff_t *ppos)
3437 {
3438         int ret;
3439         
3440         ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
3441
3442         if (write) {
3443         
3444                 /* we only care for 1 or 0. */
3445                 autoclose        = !!cdrom_sysctl_settings.autoclose;
3446                 autoeject        = !!cdrom_sysctl_settings.autoeject;
3447                 debug            = !!cdrom_sysctl_settings.debug;
3448                 lockdoor         = !!cdrom_sysctl_settings.lock;
3449                 check_media_type = !!cdrom_sysctl_settings.check;
3450
3451                 /* update the option flags according to the changes. we
3452                    don't have per device options through sysctl yet,
3453                    but we will have and then this will disappear. */
3454                 cdrom_update_settings();
3455         }
3456
3457         return ret;
3458 }
3459
3460 /* Place files in /proc/sys/dev/cdrom */
3461 static ctl_table cdrom_table[] = {
3462         {
3463                 .procname       = "info",
3464                 .data           = &cdrom_sysctl_settings.info, 
3465                 .maxlen         = CDROM_STR_SIZE,
3466                 .mode           = 0444,
3467                 .proc_handler   = &cdrom_sysctl_info,
3468         },
3469         {
3470                 .procname       = "autoclose",
3471                 .data           = &cdrom_sysctl_settings.autoclose,
3472                 .maxlen         = sizeof(int),
3473                 .mode           = 0644,
3474                 .proc_handler   = &cdrom_sysctl_handler,
3475         },
3476         {
3477                 .procname       = "autoeject",
3478                 .data           = &cdrom_sysctl_settings.autoeject,
3479                 .maxlen         = sizeof(int),
3480                 .mode           = 0644,
3481                 .proc_handler   = &cdrom_sysctl_handler,
3482         },
3483         {
3484                 .procname       = "debug",
3485                 .data           = &cdrom_sysctl_settings.debug,
3486                 .maxlen         = sizeof(int),
3487                 .mode           = 0644,
3488                 .proc_handler   = &cdrom_sysctl_handler,
3489         },
3490         {
3491                 .procname       = "lock",
3492                 .data           = &cdrom_sysctl_settings.lock,
3493                 .maxlen         = sizeof(int),
3494                 .mode           = 0644,
3495                 .proc_handler   = &cdrom_sysctl_handler,
3496         },
3497         {
3498                 .procname       = "check_media",
3499                 .data           = &cdrom_sysctl_settings.check,
3500                 .maxlen         = sizeof(int),
3501                 .mode           = 0644,
3502                 .proc_handler   = &cdrom_sysctl_handler
3503         },
3504         { .ctl_name = 0 }
3505 };
3506
3507 static ctl_table cdrom_cdrom_table[] = {
3508         {
3509                 .ctl_name       = DEV_CDROM,
3510                 .procname       = "cdrom",
3511                 .maxlen         = 0,
3512                 .mode           = 0555,
3513                 .child          = cdrom_table,
3514         },
3515         { .ctl_name = 0 }
3516 };
3517
3518 /* Make sure that /proc/sys/dev is there */
3519 static ctl_table cdrom_root_table[] = {
3520         {
3521                 .ctl_name       = CTL_DEV,
3522                 .procname       = "dev",
3523                 .maxlen         = 0,
3524                 .mode           = 0555,
3525                 .child          = cdrom_cdrom_table,
3526         },
3527         { .ctl_name = 0 }
3528 };
3529 static struct ctl_table_header *cdrom_sysctl_header;
3530
3531 static void cdrom_sysctl_register(void)
3532 {
3533         static int initialized;
3534
3535         if (initialized == 1)
3536                 return;
3537
3538         cdrom_sysctl_header = register_sysctl_table(cdrom_root_table);
3539
3540         /* set the defaults */
3541         cdrom_sysctl_settings.autoclose = autoclose;
3542         cdrom_sysctl_settings.autoeject = autoeject;
3543         cdrom_sysctl_settings.debug = debug;
3544         cdrom_sysctl_settings.lock = lockdoor;
3545         cdrom_sysctl_settings.check = check_media_type;
3546
3547         initialized = 1;
3548 }
3549
3550 static void cdrom_sysctl_unregister(void)
3551 {
3552         if (cdrom_sysctl_header)
3553                 unregister_sysctl_table(cdrom_sysctl_header);
3554 }
3555
3556 #else /* CONFIG_SYSCTL */
3557
3558 static void cdrom_sysctl_register(void)
3559 {
3560 }
3561
3562 static void cdrom_sysctl_unregister(void)
3563 {
3564 }
3565
3566 #endif /* CONFIG_SYSCTL */
3567
3568 static int __init cdrom_init(void)
3569 {
3570         cdrom_sysctl_register();
3571
3572         return 0;
3573 }
3574
3575 static void __exit cdrom_exit(void)
3576 {
3577         printk(KERN_INFO "Uniform CD-ROM driver unloaded\n");
3578         cdrom_sysctl_unregister();
3579 }
3580
3581 module_init(cdrom_init);
3582 module_exit(cdrom_exit);
3583 MODULE_LICENSE("GPL");