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