Path: blob/master/Documentation/cdrom/cdrom-standard.tex
10821 views
\documentclass{article}1\def\version{$Id: cdrom-standard.tex,v 1.9 1997/12/28 15:42:49 david Exp $}2\newcommand{\newsection}[1]{\newpage\section{#1}}34\evensidemargin=0pt5\oddsidemargin=0pt6\topmargin=-\headheight \advance\topmargin by -\headsep7\textwidth=15.99cm \textheight=24.62cm % normal A4, 1'' margin89\def\linux{{\sc Linux}}10\def\cdrom{{\sc cd-rom}}11\def\UCD{{\sc Uniform cd-rom Driver}}12\def\cdromc{{\tt {cdrom.c}}}13\def\cdromh{{\tt {cdrom.h}}}14\def\fo{\sl} % foreign words15\def\ie{{\fo i.e.}}16\def\eg{{\fo e.g.}}1718\everymath{\it} \everydisplay{\it}19\catcode `\_=\active \def_{\_\penalty100 }20\catcode`\<=\active \def<#1>{{\langle\hbox{\rm#1}\rangle}}2122\begin{document}23\title{A \linux\ \cdrom\ standard}24\author{David van Leeuwen\\{\normalsize\tt david@ElseWare.cistron.nl}25\\{\footnotesize updated by Erik Andersen {\tt(andersee@debian.org)}}26\\{\footnotesize updated by Jens Axboe {\tt(axboe@image.dk)}}}27\date{12 March 1999}2829\maketitle3031\newsection{Introduction}3233\linux\ is probably the Unix-like operating system that supports34the widest variety of hardware devices. The reasons for this are35presumably36\begin{itemize}37\item38The large list of hardware devices available for the many platforms39that \linux\ now supports (\ie, i386-PCs, Sparc Suns, etc.)40\item41The open design of the operating system, such that anybody can write a42driver for \linux.43\item44There is plenty of source code around as examples of how to write a driver.45\end{itemize}46The openness of \linux, and the many different types of available47hardware has allowed \linux\ to support many different hardware devices.48Unfortunately, the very openness that has allowed \linux\ to support49all these different devices has also allowed the behavior of each50device driver to differ significantly from one device to another.51This divergence of behavior has been very significant for \cdrom\52devices; the way a particular drive reacts to a `standard' $ioctl()$53call varies greatly from one device driver to another. To avoid making54their drivers totally inconsistent, the writers of \linux\ \cdrom\55drivers generally created new device drivers by understanding, copying,56and then changing an existing one. Unfortunately, this practice did not57maintain uniform behavior across all the \linux\ \cdrom\ drivers.5859This document describes an effort to establish Uniform behavior across60all the different \cdrom\ device drivers for \linux. This document also61defines the various $ioctl$s, and how the low-level \cdrom\ device62drivers should implement them. Currently (as of the \linux\ 2.1.$x$63development kernels) several low-level \cdrom\ device drivers, including64both IDE/ATAPI and SCSI, now use this Uniform interface.6566When the \cdrom\ was developed, the interface between the \cdrom\ drive67and the computer was not specified in the standards. As a result, many68different \cdrom\ interfaces were developed. Some of them had their69own proprietary design (Sony, Mitsumi, Panasonic, Philips), other70manufacturers adopted an existing electrical interface and changed71the functionality (CreativeLabs/SoundBlaster, Teac, Funai) or simply72adapted their drives to one or more of the already existing electrical73interfaces (Aztech, Sanyo, Funai, Vertos, Longshine, Optics Storage and74most of the `NoName' manufacturers). In cases where a new drive really75brought its own interface or used its own command set and flow control76scheme, either a separate driver had to be written, or an existing77driver had to be enhanced. History has delivered us \cdrom\ support for78many of these different interfaces. Nowadays, almost all new \cdrom\79drives are either IDE/ATAPI or SCSI, and it is very unlikely that any80manufacturer will create a new interface. Even finding drives for the81old proprietary interfaces is getting difficult.8283When (in the 1.3.70's) I looked at the existing software interface,84which was expressed through \cdromh, it appeared to be a rather wild85set of commands and data formats.\footnote{I cannot recollect what86kernel version I looked at, then, presumably 1.2.13 and 1.3.34---the87latest kernel that I was indirectly involved in.} It seemed that many88features of the software interface had been added to accommodate the89capabilities of a particular drive, in an {\fo ad hoc\/} manner. More90importantly, it appeared that the behavior of the `standard' commands91was different for most of the different drivers: \eg, some drivers92close the tray if an $open()$ call occurs when the tray is open, while93others do not. Some drivers lock the door upon opening the device, to94prevent an incoherent file system, but others don't, to allow software95ejection. Undoubtedly, the capabilities of the different drives vary,96but even when two drives have the same capability their drivers'97behavior was usually different.9899I decided to start a discussion on how to make all the \linux\ \cdrom\100drivers behave more uniformly. I began by contacting the developers of101the many \cdrom\ drivers found in the \linux\ kernel. Their reactions102encouraged me to write the \UCD\ which this document is intended to103describe. The implementation of the \UCD\ is in the file \cdromc. This104driver is intended to be an additional software layer that sits on top105of the low-level device drivers for each \cdrom\ drive. By adding this106additional layer, it is possible to have all the different \cdrom\107devices behave {\em exactly\/} the same (insofar as the underlying108hardware will allow).109110The goal of the \UCD\ is {\em not\/} to alienate driver developers who111have not yet taken steps to support this effort. The goal of \UCD\ is112simply to give people writing application programs for \cdrom\ drives113{\em one\/} \linux\ \cdrom\ interface with consistent behavior for all114\cdrom\ devices. In addition, this also provides a consistent interface115between the low-level device driver code and the \linux\ kernel. Care116is taken that 100\,\% compatibility exists with the data structures and117programmer's interface defined in \cdromh. This guide was written to118help \cdrom\ driver developers adapt their code to use the \UCD\ code119defined in \cdromc.120121Personally, I think that the most important hardware interfaces are122the IDE/ATAPI drives and, of course, the SCSI drives, but as prices123of hardware drop continuously, it is also likely that people may have124more than one \cdrom\ drive, possibly of mixed types. It is important125that these drives behave in the same way. In December 1994, one of the126cheapest \cdrom\ drives was a Philips cm206, a double-speed proprietary127drive. In the months that I was busy writing a \linux\ driver for it,128proprietary drives became obsolete and IDE/ATAPI drives became the129standard. At the time of the last update to this document (November1301997) it is becoming difficult to even {\em find} anything less than a13116 speed \cdrom\ drive, and 24 speed drives are common.132133\newsection{Standardizing through another software level}134\label{cdrom.c}135136At the time this document was conceived, all drivers directly137implemented the \cdrom\ $ioctl()$ calls through their own routines. This138led to the danger of different drivers forgetting to do important things139like checking that the user was giving the driver valid data. More140importantly, this led to the divergence of behavior, which has already141been discussed.142143For this reason, the \UCD\ was created to enforce consistent \cdrom\144drive behavior, and to provide a common set of services to the various145low-level \cdrom\ device drivers. The \UCD\ now provides another146software-level, that separates the $ioctl()$ and $open()$ implementation147from the actual hardware implementation. Note that this effort has148made few changes which will affect a user's application programs. The149greatest change involved moving the contents of the various low-level150\cdrom\ drivers' header files to the kernel's cdrom directory. This was151done to help ensure that the user is only presented with only one cdrom152interface, the interface defined in \cdromh.153154\cdrom\ drives are specific enough (\ie, different from other155block-devices such as floppy or hard disc drives), to define a set156of common {\em \cdrom\ device operations}, $<cdrom-device>_dops$.157These operations are different from the classical block-device file158operations, $<block-device>_fops$.159160The routines for the \UCD\ interface level are implemented in the file161\cdromc. In this file, the \UCD\ interfaces with the kernel as a block162device by registering the following general $struct\ file_operations$:163$$164\halign{$#$\ \hfil&$#$\ \hfil&$/*$ \rm# $*/$\hfil\cr165struct& file_operations\ cdrom_fops = \{\hidewidth\cr166&NULL, & lseek \cr167&block_read, & read---general block-dev read \cr168&block_write, & write---general block-dev write \cr169&NULL, & readdir \cr170&NULL, & select \cr171&cdrom_ioctl, & ioctl \cr172&NULL, & mmap \cr173&cdrom_open, & open \cr174&cdrom_release, & release \cr175&NULL, & fsync \cr176&NULL, & fasync \cr177&cdrom_media_changed, & media change \cr178&NULL & revalidate \cr179\};\cr180}181$$182183Every active \cdrom\ device shares this $struct$. The routines184declared above are all implemented in \cdromc, since this file is the185place where the behavior of all \cdrom-devices is defined and186standardized. The actual interface to the various types of \cdrom\187hardware is still performed by various low-level \cdrom-device188drivers. These routines simply implement certain {\em capabilities\/}189that are common to all \cdrom\ (and really, all removable-media190devices).191192Registration of a low-level \cdrom\ device driver is now done through193the general routines in \cdromc, not through the Virtual File System194(VFS) any more. The interface implemented in \cdromc\ is carried out195through two general structures that contain information about the196capabilities of the driver, and the specific drives on which the197driver operates. The structures are:198\begin{description}199\item[$cdrom_device_ops$]200This structure contains information about the low-level driver for a201\cdrom\ device. This structure is conceptually connected to the major202number of the device (although some drivers may have different203major numbers, as is the case for the IDE driver).204\item[$cdrom_device_info$]205This structure contains information about a particular \cdrom\ drive,206such as its device name, speed, etc. This structure is conceptually207connected to the minor number of the device.208\end{description}209210Registering a particular \cdrom\ drive with the \UCD\ is done by the211low-level device driver though a call to:212$$register_cdrom(struct\ cdrom_device_info * <device>_info)213$$214The device information structure, $<device>_info$, contains all the215information needed for the kernel to interface with the low-level216\cdrom\ device driver. One of the most important entries in this217structure is a pointer to the $cdrom_device_ops$ structure of the218low-level driver.219220The device operations structure, $cdrom_device_ops$, contains a list221of pointers to the functions which are implemented in the low-level222device driver. When \cdromc\ accesses a \cdrom\ device, it does it223through the functions in this structure. It is impossible to know all224the capabilities of future \cdrom\ drives, so it is expected that this225list may need to be expanded from time to time as new technologies are226developed. For example, CD-R and CD-R/W drives are beginning to become227popular, and support will soon need to be added for them. For now, the228current $struct$ is:229$$230\halign{$#$\ \hfil&$#$\ \hfil&\hbox to 10em{$#$\hss}&231$/*$ \rm# $*/$\hfil\cr232struct& cdrom_device_ops\ \{ \hidewidth\cr233&int& (* open)(struct\ cdrom_device_info *, int)\cr234&void& (* release)(struct\ cdrom_device_info *);\cr235&int& (* drive_status)(struct\ cdrom_device_info *, int);\cr236&int& (* media_changed)(struct\ cdrom_device_info *, int);\cr237&int& (* tray_move)(struct\ cdrom_device_info *, int);\cr238&int& (* lock_door)(struct\ cdrom_device_info *, int);\cr239&int& (* select_speed)(struct\ cdrom_device_info *, int);\cr240&int& (* select_disc)(struct\ cdrom_device_info *, int);\cr241&int& (* get_last_session) (struct\ cdrom_device_info *,242struct\ cdrom_multisession *{});\cr243&int& (* get_mcn)(struct\ cdrom_device_info *, struct\ cdrom_mcn *{});\cr244&int& (* reset)(struct\ cdrom_device_info *);\cr245&int& (* audio_ioctl)(struct\ cdrom_device_info *, unsigned\ int,246void *{});\cr247&int& (* dev_ioctl)(struct\ cdrom_device_info *, unsigned\ int,248unsigned\ long);\cr249\noalign{\medskip}250&const\ int& capability;& capability flags \cr251&int& n_minors;& number of active minor devices \cr252\};\cr253}254$$255When a low-level device driver implements one of these capabilities,256it should add a function pointer to this $struct$. When a particular257function is not implemented, however, this $struct$ should contain a258NULL instead. The $capability$ flags specify the capabilities of the259\cdrom\ hardware and/or low-level \cdrom\ driver when a \cdrom\ drive260is registered with the \UCD. The value $n_minors$ should be a positive261value indicating the number of minor devices that are supported by262the low-level device driver, normally~1. Although these two variables263are `informative' rather than `operational,' they are included in264$cdrom_device_ops$ because they describe the capability of the {\em265driver\/} rather than the {\em drive}. Nomenclature has always been266difficult in computer programming.267268Note that most functions have fewer parameters than their269$blkdev_fops$ counterparts. This is because very little of the270information in the structures $inode$ and $file$ is used. For most271drivers, the main parameter is the $struct$ $cdrom_device_info$, from272which the major and minor number can be extracted. (Most low-level273\cdrom\ drivers don't even look at the major and minor number though,274since many of them only support one device.) This will be available275through $dev$ in $cdrom_device_info$ described below.276277The drive-specific, minor-like information that is registered with278\cdromc, currently contains the following fields:279$$280\halign{$#$\ \hfil&$#$\ \hfil&\hbox to 10em{$#$\hss}&281$/*$ \rm# $*/$\hfil\cr282struct& cdrom_device_info\ \{ \hidewidth\cr283& struct\ cdrom_device_ops *& ops;& device operations for this major\cr284& struct\ cdrom_device_info *& next;& next device_info for this major\cr285& void *& handle;& driver-dependent data\cr286\noalign{\medskip}287& kdev_t& dev;& device number (incorporates minor)\cr288& int& mask;& mask of capability: disables them \cr289& int& speed;& maximum speed for reading data \cr290& int& capacity;& number of discs in a jukebox \cr291\noalign{\medskip}292&int& options : 30;& options flags \cr293&unsigned& mc_flags : 2;& media-change buffer flags \cr294& int& use_count;& number of times device is opened\cr295& char& name[20];& name of the device type\cr296\}\cr297}$$298Using this $struct$, a linked list of the registered minor devices is299built, using the $next$ field. The device number, the device operations300struct and specifications of properties of the drive are stored in this301structure.302303The $mask$ flags can be used to mask out some of the capabilities listed304in $ops\to capability$, if a specific drive doesn't support a feature305of the driver. The value $speed$ specifies the maximum head-rate of the306drive, measured in units of normal audio speed (176\,kB/sec raw data or307150\,kB/sec file system data). The value $n_discs$ should reflect the308number of discs the drive can hold simultaneously, if it is designed309as a juke-box, or otherwise~1. The parameters are declared $const$310because they describe properties of the drive, which don't change after311registration.312313A few registers contain variables local to the \cdrom\ drive. The314flags $options$ are used to specify how the general \cdrom\ routines315should behave. These various flags registers should provide enough316flexibility to adapt to the different users' wishes (and {\em not\/} the317`arbitrary' wishes of the author of the low-level device driver, as is318the case in the old scheme). The register $mc_flags$ is used to buffer319the information from $media_changed()$ to two separate queues. Other320data that is specific to a minor drive, can be accessed through $handle$,321which can point to a data structure specific to the low-level driver.322The fields $use_count$, $next$, $options$ and $mc_flags$ need not be323initialized.324325The intermediate software layer that \cdromc\ forms will perform some326additional bookkeeping. The use count of the device (the number of327processes that have the device opened) is registered in $use_count$. The328function $cdrom_ioctl()$ will verify the appropriate user-memory regions329for read and write, and in case a location on the CD is transferred,330it will `sanitize' the format by making requests to the low-level331drivers in a standard format, and translating all formats between the332user-software and low level drivers. This relieves much of the drivers'333memory checking and format checking and translation. Also, the necessary334structures will be declared on the program stack.335336The implementation of the functions should be as defined in the337following sections. Two functions {\em must\/} be implemented, namely338$open()$ and $release()$. Other functions may be omitted, their339corresponding capability flags will be cleared upon registration.340Generally, a function returns zero on success and negative on error. A341function call should return only after the command has completed, but of342course waiting for the device should not use processor time.343344\subsection{$Int\ open(struct\ cdrom_device_info * cdi, int\ purpose)$}345346$Open()$ should try to open the device for a specific $purpose$, which347can be either:348\begin{itemize}349\item[0] Open for reading data, as done by {\tt {mount()}} (2), or the350user commands {\tt {dd}} or {\tt {cat}}.351\item[1] Open for $ioctl$ commands, as done by audio-CD playing352programs.353\end{itemize}354Notice that any strategic code (closing tray upon $open()$, etc.)\ is355done by the calling routine in \cdromc, so the low-level routine356should only be concerned with proper initialization, such as spinning357up the disc, etc. % and device-use count358359360\subsection{$Void\ release(struct\ cdrom_device_info * cdi)$}361362363Device-specific actions should be taken such as spinning down the device.364However, strategic actions such as ejection of the tray, or unlocking365the door, should be left over to the general routine $cdrom_release()$.366This is the only function returning type $void$.367368\subsection{$Int\ drive_status(struct\ cdrom_device_info * cdi, int\ slot_nr)$}369\label{drive status}370371The function $drive_status$, if implemented, should provide372information on the status of the drive (not the status of the disc,373which may or may not be in the drive). If the drive is not a changer,374$slot_nr$ should be ignored. In \cdromh\ the possibilities are listed:375$$376\halign{$#$\ \hfil&$/*$ \rm# $*/$\hfil\cr377CDS_NO_INFO& no information available\cr378CDS_NO_DISC& no disc is inserted, tray is closed\cr379CDS_TRAY_OPEN& tray is opened\cr380CDS_DRIVE_NOT_READY& something is wrong, tray is moving?\cr381CDS_DISC_OK& a disc is loaded and everything is fine\cr382}383$$384385\subsection{$Int\ media_changed(struct\ cdrom_device_info * cdi, int\ disc_nr)$}386387This function is very similar to the original function in $struct\388file_operations$. It returns 1 if the medium of the device $cdi\to389dev$ has changed since the last call, and 0 otherwise. The parameter390$disc_nr$ identifies a specific slot in a juke-box, it should be391ignored for single-disc drives. Note that by `re-routing' this392function through $cdrom_media_changed()$, we can implement separate393queues for the VFS and a new $ioctl()$ function that can report device394changes to software (\eg, an auto-mounting daemon).395396\subsection{$Int\ tray_move(struct\ cdrom_device_info * cdi, int\ position)$}397398This function, if implemented, should control the tray movement. (No399other function should control this.) The parameter $position$ controls400the desired direction of movement:401\begin{itemize}402\item[0] Close tray403\item[1] Open tray404\end{itemize}405This function returns 0 upon success, and a non-zero value upon406error. Note that if the tray is already in the desired position, no407action need be taken, and the return value should be 0.408409\subsection{$Int\ lock_door(struct\ cdrom_device_info * cdi, int\ lock)$}410411This function (and no other code) controls locking of the door, if the412drive allows this. The value of $lock$ controls the desired locking413state:414\begin{itemize}415\item[0] Unlock door, manual opening is allowed416\item[1] Lock door, tray cannot be ejected manually417\end{itemize}418This function returns 0 upon success, and a non-zero value upon419error. Note that if the door is already in the requested state, no420action need be taken, and the return value should be 0.421422\subsection{$Int\ select_speed(struct\ cdrom_device_info * cdi, int\ speed)$}423424Some \cdrom\ drives are capable of changing their head-speed. There425are several reasons for changing the speed of a \cdrom\ drive. Badly426pressed \cdrom s may benefit from less-than-maximum head rate. Modern427\cdrom\ drives can obtain very high head rates (up to $24\times$ is428common). It has been reported that these drives can make reading429errors at these high speeds, reducing the speed can prevent data loss430in these circumstances. Finally, some of these drives can431make an annoyingly loud noise, which a lower speed may reduce. %Finally,432%although the audio-low-pass filters probably aren't designed for it,433%more than real-time playback of audio might be used for high-speed434%copying of audio tracks.435436This function specifies the speed at which data is read or audio is437played back. The value of $speed$ specifies the head-speed of the438drive, measured in units of standard cdrom speed (176\,kB/sec raw data439or 150\,kB/sec file system data). So to request that a \cdrom\ drive440operate at 300\,kB/sec you would call the CDROM_SELECT_SPEED $ioctl$441with $speed=2$. The special value `0' means `auto-selection', \ie,442maximum data-rate or real-time audio rate. If the drive doesn't have443this `auto-selection' capability, the decision should be made on the444current disc loaded and the return value should be positive. A negative445return value indicates an error.446447\subsection{$Int\ select_disc(struct\ cdrom_device_info * cdi, int\ number)$}448449If the drive can store multiple discs (a juke-box) this function450will perform disc selection. It should return the number of the451selected disc on success, a negative value on error. Currently, only452the ide-cd driver supports this functionality.453454\subsection{$Int\ get_last_session(struct\ cdrom_device_info * cdi, struct\455cdrom_multisession * ms_info)$}456457This function should implement the old corresponding $ioctl()$. For458device $cdi\to dev$, the start of the last session of the current disc459should be returned in the pointer argument $ms_info$. Note that460routines in \cdromc\ have sanitized this argument: its requested461format will {\em always\/} be of the type $CDROM_LBA$ (linear block462addressing mode), whatever the calling software requested. But463sanitization goes even further: the low-level implementation may464return the requested information in $CDROM_MSF$ format if it wishes so465(setting the $ms_info\rightarrow addr_format$ field appropriately, of466course) and the routines in \cdromc\ will make the transformation if467necessary. The return value is 0 upon success.468469\subsection{$Int\ get_mcn(struct\ cdrom_device_info * cdi, struct\470cdrom_mcn * mcn)$}471472Some discs carry a `Media Catalog Number' (MCN), also called473`Universal Product Code' (UPC). This number should reflect the number474that is generally found in the bar-code on the product. Unfortunately,475the few discs that carry such a number on the disc don't even use the476same format. The return argument to this function is a pointer to a477pre-declared memory region of type $struct\ cdrom_mcn$. The MCN is478expected as a 13-character string, terminated by a null-character.479480\subsection{$Int\ reset(struct\ cdrom_device_info * cdi)$}481482This call should perform a hard-reset on the drive (although in483circumstances that a hard-reset is necessary, a drive may very well not484listen to commands anymore). Preferably, control is returned to the485caller only after the drive has finished resetting. If the drive is no486longer listening, it may be wise for the underlying low-level cdrom487driver to time out.488489\subsection{$Int\ audio_ioctl(struct\ cdrom_device_info * cdi, unsigned\490int\ cmd, void * arg)$}491492Some of the \cdrom-$ioctl$s defined in \cdromh\ can be493implemented by the routines described above, and hence the function494$cdrom_ioctl$ will use those. However, most $ioctl$s deal with495audio-control. We have decided to leave these to be accessed through a496single function, repeating the arguments $cmd$ and $arg$. Note that497the latter is of type $void*{}$, rather than $unsigned\ long\498int$. The routine $cdrom_ioctl()$ does do some useful things,499though. It sanitizes the address format type to $CDROM_MSF$ (Minutes,500Seconds, Frames) for all audio calls. It also verifies the memory501location of $arg$, and reserves stack-memory for the argument. This502makes implementation of the $audio_ioctl()$ much simpler than in the503old driver scheme. For example, you may look up the function504$cm206_audio_ioctl()$ in {\tt {cm206.c}} that should be updated with505this documentation.506507An unimplemented ioctl should return $-ENOSYS$, but a harmless request508(\eg, $CDROMSTART$) may be ignored by returning 0 (success). Other509errors should be according to the standards, whatever they are. When510an error is returned by the low-level driver, the \UCD\ tries whenever511possible to return the error code to the calling program. (We may decide512to sanitize the return value in $cdrom_ioctl()$ though, in order to513guarantee a uniform interface to the audio-player software.)514515\subsection{$Int\ dev_ioctl(struct\ cdrom_device_info * cdi, unsigned\ int\516cmd, unsigned\ long\ arg)$}517518Some $ioctl$s seem to be specific to certain \cdrom\ drives. That is,519they are introduced to service some capabilities of certain drives. In520fact, there are 6 different $ioctl$s for reading data, either in some521particular kind of format, or audio data. Not many drives support522reading audio tracks as data, I believe this is because of protection523of copyrights of artists. Moreover, I think that if audio-tracks are524supported, it should be done through the VFS and not via $ioctl$s. A525problem here could be the fact that audio-frames are 2352 bytes long,526so either the audio-file-system should ask for 75264 bytes at once527(the least common multiple of 512 and 2352), or the drivers should528bend their backs to cope with this incoherence (to which I would be529opposed). Furthermore, it is very difficult for the hardware to find530the exact frame boundaries, since there are no synchronization headers531in audio frames. Once these issues are resolved, this code should be532standardized in \cdromc.533534Because there are so many $ioctl$s that seem to be introduced to535satisfy certain drivers,\footnote{Is there software around that536actually uses these? I'd be interested!} any `non-standard' $ioctl$s537are routed through the call $dev_ioctl()$. In principle, `private'538$ioctl$s should be numbered after the device's major number, and not539the general \cdrom\ $ioctl$ number, {\tt {0x53}}. Currently the540non-supported $ioctl$s are: {\it CDROMREADMODE1, CDROMREADMODE2,541CDROMREADAUDIO, CDROMREADRAW, CDROMREADCOOKED, CDROMSEEK,542CDROMPLAY\-BLK and CDROM\-READALL}.543544545\subsection{\cdrom\ capabilities}546\label{capability}547548Instead of just implementing some $ioctl$ calls, the interface in549\cdromc\ supplies the possibility to indicate the {\em capabilities\/}550of a \cdrom\ drive. This can be done by ORing any number of551capability-constants that are defined in \cdromh\ at the registration552phase. Currently, the capabilities are any of:553$$554\halign{$#$\ \hfil&$/*$ \rm# $*/$\hfil\cr555CDC_CLOSE_TRAY& can close tray by software control\cr556CDC_OPEN_TRAY& can open tray\cr557CDC_LOCK& can lock and unlock the door\cr558CDC_SELECT_SPEED& can select speed, in units of $\sim$150\,kB/s\cr559CDC_SELECT_DISC& drive is juke-box\cr560CDC_MULTI_SESSION& can read sessions $>\rm1$\cr561CDC_MCN& can read Media Catalog Number\cr562CDC_MEDIA_CHANGED& can report if disc has changed\cr563CDC_PLAY_AUDIO& can perform audio-functions (play, pause, etc)\cr564CDC_RESET& hard reset device\cr565CDC_IOCTLS& driver has non-standard ioctls\cr566CDC_DRIVE_STATUS& driver implements drive status\cr567}568$$569The capability flag is declared $const$, to prevent drivers from570accidentally tampering with the contents. The capability fags actually571inform \cdromc\ of what the driver can do. If the drive found572by the driver does not have the capability, is can be masked out by573the $cdrom_device_info$ variable $mask$. For instance, the SCSI \cdrom\574driver has implemented the code for loading and ejecting \cdrom's, and575hence its corresponding flags in $capability$ will be set. But a SCSI576\cdrom\ drive might be a caddy system, which can't load the tray, and577hence for this drive the $cdrom_device_info$ struct will have set578the $CDC_CLOSE_TRAY$ bit in $mask$.579580In the file \cdromc\ you will encounter many constructions of the type581$$\it582if\ (cdo\rightarrow capability \mathrel\& \mathord{\sim} cdi\rightarrow mask583\mathrel{\&} CDC_<capability>) \ldots584$$585There is no $ioctl$ to set the mask\dots The reason is that586I think it is better to control the {\em behavior\/} rather than the587{\em capabilities}.588589\subsection{Options}590591A final flag register controls the {\em behavior\/} of the \cdrom\592drives, in order to satisfy different users' wishes, hopefully593independently of the ideas of the respective author who happened to594have made the drive's support available to the \linux\ community. The595current behavior options are:596$$597\halign{$#$\ \hfil&$/*$ \rm# $*/$\hfil\cr598CDO_AUTO_CLOSE& try to close tray upon device $open()$\cr599CDO_AUTO_EJECT& try to open tray on last device $close()$\cr600CDO_USE_FFLAGS& use $file_pointer\rightarrow f_flags$ to indicate601purpose for $open()$\cr602CDO_LOCK& try to lock door if device is opened\cr603CDO_CHECK_TYPE& ensure disc type is data if opened for data\cr604}605$$606607The initial value of this register is $CDO_AUTO_CLOSE \mathrel|608CDO_USE_FFLAGS \mathrel| CDO_LOCK$, reflecting my own view on user609interface and software standards. Before you protest, there are two610new $ioctl$s implemented in \cdromc, that allow you to control the611behavior by software. These are:612$$613\halign{$#$\ \hfil&$/*$ \rm# $*/$\hfil\cr614CDROM_SET_OPTIONS& set options specified in $(int)\ arg$\cr615CDROM_CLEAR_OPTIONS& clear options specified in $(int)\ arg$\cr616}617$$618One option needs some more explanation: $CDO_USE_FFLAGS$. In the next619newsection we explain what the need for this option is.620621A software package {\tt setcd}, available from the Debian distribution622and {\tt sunsite.unc.edu}, allows user level control of these flags.623624\newsection{The need to know the purpose of opening the \cdrom\ device}625626Traditionally, Unix devices can be used in two different `modes',627either by reading/writing to the device file, or by issuing628controlling commands to the device, by the device's $ioctl()$629call. The problem with \cdrom\ drives, is that they can be used for630two entirely different purposes. One is to mount removable631file systems, \cdrom s, the other is to play audio CD's. Audio commands632are implemented entirely through $ioctl$s, presumably because the633first implementation (SUN?) has been such. In principle there is634nothing wrong with this, but a good control of the `CD player' demands635that the device can {\em always\/} be opened in order to give the636$ioctl$ commands, regardless of the state the drive is in.637638On the other hand, when used as a removable-media disc drive (what the639original purpose of \cdrom s is) we would like to make sure that the640disc drive is ready for operation upon opening the device. In the old641scheme, some \cdrom\ drivers don't do any integrity checking, resulting642in a number of i/o errors reported by the VFS to the kernel when an643attempt for mounting a \cdrom\ on an empty drive occurs. This is not a644particularly elegant way to find out that there is no \cdrom\ inserted;645it more-or-less looks like the old IBM-PC trying to read an empty floppy646drive for a couple of seconds, after which the system complains it647can't read from it. Nowadays we can {\em sense\/} the existence of a648removable medium in a drive, and we believe we should exploit that649fact. An integrity check on opening of the device, that verifies the650availability of a \cdrom\ and its correct type (data), would be651desirable.652653These two ways of using a \cdrom\ drive, principally for data and654secondarily for playing audio discs, have different demands for the655behavior of the $open()$ call. Audio use simply wants to open the656device in order to get a file handle which is needed for issuing657$ioctl$ commands, while data use wants to open for correct and658reliable data transfer. The only way user programs can indicate what659their {\em purpose\/} of opening the device is, is through the $flags$660parameter (see {\tt {open(2)}}). For \cdrom\ devices, these flags aren't661implemented (some drivers implement checking for write-related flags,662but this is not strictly necessary if the device file has correct663permission flags). Most option flags simply don't make sense to664\cdrom\ devices: $O_CREAT$, $O_NOCTTY$, $O_TRUNC$, $O_APPEND$, and665$O_SYNC$ have no meaning to a \cdrom.666667We therefore propose to use the flag $O_NONBLOCK$ to indicate668that the device is opened just for issuing $ioctl$669commands. Strictly, the meaning of $O_NONBLOCK$ is that opening and670subsequent calls to the device don't cause the calling process to671wait. We could interpret this as ``don't wait until someone has672inserted some valid data-\cdrom.'' Thus, our proposal of the673implementation for the $open()$ call for \cdrom s is:674\begin{itemize}675\item If no other flags are set than $O_RDONLY$, the device is opened676for data transfer, and the return value will be 0 only upon successful677initialization of the transfer. The call may even induce some actions678on the \cdrom, such as closing the tray.679\item If the option flag $O_NONBLOCK$ is set, opening will always be680successful, unless the whole device doesn't exist. The drive will take681no actions whatsoever.682\end{itemize}683684\subsection{And what about standards?}685686You might hesitate to accept this proposal as it comes from the687\linux\ community, and not from some standardizing institute. What688about SUN, SGI, HP and all those other Unix and hardware vendors?689Well, these companies are in the lucky position that they generally690control both the hardware and software of their supported products,691and are large enough to set their own standard. They do not have to692deal with a dozen or more different, competing hardware693configurations.\footnote{Incidentally, I think that SUN's approach to694mounting \cdrom s is very good in origin: under Solaris a695volume-daemon automatically mounts a newly inserted \cdrom\ under {\tt696{/cdrom/$<volume-name>$/}}. In my opinion they should have pushed this697further and have {\em every\/} \cdrom\ on the local area network be698mounted at the similar location, \ie, no matter in which particular699machine you insert a \cdrom, it will always appear at the same700position in the directory tree, on every system. When I wanted to701implement such a user-program for \linux, I came across the702differences in behavior of the various drivers, and the need for an703$ioctl$ informing about media changes.}704705We believe that using $O_NONBLOCK$ to indicate that a device is being opened706for $ioctl$ commands only can be easily introduced in the \linux\707community. All the CD-player authors will have to be informed, we can708even send in our own patches to the programs. The use of $O_NONBLOCK$709has most likely no influence on the behavior of the CD-players on710other operating systems than \linux. Finally, a user can always revert711to old behavior by a call to $ioctl(file_descriptor, CDROM_CLEAR_OPTIONS,712CDO_USE_FFLAGS)$.713714\subsection{The preferred strategy of $open()$}715716The routines in \cdromc\ are designed in such a way that run-time717configuration of the behavior of \cdrom\ devices (of {\em any\/} type)718can be carried out, by the $CDROM_SET/CLEAR_OPTIONS$ $ioctls$. Thus, various719modes of operation can be set:720\begin{description}721\item[$CDO_AUTO_CLOSE \mathrel| CDO_USE_FFLAGS \mathrel| CDO_LOCK$] This722is the default setting. (With $CDO_CHECK_TYPE$ it will be better, in the723future.) If the device is not yet opened by any other process, and if724the device is being opened for data ($O_NONBLOCK$ is not set) and the725tray is found to be open, an attempt to close the tray is made. Then,726it is verified that a disc is in the drive and, if $CDO_CHECK_TYPE$ is727set, that it contains tracks of type `data mode 1.' Only if all tests728are passed is the return value zero. The door is locked to prevent file729system corruption. If the drive is opened for audio ($O_NONBLOCK$ is730set), no actions are taken and a value of 0 will be returned.731\item[$CDO_AUTO_CLOSE \mathrel| CDO_AUTO_EJECT \mathrel| CDO_LOCK$] This732mimics the behavior of the current sbpcd-driver. The option flags are733ignored, the tray is closed on the first open, if necessary. Similarly,734the tray is opened on the last release, \ie, if a \cdrom\ is unmounted,735it is automatically ejected, such that the user can replace it.736\end{description}737We hope that these option can convince everybody (both driver738maintainers and user program developers) to adopt the new \cdrom\739driver scheme and option flag interpretation.740741\newsection{Description of routines in \cdromc}742743Only a few routines in \cdromc\ are exported to the drivers. In this744new section we will discuss these, as well as the functions that `take745over' the \cdrom\ interface to the kernel. The header file belonging746to \cdromc\ is called \cdromh. Formerly, some of the contents of this747file were placed in the file {\tt {ucdrom.h}}, but this file has now been748merged back into \cdromh.749750\subsection{$Struct\ file_operations\ cdrom_fops$}751752The contents of this structure were described in section~\ref{cdrom.c}.753A pointer to this structure is assigned to the $fops$ field754of the $struct gendisk$.755756\subsection{$Int\ register_cdrom( struct\ cdrom_device_info\ * cdi)$}757758This function is used in about the same way one registers $cdrom_fops$759with the kernel, the device operations and information structures,760as described in section~\ref{cdrom.c}, should be registered with the761\UCD:762$$763register_cdrom(\&<device>_info));764$$765This function returns zero upon success, and non-zero upon766failure. The structure $<device>_info$ should have a pointer to the767driver's $<device>_dops$, as in768$$769\vbox{\halign{&$#$\hfil\cr770struct\ &cdrom_device_info\ <device>_info = \{\cr771& <device>_dops;\cr772&\ldots\cr773\}\cr774}}$$775Note that a driver must have one static structure, $<device>_dops$, while776it may have as many structures $<device>_info$ as there are minor devices777active. $Register_cdrom()$ builds a linked list from these.778779\subsection{$Void\ unregister_cdrom(struct\ cdrom_device_info * cdi)$}780781Unregistering device $cdi$ with minor number $MINOR(cdi\to dev)$ removes782the minor device from the list. If it was the last registered minor for783the low-level driver, this disconnects the registered device-operation784routines from the \cdrom\ interface. This function returns zero upon785success, and non-zero upon failure.786787\subsection{$Int\ cdrom_open(struct\ inode * ip, struct\ file * fp)$}788789This function is not called directly by the low-level drivers, it is790listed in the standard $cdrom_fops$. If the VFS opens a file, this791function becomes active. A strategy is implemented in this routine,792taking care of all capabilities and options that are set in the793$cdrom_device_ops$ connected to the device. Then, the program flow is794transferred to the device_dependent $open()$ call.795796\subsection{$Void\ cdrom_release(struct\ inode *ip, struct\ file797*fp)$}798799This function implements the reverse-logic of $cdrom_open()$, and then800calls the device-dependent $release()$ routine. When the use-count has801reached 0, the allocated buffers are flushed by calls to $sync_dev(dev)$802and $invalidate_buffers(dev)$.803804805\subsection{$Int\ cdrom_ioctl(struct\ inode *ip, struct\ file *fp,806unsigned\ int\ cmd, unsigned\ long\ arg)$}807\label{cdrom-ioctl}808809This function handles all the standard $ioctl$ requests for \cdrom\810devices in a uniform way. The different calls fall into three811categories: $ioctl$s that can be directly implemented by device812operations, ones that are routed through the call $audio_ioctl()$, and813the remaining ones, that are presumable device-dependent. Generally, a814negative return value indicates an error.815816\subsubsection{Directly implemented $ioctl$s}817\label{ioctl-direct}818819The following `old' \cdrom-$ioctl$s are implemented by directly820calling device-operations in $cdrom_device_ops$, if implemented and821not masked:822\begin{description}823\item[CDROMMULTISESSION] Requests the last session on a \cdrom.824\item[CDROMEJECT] Open tray.825\item[CDROMCLOSETRAY] Close tray.826\item[CDROMEJECT_SW] If $arg\not=0$, set behavior to auto-close (close827tray on first open) and auto-eject (eject on last release), otherwise828set behavior to non-moving on $open()$ and $release()$ calls.829\item[CDROM_GET_MCN] Get the Media Catalog Number from a CD.830\end{description}831832\subsubsection{$Ioctl$s routed through $audio_ioctl()$}833\label{ioctl-audio}834835The following set of $ioctl$s are all implemented through a call to836the $cdrom_fops$ function $audio_ioctl()$. Memory checks and837allocation are performed in $cdrom_ioctl()$, and also sanitization of838address format ($CDROM_LBA$/$CDROM_MSF$) is done.839\begin{description}840\item[CDROMSUBCHNL] Get sub-channel data in argument $arg$ of type $struct\841cdrom_subchnl *{}$.842\item[CDROMREADTOCHDR] Read Table of Contents header, in $arg$ of type843$struct\ cdrom_tochdr *{}$.844\item[CDROMREADTOCENTRY] Read a Table of Contents entry in $arg$ and845specified by $arg$ of type $struct\ cdrom_tocentry *{}$.846\item[CDROMPLAYMSF] Play audio fragment specified in Minute, Second,847Frame format, delimited by $arg$ of type $struct\ cdrom_msf *{}$.848\item[CDROMPLAYTRKIND] Play audio fragment in track-index format849delimited by $arg$ of type $struct\ \penalty-1000 cdrom_ti *{}$.850\item[CDROMVOLCTRL] Set volume specified by $arg$ of type $struct\851cdrom_volctrl *{}$.852\item[CDROMVOLREAD] Read volume into by $arg$ of type $struct\853cdrom_volctrl *{}$.854\item[CDROMSTART] Spin up disc.855\item[CDROMSTOP] Stop playback of audio fragment.856\item[CDROMPAUSE] Pause playback of audio fragment.857\item[CDROMRESUME] Resume playing.858\end{description}859860\subsubsection{New $ioctl$s in \cdromc}861862The following $ioctl$s have been introduced to allow user programs to863control the behavior of individual \cdrom\ devices. New $ioctl$864commands can be identified by the underscores in their names.865\begin{description}866\item[CDROM_SET_OPTIONS] Set options specified by $arg$. Returns the867option flag register after modification. Use $arg = \rm0$ for reading868the current flags.869\item[CDROM_CLEAR_OPTIONS] Clear options specified by $arg$. Returns870the option flag register after modification.871\item[CDROM_SELECT_SPEED] Select head-rate speed of disc specified as872by $arg$ in units of standard cdrom speed (176\,kB/sec raw data or873150\,kB/sec file system data). The value 0 means `auto-select', \ie,874play audio discs at real time and data discs at maximum speed. The value875$arg$ is checked against the maximum head rate of the drive found in the876$cdrom_dops$.877\item[CDROM_SELECT_DISC] Select disc numbered $arg$ from a juke-box.878First disc is numbered 0. The number $arg$ is checked against the879maximum number of discs in the juke-box found in the $cdrom_dops$.880\item[CDROM_MEDIA_CHANGED] Returns 1 if a disc has been changed since881the last call. Note that calls to $cdrom_media_changed$ by the VFS882are treated by an independent queue, so both mechanisms will detect883a media change once. For juke-boxes, an extra argument $arg$884specifies the slot for which the information is given. The special885value $CDSL_CURRENT$ requests that information about the currently886selected slot be returned.887\item[CDROM_DRIVE_STATUS] Returns the status of the drive by a call to888$drive_status()$. Return values are defined in section~\ref{drive889status}. Note that this call doesn't return information on the890current playing activity of the drive; this can be polled through an891$ioctl$ call to $CDROMSUBCHNL$. For juke-boxes, an extra argument892$arg$ specifies the slot for which (possibly limited) information is893given. The special value $CDSL_CURRENT$ requests that information894about the currently selected slot be returned.895\item[CDROM_DISC_STATUS] Returns the type of the disc currently in the896drive. It should be viewed as a complement to $CDROM_DRIVE_STATUS$.897This $ioctl$ can provide \emph {some} information about the current898disc that is inserted in the drive. This functionality used to be899implemented in the low level drivers, but is now carried out900entirely in \UCD.901902The history of development of the CD's use as a carrier medium for903various digital information has lead to many different disc types.904This $ioctl$ is useful only in the case that CDs have \emph {only905one} type of data on them. While this is often the case, it is906also very common for CDs to have some tracks with data, and some907tracks with audio. Because this is an existing interface, rather908than fixing this interface by changing the assumptions it was made909under, thereby breaking all user applications that use this910function, the \UCD\ implements this $ioctl$ as follows: If the CD in911question has audio tracks on it, and it has absolutely no CD-I, XA,912or data tracks on it, it will be reported as $CDS_AUDIO$. If it has913both audio and data tracks, it will return $CDS_MIXED$. If there914are no audio tracks on the disc, and if the CD in question has any915CD-I tracks on it, it will be reported as $CDS_XA_2_2$. Failing916that, if the CD in question has any XA tracks on it, it will be917reported as $CDS_XA_2_1$. Finally, if the CD in question has any918data tracks on it, it will be reported as a data CD ($CDS_DATA_1$).919920This $ioctl$ can return:921$$922\halign{$#$\ \hfil&$/*$ \rm# $*/$\hfil\cr923CDS_NO_INFO& no information available\cr924CDS_NO_DISC& no disc is inserted, or tray is opened\cr925CDS_AUDIO& Audio disc (2352 audio bytes/frame)\cr926CDS_DATA_1& data disc, mode 1 (2048 user bytes/frame)\cr927CDS_XA_2_1& mixed data (XA), mode 2, form 1 (2048 user bytes)\cr928CDS_XA_2_2& mixed data (XA), mode 2, form 1 (2324 user bytes)\cr929CDS_MIXED& mixed audio/data disc\cr930}931$$932For some information concerning frame layout of the various disc933types, see a recent version of \cdromh.934935\item[CDROM_CHANGER_NSLOTS] Returns the number of slots in a936juke-box.937\item[CDROMRESET] Reset the drive.938\item[CDROM_GET_CAPABILITY] Returns the $capability$ flags for the939drive. Refer to section \ref{capability} for more information on940these flags.941\item[CDROM_LOCKDOOR] Locks the door of the drive. $arg == \rm0$942unlocks the door, any other value locks it.943\item[CDROM_DEBUG] Turns on debugging info. Only root is allowed944to do this. Same semantics as CDROM_LOCKDOOR.945\end{description}946947\subsubsection{Device dependent $ioctl$s}948949Finally, all other $ioctl$s are passed to the function $dev_ioctl()$,950if implemented. No memory allocation or verification is carried out.951952\newsection{How to update your driver}953954\begin{enumerate}955\item Make a backup of your current driver.956\item Get hold of the files \cdromc\ and \cdromh, they should be in957the directory tree that came with this documentation.958\item Make sure you include \cdromh.959\item Change the 3rd argument of $register_blkdev$ from960$\&<your-drive>_fops$ to $\&cdrom_fops$.961\item Just after that line, add the following to register with the \UCD:962$$register_cdrom(\&<your-drive>_info);$$963Similarly, add a call to $unregister_cdrom()$ at the appropriate place.964\item Copy an example of the device-operations $struct$ to your965source, \eg, from {\tt {cm206.c}} $cm206_dops$, and change all966entries to names corresponding to your driver, or names you just967happen to like. If your driver doesn't support a certain function,968make the entry $NULL$. At the entry $capability$ you should list all969capabilities your driver currently supports. If your driver970has a capability that is not listed, please send me a message.971\item Copy the $cdrom_device_info$ declaration from the same example972driver, and modify the entries according to your needs. If your973driver dynamically determines the capabilities of the hardware, this974structure should also be declared dynamically.975\item Implement all functions in your $<device>_dops$ structure,976according to prototypes listed in \cdromh, and specifications given977in section~\ref{cdrom.c}. Most likely you have already implemented978the code in a large part, and you will almost certainly need to adapt the979prototype and return values.980\item Rename your $<device>_ioctl()$ function to $audio_ioctl$ and981change the prototype a little. Remove entries listed in the first982part in section~\ref{cdrom-ioctl}, if your code was OK, these are983just calls to the routines you adapted in the previous step.984\item You may remove all remaining memory checking code in the985$audio_ioctl()$ function that deals with audio commands (these are986listed in the second part of section~\ref{cdrom-ioctl}). There is no987need for memory allocation either, so most $case$s in the $switch$988statement look similar to:989$$990case\ CDROMREADTOCENTRY\colon get_toc_entry\bigl((struct\991cdrom_tocentry *{})\ arg\bigr);992$$993\item All remaining $ioctl$ cases must be moved to a separate994function, $<device>_ioctl$, the device-dependent $ioctl$s. Note that995memory checking and allocation must be kept in this code!996\item Change the prototypes of $<device>_open()$ and997$<device>_release()$, and remove any strategic code (\ie, tray998movement, door locking, etc.).999\item Try to recompile the drivers. We advise you to use modules, both1000for {\tt {cdrom.o}} and your driver, as debugging is much easier this1001way.1002\end{enumerate}10031004\newsection{Thanks}10051006Thanks to all the people involved. First, Erik Andersen, who has1007taken over the torch in maintaining \cdromc\ and integrating much1008\cdrom-related code in the 2.1-kernel. Thanks to Scott Snyder and1009Gerd Knorr, who were the first to implement this interface for SCSI1010and IDE-CD drivers and added many ideas for extension of the data1011structures relative to kernel~2.0. Further thanks to Heiko Ei{\sz}feldt,1012Thomas Quinot, Jon Tombs, Ken Pizzini, Eberhard M\"onkeberg and Andrew1013Kroll, the \linux\ \cdrom\ device driver developers who were kind1014enough to give suggestions and criticisms during the writing. Finally1015of course, I want to thank Linus Torvalds for making this possible in1016the first place.10171018\vfill1019$ \version\ $1020\eject1021\end{document}102210231024