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