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