1<?xml version="1.0" encoding="ISO-8859-1"?>
2<!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.3//EN"
3 "http://www.oasis-open.org/docbook/xml/4.3/docbookx.dtd" [
4 <!ENTITY % xorg-defs SYSTEM "defs.ent"> %xorg-defs;
5 <!ENTITY % defs SYSTEM "/xserver/doc/xml/xserver.ent"> %defs;
6 <!-- config file keyword markup -->
7 <!-- specific config file keywords -->
8 <!ENTITY k.device "<emphasis>Device</emphasis>">
9 <!ENTITY k.monitor "<emphasis>Monitor</emphasis>">
10 <!ENTITY k.display "<emphasis>Display</emphasis>">
11 <!ENTITY k.inputdevice "<emphasis>InputDevice</emphasis>">
12 <!ENTITY k.screen "<emphasis>Screen</emphasis>">
13 <!ENTITY k.serverlayout "<emphasis>ServerLayout</emphasis>">
14 <!ENTITY k.driver "<emphasis>Driver</emphasis>">
15 <!ENTITY k.module "<emphasis>Module</emphasis>">
16 <!ENTITY k.identifier "<emphasis>Identifier</emphasis>">
17 <!ENTITY k.serverflags "<emphasis>ServerFlags</emphasis>">
18] >
19
20<article id="ddxDesign">
21  <articleinfo>
22
23    <title>XFree86 DDX Design</title>
24
25    <authorgroup>
26      <corpauthor>The XFree86 Project</corpauthor>
27      <corpauthor>The X.Org Foundation</corpauthor>
28
29      <othercredit>
30	<firstname>Jim</firstname><surname>Gettys</surname>
31	<contrib>Updates for X11R6.7</contrib>
32      </othercredit>
33    </authorgroup>
34
35    <pubdate>&xserver.reldate;</pubdate>
36    <releaseinfo>X Version 11, Release &fullrelvers;</releaseinfo>
37    <releaseinfo>X Server Version &xserver.version;</releaseinfo>
38
39  </articleinfo>
40
41  <note><para>
42This document describes software undergoing continual evolution, and
43the interfaces described here are subject to change without notice.
44This document is intended to cover the interfaces as found in the
45xorg-server-&xserver.version; release, but is probably not completely
46in sync with the code base.
47    </para></note>
48
49
50  <sect1>
51    <title>Preface</title>
52
53    <para>
54This document was originally the design spec for the DDX layer of the
55XFree86 4.0 X server.  The X.Org Foundation adopted the XFree86 4.4rc2
56version of that server as the basis of the Xorg server project, and has
57evolved the XFree86 DDX layer greatly since forking.   This document thus
58covers only the current implementation of the XFree86 DDX as found in the
59Xorg server &xserver.version; release, and no longer matches the XFree86
60server itself.
61      </para>
62
63    <para>
64The XFree86 Project's broad design principles for XFree86 4.0 were:
65      <itemizedlist>
66	<listitem><para>keep it reasonable
67	    <itemizedlist>
68	      <listitem><para>We cannot rewrite the complete server
69		</para></listitem>
70	      <listitem><para>We don't want to re-invent the wheel
71		</para></listitem>
72	    </itemizedlist></para></listitem>
73	<listitem><para>keep it modular
74	    <itemizedlist>
75	      <listitem><para>As many things as possible should go into modules
76		</para></listitem>
77	      <listitem><para>The basic loader binary should be minimal
78		</para></listitem>
79	      <listitem><para>A clean design with well defined layering is
80		  important</para></listitem>
81	      <listitem><para>DDX specific global variables are a nono
82		</para></listitem>
83	      <listitem><para>The structure should be flexible enough to allow
84		  future extensions</para></listitem>
85	      <listitem><para>The structure should minimize duplication of
86		  common code</para></listitem>
87	    </itemizedlist></para></listitem>
88	<listitem><para>keep important features in mind
89	    <itemizedlist>
90	      <listitem><para>multiple screens, including multiple instances
91		  of drivers</para></listitem>
92	      <listitem><para>mixing different color depths and visuals on
93		  different and ideally even on the same screen
94		</para></listitem>
95	      <listitem><para>better control of the PCI device used
96		</para></listitem>
97	      <listitem><para>better config file parser</para></listitem>
98	      <listitem><para>get rid of all VGA compatibility assumptions
99		</para></listitem>
100	    </itemizedlist></para></listitem>
101      </itemizedlist>
102    </para>
103
104    <para>
105While the XFree86 project had a goal of avoiding changes to the DIX
106layer unless they found major deficiencies there, to avoid divergence from
107the X.Org sample implementation they were integrating changes from, the
108X.Org developers now maintain both sides, and make changes where they are
109most appropriate.   This document concentrates on the XFree86 DDX layer used
110in the Xorg server itself (the code found in <filename>hw/xfree86</filename>
111in the source tree), and developers will also want to refer to the
112<filename>Xserver-spec</filename> documentation that covers the DIX layer
113routines common to all the X servers in the sample implementation.
114    </para>
115  </sect1>
116
117  <sect1>
118    <title>The xorg.conf File</title>
119
120    <para>
121The xorg.conf file format is based on the XF86Config format from XFree86 4.4,
122which is in turn similar to the old XFree86 3.x XF86Config format, with the
123following changes:
124    </para>
125
126    <sect2>
127      <title>&k.device; section</title>
128
129      <para>
130    The &k.device; sections are similar to what they used to be, and
131    describe hardware-specific information for a single video card.
132    &k.device;
133    Some new keywords are added:
134
135
136	<variablelist>
137	  <varlistentry><term>Driver "drivername"</term>
138	    <listitem><para>
139        Specifies the name of the driver to be used for the card.  This
140        is mandatory.
141	      </para></listitem></varlistentry>
142	  <varlistentry><term>BusID "busslot"</term>
143	    <listitem><para>
144        Specifies uniquely the location of the card on the bus.  The
145        purpose is to identify particular cards in a multi-headed
146        configuration.  The format of the argument is intentionally
147        vague, and may be architecture dependent.  For a PCI bus, it
148        is something like "bus@domain:slot:func". The "@domain" part
149        can be left out for domain 0.
150	      </para></listitem></varlistentry>
151	</variablelist>
152      </para>
153
154      <para>
155    A &k.device; section is considered <quote>active</quote> if there is a reference
156    to it in an active &k.screen; section.
157      </para>
158    </sect2>
159
160    <sect2>
161      <title>&k.screen; section</title>
162
163      <para>
164    The &k.screen; sections are similar to what they used to be.  They
165    no longer have a &k.driver; keyword, but an &k.identifier; keyword
166    is added.  (The &k.driver; keyword may be accepted in place of the
167    &k.identifier; keyword for compatibility purposes.)  The identifier
168    can be used to identify which screen is to be active when multiple
169    &k.screen; sections are present.  It is possible to specify the active
170    screen from the command line.  A default is chosen in the absence
171    of one being specified.  A &k.screen; section is considered <quote>active</quote>
172    if there is a reference to it either from the command line, or from
173    an active &k.serverlayout; section.
174      </para>
175    </sect2>
176
177    <sect2>
178      <title>&k.inputdevice; section</title>
179
180      <para>
181    The &k.inputdevice; section is a new section that describes
182    configuration information for input devices.  It replaces the old
183    <emphasis>Keyboard</emphasis>, <emphasis>Pointer</emphasis> and <emphasis>XInput</emphasis>
184    sections.  Like the &k.device; section, it has two mandatory keywords:
185    &k.identifier; and &k.driver;.  For compatibility purposes the old
186    <emphasis>Keyboard</emphasis> and <emphasis>Pointer</emphasis> sections are
187    converted by the parser into &k.inputdevice; sections as follows:
188
189	<variablelist>
190	  <varlistentry><term><emphasis>Keyboard</emphasis></term>
191	    <listitem><literallayout>
192             &k.identifier; "Implicit Core Keyboard"
193             &k.driver; "kbd"
194	      </literallayout></listitem></varlistentry>
195	  <varlistentry><term><emphasis>Pointer</emphasis></term>
196	    <listitem><literallayout>
197             &k.identifier; "Implicit Core Pointer"
198             &k.driver; "mouse"
199	      </literallayout></listitem></varlistentry>
200	</variablelist>
201      </para>
202
203      <para>
204    An &k.inputdevice; section is considered active if there is a
205    reference to it in an active &k.serverlayout; section.  An
206    &k.inputdevice; section may also be referenced implicitly if there
207    is no &k.serverlayout; section, if the <option>-screen</option> command
208    line options is used, or if the &k.serverlayout; section doesn't
209    reference any &k.inputdevice; sections.  In this case, the first
210    sections with drivers "kbd" and "mouse" are used as the core
211    keyboard and pointer respectively.
212      </para>
213    </sect2>
214
215    <sect2>
216      <title>&k.serverlayout; section</title>
217
218      <para>
219    The &k.serverlayout; section is a new section that is used to identify
220    which &k.screen; sections are to be used in a multi-headed configuration,
221    and the relative layout of those screens.  It also identifies which
222    &k.inputdevice; sections are to be used.  Each &k.serverlayout; section
223    has an identifier, a list of &k.screen; section identifiers, and a list of
224    &k.inputdevice; section identifiers.  &k.serverflags; options may also be
225    included in a &k.serverlayout; section, making it possible to override
226    the global values in the &k.serverflags; section.
227      </para>
228
229      <para>
230    A &k.serverlayout; section can be made active by being referenced on
231    the command line.  In the absence of this, a default will be chosen
232    (the first one found).  The screen names may optionally be followed
233    by a number specifying the preferred screen number, and optionally
234    by information specifying the physical positioning of the screen,
235    either in absolute terms or relative to another screen (or screens).
236    When no screen number is specified, they are numbered according to
237    the order in which they are listed.  The old (now obsolete) method
238    of providing the positioning information is to give the names of
239    the four adjacent screens.  The order of these is top, bottom, left,
240    right.  Here is an example of a &k.serverlayout; section for two
241    screens using the old method, with the second located to the right
242    of the first:
243
244	<programlisting>
245      Section "ServerLayout"
246        Identifier "Main Layout"
247        Screen     0 "Screen 1" ""  ""  ""  "Screen 2"
248        Screen     1 "Screen 2"
249        Screen     "Screen 3"
250      EndSection
251	</programlisting>
252      </para>
253
254      <para>
255    The preferred way of specifying the layout is to explicitly specify
256    the screen's location in absolute terms or relative to another
257    screen.
258      </para>
259
260      <para>
261    In the absolute case, the upper left corner's coordinates are given
262    after the <emphasis>Absolute</emphasis> keyword.  If the coordinates are
263    omitted, a value of <code>(0,0)</code> is assumed.  An example
264    of absolute positioning follows:
265
266	<programlisting>
267      Section "ServerLayout"
268        Identifier "Main Layout"
269        Screen     0 "Screen 1" Absolute 0 0
270        Screen     1 "Screen 2" Absolute 1024 0
271        Screen     "Screen 3" Absolute 2048 0
272      EndSection
273	</programlisting>
274      </para>
275
276      <para>
277    In the relative case, the position is specified by either using one of
278    the following keywords followed by the name of the reference screen:
279
280	<simplelist type='vert' columns='1'>
281	  <member><emphasis>RightOf</emphasis></member>
282	  <member><emphasis>LeftOf</emphasis></member>
283	  <member><emphasis>Above</emphasis></member>
284	  <member><emphasis>Below</emphasis></member>
285	  <member><emphasis>Relative</emphasis></member>
286	</simplelist>
287      </para>
288
289      <para>
290    When the <emphasis>Relative</emphasis> keyword is used, the reference screen
291    name is followed by the coordinates of the new screen's origin
292    relative to reference screen.  The following example shows how to use
293    some of the relative positioning options.
294
295	<programlisting>
296      Section "ServerLayout"
297        Identifier "Main Layout"
298        Screen     0 "Screen 1"
299        Screen     1 "Screen 2" RightOf "Screen 1"
300        Screen     "Screen 3" Relative "Screen 1" 2048 0
301      EndSection
302	</programlisting>
303      </para>
304    </sect2>
305
306    <sect2>
307      <title>Options</title>
308
309      <para>
310    Options are used more extensively.  They may appear in most sections
311    now.  Options related to drivers can be present in the &k.screen;,
312    &k.device; and &k.monitor; sections and the &k.display; subsections.
313    The order of precedence is &k.display;, &k.screen;, &k.monitor;,
314    &k.device;.  Options have been extended to allow an optional value
315    to be specified in addition to the option name.  For more details
316    about options, see the <link linkend="options">Options</link> section
317    for details.
318      </para>
319    </sect2>
320  </sect1>
321
322  <sect1>
323    <title>Driver Interface</title>
324
325    <para>
326The driver interface consists of a minimal set of entry points that are
327required based on the external events that the driver must react to.
328No non-essential structure is imposed on the way they are used beyond
329that.  This is a significant difference compared with the old design.
330    </para>
331
332    <para>
333The entry points for drawing operations are already taken care of by
334the framebuffer code.  Extensions and enhancements to framebuffer code
335are outside the scope of this document.
336    </para>
337
338    <para>
339This approach to the driver interface provides good flexibility, but does
340increase the complexity of drivers.  To help address this, the XFree86
341common layer provides a set of <quote>helper</quote> functions to take care of things
342that most drivers need.  These helpers help minimise the amount of code
343duplication between drivers.  The use of helper functions by drivers is
344however optional, though encouraged.  The basic philosophy behind the
345helper functions is that they should be useful to many drivers, that
346they should balance this against the complexity of their interface.  It
347is inevitable that some drivers may find some helpers unsuitable and
348need to provide their own code.
349    </para>
350
351    <para>
352Events that a driver needs to react to are:
353
354      <variablelist>
355	<varlistentry><term>ScreenInit</term>
356
357	  <listitem><para>
358     An initialisation function is called from the DIX layer for each
359     screen at the start of each server generation.
360	    </para></listitem></varlistentry>
361
362	<varlistentry><term>Enter VT</term>
363
364	  <listitem><para>
365     The server takes control of the console.
366	    </para></listitem></varlistentry>
367
368	<varlistentry><term>Leave VT</term>
369
370	  <listitem><para>
371     The server releases control of the console.
372	    </para></listitem></varlistentry>
373
374	<varlistentry><term>Mode Switch</term>
375
376	  <listitem><para>
377     Change video mode.
378	    </para></listitem></varlistentry>
379
380	<varlistentry><term>ViewPort change</term>
381
382	  <listitem><para>
383     Change the origin of the physical view port.
384	    </para></listitem></varlistentry>
385
386	<varlistentry><term>ScreenSaver state change</term>
387
388	  <listitem><para>
389     Screen saver activation/deactivation.
390	    </para></listitem></varlistentry>
391
392	<varlistentry><term>CloseScreen</term>
393
394	  <listitem><para>
395     A close screen function is called from the DIX layer for each screen
396     at the end of each server generation.
397	    </para></listitem></varlistentry>
398      </variablelist>
399    </para>
400
401
402    <para>
403In addition to these events, the following functions are required by
404the XFree86 common layer:
405
406      <variablelist>
407	<varlistentry><term>Identify</term>
408
409	  <listitem><para>
410     Print a driver identifying message.
411	    </para></listitem></varlistentry>
412
413	<varlistentry><term>Probe</term>
414
415	  <listitem><para>
416     This is how a driver identifies if there is any hardware present that
417     it knows how to drive.
418	    </para></listitem></varlistentry>
419
420	<varlistentry><term>PreInit</term>
421
422	  <listitem><para>
423     Process information from the xorg.conf file, determine the
424     full characteristics of the hardware, and determine if a valid
425     configuration is present.
426	    </para></listitem></varlistentry>
427      </variablelist>
428    </para>
429
430    <para>
431The VidMode extension also requires:
432
433      <variablelist>
434	<varlistentry><term>ValidMode</term>
435
436	  <listitem><para>
437     Identify if a new mode is usable with the current configuration.
438     The PreInit function (and/or helpers it calls) may also make use
439     of the ValidMode function or something similar.
440	    </para></listitem></varlistentry>
441      </variablelist>
442    </para>
443
444
445    <para>
446Other extensions may require other entry points.  The drivers will
447inform the common layer of these in such cases.
448    </para>
449  </sect1>
450
451  <sect1>
452    <title>Resource Access Control Introduction</title>
453
454    <para>
455Graphics devices are accessed through ranges in I/O or memory space.
456While most modern graphics devices allow relocation of such ranges many
457of them still require the use of well established interfaces such as
458VGA memory and IO ranges or 8514/A IO ranges.  With modern buses (like
459PCI) it is possible for multiple video devices to share access to these
460resources.  The RAC (Resource Access Control) subsystem provides a
461mechanism for this.
462    </para>
463
464    <sect2>
465      <title>Terms and Definitions</title>
466
467      <sect3>
468	<title>Bus</title>
469
470	<para>
471    <quote>Bus</quote> is ambiguous as it is used for different things: it may refer
472    to physical incompatible extension connectors in a computer system.
473    The RAC system knows two such systems: The ISA bus and the PCI bus.
474    (On the software level EISA, MCA and VL buses are currently treated
475    like ISA buses).  <quote>Bus</quote> may also refer to logically different
476    entities on a single bus system which are connected via bridges.  A
477    PCI system may have several distinct PCI buses connecting each other
478    by PCI-PCI bridges or to the host CPU by HOST-PCI bridges.
479	</para>
480
481	<para>
482    Systems that host more than one bus system link these together using
483    bridges.  Bridges are a concern to RAC as they might block or pass
484    specific resources.  PCI-PCI bridges may be set up to pass VGA
485    resources to the secondary bus.  PCI-ISA buses pass any resources not
486    decoded on the primary PCI bus to the ISA bus.  This way VGA resources
487    (although exclusive on the ISA bus) can be shared by ISA and PCI
488    cards.  Currently HOST-PCI bridges are not yet handled by RAC as they
489    require specific drivers.
490	</para>
491      </sect3>
492
493      <sect3>
494	<title>Entity</title>
495
496	<para>
497    The smallest independently addressable unit on a system bus is
498    referred to as an entity.  So far we know ISA and PCI entities.  PCI
499    entities can be located on the PCI bus by an unique ID consisting of
500    the bus, card and function number.
501	</para>
502      </sect3>
503
504      <sect3>
505	<title>Resource</title>
506
507	<para>
508    <quote>Resource</quote> refers to a range of memory or I/O addresses an entity
509    can decode.
510	</para>
511
512	<para>
513    If a device is capable of disabling this decoding the resource is
514    called shareable.  For PCI devices a generic method is provided to
515    control resource decoding.  Other devices will have to provide a
516    device specific function to control decoding.
517	</para>
518
519	<para>
520    If the entity is capable of decoding this range at a different
521    location this resource is considered relocatable.
522	</para>
523
524	<para>
525    Resources which start at a specific address and occupy a single
526    continuous range are called block resources.
527	</para>
528
529	<para>
530    Alternatively resource addresses can be decoded in a way that they
531    satisfy the conditions:
532	  <programlisting>
533                    address &amp; mask == base
534	  </programlisting>
535    and
536	  <programlisting>
537                       base &amp; mask == base
538	  </programlisting>
539    Resources addressed in such a way are called sparse resources.
540	</para>
541
542      </sect3>
543
544      <sect3>
545	<title>Server States</title>
546
547	<para>
548    The resource access control system knows two server states: the
549    SETUP and the OPERATING state.  The SETUP state is entered whenever
550    a mode change takes place or the server exits or does VT switching.
551    During this state all entity resources are under resource access
552    control.  During OPERATING state only those entities are controlled
553    which actually have shared resources that conflict with others.
554	</para>
555      </sect3>
556    </sect2>
557  </sect1>
558
559  <sect1>
560    <title>Control Flow in the Server and Mandatory Driver Functions</title>
561
562    <para>
563At the start of each server generation, <function>main()</function>
564(<filename>dix/main.c</filename>) calls the DDX function
565<function>InitOutput()</function>.  This is the first place that the DDX gets
566control.  <function>InitOutput()</function> is expected to fill in the global
567<structname>screenInfo</structname> struct, and one
568<structfield>screenInfo.screen[]</structfield> entry for each screen present.
569Here is what <function>InitOutput()</function> does:
570    </para>
571
572    <sect2>
573      <title>Parse the xorg.conf file</title>
574
575      <para>
576    This is done at the start of the first server generation only.
577      </para>
578
579      <para>
580    The xorg.conf file is read in full, and the resulting information
581    stored in data structures.  None of the parsed information is
582    processed at this point.  The parser data structures are opaque to
583    the video drivers and to most of the common layer code.
584      </para>
585
586      <para>
587    The entire file is parsed first to remove any section ordering
588    requirements.
589      </para>
590    </sect2>
591
592
593    <sect2>
594      <title>Initial processing of parsed information and command line options
595      </title>
596
597      <para>
598    This is done at the start of the first server generation only.
599      </para>
600
601      <para>
602    The initial processing is to determine paths like the
603    <emphasis>ModulePath</emphasis>, etc, and to determine which &k.serverlayout;,
604    &k.screen; and &k.device; sections are active.
605      </para>
606    </sect2>
607
608
609    <sect2>
610      <title>Enable port I/O access</title>
611
612      <para>
613    Port I/O access is controlled from the XFree86 common layer, and is
614    <quote>all or nothing</quote>.  It is enabled prior to calling driver probes, at
615    the start of subsequent server generations, and when VT switching
616    back to the Xserver.  It is disabled at the end of server generations,
617    and when VT switching away from the Xserver.
618      </para>
619
620      <para>
621    The implementation details of this may vary on different platforms.
622      </para>
623    </sect2>
624
625
626    <sect2>
627      <title>General bus probe</title>
628
629      <para>
630    This is done at the start of the first server generation only.
631      </para>
632
633      <para>
634    In the case of ix86 machines, this will be a general PCI probe.
635    The full information obtained here will be available to the drivers.
636    This information persists for the life of the Xserver.  In the PCI
637    case, the PCI information for all video cards found is available by
638    calling <function>xf86GetPciVideoInfo()</function>.
639      </para>
640
641      <blockquote><para>
642	  <programlisting>
643    pciVideoPtr *xf86GetPciVideoInfo(void);
644	  </programlisting>
645	  <blockquote><para>
646	returns a pointer to a list of pointers to
647	<structname>pciVideoRec</structname> entries, of which there is one for
648	each detected PCI video card.  The list is terminated with a
649	<constant>NULL</constant> pointer.  If no PCI video cards were
650	detected, the return value is <constant>NULL</constant>.
651
652	    </para></blockquote>
653	</para></blockquote>
654
655      <para>
656    After the bus probe, the resource broker is initialised.
657      </para>
658    </sect2>
659
660
661    <sect2>
662      <title>Load initial set of modules</title>
663
664      <para>
665    This is done at the start of the first server generation only.
666      </para>
667
668      <para>
669    The next set of modules loaded are those specified explicitly in the
670    &k.module; section of the config file.
671      </para>
672
673      <para>
674    The final set of initial modules are the driver modules referenced
675    by the active &k.device; and &k.inputdevice; sections in the config
676    file.  Each of these modules is loaded exactly once.
677      </para>
678    </sect2>
679
680
681    <sect2>
682      <title>Register Video and Input Drivers</title>
683
684      <para>
685    This is done at the start of the first server generation only.
686      </para>
687
688      <para>
689    When a driver module is loaded, the loader calls its
690    <function>Setup</function> function.  For video drivers, this function
691    calls <function>xf86AddDriver()</function> to register the driver's
692    <structname>DriverRec</structname>, which contains a small set of essential
693    details and driver entry points required during the early phase of
694    <function>InitOutput()</function>.  <function>xf86AddDriver()</function>
695    adds it to the global <varname>xf86DriverList[]</varname> array.
696      </para>
697
698      <para>
699    The <structname>DriverRec</structname> contains the driver canonical name,
700    the <function>Identify()</function>,
701    <function>Probe()</function> and <function>AvailableOptions()</function>
702    function entry points as well as a pointer
703    to the driver's module (as returned from the loader when the driver
704    was loaded) and a reference count which keeps track of how many
705    screens are using the driver.  The entry driver entry points are
706    those required prior to the driver allocating and filling in its
707    <structname>ScrnInfoRec</structname>.
708      </para>
709
710      <para>
711    For a static server, the <varname>xf86DriverList[]</varname> array is
712    initialised at build time, and the loading of modules is not done.
713      </para>
714
715      <para>
716    A similar procedure is used for input drivers.  The input driver's
717    <function>Setup</function> function calls
718    <function>xf86AddInputDriver()</function> to register the driver's
719    <structname>InputDriverRec</structname>, which contains a small set of
720    essential details and driver entry points required during the early
721    phase of <function>InitInput()</function>.
722    <function>xf86AddInputDriver()</function> adds it to the global
723    <varname>xf86InputDriverList[]</varname> array.  For a static server,
724    the <varname>xf86InputDriverList[]</varname> array is initialised at
725    build time.
726      </para>
727
728      <para>
729    Both the <varname>xf86DriverList[]</varname> and
730    <varname>xf86InputDriverList[]</varname> arrays have been initialised
731    by the end of this stage.
732      </para>
733
734      <para>
735    Once all the drivers are registered, their
736    <function>ChipIdentify()</function> functions are called.
737      </para>
738
739      <blockquote><para>
740	  <programlisting>
741    void ChipIdentify(int flags);
742	  </programlisting>
743	  <blockquote><para>
744      This is expected to print a message indicating the driver name,
745      a short summary of what it supports, and a list of the chipset
746      names that it supports.  It may use the xf86PrintChipsets() helper
747      to do this.
748	    </para></blockquote>
749	</para></blockquote>
750
751      <blockquote><para>
752	  <programlisting>
753    void xf86PrintChipsets(const char *drvname, const char *drvmsg,
754                           SymTabPtr chips);
755	  </programlisting>
756	  <blockquote><para>
757      This function provides an easy way for a driver's ChipIdentify
758      function to format the identification message.
759	    </para></blockquote>
760	</para></blockquote>
761    </sect2>
762
763    <sect2>
764      <title>Initialise Access Control</title>
765
766      <para>
767    This is done at the start of the first server generation only.
768      </para>
769
770      <para>
771    The Resource Access Control (RAC) subsystem is initialised before
772    calling any driver functions that may access hardware.  All generic
773    bus information is probed and saved (for restoration later).  All
774    (shared resource) video devices are disabled at the generic bus
775    level, and a probe is done to find the <quote>primary</quote> video device.  These
776    devices remain disabled for the next step.
777      </para>
778    </sect2>
779
780
781    <sect2 id="probe">
782      <title>Video Driver Probe</title>
783
784      <para>
785    This is done at the start of the first server generation only.  The
786    <function>ChipProbe()</function> function of each registered video driver
787    is called.
788      </para>
789
790      <blockquote><para>
791	  <programlisting>
792    Bool ChipProbe(DriverPtr drv, int flags);
793	  </programlisting>
794	  <blockquote><para>
795      The purpose of this is to identify all instances of hardware
796      supported by the driver.  The flags value is currently either 0,
797      <constant>PROBE_DEFAULT</constant> or <constant>PROBE_DETECT</constant>.
798      <constant>PROBE_DETECT</constant> is used if "-configure" or "-probe"
799      command line arguments are given and indicates to the
800      <function>Probe()</function> function that it should not configure the
801      bus entities and that no xorg.conf information is available.
802	    </para>
803
804	  <para>
805      The probe must find the active device sections that match the
806      driver by calling <function>xf86MatchDevice()</function>.  The number
807      of matches found limits the maximum number of instances for this
808      driver.  If no matches are found, the function should return
809      <constant>FALSE</constant> immediately.
810	  </para>
811
812	<para>
813      Devices that cannot be identified by using device-independent
814      methods should be probed at this stage (keeping in mind that access
815      to all resources that can be disabled in a device-independent way
816      are disabled during this phase).  The probe must be a minimal
817      probe.  It should just determine if there is a card present that
818      the driver can drive.  It should use the least intrusive probe
819      methods possible.  It must not do anything that is not essential,
820      like probing for other details such as the amount of memory
821      installed, etc.  It is recommended that the
822      <function>xf86MatchPciInstances()</function> helper function be used
823      for identifying matching PCI devices
824      (see the <link linkend="rac">RAC</link> section).  These helpers also
825      checks and claims the appropriate entity.  When not using the
826      helper, that should be done with <function>xf86CheckPciSlot()</function>
827      and <function>xf86ClaimPciSlot()</function> for PCI devices (see the
828      <link linkend="rac">RAC</link> section).
829	</para>
830
831	<para>
832      The probe must register all non-relocatable resources at this
833      stage.  If a resource conflict is found between exclusive resources
834      the driver will fail immediately.  This is usually best done with
835      the <function>xf86ConfigPciEntity()</function> helper function
836      for PCI.
837        </para>
838
839	<para>
840      If a chipset is specified in an active device section which the
841      driver considers relevant (ie it has no driver specified, or the
842      driver specified matches the driver doing the probe), the Probe
843      must return <constant>FALSE</constant> if the chipset doesn't match
844      one supported by the driver.
845	</para>
846
847	<para>
848      If there are no active device sections that the driver considers
849      relevant, it must return <constant>FALSE</constant>.
850	</para>
851
852	<para>
853      Allocate a <structname>ScrnInfoRec</structname> for each active instance of the
854      hardware found, and fill in the basic information, including the
855      other driver entry points.   This is best done with the
856      <function>xf86ConfigPciEntity()</function> for PCI instances.
857      These functions allocate a <structname>ScrnInfoRec</structname> for active
858      entities. Optionally <function>xf86AllocateScreen()</function>
859      function may also be used to allocate the <structname>ScrnInfoRec</structname>.
860      Any of these functions take care of initialising fields to defined
861      <quote>unused</quote> values.
862	</para>
863
864	<para>
865      Claim the entities for each instance of the hardware found.  This
866      prevents other drivers from claiming the same hardware.
867	</para>
868
869	<para>
870      Must leave hardware in the same state it found it in, and must not
871      do any hardware initialisation.
872	</para>
873
874	<para>
875      All detection can be overridden via the config file, and that
876      parsed information is available to the driver at this stage.
877	</para>
878
879	<para>
880      Returns <constant>TRUE</constant> if one or more instances are found,
881      and <constant>FALSE</constant> otherwise.
882	</para>
883
884	  </blockquote></para></blockquote>
885
886      <blockquote><para>
887	  <programlisting>
888    int xf86MatchDevice(const char *drivername,
889                        GDevPtr **driversectlist)
890	  </programlisting>
891	  <blockquote><para>
892      This function takes the name of the driver and returns via
893      <parameter>driversectlist</parameter> a list of device sections that
894      match the driver name.  The function return value is the number
895      of matches found.  If a fatal error is encountered the return
896      value is <literal>-1</literal>.
897	    </para>
898
899	    <para>
900      The caller should use <function>xfree()</function> to free
901      <parameter>*driversectlist</parameter> when it is no longer needed.
902	    </para>
903
904	  </blockquote></para></blockquote>
905
906      <blockquote><para>
907	  <programlisting>
908    ScrnInfoPtr xf86AllocateScreen(DriverPtr drv, int flags)
909	  </programlisting>
910	  <blockquote><para>
911      This function allocates a new <structname>ScrnInfoRec</structname> in the
912      <varname>xf86Screens[]</varname> array.  This function is normally
913      called by the video driver <function>ChipProbe()</function> functions.
914      The return value is a pointer to the newly allocated
915      <structname>ScrnInfoRec</structname>.  The <structfield>scrnIndex</structfield>,
916      <structfield>origIndex</structfield>, <structfield>module</structfield> and
917      <structfield>drv</structfield> fields are initialised.  The reference count
918      in <parameter>drv</parameter> is incremented.  The storage for any
919      currently allocated <quote>privates</quote> pointers is also allocated and
920      the <structfield>privates</structfield> field initialised (the privates data
921      is of course not allocated or initialised).  This function never
922      returns on failure.  If the allocation fails, the server exits
923      with a fatal error.  The flags value is not currently used, and
924      should be set to zero.
925	    </para></blockquote>
926	</para></blockquote>
927
928      <para>
929    At the completion of this, a list of <structname>ScrnInfoRecs</structname>
930    have been allocated in the <varname>xf86Screens[]</varname> array, and
931    the associated entities and fixed resources have been claimed.  The
932    following <structname>ScrnInfoRec</structname> fields must be initialised at
933    this point:
934
935	<literallayout>
936          driverVersion
937          driverName
938          scrnIndex(*)
939          origIndex(*)
940          drv(*)
941          module(*)
942          name
943          Probe
944          PreInit
945          ScreenInit
946          EnterVT
947          LeaveVT
948          numEntities
949          entityList
950          access
951	</literallayout>
952
953    <literal>(*)</literal> These are initialised when the <structname>ScrnInfoRec</structname>
954    is allocated, and not explicitly by the driver.
955      </para>
956
957      <para>
958    The following <structname>ScrnInfoRec</structname> fields must be initialised
959    if the driver is going to use them:
960
961	<literallayout>
962          SwitchMode
963          AdjustFrame
964          FreeScreen
965          ValidMode
966	</literallayout>
967      </para>
968    </sect2>
969
970    <sect2>
971      <title>Matching Screens</title>
972
973      <para>
974    This is done at the start of the first server generation only.
975      </para>
976
977      <para>
978    After the Probe phase is finished, there will be some number of
979    <structname>ScrnInfoRec</structname>s.  These are then matched with the active
980    &k.screen; sections in the xorg.conf, and those not having an active
981    &k.screen; section are deleted.  If the number of remaining screens
982    is 0, <function>InitOutput()</function> sets
983    <structfield>screenInfo.numScreens</structfield> to <constant>0</constant> and
984    returns.
985      </para>
986
987      <para>
988    At this point the following fields of the <structname>ScrnInfoRec</structname>s
989    must be initialised:
990
991	<literallayout>
992          confScreen
993	</literallayout>
994      </para>
995
996    </sect2>
997
998    <sect2>
999      <title>Allocate non-conflicting resources</title>
1000
1001      <para>
1002    This is done at the start of the first server generation only.
1003      </para>
1004
1005      <para>
1006    Before calling the drivers again, the resource information collected
1007    from the Probe phase is processed.  This includes checking the extent
1008    of PCI resources for the probed devices, and resolving any conflicts
1009    in the relocatable PCI resources.  It also reports conflicts, checks
1010    bus routing issues, and anything else that is needed to enable the
1011    entities for the next phase.
1012      </para>
1013
1014      <para>
1015    If any drivers registered an <function>EntityInit()</function> function
1016    during the Probe phase, then they are called here.
1017      </para>
1018
1019    </sect2>
1020
1021    <sect2>
1022      <title>Sort the Screens and pre-check Monitor Information</title>
1023
1024      <para>
1025    This is done at the start of the first server generation only.
1026      </para>
1027
1028      <para>
1029    The list of screens is sorted to match the ordering requested in the
1030    config file.
1031      </para>
1032
1033      <para>
1034    The list of modes for each active monitor is checked against the
1035    monitor's parameters.  Invalid modes are pruned.
1036      </para>
1037
1038    </sect2>
1039
1040    <sect2>
1041      <title>PreInit</title>
1042
1043      <para>
1044    This is done at the start of the first server generation only.
1045      </para>
1046
1047      <para>
1048    For each <structname>ScrnInfoRec</structname>, enable access to the screens entities and call
1049    the <function>ChipPreInit()</function> function.
1050      </para>
1051
1052      <blockquote><para>
1053	  <programlisting>
1054    Bool ChipPreInit(ScrnInfoRec screen, int flags);
1055	  </programlisting>
1056	  <blockquote><para>
1057      The purpose of this function is to find out all the information
1058      required to determine if the configuration is usable, and to
1059      initialise those parts of the <structname>ScrnInfoRec</structname> that
1060      can be set once at the beginning of the first server generation.
1061	    </para>
1062
1063	    <para>
1064      The number of entities registered for the screen should be checked
1065      against the expected number (most drivers expect only one).  The
1066      entity information for each of them should be retrieved (with
1067      <function>xf86GetEntityInfo()</function>) and checked for the correct
1068      bus type and that none of the shareable resources registered during
1069      the Probe phase was rejected.
1070	    </para>
1071
1072	    <para>
1073      Access to resources for the entities that can be controlled in a
1074      device-independent way are enabled before this function is called.
1075      If the driver needs to access any resources that it has disabled
1076      in an <function>EntityInit()</function> function that it registered,
1077      then it may enable them here providing that it disables them before
1078      this function returns.
1079	    </para>
1080
1081	    <para>
1082      This includes probing for video memory, clocks, ramdac, and all
1083      other HW info that is needed.  It includes determining the
1084      depth/bpp/visual and related info.  It includes validating and
1085      determining the set of video modes that will be used (and anything
1086      that is required to determine that).
1087	    </para>
1088
1089	    <para>
1090      This information should be determined in the least intrusive way
1091      possible.  The state of the HW must remain unchanged by this
1092      function.  Although video memory (including MMIO) may be mapped
1093      within this function, it must be unmapped before returning.  Driver
1094      specific information should be stored in a structure hooked into
1095      the <structname>ScrnInfoRec</structname>'s <structfield>driverPrivate</structfield>
1096      field.  Any other modules which require persistent data (ie data
1097      that persists across server generations) should be initialised in
1098      this function, and they should allocate a <quote>privates</quote> index to
1099      hook their data into by calling
1100      <function>xf86AllocateScrnInfoPrivateIndex()</function>.  The <quote>privates</quote>
1101      data is persistent.
1102	    </para>
1103
1104	    <para>
1105      Helper functions for some of these things are provided at the
1106      XFree86 common level, and the driver can choose to make use of
1107      them.
1108	    </para>
1109
1110	    <para>
1111      Modules may be loaded at any point in this function, and all
1112      modules that the driver will need must be loaded before the end
1113      of this function.  Either the  <function>xf86LoadSubModule()</function>
1114      or the <function>xf86LoadDrvSubModule()</function> function should be
1115      used to load modules depending on whether a
1116      <structname>ScrnInfoRec</structname> has been set up. A driver may unload
1117      a module within this function if it was only needed temporarily,
1118      and the <function>xf86UnloadSubModule()</function> function should be used
1119      to do that.  Otherwise there is no need to explicitly unload modules
1120      because the loader takes care of module dependencies and will
1121      unload submodules automatically if/when the driver module is
1122      unloaded.
1123	    </para>
1124
1125	    <para>
1126      The bulk of the <structname>ScrnInfoRec</structname> fields should be filled
1127      out in this function.
1128	    </para>
1129
1130	    <para>
1131      <function>ChipPreInit()</function> returns <constant>FALSE</constant> when
1132      the configuration is unusable in some way (unsupported depth, no
1133      valid modes, not enough video memory, etc), and <constant>TRUE</constant>
1134      if it is usable.
1135	    </para>
1136
1137	    <para>
1138      It is expected that if the <function>ChipPreInit()</function> function
1139      returns <constant>TRUE</constant>, then the only reasons that subsequent
1140      stages in the driver might fail are lack or resources (like xalloc
1141      failures).  All other possible reasons for failure should be
1142      determined by the <function>ChipPreInit()</function> function.
1143	    </para></blockquote>
1144	</para></blockquote>
1145
1146      <para>
1147    The <structname>ScrnInfoRec</structname>s for screens where the <function>ChipPreInit()</function> fails are removed.
1148    If none remain, <function>InitOutput()</function> sets <structfield>screenInfo.numScreens</structfield> to <constant>0</constant> and returns.
1149      </para>
1150
1151      <para>
1152    At this point, further fields of the <structname>ScrnInfoRec</structname>s would normally be
1153    filled in.  Most are not strictly mandatory, but many are required
1154    by other layers and/or helper functions that the driver may choose
1155    to use.  The documentation for those layers and helper functions
1156    indicates which they require.
1157      </para>
1158
1159      <para>
1160    The following fields of the <structname>ScrnInfoRec</structname>s should be filled in if the
1161    driver is going to use them:
1162
1163	<literallayout>
1164          monitor
1165          display
1166          depth
1167          pixmapBPP
1168          bitsPerPixel
1169          weight                (&gt;8bpp only)
1170          mask                  (&gt;8bpp only)
1171          offset                (&gt;8bpp only)
1172          rgbBits               (8bpp only)
1173          gamma
1174          defaultVisual
1175          virtualX
1176          virtualY
1177          displayWidth
1178          frameX0
1179          frameY0
1180          frameX1
1181          frameY1
1182          zoomLocked
1183          modePool
1184          modes
1185          currentMode
1186          progClock             (TRUE if clock is programmable)
1187          chipset
1188          ramdac
1189          clockchip
1190          numClocks             (if not programmable)
1191          clock[]               (if not programmable)
1192          videoRam
1193          memBase
1194          driverPrivate
1195          chipID
1196          chipRev
1197	</literallayout>
1198      </para>
1199
1200      <blockquote><para>
1201	  <programlisting>
1202    pointer xf86LoadSubModule(ScrnInfoPtr pScrn, const char *name);
1203      and
1204    pointer xf86LoadDrvSubModule(DriverPtr drv, const char *name);
1205	  </programlisting>
1206	  <blockquote><para>
1207      Load a module that a driver depends on.  This function loads the
1208      module <parameter>name</parameter> as a sub module of the driver.  The
1209      return value is a handle identifying the new module.  If the load
1210      fails, the return value will be <constant>NULL</constant>.  If a driver
1211      needs to explicitly unload a module it has loaded in this way,
1212      the return value must be saved and passed to
1213      <function>xf86UnloadSubModule()</function> when unloading.
1214
1215	    </para></blockquote>
1216	</para></blockquote>
1217
1218      <blockquote><para>
1219	  <programlisting>
1220    void xf86UnloadSubModule(pointer module);
1221	  </programlisting>
1222	  <blockquote><para>
1223      Unloads the module referenced by <parameter>module</parameter>.
1224      <parameter>module</parameter> should be a pointer returned previously
1225      by <function>xf86LoadSubModule()</function> or
1226      <function>xf86LoadDrvSubModule()</function> .
1227
1228	    </para></blockquote>
1229	</para></blockquote>
1230    </sect2>
1231
1232    <sect2>
1233      <title>Cleaning up Unused Drivers</title>
1234
1235      <para>
1236    At this point it is known which screens will be in use, and which
1237    drivers are being used.  Unreferenced drivers (and modules they
1238    may have loaded) are unloaded here.
1239      </para>
1240
1241    </sect2>
1242
1243    <sect2>
1244      <title>Consistency Checks</title>
1245
1246      <para>
1247    The parameters that must be global to the server, like pixmap formats,
1248    bitmap bit order, bitmap scanline unit and image byte order are
1249    compared for each of the screens.  If a mismatch is found, the server
1250    exits with an appropriate message.
1251      </para>
1252
1253    </sect2>
1254
1255    <sect2>
1256      <title>Check if Resource Control is Needed</title>
1257
1258      <para>
1259    Determine if resource access control is needed.  This is the case
1260    if more than one screen is used.  If necessary the RAC wrapper module
1261    is loaded.
1262      </para>
1263    </sect2>
1264
1265    <sect2>
1266      <title>AddScreen (ScreenInit)</title>
1267
1268      <para>
1269    At this point, the valid screens are known.
1270    <function>AddScreen()</function> is called for each of them, passing
1271    <function>ChipScreenInit()</function> as the argument.
1272    <function>AddScreen()</function> is a DIX function that allocates a new
1273    <structfield>screenInfo.screen[]</structfield> entry (aka
1274    <varname>pScreen</varname>), and does some basic initialisation of it.
1275    It then calls the <function>ChipScreenInit()</function> function, with
1276    <parameter>pScreen</parameter> as one of its arguments.  If
1277    <function>ChipScreenInit()</function> returns <constant>FALSE</constant>,
1278    <function>AddScreen()</function> returns <constant>-1</constant>.  Otherwise
1279    it returns the index of the screen.  <function>AddScreen()</function>
1280    should only fail because of programming errors or failure to allocate
1281    resources (like memory).  All configuration problems should be
1282    detected BEFORE this point.
1283      </para>
1284
1285      <blockquote><para>
1286	  <programlisting>
1287    Bool ChipScreenInit(ScreenPtr pScreen,
1288                        int argc, char **argv);
1289	  </programlisting>
1290	  <blockquote><para>
1291      This is called at the start of each server generation.
1292	    </para>
1293
1294	    <para>
1295      Fill in all of <parameter>pScreen</parameter>, possibly doing some of
1296      this by calling ScreenInit functions from other layers like mi,
1297      framebuffers (cfb, etc), and extensions.
1298	    </para>
1299
1300	    <para>
1301      Decide which operations need to be placed under resource access
1302      control.  The classes of operations are the frame buffer operations
1303      (<constant>RAC_FB</constant>), the pointer operations
1304      (<constant>RAC_CURSOR</constant>), the viewport change operations
1305      (<constant>RAC_VIEWPORT</constant>) and the colormap operations
1306      (<constant>RAC_COLORMAP</constant>).  Any operation that requires
1307      resources which might be disabled during OPERATING state should
1308      be set to use RAC.  This can be specified separately for memory
1309      and IO resources (the <structfield>racMemFlags</structfield> and
1310      <structfield>racIoFlags</structfield> fields of the <structname>ScrnInfoRec</structname>
1311      respectively).
1312	    </para>
1313
1314	    <para>
1315      Map any video memory or other memory regions.
1316	    </para>
1317
1318	    <para>
1319      Save the video card state.  Enough state must be saved so that
1320      the original state can later be restored.
1321	    </para>
1322
1323	    <para>
1324      Initialise the initial video mode.  The <structname>ScrnInfoRec</structname>'s
1325      <structfield>vtSema</structfield> field should be set to <constant>TRUE</constant>
1326      just prior to changing the video hardware's state.
1327
1328	    </para></blockquote>
1329	</para></blockquote>
1330
1331
1332      <para>
1333    The <function>ChipScreenInit()</function> function (or functions from other
1334    layers that it calls) should allocate entries in the
1335    <structname>ScreenRec</structname>'s <structfield>devPrivates</structfield> area by
1336    calling <function>AllocateScreenPrivateIndex()</function> if it needs
1337    per-generation storage.  Since the <structname>ScreenRec</structname>'s
1338    <structfield>devPrivates</structfield> information is cleared for each server
1339    generation, this is the correct place to initialise it.
1340      </para>
1341
1342      <para>
1343    After <function>AddScreen()</function> has successfully returned, the
1344    following <structname>ScrnInfoRec</structname> fields are initialised:
1345
1346	<literallayout>
1347          pScreen
1348          racMemFlags
1349          racIoFlags
1350	</literallayout>
1351      </para>
1352
1353      <para>
1354    The <function>ChipScreenInit()</function> function should initialise the
1355    <structfield>CloseScreen</structfield> and <structfield>SaveScreen</structfield> fields
1356    of <parameter>pScreen</parameter>.  The old value of
1357    <structfield>pScreen-&gt;CloseScreen</structfield> should be saved as part of
1358    the driver's per-screen private data, allowing it to be called from
1359    <function>ChipCloseScreen()</function>.  This means that the existing
1360    <function>CloseScreen()</function> function is wrapped.
1361      </para>
1362    </sect2>
1363
1364    <sect2>
1365      <title>Finalising RAC Initialisation</title>
1366
1367      <para>
1368    After all the <function>ChipScreenInit()</function> functions have been
1369    called, each screen has registered its RAC requirements.  This
1370    information is used to determine which shared resources are requested
1371    by more than one driver and set the access functions accordingly.
1372    This is done following these rules:
1373
1374	<orderedlist>
1375	  <listitem><para>
1376	The shareable resources registered by each entity are compared.
1377	If a resource is registered by more than one entity the entity
1378	will be marked to indicate that it needs to share this resources
1379	type (IO or MEM).
1380	    </para></listitem>
1381
1382	  <listitem><para>
1383        A resource marked <quote>disabled</quote> during OPERATING state will be
1384        ignored entirely.
1385	    </para></listitem>
1386
1387	  <listitem><para>
1388	A resource marked <quote>unused</quote> will only conflict with an overlapping
1389	resource of an other entity if the second is actually in use
1390	during OPERATING state.
1391	    </para></listitem>
1392
1393	  <listitem><para>
1394	If an <quote>unused</quote> resource was found to conflict but the entity
1395	does not use any other resource of this type the entire resource
1396	type will be disabled for that entity.
1397	    </para></listitem>
1398	</orderedlist>
1399      </para>
1400
1401    </sect2>
1402
1403    <sect2>
1404      <title>Finishing InitOutput()</title>
1405
1406      <para>
1407    At this point <function>InitOutput()</function> is finished, and all the
1408    screens have been setup in their initial video mode.
1409      </para>
1410
1411    </sect2>
1412
1413    <sect2>
1414      <title>Mode Switching</title>
1415
1416      <para>
1417    When a SwitchMode event is received, <function>ChipSwitchMode()</function>
1418    is called (when it exists):
1419      </para>
1420
1421      <blockquote><para>
1422	  <programlisting>
1423    Bool ChipSwitchMode(int index, DisplayModePtr mode);
1424	  </programlisting>
1425	  <blockquote><para>
1426      Initialises the new mode for the screen identified by
1427      <parameter>index;</parameter>.  The viewport may need to be adjusted
1428      also.
1429
1430	    </para></blockquote>
1431	</para></blockquote>
1432
1433    </sect2>
1434
1435    <sect2>
1436      <title>Changing Viewport</title>
1437
1438      <para>
1439    When a Change Viewport event is received,
1440    <function>ChipAdjustFrame()</function> is called (when it exists):
1441      </para>
1442
1443      <blockquote><para>
1444	  <programlisting>
1445    void ChipAdjustFrame(int index, int x, int y);
1446	  </programlisting>
1447	  <blockquote><para>
1448      Changes the viewport for the screen identified by
1449      <parameter>index;</parameter>.
1450	    </para>
1451
1452	    <para>
1453      It should be noted that many chipsets impose restrictions on where the
1454      viewport may be placed in the virtual resolution, either for alignment
1455      reasons, or to prevent the start of the viewport from being positioned
1456      within a pixel (as can happen in a 24bpp mode).  After calculating the
1457      value the chipset's panning registers need to be set to for non-DGA
1458      modes, this function should recalculate the ScrnInfoRec's
1459      <structfield>frameX0</structfield>, <structfield>frameY0</structfield>, <structfield>frameX1</structfield>
1460      and <structfield>frameY1</structfield> fields to correspond to that value.  If
1461      this is not done, switching to another mode might cause the position
1462      of a hardware cursor to change.
1463
1464	    </para></blockquote>
1465	</para></blockquote>
1466
1467    </sect2>
1468
1469    <sect2>
1470      <title>VT Switching</title>
1471
1472      <para>
1473    When a VT switch event is received, <function>xf86VTSwitch()</function>
1474    is called.  <function>xf86VTSwitch()</function> does the following:
1475
1476	<variablelist>
1477	  <varlistentry><term>On ENTER:</term>
1478	    <listitem>
1479	      <itemizedlist>
1480		<listitem><para>
1481		    enable port I/O access
1482		  </para></listitem>
1483
1484		<listitem><para>
1485		    save and initialise the bus/resource state
1486		  </para></listitem>
1487
1488		<listitem><para>
1489		    enter the SETUP server state
1490		  </para></listitem>
1491
1492		<listitem><para>
1493		    calls <function>ChipEnterVT()</function> for each screen
1494		  </para></listitem>
1495
1496		<listitem><para>
1497		    enter the OPERATING server state
1498		  </para></listitem>
1499
1500		<listitem><para>
1501		    validate GCs
1502		  </para></listitem>
1503
1504		<listitem><para>
1505		    Restore fb from saved pixmap for each screen
1506		  </para></listitem>
1507
1508		<listitem><para>
1509		    Enable all input devices
1510		  </para></listitem>
1511	      </itemizedlist>
1512	    </listitem>
1513	  </varlistentry>
1514	  <varlistentry>
1515	    <term>On LEAVE:</term>
1516	    <listitem>
1517	      <itemizedlist>
1518		<listitem><para>
1519		    Save fb to pixmap for each screen
1520		  </para></listitem>
1521
1522		<listitem><para>
1523		    validate GCs
1524		  </para></listitem>
1525
1526		<listitem><para>
1527		    enter the SETUP server state
1528		  </para></listitem>
1529
1530		<listitem><para>
1531		    calls <function>ChipLeaveVT()</function> for each screen
1532		  </para></listitem>
1533
1534		<listitem><para>
1535		    disable all input devices
1536		  </para></listitem>
1537
1538		<listitem><para>
1539		    restore bus/resource state
1540		  </para></listitem>
1541
1542		<listitem><para>
1543		    disables port I/O access
1544		  </para></listitem>
1545	      </itemizedlist>
1546	    </listitem>
1547	  </varlistentry>
1548	</variablelist>
1549      </para>
1550
1551      <blockquote><para>
1552	  <programlisting>
1553    Bool ChipEnterVT(ScrnInfoPtr pScrn);
1554	  </programlisting>
1555	  <blockquote><para>
1556      This function should initialise the current video mode and
1557      initialise the viewport, turn on the HW cursor if appropriate,
1558      etc.
1559	    </para>
1560
1561	    <para>
1562      Should it re-save the video state before initialising the video
1563      mode?
1564	    </para>
1565
1566	  </blockquote></para></blockquote>
1567
1568      <blockquote><para>
1569	  <programlisting>
1570    void ChipLeaveVT(ScrnInfoPtr pScrn);
1571	  </programlisting>
1572	  <blockquote><para>
1573      This function should restore the saved video state.  If
1574      appropriate it should also turn off the HW cursor, and invalidate
1575      any pixmap/font caches.
1576	    </para>
1577
1578	  </blockquote></para></blockquote>
1579
1580      <para>
1581    Optionally, <function>ChipLeaveVT()</function> may also unmap memory
1582    regions.  If so, <function>ChipEnterVT()</function> will need to remap
1583    them.  Additionally, if an aperture used to access video memory is
1584    unmapped and remapped in this fashion, <function>ChipEnterVT()</function>
1585    will also need to notify the framebuffer layers of the aperture's new
1586    location in virtual memory.  This is done with a call to the screen's
1587    <function>ModifyPixmapHeader()</function> function, as follows
1588      </para>
1589
1590      <blockquote><para>
1591	  <programlisting>
1592    (*pScreen-&gt;ModifyPixmapHeader)(pScrn-&gt;ppix,
1593                                   -1, -1, -1, -1, -1, NewApertureAddress);
1594	  </programlisting>
1595	  <blockquote><para>
1596        where the <structfield>ppix</structfield> field in a ScrnInfoRec
1597        points to the pixmap used by the screen's
1598        <function>SaveRestoreImage()</function> function to hold the screen's
1599        contents while switched out.
1600	    </para>
1601
1602	  </blockquote></para></blockquote>
1603
1604      <para>
1605    Other layers may wrap the <function>ChipEnterVT()</function> and
1606    <function>ChipLeaveVT()</function> functions if they need to take some
1607    action when these events are received.
1608      </para>
1609    </sect2>
1610
1611    <sect2>
1612      <title>End of server generation</title>
1613
1614      <para>
1615    At the end of each server generation, the DIX layer calls
1616    <function>ChipCloseScreen()</function> for each screen:
1617      </para>
1618
1619      <blockquote><para>
1620	  <programlisting>
1621    Bool ChipCloseScreen(int index, ScreenPtr pScreen);
1622	  </programlisting>
1623	  <blockquote><para>
1624      This function should restore the saved video state and unmap the
1625      memory regions.
1626	    </para>
1627
1628	    <para>
1629      It should also free per-screen data structures allocated by the
1630      driver.  Note that the persistent data held in the
1631      <structname>ScrnInfoRec</structname>'s <structfield>driverPrivate</structfield> field
1632      should not be freed here because it is needed by subsequent server
1633      generations.
1634	    </para>
1635
1636	    <para>
1637      The <structname>ScrnInfoRec</structname>'s <structfield>vtSema</structfield> field
1638      should be set to <constant>FALSE</constant> once the video HW state
1639      has been restored.
1640	    </para>
1641
1642	    <para>
1643      Before freeing the per-screen driver data the saved
1644      <structfield>CloseScreen</structfield> value should be restored to
1645      <structfield>pScreen-&gt;CloseScreen</structfield>, and that function should
1646      be called after freeing the data.
1647
1648	    </para></blockquote>
1649	</para></blockquote>
1650    </sect2>
1651  </sect1>
1652
1653  <sect1>
1654    <title>Optional Driver Functions</title>
1655
1656    <para>
1657The functions outlined here can be called from the XFree86 common layer,
1658but their presence is optional.
1659    </para>
1660
1661    <sect2>
1662      <title>Mode Validation</title>
1663
1664      <para>
1665    When a mode validation helper supplied by the XFree86-common layer is
1666    being used, it can be useful to provide a function to check for hw
1667    specific mode constraints:
1668      </para>
1669
1670      <blockquote><para>
1671	  <programlisting>
1672    ModeStatus ChipValidMode(ScrnInfoPtr pScrn, DisplayModePtr mode,
1673                             Bool verbose, int flags);
1674	  </programlisting>
1675	  <blockquote><para>
1676      Check the passed mode for hw-specific constraints, and return the
1677      appropriate status value.
1678
1679	    </para></blockquote>
1680	</para></blockquote>
1681
1682      <para>
1683This function may also modify the effective timings and clock of the passed
1684mode.  These have been stored in the mode's <structfield>Crtc*</structfield> and
1685<structfield>SynthClock</structfield> elements, and have already been adjusted for
1686interlacing, doublescanning, multiscanning and clock multipliers and dividers.
1687The function should not modify any other mode field, unless it wants to modify
1688the mode timings reported to the user by <function>xf86PrintModes()</function>.
1689      </para>
1690
1691      <para>
1692The function is called once for every mode in the xorg.conf Monitor section
1693assigned to the screen, with <parameter>flags</parameter> set to
1694<constant>MODECHECK_INITIAL</constant>.  It is subsequently called for every mode
1695in the xorg.conf Display subsection assigned to the screen, with
1696<parameter>flags</parameter> set to <constant>MODECHECK_FINAL</constant>.  In the second
1697case, the mode will have successfully passed all other tests.  In addition,
1698the <structname>ScrnInfoRec</structname>'s <structfield>virtualX</structfield>,
1699<structfield>virtualY</structfield> and <structfield>displayWidth</structfield> fields will have been
1700set as if the mode to be validated were to be the last mode accepted.
1701      </para>
1702
1703      <para>
1704In effect, calls with MODECHECK_INITIAL are intended for checks that do not
1705depend on any mode other than the one being validated, while calls with
1706MODECHECK_FINAL are intended for checks that may involve more than one mode.
1707      </para>
1708    </sect2>
1709
1710    <sect2>
1711      <title>Free screen data</title>
1712
1713      <para>
1714    When a screen is deleted prior to the completion of the ScreenInit
1715    phase the <function>ChipFreeScreen()</function> function is called when defined.
1716      </para>
1717
1718      <blockquote><para>
1719	  <programlisting>
1720    void ChipFreeScreen(ScrnInfoPtr pScrn);
1721	  </programlisting>
1722	  <blockquote><para>
1723      Free any driver-allocated data that may have been allocated up to
1724      and including an unsuccessful <function>ChipScreenInit()</function>
1725      call.  This would predominantly be data allocated by
1726      <function>ChipPreInit()</function> that persists across server
1727      generations.  It would include the <structfield>driverPrivate</structfield>,
1728      and any <quote>privates</quote> entries that modules may have allocated.
1729
1730	    </para></blockquote>
1731	</para></blockquote>
1732
1733    </sect2>
1734</sect1>
1735
1736  <sect1>
1737    <title>Recommended driver functions</title>
1738
1739    <para>
1740The functions outlined here are for internal use by the driver only.
1741They are entirely optional, and are never accessed directly from higher
1742layers.  The sample function declarations shown here are just examples.
1743The interface (if any) used is up to the driver.
1744    </para>
1745
1746    <sect2>
1747      <title>Save</title>
1748
1749      <para>
1750    Save the video state.  This could be called from <function>ChipScreenInit()</function> and
1751    (possibly) <function>ChipEnterVT()</function>.
1752      </para>
1753
1754      <blockquote><para>
1755	  <programlisting>
1756    void ChipSave(ScrnInfoPtr pScrn);
1757	  </programlisting>
1758	  <blockquote><para>
1759      Saves the current state.  This will only be saving pre-server
1760      states or states before returning to the server.  There is only
1761      one current saved state per screen and it is stored in private
1762      storage in the screen.
1763
1764	    </para></blockquote>
1765	</para></blockquote>
1766    </sect2>
1767
1768    <sect2>
1769      <title>Restore</title>
1770
1771      <para>
1772    Restore the original video state.  This could be called from the
1773    <function>ChipLeaveVT()</function> and <function>ChipCloseScreen()</function>
1774    functions.
1775      </para>
1776
1777      <blockquote><para>
1778	  <programlisting>
1779    void ChipRestore(ScrnInfoPtr pScrn);
1780	  </programlisting>
1781	  <blockquote><para>
1782      Restores the saved state from the private storage.  Usually only
1783      used for restoring text modes.
1784
1785	    </para></blockquote>
1786	</para></blockquote>
1787
1788    </sect2>
1789
1790    <sect2>
1791      <title>Initialise Mode</title>
1792
1793      <para>
1794    Initialise a video mode.  This could be called from the
1795    <function>ChipScreenInit()</function>, <function>ChipSwitchMode()</function>
1796    and <function>ChipEnterVT()</function> functions.
1797      </para>
1798
1799      <blockquote><para>
1800	  <programlisting>
1801    Bool ChipModeInit(ScrnInfoPtr pScrn, DisplayModePtr mode);
1802	  </programlisting>
1803	  <blockquote><para>
1804      Programs the hardware for the given video mode.
1805
1806	    </para></blockquote>
1807	</para></blockquote>
1808
1809    </sect2>
1810  </sect1>
1811
1812  <sect1>
1813    <title>Data and Data Structures</title>
1814
1815    <sect2>
1816      <title>Command line data</title>
1817
1818      <para>
1819Command line options are typically global, and are stored in global
1820variables.  These variables are read-only and are available to drivers
1821via a function call interface.  Most of these command line values are
1822processed via helper functions to ensure that they are treated consistently
1823by all drivers.  The other means of access is provided for cases where
1824the supplied helper functions might not be appropriate.
1825      </para>
1826
1827      <para>
1828Some of them are:
1829
1830	<literallayout>
1831    xf86Verbose               verbosity level
1832    xf86Bpp                   -bpp from the command line
1833    xf86Depth                 -depth from the command line
1834    xf86Weight                -weight from the command line
1835    xf86Gamma                 -{r,g,b,}gamma from the command line
1836    xf86FlipPixels            -flippixels from the command line
1837    xf86ProbeOnly             -probeonly from the command line
1838    defaultColorVisualClass   -cc from the command line
1839	</literallayout>
1840      </para>
1841
1842      <para>
1843If we ever do allow for screen-specific command line options, we may
1844need to rethink this.
1845      </para>
1846
1847      <para>
1848These can be accessed in a read-only manner by drivers with the following
1849functions:
1850      </para>
1851
1852     <blockquote><para>
1853	  <programlisting>
1854    int xf86GetVerbosity();
1855	  </programlisting>
1856	  <blockquote><para>
1857      Returns the value of <varname>xf86Verbose</varname>.
1858	    </para></blockquote>
1859
1860	</para></blockquote>
1861
1862     <blockquote><para>
1863	  <programlisting>
1864    int xf86GetDepth();
1865	  </programlisting>
1866	  <blockquote><para>
1867      Returns the <option>-depth</option> command line setting.  If not
1868      set on the command line, <constant>-1</constant> is returned.
1869	    </para></blockquote>
1870
1871	</para></blockquote>
1872
1873     <blockquote><para>
1874	  <programlisting>
1875    rgb xf86GetWeight();
1876	  </programlisting>
1877	  <blockquote><para>
1878      Returns the <option>-weight</option> command line setting.  If not
1879      set on the command line, <literal remap="tt">{0, 0, 0}</literal> is returned.
1880	    </para></blockquote>
1881
1882	</para></blockquote>
1883
1884      <blockquote><para>
1885	  <programlisting>
1886    Gamma xf86GetGamma();
1887	  </programlisting>
1888	  <blockquote><para>
1889      Returns the <option>-gamma</option> or <option>-rgamma</option>,
1890      <option>-ggamma</option>, <option>-bgamma</option> command line settings.
1891      If not set on the command line, <literal remap="tt">{0.0, 0.0, 0.0}</literal>
1892      is returned.
1893	    </para></blockquote>
1894
1895	</para></blockquote>
1896
1897      <blockquote><para>
1898	  <programlisting>
1899    Bool xf86GetFlipPixels();
1900	  </programlisting>
1901	  <blockquote><para>
1902      Returns <constant>TRUE</constant> if <option>-flippixels</option> is
1903      present on the command line, and <constant>FALSE</constant> otherwise.
1904	    </para></blockquote>
1905
1906	</para></blockquote>
1907
1908    </sect2>
1909
1910    <sect2>
1911      <title>Data handling</title>
1912
1913      <para>
1914Config file data contains parts that are global, and parts that are
1915Screen specific.  All of it is parsed into data structures that neither
1916the drivers or most other parts of the server need to know about.
1917      </para>
1918
1919      <para>
1920The global data is typically not required by drivers, and as such, most
1921of it is stored in the private <structname>xf86InfoRec</structname>.
1922      </para>
1923
1924      <para>
1925The screen-specific data collected from the config file is stored in
1926screen, device, display, monitor-specific data structures that are separate
1927from the <varname>ScrnInfoRecs</varname>, with the appropriate elements/fields
1928hooked into the <varname>ScrnInfoRecs</varname> as required.  The screen
1929config data is held in <structname>confScreenRec</structname>, device data in
1930the <structname>GDevRec</structname>, monitor data in the <structname>MonRec</structname>,
1931and display data in the <structname>DispRec</structname>.
1932      </para>
1933
1934      <para>
1935The XFree86 common layer's screen specific data (the actual data in use
1936for each screen) is held in the <varname>ScrnInfoRecs</varname>.  As has
1937been outlined above, the <varname>ScrnInfoRecs</varname> are allocated at probe
1938time, and it is the responsibility of the Drivers' <function>Probe()</function>
1939and <function>PreInit()</function> functions to finish filling them in based
1940on both data provided on the command line and data provided from the
1941Config file.  The precedence for this is:
1942
1943	<blockquote><para>
1944    command line  -&gt;  config file  -&gt;  probed/default data
1945	</para></blockquote>
1946      </para>
1947
1948      <para>
1949For most things in this category there are helper functions that the
1950drivers can use to ensure that the above precedence is consistently
1951used.
1952      </para>
1953
1954      <para>
1955As well as containing screen-specific data that the XFree86 common layer
1956(including essential parts of the server infrastructure as well as helper
1957functions) needs to access, it also contains some data that drivers use
1958internally.  When considering whether to add a new field to the
1959<structname>ScrnInfoRec</structname>, consider the balance between the convenience
1960of things that lots of drivers need and the size/obscurity of the
1961<structname>ScrnInfoRec</structname>.
1962      </para>
1963
1964      <para>
1965Per-screen driver specific data that cannot be accommodated with the
1966static <structname>ScrnInfoRec</structname> fields is held in a driver-defined
1967data structure, a pointer to which is assigned to the
1968<structname>ScrnInfoRec</structname>'s <structfield>driverPrivate</structfield> field.  This
1969is per-screen data that persists across server generations (as does the
1970bulk of the static <structname>ScrnInfoRec</structname> data).  It would typically
1971also include the video card's saved state.
1972      </para>
1973
1974      <para>
1975Per-screen data for other modules that the driver uses that is reset for each
1976server generation is hooked into the <structname>ScrnInfoRec</structname>
1977through its <structfield>privates</structfield> field.
1978      </para>
1979
1980      <para>
1981Once it has stabilised, the data structures and variables accessible to
1982video drivers will be documented here.  In the meantime, those things
1983defined in the <filename>xf86.h</filename> and <filename>xf86str.h</filename>
1984files are visible to video drivers.  Things defined in
1985<filename>xf86Priv.h</filename> and <filename>xf86Privstr.h</filename> are NOT
1986intended to be visible to video drivers, and it is an error for a driver
1987to include those files.
1988      </para>
1989
1990    </sect2>
1991
1992    <sect2>
1993      <title>Accessing global data</title>
1994
1995      <para>
1996Some other global state information that the drivers may access via
1997functions is as follows:
1998      </para>
1999
2000      <blockquote><para>
2001	  <programlisting>
2002    Bool xf86ServerIsExiting();
2003	  </programlisting>
2004	  <blockquote><para>
2005      Returns <constant>TRUE</constant> if the server is at the end of a
2006      generation and is in the process of exiting, and
2007      <constant>FALSE</constant> otherwise.
2008	    </para></blockquote>
2009
2010	</para></blockquote>
2011
2012      <blockquote><para>
2013	  <programlisting>
2014    Bool xf86ServerIsResetting();
2015	  </programlisting>
2016	  <blockquote><para>
2017      Returns <constant>TRUE</constant> if the server is at the end of a
2018      generation and is in the process of resetting, and
2019      <constant>FALSE</constant> otherwise.
2020	    </para></blockquote>
2021
2022	</para></blockquote>
2023
2024      <blockquote><para>
2025	  <programlisting>
2026    Bool xf86ServerIsOnlyProbing();
2027	  </programlisting>
2028	  <blockquote><para>
2029      Returns <constant>TRUE</constant> if the -probeonly command line flag
2030      was specified, and <constant>FALSE</constant> otherwise.
2031	    </para></blockquote>
2032
2033	</para></blockquote>
2034
2035    </sect2>
2036
2037    <sect2>
2038      <title>Allocating private data</title>
2039
2040      <para>
2041A driver and any module it uses may allocate per-screen private storage
2042in either the <structname>ScreenRec</structname> (DIX level) or
2043<structname>ScrnInfoRec</structname> (XFree86 common layer level).
2044<structname>ScreenRec</structname> storage persists only for a single server
2045generation, and <structname>ScrnInfoRec</structname> storage persists across
2046generations for the lifetime of the server.
2047      </para>
2048
2049      <para>
2050The <structname>ScreenRec</structname> <structfield>devPrivates</structfield> data must be
2051reallocated/initialised at the start of each new generation.  This is
2052normally done from the <function>ChipScreenInit()</function> function, and
2053Init functions for other modules that it calls.  Data allocated in this
2054way should be freed by the driver's <function>ChipCloseScreen()</function>
2055functions, and Close functions for other modules that it calls.  A new
2056<structfield>devPrivates</structfield> entry is allocated by calling the
2057<function>AllocateScreenPrivateIndex()</function> function.
2058      </para>
2059
2060      <blockquote><para>
2061	  <programlisting>
2062    int AllocateScreenPrivateIndex();
2063	  </programlisting>
2064	  <blockquote><para>
2065      This function allocates a new element in the
2066      <structfield>devPrivates</structfield> field of all currently existing
2067      <literal remap="tt">ScreenRecs</literal>.  The return value is the index of this
2068      new element in the <structfield>devPrivates</structfield> array.  The
2069      <structfield>devPrivates</structfield> field is of type
2070      <structname>DevUnion</structname>:
2071
2072	      <programlisting>
2073        typedef union _DevUnion {
2074            pointer             ptr;
2075            long                val;
2076            unsigned long       uval;
2077            pointer             (*fptr)(void);
2078        } DevUnion;
2079	      </programlisting>
2080
2081      which allows the element to be used for any of the above types.
2082      It is commonly used as a pointer to data that the caller allocates
2083      after the new index has been allocated.
2084	    </para>
2085
2086	    <para>
2087      This function will return <constant>-1</constant> when there is an
2088      error allocating the new index.
2089	    </para>
2090
2091	  </blockquote>
2092	</para></blockquote>
2093
2094      <para>
2095The <structname>ScrnInfoRec</structname> <structfield>privates</structfield> data persists
2096for the life of the server, so only needs to be allocated once.  This
2097should be done from the <function>ChipPreInit()</function> function, and Init
2098functions for other modules that it calls.  Data allocated in this way
2099should be freed by the driver's <function>ChipFreeScreen()</function> functions,
2100and Free functions for other modules that it calls.  A new
2101<structfield>privates</structfield> entry is allocated by calling the
2102<function>xf86AllocateScrnInfoPrivateIndex()</function> function.
2103      </para>
2104
2105      <blockquote><para>
2106	  <programlisting>
2107    int xf86AllocateScrnInfoPrivateIndex();
2108	  </programlisting>
2109	  <blockquote><para>
2110      This function allocates a new element in the <structfield>privates</structfield>
2111      field of all currently existing <varname>ScrnInfoRecs</varname>.
2112      The return value is the index of this new element in the
2113      <structfield>privates</structfield> array.  The <structfield>privates</structfield>
2114      field is of type <structfield>DevUnion</structfield>:
2115
2116	      <programlisting>
2117        typedef union _DevUnion {
2118            pointer             ptr;
2119            long                val;
2120            unsigned long       uval;
2121            pointer             (*fptr)(void);
2122        } DevUnion;
2123	      </programlisting>
2124
2125      which allows the element to be used for any of the above types.
2126      It is commonly used as a pointer to data that the caller allocates
2127      after the new index has been allocated.
2128	    </para>
2129
2130	    <para>
2131      This function will not return when there is an error allocating
2132      the new index.  When there is an error it will cause the server
2133      to exit with a fatal error.  The similar function for allocation
2134      privates in the <structname>ScreenRec</structname>
2135      (<function>AllocateScreenPrivateIndex()</function>) differs in this
2136      respect by returning <constant>-1</constant> when the allocation fails.
2137	    </para>
2138
2139	  </blockquote>
2140	</para></blockquote>
2141    </sect2>
2142  </sect1>
2143
2144  <sect1 id="rac">
2145    <title>Keeping Track of Bus Resources</title>
2146
2147    <sect2>
2148      <title>Theory of Operation</title>
2149
2150      <para>
2151The XFree86 common layer has knowledge of generic access control mechanisms
2152for devices on certain bus systems (currently the PCI bus) as well as
2153of methods to enable or disable access to the buses itself.  Furthermore
2154it can access information on resources decoded by these devices and if
2155necessary modify it.
2156      </para>
2157
2158      <para>
2159When first starting the Xserver collects all this information, saves it
2160for restoration, checks it for consistency, and if necessary, corrects
2161it.  Finally it disables all resources on a generic level prior to
2162calling any driver function.
2163      </para>
2164
2165      <para>
2166When the <function>Probe()</function> function of each driver is called the
2167device sections are matched against the devices found in the system.
2168The driver may probe devices at this stage that cannot be identified by
2169using device independent methods.  Access to all resources that can be
2170controlled in a device independent way is disabled.  The
2171<function>Probe()</function> function should register all non-relocatable
2172resources at this stage.  If a resource conflict is found between
2173exclusive resources the driver will fail immediately.  Optionally the
2174driver might specify an <function>EntityInit()</function>,
2175<function>EntityLeave()</function> and <function>EntityEnter()</function> function.
2176      </para>
2177
2178      <para>
2179<function>EntityInit()</function> can be used to disable any shared resources
2180that are not controlled by the generic access control functions.  It is
2181called prior to the PreInit phase regardless if an entity is active or
2182not.  When calling the <function>EntityInit()</function>,
2183<function>EntityEnter()</function> and <function>EntityLeave()</function> functions
2184the common level will disable access to all other entities on a generic
2185level.  Since the common level has no knowledge of device specific
2186methods to disable access to resources it cannot be guaranteed that
2187certain resources are not decoded by any other entity until the
2188<function>EntityInit()</function> or <function>EntityEnter()</function> phase is
2189finished.  Device drivers should therefore register all those resources
2190which they are going to disable.  If these resources are never to be
2191used by any driver function they may be flagged <constant>ResInit</constant>
2192so that they can be removed from the resource list after processing all
2193<function>EntityInit()</function> functions.  <function>EntityEnter()</function>
2194should disable decoding of all resources which are not registered as
2195exclusive and which are not handled by the generic access control in
2196the common level.  The difference to <function>EntityInit()</function> is
2197that the latter one is only called once during lifetime of the server.
2198It can therefore be used to set up variables prior to disabling resources.
2199<function>EntityLeave()</function> should restore the original state when
2200exiting the server or switching to a different VT.  It also needs to
2201disable device specific access functions if they need to be disabled on
2202server exit or VT switch.  The default state is to enable them before
2203giving up the VT.
2204      </para>
2205
2206      <para>
2207In <function>PreInit()</function> phase each driver should check if any
2208shareable resources it has registered during <function>Probe()</function> has
2209been denied and take appropriate action which could simply be to fail.
2210If it needs to access resources it has disabled during
2211<function>EntitySetup()</function> it can do so provided it has registered
2212these and will disable them before returning from
2213<function>PreInit()</function>.  This also applies to all other driver
2214functions.  Several functions are provided to request resource ranges,
2215register these, correct PCI config space and add replacements for the
2216generic access functions.  Resources may be marked <quote>disabled</quote> or
2217<quote>unused</quote> during OPERATING stage.  Although these steps could also be
2218performed in <function>ScreenInit()</function>, this is not desirable.
2219      </para>
2220
2221      <para>
2222Following <function>PreInit()</function> phase the common level determines
2223if resource access control is needed.  This is the case if more than
2224one screen is used.  If necessary the RAC wrapper module is loaded.  In
2225<function>ScreenInit()</function> the drivers can decide which operations
2226need to be placed under RAC.  Available are the frame buffer operations,
2227the pointer operations and the colormap operations.  Any operation that
2228requires resources which might be disabled during OPERATING state should
2229be set to use RAC.  This can be specified separately for memory and IO
2230resources.
2231      </para>
2232
2233      <para>
2234When <function>ScreenInit()</function> phase is done the common level will
2235determine which shared resources are requested by more than one driver
2236and set the access functions accordingly.  This is done following these
2237rules:
2238
2239	<orderedlist>
2240	  <listitem><para>
2241   The shareable resources registered by each entity are compared.  If
2242   a resource is registered by more than one entity the entity will be
2243   marked to need to share this resources type (<constant>IO</constant> or
2244   <constant>MEM</constant>).
2245	    </para></listitem>
2246
2247	  <listitem><para>
2248   A resource marked <quote>disabled</quote> during OPERATING state will be ignored
2249   entirely.
2250	    </para></listitem>
2251
2252	  <listitem><para>
2253   A resource marked <quote>unused</quote> will only conflicts with an overlapping
2254   resource of an other entity if the second is actually in use during
2255   OPERATING state.
2256	    </para></listitem>
2257
2258	  <listitem><para>
2259   If an <quote>unused</quote> resource was found to conflict however the entity
2260   does not use any other resource of this type the entire resource type
2261   will be disabled for that entity.
2262	    </para></listitem>
2263	</orderedlist>
2264      </para>
2265
2266      <para>
2267The driver has the choice among different ways to control access to
2268certain resources:
2269
2270	<orderedlist>
2271	  <listitem><para>
2272   It can rely on the generic access functions.  This is probably the
2273   most common case.  Here the driver only needs to register any resource
2274   it is going to use.
2275	    </para></listitem>
2276
2277	  <listitem><para>
2278   It can replace the generic access functions by driver specific
2279   ones.  This will mostly be used in cases where no generic access
2280   functions are available.  In this case the driver has to make sure
2281   these resources are disabled when entering the <function>PreInit()</function>
2282   stage.  Since the replacement functions are registered in
2283   <function>PreInit()</function> the driver will have to enable these
2284   resources itself if it needs to access them during this state.  The
2285   driver can specify if the replacement functions can control memory
2286   and/or I/O resources separately.
2287	    </para></listitem>
2288
2289	  <listitem><para>
2290   The driver can enable resources itself when it needs them.  Each
2291   driver function enabling them needs to disable them before it will
2292   return.  This should be used if a resource which can be controlled
2293   in a device dependent way is only required during SETUP state.  This
2294   way it can be marked <quote>unused</quote> during OPERATING state.
2295	    </para></listitem>
2296	</orderedlist>
2297      </para>
2298
2299      <para>
2300A resource which is decoded during OPERATING state however never accessed
2301by the driver should be marked unused.
2302      </para>
2303
2304      <para>
2305Since access switching latencies are an issue during Xserver operation,
2306the common level attempts to minimize the number of entities that need
2307to be placed under RAC control.  When a wrapped operation is called,
2308the <function>EnableAccess()</function> function is called before control is
2309passed on.  <function>EnableAccess()</function> checks if a screen is under
2310access control.  If not it just establishes bus routing and returns.
2311If the screen needs to be under access control,
2312<function>EnableAccess()</function> determines which resource types
2313(<literal remap="tt">MEM</literal>, <literal remap="tt">IO</literal>) are required.  Then it tests
2314if this access is already established.  If so it simply returns.  If
2315not it disables the currently established access, fixes bus routing and
2316enables access to all entities registered for this screen.
2317      </para>
2318
2319      <para>
2320Whenever a mode switch or a VT-switch is performed the common level will
2321return to SETUP state.
2322      </para>
2323    </sect2>
2324
2325    <sect2>
2326      <title>Resource Types</title>
2327
2328      <para>
2329Resource have certain properties.  When registering resources each range
2330is accompanied by a flag consisting of the ORed flags of the different
2331properties the resource has.  Each resource range may be classified
2332according to
2333
2334	<itemizedlist>
2335	  <listitem><para>
2336       its physical properties i.e., if it addresses
2337       memory (<constant>ResMem</constant>)  or
2338       I/O space (<constant>ResIo</constant>),
2339	    </para></listitem>
2340	  <listitem><para>
2341       if it addresses a
2342       block (<constant>ResBlock</constant>) or
2343       sparse (<constant>ResSparse</constant>)
2344       range,
2345	    </para></listitem>
2346	  <listitem><para>
2347       its access properties.
2348	    </para></listitem>
2349	</itemizedlist>
2350      </para>
2351
2352      <para>
2353There are two known access properties:
2354
2355	<itemizedlist>
2356	  <listitem><para>
2357  <constant>ResExclusive</constant>
2358    for resources which may not be shared with any other device and
2359	    </para></listitem>
2360	  <listitem><para>
2361  <constant>ResShared</constant>
2362    for resources which can be disabled and therefore can be shared.
2363	    </para></listitem>
2364	</itemizedlist>
2365      </para>
2366
2367      <para>
2368If it is necessary to test a resource against any type a generic access
2369type <constant>ResAny</constant> is provided.  If this is set the resource
2370will conflict with any resource of a different entity intersecting its
2371range.  Further it can be specified that a resource is decoded however
2372never used during any stage (<constant>ResUnused</constant>) or during
2373OPERATING state (<constant>ResUnusedOpr</constant>).  A resource only visible
2374during the init functions (ie.  <function>EntityInit()</function>,
2375<function>EntityEnter()</function> and <function>EntityLeave()</function> should
2376be registered with the flag <constant>ResInit</constant>.  A resource that
2377might conflict with background resource ranges may be flagged with
2378<constant>ResBios</constant>.  This might be useful when registering resources
2379ranges that were assigned by the system Bios.
2380      </para>
2381
2382      <para>
2383Several predefined resource lists are available for VGA and 8514/A
2384resources in <filename>common/xf86Resources.h</filename>.
2385      </para>
2386    </sect2>
2387
2388    <sect2 id="avail">
2389      <title>Available Functions</title>
2390
2391      <para>
2392The functions provided for resource management are listed in their order
2393of use in the driver.
2394      </para>
2395
2396      <sect3>
2397	<title>Probe Phase</title>
2398
2399	<para>
2400In this phase each driver detects those resources it is able to drive,
2401creates an entity record for each of them, registers non-relocatable
2402resources and allocates screens and adds the resources to screens.
2403	</para>
2404
2405	<para>
2406Two helper functions are provided for matching device sections in the
2407xorg.conf file to the devices:
2408	</para>
2409
2410	<blockquote><para>
2411	    <programlisting>
2412    int xf86MatchPciInstances(const char *driverName, int vendorID,
2413                              SymTabPtr chipsets, PciChipsets *PCIchipsets,
2414                              GDevPtr *devList, int numDevs, DriverPtr drvp,
2415                              int **foundEntities);
2416	    </programlisting>
2417	    <blockquote><para>
2418      This function finds matches between PCI cards that a driver supports
2419      and config file device sections.  It is intended for use in the
2420      <function>ChipProbe()</function> function of drivers for PCI cards.
2421      Only probed PCI devices with a vendor ID matching
2422      <parameter>vendorID</parameter> are considered.  <parameter>devList</parameter>
2423      and <parameter>numDevs</parameter> are typically those found from
2424      calling <function>xf86MatchDevice()</function>, and represent the active
2425      config file device sections relevant to the driver.
2426      <parameter>PCIchipsets</parameter> is a table that provides a mapping
2427      between the PCI device IDs, the driver's internal chipset tokens
2428      and a list of fixed resources.
2429	      </para>
2430
2431	      <para>
2432      When a device section doesn't have a <emphasis>BusID</emphasis> entry it
2433      can only match the primary video device.  Secondary devices are
2434      only matched with device sections that have a matching
2435      <emphasis>BusID</emphasis> entry.
2436	      </para>
2437
2438	      <para>
2439      Once the preliminary matches have been found, a final match is
2440      confirmed by checking if the chipset override, ChipID override or
2441      probed PCI chipset type match one of those given in the
2442      <parameter>chipsets</parameter> and <parameter>PCIchipsets</parameter> lists.
2443      The <parameter>PCIchipsets</parameter> list includes a list of the PCI
2444      device IDs supported by the driver.  The list should be terminated
2445      with an entry with PCI ID <constant>-1</constant>".  The
2446      <parameter>chipsets</parameter> list is a table mapping the driver's
2447      internal chipset tokens to names, and should be terminated with
2448      a <constant>NULL</constant> entry.  Only those entries with a
2449      corresponding entry in the <parameter>PCIchipsets</parameter> list are
2450      considered.  The order of precedence is: config file chipset,
2451      config file ChipID, probed PCI device ID.
2452	      </para>
2453
2454	      <para>
2455      In cases where a driver handles PCI chipsets with more than one
2456      vendor ID, it may set <parameter>vendorID</parameter> to
2457      <constant>0</constant>, and OR each devID in the list with (the
2458      vendor&nbsp;ID&nbsp;&lt;&lt;&nbsp;16).
2459	      </para>
2460
2461	      <para>
2462      Entity index numbers for confirmed matches are returned as an
2463      array via <parameter>foundEntities</parameter>.  The PCI information,
2464      chipset token and device section for each match are found in the
2465      <structname>EntityInfoRec</structname> referenced by the indices.
2466	      </para>
2467
2468	      <para>
2469      The function return value is the number of confirmed matches.  A
2470      return value of <constant>-1</constant> indicates an internal error.
2471      The returned <parameter>foundEntities</parameter> array should be freed
2472      by the driver with <function>xfree()</function> when it is no longer
2473      needed in cases where the return value is greater than zero.
2474	      </para>
2475
2476	    </blockquote></para></blockquote>
2477
2478	<para>
2479These two helper functions make use of several core functions that are
2480available at the driver level:
2481	</para>
2482
2483	<blockquote><para>
2484	    <programlisting>
2485    Bool xf86ParsePciBusString(const char *busID, int *bus,
2486                               int *device, int *func);
2487	    </programlisting>
2488	    <blockquote><para>
2489      Takes a <parameter>BusID</parameter> string, and if it is in the correct
2490      format, returns the PCI <parameter>bus</parameter>, <parameter>device</parameter>,
2491      <parameter>func</parameter> values that it indicates.  The format of the
2492      string is expected to be "PCI:bus:device:func" where each of <quote>bus</quote>,
2493      <quote>device</quote> and <quote>func</quote> are decimal integers.  The ":func" part may
2494      be omitted, and the func value assumed to be zero, but this isn't
2495      encouraged.  The "PCI" prefix may also be omitted.  The prefix
2496      "AGP" is currently equivalent to the "PCI" prefix.  If the string
2497      isn't a valid PCI BusID, the return value is <constant>FALSE</constant>.
2498	      </para>
2499
2500	    </blockquote></para></blockquote>
2501
2502	<blockquote><para>
2503	    <programlisting>
2504    Bool xf86ComparePciBusString(const char *busID, int bus,
2505                                 int device, int func);
2506	    </programlisting>
2507	    <blockquote><para>
2508      Compares a <parameter>BusID</parameter> string with PCI <parameter>bus</parameter>,
2509      <parameter>device</parameter>, <parameter>func</parameter> values.  If they
2510      match <constant>TRUE</constant> is returned, and <constant>FALSE</constant>
2511      if they don't.
2512	      </para>
2513
2514	    </blockquote></para></blockquote>
2515
2516	<blockquote><para>
2517	    <programlisting>
2518    Bool xf86CheckPciSlot(int bus, int device, int func);
2519	    </programlisting>
2520	    <blockquote><para>
2521      Checks if the PCI slot <literal remap="tt">bus:device:func</literal> has been
2522      claimed.  If so, it returns <constant>FALSE</constant>, and otherwise
2523      <constant>TRUE</constant>.
2524	      </para>
2525
2526	    </blockquote></para></blockquote>
2527
2528	<blockquote><para>
2529	    <programlisting>
2530    int xf86ClaimPciSlot(int bus, int device, int func, DriverPtr drvp,
2531                         int chipset, GDevPtr dev, Bool active);
2532	    </programlisting>
2533	    <blockquote><para>
2534      This function is used to claim a PCI slot, allocate the associated
2535      entity record and initialise their data structures.  The return
2536      value is the index of the newly allocated entity record, or
2537      <constant>-1</constant> if the claim fails.  This function should always
2538      succeed if <function>xf86CheckPciSlot()</function> returned
2539      <constant>TRUE</constant> for the same PCI slot.
2540	      </para>
2541
2542	    </blockquote></para></blockquote>
2543
2544	<blockquote><para>
2545	    <programlisting>
2546    Bool xf86IsPrimaryPci(void);
2547	    </programlisting>
2548	    <blockquote><para>
2549      This function returns <constant>TRUE</constant> if the primary card is
2550      a PCI device, and <constant>FALSE</constant> otherwise.
2551	      </para>
2552
2553	    </blockquote></para></blockquote>
2554
2555	<para>
2556Two helper functions are provided to aid configuring entities:
2557	</para>
2558
2559	<blockquote><para>
2560	    <programlisting>
2561    ScrnInfoPtr xf86ConfigPciEntity(ScrnInfoPtr pScrn,
2562                                    int scrnFlag, int entityIndex,
2563                                    PciChipsets *p_chip,
2564                                    void *res, EntityProc init,
2565                                    EntityProc enter, EntityProc leave,
2566                                    pointer private);
2567
2568	    </programlisting>
2569	    <blockquote><para>
2570      This functions is used to register the entity. The <parameter>res</parameter>, <parameter>init</parameter>, <parameter>enter</parameter>, and <parameter>leave</parameter> arguments are unused, and should be <constant>NULL</constant>.
2571      For active entities a <structname>ScrnInfoRec</structname> is allocated
2572      if the <parameter>pScrn</parameter> argument is <constant>NULL</constant>.
2573The
2574      return value is <constant>TRUE</constant> when successful.
2575	      </para>
2576
2577	    </blockquote></para></blockquote>
2578
2579	<para>
2580These two helper functions make use of several core functions that are
2581available at the driver level:
2582
2583	  <blockquote><para>
2584	      <programlisting>
2585    void xf86AddEntityToScreen(ScrnInfoPtr pScrn, int entityIndex);
2586	      </programlisting>
2587	      <blockquote><para>
2588      This function associates the entity referenced by
2589      <parameter>entityIndex</parameter> with the screen.
2590		</para>
2591
2592	      </blockquote></para></blockquote>
2593	</para>
2594      </sect3>
2595
2596      <sect3>
2597	<title>PreInit Phase</title>
2598
2599	<para>
2600During this phase the remaining resources should be registered.
2601<function>PreInit()</function> should call <function>xf86GetEntityInfo()</function>
2602to obtain a pointer to an <structname>EntityInfoRec</structname> for each entity
2603it is able to drive and check if any resource are listed in its
2604<structfield>resources</structfield> field.  If resources registered in the Probe
2605phase have been rejected in the post-Probe phase
2606(<structfield>resources</structfield> is non-<constant>NULL</constant>), then the driver should
2607decide if it can continue without using these or if it should fail.
2608	</para>
2609
2610	<blockquote><para>
2611	    <programlisting>
2612    EntityInfoPtr xf86GetEntityInfo(int entityIndex);
2613	    </programlisting>
2614	    <blockquote><para>
2615      This function returns a pointer to the <structname>EntityInfoRec</structname>
2616      referenced by <parameter>entityIndex</parameter>.  The returned
2617      <structname>EntityInfoRec</structname> should be freed with
2618      <function>xfree()</function> when no longer needed.
2619	      </para>
2620
2621	    </blockquote></para></blockquote>
2622
2623	<para>
2624Several functions are provided to simplify resource registration:
2625	  <blockquote><para>
2626	      <programlisting>
2627    Bool xf86IsEntityPrimary(int entityIndex);
2628	      </programlisting>
2629	      <blockquote><para>
2630      This function returns <constant>TRUE</constant> if the entity referenced
2631      by <parameter>entityIndex</parameter> is the primary display device (i.e.,
2632      the one initialised at boot time and used in text mode).
2633		</para>
2634
2635	      </blockquote></para></blockquote>
2636
2637	  <blockquote><para>
2638	      <programlisting>
2639    Bool xf86IsScreenPrimary(ScrnInfoPtr pScrn);
2640	      </programlisting>
2641	      <blockquote><para>
2642      This function returns <constant>TRUE</constant> if the primary entity
2643      is registered with the screen referenced by
2644      <parameter>pScrn</parameter>.
2645		</para>
2646
2647	      </blockquote></para></blockquote>
2648
2649	  <blockquote><para>
2650	      <programlisting>
2651    pciVideoPtr xf86GetPciInfoForEntity(int entityIndex);
2652	      </programlisting>
2653	      <blockquote><para>
2654      This function returns a pointer to the <structname>pciVideoRec</structname>
2655      for the specified entity.  If the entity is not a PCI device,
2656      <constant>NULL</constant> is returned.
2657		</para>
2658
2659	      </blockquote></para></blockquote>
2660	</para>
2661
2662<para>
2663Two functions are provided to obtain a resource range of a given type:
2664	  <blockquote><para>
2665	      <programlisting>
2666    resRange xf86GetBlock(long type, memType size,
2667                          memType window_start, memType window_end,
2668                          memType align_mask, resPtr avoid);
2669	      </programlisting>
2670	      <blockquote><para>
2671      This function tries to find a block range of size
2672      <parameter>size</parameter> and type <parameter>type</parameter> in a window
2673      bound by <parameter>window_start</parameter> and <parameter>window_end</parameter>
2674      with the alignment specified in <parameter>align_mask</parameter>.
2675      Optionally a list of resource ranges which should be avoided within
2676      the window can be supplied.  On failure a zero-length range of
2677      type <constant>ResEnd</constant> will be returned.
2678		</para>
2679	      </blockquote></para></blockquote>
2680
2681	  <blockquote><para>
2682	      <programlisting>
2683    resRange xf86GetSparse(long type, memType fixed_bits,
2684                           memType decode_mask, memType address_mask,
2685                           resPtr avoid);
2686	      </programlisting>
2687	      <blockquote><para>
2688      This function is like the previous one, but attempts to find a
2689      sparse range instead of a block range.  Here three values have to
2690      be specified: the <parameter>address_mask</parameter> which marks all
2691      bits of the mask part of the address, the <parameter>decode_mask</parameter>
2692      which masks out the bits which are hardcoded and are therefore
2693      not available for relocation and the values of the fixed bits.
2694      The function tries to find a base that satisfies the given condition.
2695      If the function fails it will return a zero range of type
2696      <constant>ResEnd</constant>.  Optionally it might be passed a list of
2697      resource ranges to avoid.
2698		</para>
2699
2700	      </blockquote></para></blockquote>
2701	</para>
2702
2703	<para>
2704	  <blockquote><para>
2705	      <programlisting>
2706    Bool xf86CheckPciMemBase(pciVideoPtr pPci, memType base);
2707	      </programlisting>
2708	      <blockquote><para>
2709      This function checks that the memory base address specified matches
2710      one of the PCI base address register values for the given PCI
2711      device.  This is mostly used to check that an externally provided
2712      base address (e.g., from a config file) matches an actual value
2713      allocated to a device.
2714		</para>
2715
2716	      </blockquote></para></blockquote>
2717	</para>
2718
2719	<para>
2720The following two functions are provided for special cases:
2721	  <blockquote><para>
2722	      <programlisting>
2723    void xf86RemoveEntityFromScreen(ScrnInfoPtr pScrn, int entityIndex);
2724	      </programlisting>
2725	      <blockquote><para>
2726      This function may be used to remove an entity from a screen.  This
2727      only makes sense if a screen has more than one entity assigned or
2728      the screen is to be deleted.  No test is made if the screen has
2729      any entities left.
2730		</para>
2731
2732	      </blockquote></para></blockquote>
2733	</para>
2734
2735      </sect3>
2736
2737      <sect3>
2738	<title>ScreenInit Phase</title>
2739
2740	<para>
2741All that is required in this phase is to setup the RAC flags.  Note that
2742it is also permissible to set these flags up in the PreInit phase.  The
2743RAC flags are held in the <structfield>racIoFlags</structfield> and <structfield>racMemFlags</structfield> fields of the
2744<structname>ScrnInfoRec</structname> for each screen.  They specify which graphics operations
2745might require the use of shared resources.  This can be specified
2746separately for memory and I/O resources.  The available flags are defined
2747in <filename>rac/xf86RAC.h</filename>.  They are:
2748
2749	  <variablelist>
2750	    <varlistentry><term><constant>RAC_FB</constant></term>
2751	      <listitem><para>
2752	for framebuffer operations (including hw acceleration)
2753		</para></listitem></varlistentry>
2754	    <varlistentry><term><constant>RAC_CURSOR</constant></term>
2755	      <listitem><para>
2756	for Cursor operations
2757        (??? I'm not sure if we need this for SW cursor it depends
2758         on which level the sw cursor is drawn)
2759		</para></listitem></varlistentry>
2760	    <varlistentry><term><constant>RAC_COLORMAP</constant></term>
2761	      <listitem><para>
2762	for colormap operations
2763		</para></listitem></varlistentry>
2764	    <varlistentry><term><constant>RAC_VIEWPORT</constant></term>
2765	      <listitem><para>
2766	for the call to <function>ChipAdjustFrame()</function>
2767		</para></listitem></varlistentry>
2768	    </variablelist>
2769
2770
2771The flags are ORed together.
2772	</para>
2773      </sect3>
2774    </sect2>
2775  </sect1>
2776
2777  <sect1 id="options">
2778    <title>Config file <quote>Option</quote> entries</title>
2779
2780    <para>
2781Option entries are permitted in most sections and subsections of the
2782config file.  There are two forms of option entries:
2783
2784      <variablelist>
2785	<varlistentry><term>Option "option-name"</term>
2786	  <listitem><para>
2787	A boolean option.
2788	    </para></listitem></varlistentry>
2789	<varlistentry><term>Option "option-name" "option-value"</term>
2790	  <listitem><para>
2791	An option with an arbitrary value.
2792	    </para></listitem></varlistentry>
2793      </variablelist>
2794    </para>
2795
2796    <para>
2797The option entries are handled by the parser, and a list of the parsed
2798options is included with each of the appropriate data structures that
2799the drivers have access to.  The data structures used to hold the option
2800information are opaque to the driver, and a driver must not access the
2801option data directly.  Instead, the common layer provides a set of
2802functions that may be used to access, check and manipulate the option
2803data.
2804    </para>
2805
2806    <para>
2807First, the low level option handling functions.  In most cases drivers
2808would not need to use these directly.
2809    </para>
2810
2811    <blockquote><para>
2812	<programlisting>
2813    XF86OptionPtr xf86FindOption(XF86OptionPtr options, const char *name);
2814	</programlisting>
2815	<blockquote><para>
2816      Takes a list of options and an option name, and returns a handle
2817      for the first option entry in the list matching the name.  Returns
2818      <constant>NULL</constant> if no match is found.
2819	  </para>
2820
2821	</blockquote></para></blockquote>
2822
2823    <blockquote><para>
2824	<programlisting>
2825    const char *xf86FindOptionValue(XF86OptionPtr options, const char *name);
2826	</programlisting>
2827	<blockquote><para>
2828      Takes a list of options and an option name, and returns the value
2829      associated with the first option entry in the list matching the
2830      name.  If the matching option has no value, an empty string
2831      (<constant>""</constant>) is returned.  Returns <constant>NULL</constant>
2832      if no match is found.
2833	  </para>
2834
2835	</blockquote></para></blockquote>
2836
2837    <blockquote><para>
2838	<programlisting>
2839    void xf86MarkOptionUsed(XF86OptionPtr option);
2840	</programlisting>
2841	<blockquote><para>
2842      Takes a handle for an option, and marks that option as used.
2843	  </para>
2844
2845	</blockquote></para></blockquote>
2846
2847    <blockquote><para>
2848	<programlisting>
2849    void xf86MarkOptionUsedByName(XF86OptionPtr options, const char *name);
2850	</programlisting>
2851	<blockquote><para>
2852      Takes a list of options and an option name and marks the first
2853      option entry in the list matching the name as used.
2854	  </para>
2855
2856	</blockquote></para></blockquote>
2857
2858    <para>
2859Next, the higher level functions that most drivers would use.
2860    </para>
2861    <blockquote><para>
2862	<programlisting>
2863    void xf86CollectOptions(ScrnInfoPtr pScrn, XF86OptionPtr extraOpts);
2864	</programlisting>
2865	<blockquote><para>
2866      Collect the options from each of the config file sections used by
2867      the screen (<parameter>pScrn</parameter>) and return the merged list as
2868      <structfield>pScrn-&gt;options</structfield>.  This function requires that
2869      <structfield>pScrn-&gt;confScreen</structfield>, <structfield>pScrn-&gt;display</structfield>,
2870      <structfield>pScrn-&gt;monitor</structfield>,
2871      <structfield>pScrn-&gt;numEntities</structfield>, and
2872      <structfield>pScrn-&gt;entityList</structfield> are initialised.
2873      <parameter>extraOpts</parameter> may optionally be set to an additional
2874      list of options to be combined with the others.  The order of
2875      precedence for options is <parameter>extraOpts</parameter>, display,
2876      confScreen, monitor, device.
2877	  </para>
2878
2879	</blockquote></para></blockquote>
2880
2881    <blockquote><para>
2882	<programlisting>
2883    void xf86ProcessOptions(int scrnIndex, XF86OptionPtr options,
2884                            OptionInfoPtr optinfo);
2885	</programlisting>
2886	<blockquote><para>
2887      Processes a list of options according to the information in the
2888      array of <structname>OptionInfoRecs</structname> (<parameter>optinfo</parameter>).
2889      The resulting information is stored in the <structfield>value</structfield>
2890      fields of the appropriate <parameter>optinfo</parameter> entries.  The
2891      <structfield>found</structfield> fields are set to <constant>TRUE</constant>
2892      when an option with a value of the correct type if found, and
2893      <constant>FALSE</constant> otherwise.  The <structfield>type</structfield> field
2894      is used to determine the expected value type for each option.
2895      Each option in the list of options for which there is a name match
2896      (but not necessarily a value type match) is marked as used.
2897      Warning messages are printed when option values don't match the
2898      types specified in the optinfo data.
2899	  </para>
2900
2901	  <para>
2902      NOTE: If this function is called before a driver's screen number
2903      is known (e.g., from the <function>ChipProbe()</function> function) a
2904      <parameter>scrnIndex</parameter> value of <constant>-1</constant> should be
2905      used.
2906	  </para>
2907
2908	  <para>
2909      NOTE 2: Given that this function stores into the
2910      <literal remap="tt">OptionInfoRecs</literal> pointed to by <parameter>optinfo</parameter>,
2911      the caller should ensure the <literal remap="tt">OptionInfoRecs</literal> are
2912      (re-)initialised before the call, especially if the caller expects
2913      to use the predefined option values as defaults.
2914	  </para>
2915
2916	  <para>
2917      The <structname>OptionInfoRec</structname> is defined as follows:
2918
2919	    <programlisting>
2920      typedef struct {
2921          double freq;
2922          int units;
2923      } OptFrequency;
2924
2925      typedef union {
2926          unsigned long       num;
2927          char *              str;
2928          double              realnum;
2929          Bool                bool;
2930          OptFrequency        freq;
2931      } ValueUnion;
2932
2933      typedef enum {
2934          OPTV_NONE = 0,
2935          OPTV_INTEGER,
2936          OPTV_STRING,  /* a non-empty string */
2937          OPTV_ANYSTR,  /* Any string, including an empty one */
2938          OPTV_REAL,
2939          OPTV_BOOLEAN,
2940          OPTV_PERCENT,
2941          OPTV_FREQ
2942      } OptionValueType;
2943
2944      typedef enum {
2945          OPTUNITS_HZ = 1,
2946          OPTUNITS_KHZ,
2947          OPTUNITS_MHZ
2948      } OptFreqUnits;
2949
2950      typedef struct {
2951          int                 token;
2952          const char*         name;
2953          OptionValueType     type;
2954          ValueUnion          value;
2955          Bool                found;
2956      } OptionInfoRec, *OptionInfoPtr;
2957	    </programlisting>
2958	  </para>
2959	  <para>
2960      <constant>OPTV_FREQ</constant> can be used for options values that are
2961      frequencies.  These values are a floating point number with an
2962      optional unit name appended.  The unit name can be one of "Hz",
2963      "kHz", "k", "MHz", "M".  The multiplier associated with the unit
2964      is stored in <structfield>freq.units</structfield>, and the scaled frequency
2965      is stored in <structfield>freq.freq</structfield>.  When no unit is specified,
2966      <structfield>freq.units</structfield> is set to <constant>0</constant>, and
2967      <structfield>freq.freq</structfield> is unscaled.
2968	  </para>
2969
2970	  <para>
2971      <constant>OPTV_PERCENT</constant> can be used for option values that are
2972      specified in percent (e.g. "20%"). These values are a floating point
2973      number with a percent sign appended. If the percent sign is missing,
2974      the parser will fail to match the value.
2975	  </para>
2976
2977	  <para>
2978      Typical usage is to setup an array of
2979      <structname>OptionInfoRec</structname>s with all fields initialised.
2980      The <structfield>value</structfield> and <structfield>found</structfield> fields get
2981      set by <function>xf86ProcessOptions()</function>.  For cases where the
2982      value parsing is more complex, the driver should specify
2983      <constant>OPTV_STRING</constant>, and parse the string itself.  An
2984      example of using this option handling is included in the
2985      <link linkend="sample">Sample Driver</link> section.
2986	  </para>
2987
2988	</blockquote></para></blockquote>
2989
2990    <blockquote><para>
2991	<programlisting>
2992    void xf86ShowUnusedOptions(int scrnIndex, XF86OptionPtr options);
2993	</programlisting>
2994	<blockquote><para>
2995      Prints out warning messages for each option in the list of options
2996      that isn't marked as used.  This is intended to show options that
2997      the driver hasn't recognised.  It would normally be called near
2998      the end of the <function>ChipScreenInit()</function> function, but only
2999      when <code>serverGeneration&nbsp;==&nbsp;1</code>
3000	  </para>
3001	</blockquote></para></blockquote>
3002
3003    <blockquote><para>
3004	<programlisting>
3005    OptionInfoPtr xf86TokenToOptinfo(const OptionInfoRec *table,
3006                                     int token);
3007	</programlisting>
3008	<blockquote><para>
3009      Returns a pointer to the <structname>OptionInfoRec</structname> in
3010      <parameter>table</parameter> with a token field matching
3011      <parameter>token</parameter>.  Returns <constant>NULL</constant> if no match
3012      is found.
3013	  </para>
3014
3015	</blockquote></para></blockquote>
3016
3017    <blockquote><para>
3018	<programlisting>
3019    Bool xf86IsOptionSet(const OptionInfoRec *table, int token);
3020	</programlisting>
3021	<blockquote><para>
3022      Returns the <literal remap="tt">found</literal> field of the
3023      <structname>OptionInfoRec</structname> in <parameter>table</parameter> with a
3024      <structfield>token</structfield> field matching <parameter>token</parameter>.  This
3025      can be used for options of all types.  Note that for options of
3026      type <constant>OPTV_BOOLEAN</constant>, it isn't sufficient to check
3027      this to determine the value of the option.  Returns
3028      <constant>FALSE</constant> if no match is found.
3029	  </para>
3030
3031	</blockquote></para></blockquote>
3032
3033    <blockquote><para>
3034	<programlisting>
3035    char *xf86GetOptValString(const OptionInfoRec *table, int token);
3036	</programlisting>
3037	<blockquote><para>
3038      Returns the <structfield>value.str</structfield> field of the
3039      <structname>OptionInfoRec</structname> in <parameter>table</parameter> with a
3040      token field matching <parameter>token</parameter>.  Returns
3041      <constant>NULL</constant> if no match is found.
3042	  </para>
3043
3044	</blockquote></para></blockquote>
3045
3046	  <blockquote><para>
3047	      <programlisting>
3048    Bool xf86GetOptValInteger(const OptionInfoRec *table, int token,
3049
3050                                int *value);
3051	      </programlisting>
3052	      <blockquote><para>
3053      Returns via <parameter>*value</parameter> the <structfield>value.num</structfield>
3054      field of the <structname>OptionInfoRec</structname> in <parameter>table</parameter>
3055      with a <structfield>token</structfield> field matching <parameter>token</parameter>.
3056      <parameter>*value</parameter> is only changed when a match is found so
3057      it can be safely initialised with a default prior to calling this
3058      function.  The function return value is as for
3059      <function>xf86IsOptionSet()</function>.
3060	  </para>
3061
3062	</blockquote></para></blockquote>
3063
3064    <blockquote><para>
3065	<programlisting>
3066    Bool xf86GetOptValULong(const OptionInfoRec *table, int token,
3067                            unsigned long *value);
3068	</programlisting>
3069	<blockquote><para>
3070      Like <function>xf86GetOptValInteger()</function>, except the value is
3071      treated as an <type>unsigned long</type>.
3072	  </para>
3073
3074	</blockquote></para></blockquote>
3075
3076    <blockquote><para>
3077	<programlisting>
3078    Bool xf86GetOptValReal(const OptionInfoRec *table, int token,
3079                           double *value);
3080	</programlisting>
3081	<blockquote><para>
3082      Like <function>xf86GetOptValInteger()</function>, except that
3083      <structfield>value.realnum</structfield> is used.
3084	  </para>
3085
3086	</blockquote></para></blockquote>
3087
3088    <blockquote><para>
3089	<programlisting>
3090    Bool xf86GetOptValFreq(const OptionInfoRec *table, int token,
3091                           OptFreqUnits expectedUnits, double *value);
3092	</programlisting>
3093	<blockquote><para>
3094      Like <function>xf86GetOptValInteger()</function>, except that the
3095      <structfield>value.freq</structfield> data is returned.  The frequency value
3096      is scaled to the units indicated by <parameter>expectedUnits</parameter>.
3097      The scaling is exact when the units were specified explicitly in
3098      the option's value.  Otherwise, the <parameter>expectedUnits</parameter>
3099      field is used as a hint when doing the scaling.  In this case,
3100      values larger than <constant>1000</constant> are assumed to have be
3101      specified in the next smallest units.  For example, if the Option
3102      value is "10000" and expectedUnits is <constant>OPTUNITS_MHZ</constant>,
3103      the value returned is <constant>10</constant>.
3104	  </para>
3105
3106	</blockquote></para></blockquote>
3107
3108    <blockquote><para>
3109      <programlisting>
3110    Bool xf86GetOptValBool(const OptionInfoRec *table, int token, Bool *value);
3111      </programlisting>
3112	<blockquote><para>
3113      This function is used to check boolean options
3114      (<constant>OPTV_BOOLEAN</constant>).  If the function return value is
3115      <constant>FALSE</constant>, it means the option wasn't set.  Otherwise
3116      <parameter>*value</parameter> is set to the boolean value indicated by
3117      the option's value.  No option <parameter>value</parameter> is interpreted
3118      as <constant>TRUE</constant>.  Option values meaning <constant>TRUE</constant>
3119      are "1", "yes", "on", "true", and option values meaning
3120      <constant>FALSE</constant> are "0", "no", "off", "false".  Option names
3121      both with the "no" prefix in their names, and with that prefix
3122      removed are also checked and handled in the obvious way.
3123      <parameter>*value</parameter> is not changed when the option isn't present.
3124      It should normally be set to a default value before calling this
3125      function.
3126	  </para>
3127
3128	</blockquote></para></blockquote>
3129
3130    <blockquote><para>
3131	<programlisting>
3132    Bool xf86ReturnOptValBool(const OptionInfoRec *table, int token, Bool def);
3133	</programlisting>
3134	<blockquote><para>
3135      This function is used to check boolean options
3136      (<constant>OPTV_BOOLEAN</constant>).  If the option is set, its value
3137      is returned.  If the options is not set, the default value specified
3138      by <parameter>def</parameter> is returned.  The option interpretation is
3139      the same as for <function>xf86GetOptValBool()</function>.
3140	  </para>
3141
3142	</blockquote></para></blockquote>
3143
3144    <blockquote><para>
3145	<programlisting>
3146    int xf86NameCmp(const char *s1, const char *s2);
3147	</programlisting>
3148	<blockquote><para>
3149      This function should be used when comparing strings from the config
3150      file with expected values.  It works like <function>strcmp()</function>,
3151      but is not case sensitive and space, tab, and <quote><literal>_</literal></quote> characters
3152      are ignored in the comparison.  The use of this function isn't
3153      restricted to parsing option values.  It may be used anywhere
3154      where this functionality required.
3155	  </para>
3156
3157	</blockquote></para></blockquote>
3158  </sect1>
3159
3160  <sect1>
3161    <title>Modules, Drivers, Include Files and Interface Issues</title>
3162
3163    <para>
3164NOTE: this section is incomplete.
3165    </para>
3166
3167
3168    <sect2>
3169      <title>Include files</title>
3170
3171      <para>
3172The following include files are typically required by video drivers:
3173
3174	<blockquote><para>
3175  All drivers should include these:
3176	    <literallayout><filename>
3177    "xf86.h"
3178    "xf86_OSproc.h"
3179    "xf86_ansic.h"
3180    "xf86Resources.h"
3181	    </filename></literallayout>
3182  Wherever inb/outb (and related things) are used the following should be
3183  included:
3184	    <literallayout><filename>
3185    "compiler.h"
3186	      </filename></literallayout>
3187  Note: in drivers, this must be included after <filename>"xf86_ansic.h"</filename>.
3188	  </para>
3189
3190	  <para>
3191  Drivers that need to access the PCI config space need this:
3192	    <literallayout><filename>
3193    "xf86Pci.h"
3194	      </filename></literallayout>
3195	  </para>
3196
3197	  <para>
3198  Drivers that initialise a SW cursor need this:
3199	    <literallayout><filename>
3200    "mipointer.h"
3201	      </filename></literallayout>
3202	  </para>
3203
3204	  <para>
3205  All drivers using the mi colourmap code need this:
3206	    <literallayout><filename>
3207    "micmap.h"
3208	      </filename></literallayout>
3209	  </para>
3210
3211	  <para>
3212  If a driver uses the vgahw module, it needs this:
3213	    <literallayout><filename>
3214    "vgaHW.h"
3215	      </filename></literallayout>
3216	  </para>
3217
3218	  <para>
3219  Drivers supporting VGA or Hercules monochrome screens need:
3220	    <literallayout><filename>
3221    "xf1bpp.h"
3222	      </filename></literallayout>
3223	  </para>
3224
3225	  <para>
3226  Drivers supporting VGA or EGC 16-colour screens need:
3227	    <literallayout><filename>
3228    "xf4bpp.h"
3229	      </filename></literallayout>
3230	  </para>
3231
3232	  <para>
3233  Drivers using cfb need:
3234	    <programlisting>
3235    #define PSZ 8
3236    #include "cfb.h"
3237    #undef PSZ
3238	    </programlisting>
3239	  </para>
3240
3241	  <para>
3242  Drivers supporting bpp 16, 24 or 32 with cfb need one or more of:
3243	    <literallayout><filename>
3244    "cfb16.h"
3245    "cfb24.h"
3246    "cfb32.h"
3247	      </filename></literallayout>
3248	  </para>
3249
3250	  <para>
3251  If a driver uses the fb manager, it needs this:
3252	    <literallayout><filename>
3253    "xf86fbman.h"
3254	      </filename></literallayout>
3255	  </para>
3256	</blockquote>
3257      </para>
3258
3259      <para>
3260Non-driver modules should include <filename>"xf86_ansic.h"</filename> to get the correct
3261wrapping of ANSI C/libc functions.
3262      </para>
3263
3264      <para>
3265All modules must NOT include any system include files, or the following:
3266
3267	<literallayout><filename>
3268    "xf86Priv.h"
3269    "xf86Privstr.h"
3270    "xf86_OSlib.h"
3271    "Xos.h"
3272	  </filename></literallayout>
3273      </para>
3274
3275      <para>
3276In addition, "xf86_libc.h" must not be included explicitly.  It is
3277included implicitly by "xf86_ansic.h".
3278      </para>
3279
3280    </sect2>
3281  </sect1>
3282
3283  <sect1>
3284    <title>Offscreen Memory Manager</title>
3285
3286    <para>
3287Management of offscreen video memory may be handled by the XFree86
3288framebuffer manager.  Once the offscreen memory manager is running,
3289drivers or extensions may allocate, free or resize areas of offscreen
3290video memory using the following functions (definitions taken from
3291<filename>xf86fbman.h</filename>):
3292
3293      <programlisting>
3294    typedef struct _FBArea {
3295        ScreenPtr    pScreen;
3296        BoxRec       box;
3297        int          granularity;
3298        void         (*MoveAreaCallback)(struct _FBArea*, struct _FBArea*)
3299        void         (*RemoveAreaCallback)(struct _FBArea*)
3300        DevUnion     devPrivate;
3301    } FBArea, *FBAreaPtr;
3302
3303    typedef void (*MoveAreaCallbackProcPtr)(FBAreaPtr from, FBAreaPtr to)
3304    typedef void (*RemoveAreaCallbackProcPtr)(FBAreaPtr)
3305
3306    FBAreaPtr xf86AllocateOffscreenArea (
3307        ScreenPtr pScreen,
3308        int width, int height,
3309        int granularity,
3310        MoveAreaCallbackProcPtr MoveAreaCallback,
3311        RemoveAreaCallbackProcPtr RemoveAreaCallback,
3312        pointer privData
3313    )
3314
3315    void xf86FreeOffscreenArea (FBAreaPtr area)
3316
3317    Bool xf86ResizeOffscreenArea (
3318	FBAreaPtr area
3319	int w, int h
3320    )
3321      </programlisting>
3322    </para>
3323
3324    <para>
3325The function:
3326      <programlisting>
3327    Bool xf86FBManagerRunning(ScreenPtr pScreen);
3328      </programlisting>
3329
3330can be used by an extension to check if the driver has initialized
3331the memory manager.  The manager is not available if this returns
3332<constant>FALSE</constant> and the functions above will all fail.
3333    </para>
3334
3335
3336    <para>
3337<function>xf86AllocateOffscreenArea()</function> can be used to request a
3338rectangle of dimensions <parameter>width</parameter>&nbsp;&times;&nbsp;<parameter>height</parameter>
3339(in pixels) from unused offscreen memory.  <parameter>granularity</parameter>
3340specifies that the leftmost edge of the rectangle must lie on some
3341multiple of <parameter>granularity</parameter> pixels.  A granularity of zero
3342means the same thing as a granularity of one - no alignment preference.
3343A <parameter>MoveAreaCallback</parameter> can be provided to notify the requester
3344when the offscreen area is moved.  If no <parameter>MoveAreaCallback</parameter>
3345is supplied then the area is considered to be immovable.  The
3346<parameter>privData</parameter> field will be stored in the manager's internal
3347structure for that allocated area and will be returned to the requester
3348in the <parameter>FBArea</parameter> passed via the
3349<parameter>MoveAreaCallback</parameter>.  An optional
3350<parameter>RemoveAreaCallback</parameter> is provided.  If the driver provides
3351this it indicates that the area should be allocated with a lower priority.
3352Such an area may be removed when a higher priority request (one that
3353doesn't have a <parameter>RemoveAreaCallback</parameter>) is made.  When this
3354function is called, the driver will have an opportunity to do whatever
3355cleanup it needs to do to deal with the loss of the area, but it must
3356finish its cleanup before the function exits since the offscreen memory
3357manager will free the area immediately after.
3358    </para>
3359
3360    <para>
3361<function>xf86AllocateOffscreenArea()</function> returns <constant>NULL</constant>
3362if it was unable to allocate the requested area.  When no longer needed,
3363areas should be freed with <function>xf86FreeOffscreenArea()</function>.
3364    </para>
3365
3366    <para>
3367<function>xf86ResizeOffscreenArea()</function> resizes an existing
3368<literal remap="tt">FBArea</literal>.  <function>xf86ResizeOffscreenArea()</function>
3369returns <constant>TRUE</constant> if the resize was successful.  If
3370<function>xf86ResizeOffscreenArea()</function> returns <constant>FALSE</constant>,
3371the original <literal remap="tt">FBArea</literal> is left unmodified.  Resizing an
3372area maintains the area's original <literal remap="tt">granularity</literal>,
3373<literal remap="tt">devPrivate</literal>, and <literal remap="tt">MoveAreaCallback</literal>.
3374<function>xf86ResizeOffscreenArea()</function> has considerably less overhead
3375than freeing the old area then reallocating the new size, so it should
3376be used whenever possible.
3377    </para>
3378
3379    <para>
3380The function:
3381      <programlisting>
3382    Bool xf86QueryLargestOffscreenArea(
3383      ScreenPtr pScreen,
3384      int *width, int *height,
3385      int granularity,
3386      int preferences,
3387      int priority
3388    );
3389	</programlisting>
3390
3391is provided to query the width and height of the largest single
3392<structname>FBArea</structname> allocatable given a particular priority.
3393<parameter>preferences</parameter> can be one of the following to indicate
3394whether width, height or area should be considered when determining
3395which is the largest single <structname>FBArea</structname> available.
3396
3397      <programlisting>
3398  FAVOR_AREA_THEN_WIDTH
3399  FAVOR_AREA_THEN_HEIGHT
3400  FAVOR_WIDTH_THEN_AREA
3401  FAVOR_HEIGHT_THEN_AREA
3402      </programlisting>
3403    </para>
3404
3405    <para>
3406<parameter>priority</parameter> is one of the following:
3407
3408      <blockquote>
3409	<para>
3410  <constant>PRIORITY_LOW</constant>
3411	  <blockquote><para>
3412     Return the largest block available without stealing anyone else's
3413     space.  This corresponds to the priority of allocating a
3414     <structname>FBArea</structname> when a <function>RemoveAreaCallback</function>
3415     is provided.
3416	    </para></blockquote>
3417	</para>
3418
3419	<para>
3420   <constant>PRIORITY_NORMAL</constant>
3421	  <blockquote><para>
3422     Return the largest block available if it is acceptable to steal a
3423     lower priority area from someone.  This corresponds to the priority
3424     of allocating a <structname>FBArea</structname> without providing a
3425     <function>RemoveAreaCallback</function>.
3426	    </para></blockquote>
3427	</para>
3428
3429	<para>
3430    <constant>PRIORITY_EXTREME</constant>
3431	  <blockquote><para>
3432     Return the largest block available if all <structname>FBArea</structname>s
3433     that aren't locked down were expunged from memory first.  This
3434     corresponds to any allocation made directly after a call to
3435     <function>xf86PurgeUnlockedOffscreenAreas()</function>.
3436	    </para></blockquote>
3437	</para>
3438
3439      </blockquote>
3440    </para>
3441
3442
3443    <para>
3444The function:
3445
3446      <programlisting>
3447    Bool xf86PurgeUnlockedOffscreenAreas(ScreenPtr pScreen);
3448      </programlisting>
3449
3450is provided as an extreme method to free up offscreen memory.  This
3451will remove all removable <structname>FBArea</structname> allocations.
3452    </para>
3453
3454
3455    <para>
3456Initialization of the XFree86 framebuffer manager is done via
3457
3458      <programlisting>
3459    Bool xf86InitFBManager(ScreenPtr pScreen, BoxPtr FullBox);
3460      </programlisting>
3461
3462<parameter>FullBox</parameter> represents the area of the framebuffer that the
3463manager is allowed to manage.  This is typically a box with a width of
3464<structfield>pScrn-&gt;displayWidth</structfield> and a height of as many lines as
3465can be fit within the total video memory, however, the driver can reserve
3466areas at the extremities by passing a smaller area to the manager.
3467    </para>
3468  </sect1>
3469
3470  <sect1 id="cmap">
3471    <title>Colormap Handling</title>
3472
3473    <para>
3474A generic colormap handling layer is provided within the XFree86 common
3475layer.  This layer takes care of most of the details, and only requires
3476a function from the driver that loads the hardware palette when required.
3477To use the colormap layer, a driver calls the
3478<function>xf86HandleColormaps()</function> function.
3479
3480      <blockquote><para>
3481	  <programlisting>
3482    Bool xf86HandleColormaps(ScreenPtr pScreen, int maxColors,
3483                             int sigRGBbits, LoadPaletteFuncPtr loadPalette,
3484                             SetOverscanFuncPtr setOverscan,
3485                             unsigned int flags);
3486	  </programlisting>
3487	  <blockquote><para>
3488      This function must be called after the default colormap has been
3489      initialised.  The <structfield>pScrn-&gt;gamma</structfield> field must also
3490      be initialised, preferably by calling <function>xf86SetGamma()</function>.
3491      <parameter>maxColors</parameter> is the number of entries in the palette.
3492      <parameter>sigRGBbits</parameter> is the size in bits of each color
3493      component in the DAC's palette.  <parameter>loadPalette</parameter>
3494      is a driver-provided function for loading a colormap into the
3495      hardware, and is described below.  <parameter>setOverscan</parameter> is
3496      an optional function that may be provided when the overscan color
3497      is an index from the standard LUT and when it needs to be adjusted
3498      to keep it as close to black as possible.  The
3499      <parameter>setOverscan</parameter> function programs the overscan index.
3500      It shouldn't normally be used for depths other than 8.
3501      <parameter>setOverscan</parameter> should be set to <constant>NULL</constant>
3502      when it isn't needed.  <parameter>flags</parameter> may be set to the
3503      following (which may be ORed together):
3504
3505	      <variablelist>
3506		<varlistentry>
3507		  <term><constant>CMAP_PALETTED_TRUECOLOR</constant></term>
3508		  <listitem><para>
3509				    the TrueColor visual is paletted and is
3510				    just a special case of DirectColor.
3511				    This flag is only valid for
3512				    <code>bpp&nbsp;&gt;&nbsp;8</code>.
3513
3514		    </para></listitem></varlistentry>
3515		<varlistentry>
3516		  <term><constant>CMAP_RELOAD_ON_MODE_SWITCH</constant></term>
3517		  <listitem><para>
3518				    reload the colormap automatically
3519                                    after mode switches.  This is useful
3520                                    for when the driver is resetting the
3521                                    hardware during mode switches and
3522                                    corrupting or erasing the hardware
3523                                    palette.
3524
3525		    </para></listitem></varlistentry>
3526		<varlistentry>
3527		  <term><constant>CMAP_LOAD_EVEN_IF_OFFSCREEN</constant></term>
3528		  <listitem><para>
3529				    reload the colormap even if the screen
3530				    is switched out of the server's VC.
3531				    The palette is <emphasis>not</emphasis> reloaded when
3532				    the screen is switched back in, nor after
3533				    mode switches.  This is useful when the
3534				    driver needs to keep track of palette
3535				    changes.
3536
3537		    </para></listitem></varlistentry>
3538	      </variablelist>
3539	    </para>
3540
3541	    <para>
3542      The colormap layer normally reloads the palette after VT enters so it
3543      is not necessary for the driver to save and restore the palette
3544      when switching VTs.  The driver must, however, still save the
3545      initial palette during server start up and restore it during
3546      server exit.
3547	    </para>
3548
3549	  </blockquote></para></blockquote>
3550
3551      <blockquote><para>
3552	  <programlisting>
3553    void LoadPalette(ScrnInfoPtr pScrn, int numColors, int *indices,
3554                     LOCO *colors, VisualPtr pVisual);
3555	  </programlisting>
3556	  <blockquote><para>
3557      <function>LoadPalette()</function> is a driver-provided function for
3558      loading a colormap into hardware.  <parameter>colors</parameter> is the
3559      array of RGB values that represent the full colormap.
3560      <parameter>indices</parameter> is a list of index values into the colors
3561      array.  These indices indicate the entries that need to be updated.
3562      <parameter>numColors</parameter> is the number of the indices to be
3563      updated.
3564	    </para>
3565
3566	  </blockquote></para></blockquote>
3567
3568      <blockquote><para>
3569	  <programlisting>
3570    void SetOverscan(ScrnInfoPtr pScrn, int overscan);
3571	  </programlisting>
3572	  <blockquote><para>
3573      <function>SetOverscan()</function> is a driver-provided function for
3574      programming the <parameter>overscan</parameter> index.  As described
3575      above, it is normally only appropriate for LUT modes where all
3576      colormap entries are available for the display, but where one of
3577      them is also used for the overscan (typically 8bpp for VGA compatible
3578      LUTs).  It isn't required in cases where the overscan area is
3579      never visible.
3580	    </para>
3581
3582	  </blockquote></para>
3583      </blockquote></para>
3584
3585  </sect1>
3586
3587  <sect1>
3588    <title>DPMS Extension</title>
3589
3590    <para>
3591Support code for the DPMS extension is included in the XFree86 common layer.
3592This code provides an interface between the main extension code, and a means
3593for drivers to initialise DPMS when they support it.  One function is
3594available to drivers to do this initialisation, and it is always available,
3595even when the DPMS extension is not supported by the core server (in
3596which case it returns a failure result).
3597    </para>
3598
3599    <blockquote><para>
3600	<programlisting>
3601    Bool xf86DPMSInit(ScreenPtr pScreen, DPMSSetProcPtr set, int flags);
3602	</programlisting>
3603	<blockquote><para>
3604      This function registers a driver's DPMS level programming function
3605      <parameter>set</parameter>.  It also checks
3606      <structfield>pScrn-&gt;options</structfield> for the "dpms" option, and when
3607      present marks DPMS as being enabled for that screen.  The
3608      <parameter>set</parameter> function is called whenever the DPMS level
3609      changes, and is used to program the requested level.
3610      <parameter>flags</parameter> is currently not used, and should be
3611      <constant>0</constant>.  If the initialisation fails for any reason,
3612      including when there is no DPMS support in the core server, the
3613      function returns <constant>FALSE</constant>.
3614	  </para>
3615
3616	</blockquote></para></blockquote>
3617
3618
3619    <para>
3620Drivers that implement DPMS support must provide the following function,
3621that gets called when the DPMS level is changed:
3622
3623
3624      <blockquote><para>
3625	  <programlisting>
3626    void ChipDPMSSet(ScrnInfoPtr pScrn, int level, int flags);
3627	  </programlisting>
3628	  <blockquote><para>
3629      Program the DPMS level specified by <parameter>level</parameter>.  Valid
3630      values of <parameter>level</parameter> are <constant>DPMSModeOn</constant>,
3631      <constant>DPMSModeStandby</constant>, <constant>DPMSModeSuspend</constant>,
3632      <constant>DPMSModeOff</constant>.  These values are defined in
3633      <filename>"extensions/dpms.h"</filename>.
3634	    </para>
3635
3636	  </blockquote></para></blockquote>
3637    </para>
3638
3639  </sect1>
3640
3641  <sect1>
3642    <title>DGA Extension</title>
3643
3644    <para>
3645Drivers can support the XFree86 Direct Graphics Architecture (DGA) by
3646filling out a structure of function pointers and a list of modes and
3647passing them to DGAInit.
3648    </para>
3649
3650    <blockquote><para>
3651	<programlisting>
3652    Bool DGAInit(ScreenPtr pScreen, DGAFunctionPtr funcs,
3653                 DGAModePtr modes, int num);
3654
3655/** The DGAModeRec **/
3656
3657typedef struct {
3658  int num;
3659  DisplayModePtr mode;
3660  int flags;
3661  int imageWidth;
3662  int imageHeight;
3663  int pixmapWidth;
3664  int pixmapHeight;
3665  int bytesPerScanline;
3666  int byteOrder;
3667  int depth;
3668  int bitsPerPixel;
3669  unsigned long red_mask;
3670  unsigned long green_mask;
3671  unsigned long blue_mask;
3672  int viewportWidth;
3673  int viewportHeight;
3674  int xViewportStep;
3675  int yViewportStep;
3676  int maxViewportX;
3677  int maxViewportY;
3678  int viewportFlags;
3679  int offset;
3680  unsigned char *address;
3681  int reserved1;
3682  int reserved2;
3683} DGAModeRec, *DGAModePtr;
3684	</programlisting>
3685
3686	<variablelist>
3687	  <varlistentry>
3688	    <term><structfield>num</structfield></term>
3689	    <listitem><para>
3690	 Can be ignored.  The DGA DDX will assign these numbers.
3691	      </para></listitem></varlistentry>
3692
3693	  <varlistentry>
3694	    <term><structfield>mode</structfield></term>
3695	    <listitem><para>
3696	A pointer to the <structname>DisplayModeRec</structname> for this mode.
3697	      </para></listitem></varlistentry>
3698
3699	  <varlistentry>
3700	    <term><structfield>flags</structfield></term>
3701	      <listitem><para>
3702	The following flags are defined and may be OR'd together:
3703
3704		  <variablelist>
3705		    <varlistentry>
3706		      <term><constant>DGA_CONCURRENT_ACCESS</constant></term>
3707		      <listitem><para>
3708            Indicates that the driver supports concurrent graphics
3709            accelerator and linear framebuffer access.
3710
3711			</para></listitem></varlistentry>
3712
3713		    <varlistentry>
3714		      <term><constant>DGA_FILL_RECT
3715			  DGA_BLIT_RECT
3716			  DGA_BLIT_RECT_TRANS</constant></term>
3717		      <listitem><para>
3718	    Indicates that the driver supports the FillRect, BlitRect
3719            or BlitTransRect functions in this mode.
3720
3721			</para></listitem></varlistentry>
3722
3723		    <varlistentry>
3724		      <term><constant>DGA_PIXMAP_AVAILABLE</constant></term>
3725		      <listitem><para>
3726	    Indicates that Xlib may be used on the framebuffer.
3727            This flag will usually be set unless the driver wishes
3728            to prohibit this for some reason.
3729
3730			</para></listitem></varlistentry>
3731
3732		    <varlistentry>
3733		      <term><constant>DGA_INTERLACED
3734			  DGA_DOUBLESCAN</constant></term>
3735		      <listitem><para>
3736            Indicates that these are interlaced or double scan modes.
3737
3738			</para></listitem></varlistentry>
3739		  </variablelist>
3740		</para></listitem></varlistentry>
3741
3742	  <varlistentry>
3743	    <term><structfield>imageWidth
3744		imageHeight</structfield></term>
3745	    <listitem><para>
3746		    These are the dimensions of the linear framebuffer
3747                     accessible by the client.
3748
3749	      </para></listitem></varlistentry>
3750
3751	  <varlistentry>
3752	    <term><structfield>pixmapWidth
3753		pixmapHeight</structfield></term>
3754	    <listitem><para>
3755		     These are the dimensions of the area of the
3756                     framebuffer accessible by the graphics accelerator.
3757
3758	      </para></listitem></varlistentry>
3759
3760	  <varlistentry>
3761	    <term><structfield>bytesPerScanline</structfield></term>
3762	    <listitem><para>
3763		      Pitch of the framebuffer in bytes.
3764
3765	      </para></listitem></varlistentry>
3766
3767	  <varlistentry>
3768	    <term><structfield>byteOrder</structfield></term>
3769	    <listitem><para>
3770		     Usually the same as
3771		     <structfield>pScrn-&gt;imageByteOrder</structfield>.
3772
3773	      </para></listitem></varlistentry>
3774
3775	  <varlistentry>
3776	    <term><structfield>depth</structfield></term>
3777	    <listitem><para>
3778		     The depth of the framebuffer in this mode.
3779
3780	      </para></listitem></varlistentry>
3781
3782	  <varlistentry>
3783	    <term><structfield>bitsPerPixel</structfield></term>
3784	    <listitem><para>
3785		      The number of bits per pixel in this mode.
3786
3787	      </para></listitem></varlistentry>
3788
3789	  <varlistentry>
3790	    <term><structfield>red_mask</structfield></term>
3791	    <term><structfield>green_mask</structfield></term>
3792	    <term><structfield>blue_mask</structfield></term>
3793	    <listitem><para>
3794		      The RGB masks for this mode, if applicable.
3795
3796	      </para></listitem></varlistentry>
3797
3798	  <varlistentry>
3799	    <term><structfield>viewportWidth</structfield></term>
3800	    <term><structfield>viewportHeight</structfield></term>
3801	    <listitem><para>
3802		      Dimensions of the visible part of the framebuffer.
3803		      Usually <structfield>mode-&gt;HDisplay</structfield> and
3804		      <structfield>mode-&gt;VDisplay</structfield>.
3805
3806	      </para></listitem></varlistentry>
3807
3808	  <varlistentry>
3809	    <term><structfield>xViewportStep
3810		yViewportStep</structfield></term>
3811	    <listitem><para>
3812		     The granularity of x and y viewport positions that
3813                     the driver supports in this mode.
3814
3815	      </para></listitem></varlistentry>
3816
3817	  <varlistentry>
3818	    <term><structfield>maxViewportX
3819		maxViewportY</structfield></term>
3820	    <listitem><para>
3821		      The maximum viewport position supported by the
3822                       driver in this mode.
3823	      </para></listitem></varlistentry>
3824
3825	  <varlistentry>
3826	    <term><structfield>viewportFlags</structfield></term>
3827	    <listitem><para>
3828		     The following may be OR'd together:
3829
3830		<variablelist>
3831		  <varlistentry>
3832		    <term><constant>DGA_FLIP_IMMEDIATE</constant></term>
3833		    <listitem><para>
3834		The driver supports immediate viewport changes.
3835		      </para></listitem></varlistentry>
3836
3837		  <varlistentry>
3838		    <term><constant>DGA_FLIP_RETRACE</constant></term>
3839
3840		    <listitem><para>
3841		The driver supports viewport changes at retrace.
3842		      </para></listitem></varlistentry>
3843		  </variablelist>
3844	      </para></listitem></varlistentry>
3845
3846	  <varlistentry>
3847	    <term><structfield>offset</structfield></term>
3848	    <listitem><para>
3849	        The offset into the linear framebuffer that corresponds to
3850                pixel (0,0) for this mode.
3851	      </para></listitem></varlistentry>
3852
3853	  <varlistentry>
3854	    <term><structfield>address</structfield></term>
3855	    <listitem><para>
3856                The virtual address of the framebuffer as mapped by the driver.
3857                This is needed when DGA_PIXMAP_AVAILABLE is set.
3858	      </para></listitem></varlistentry>
3859
3860	</variablelist>
3861
3862	<programlisting>
3863/** The DGAFunctionRec **/
3864
3865typedef struct {
3866  Bool (*OpenFramebuffer)(
3867       ScrnInfoPtr pScrn,
3868       char **name,
3869       unsigned char **mem,
3870       int *size,
3871       int *offset,
3872       int *extra
3873  );
3874  void (*CloseFramebuffer)(ScrnInfoPtr pScrn);
3875  Bool (*SetMode)(ScrnInfoPtr pScrn, DGAModePtr pMode);
3876  void (*SetViewport)(ScrnInfoPtr pScrn, int x, int y, int flags);
3877  int  (*GetViewport)(ScrnInfoPtr pScrn);
3878  void (*Sync)(ScrnInfoPtr);
3879  void (*FillRect)(
3880       ScrnInfoPtr pScrn,
3881       int x, int y, int w, int h,
3882       unsigned long color
3883  );
3884  void (*BlitRect)(
3885       ScrnInfoPtr pScrn,
3886       int srcx, int srcy,
3887       int w, int h,
3888       int dstx, int dsty
3889  );
3890  void (*BlitTransRect)(
3891       ScrnInfoPtr pScrn,
3892       int srcx, int srcy,
3893       int w, int h,
3894       int dstx, int dsty,
3895       unsigned long color
3896  );
3897} DGAFunctionRec, *DGAFunctionPtr;
3898	</programlisting>
3899
3900
3901	<blockquote><para>
3902	    <programlisting>
3903    Bool OpenFramebuffer (pScrn, name, mem, size, offset, extra);
3904	    </programlisting>
3905	    <blockquote><para>
3906      <function>OpenFramebuffer()</function> should pass the client everything
3907      it needs to know to be able to open the framebuffer.  These
3908      parameters are OS specific and their meanings are to be interpreted
3909      by an OS specific client library.
3910
3911		<variablelist>
3912		  <varlistentry>
3913		    <term><parameter>name</parameter></term>
3914		    <listitem><para>
3915	      The name of the device to open or <constant>NULL</constant> if
3916	      there is no special device to open.  A <constant>NULL</constant>
3917	      name tells the client that it should open whatever device
3918	      one would usually open to access physical memory.
3919		      </para></listitem></varlistentry>
3920
3921		  <varlistentry>
3922		    <term><parameter>mem</parameter></term>
3923		    <listitem><para>
3924	      The physical address of the start of the framebuffer.
3925		      </para></listitem></varlistentry>
3926
3927		  <varlistentry>
3928		    <term><parameter>size</parameter></term>
3929		    <listitem><para>
3930	      The size of the framebuffer in bytes.
3931		      </para></listitem></varlistentry>
3932
3933		  <varlistentry>
3934		    <term><parameter>offset</parameter></term>
3935		    <listitem><para>
3936	      Any offset into the device, if applicable.
3937		      </para></listitem></varlistentry>
3938
3939		  <varlistentry>
3940		    <term><parameter>flags</parameter></term>
3941		    <listitem><para>
3942	      Any additional information that the client may need.
3943	      Currently, only the <constant>DGA_NEED_ROOT</constant> flag is
3944	      defined.
3945		      </para></listitem></varlistentry>
3946
3947		</variablelist>
3948	      </para></blockquote>
3949	  </para></blockquote>
3950
3951	<blockquote><para>
3952	    <programlisting>
3953    void CloseFramebuffer (pScrn);
3954	    </programlisting>
3955	    <blockquote><para>
3956      <function>CloseFramebuffer()</function> merely informs the driver (if it
3957      even cares) that client no longer needs to access the framebuffer
3958      directly.  This function is optional.
3959	      </para>
3960
3961	    </blockquote></para></blockquote>
3962
3963	<blockquote><para>
3964	    <programlisting>
3965    Bool SetMode (pScrn, pMode);
3966	    </programlisting>
3967	    <blockquote><para>
3968      <function>SetMode()</function> tells the driver to initialize the mode
3969      passed to it.  If <parameter>pMode</parameter> is <constant>NULL</constant>,
3970      then the driver should restore the original pre-DGA mode.
3971	      </para>
3972
3973	    </blockquote></para></blockquote>
3974
3975	<blockquote><para>
3976	    <programlisting>
3977    void SetViewport (pScrn, x, y, flags);
3978	    </programlisting>
3979	    <blockquote><para>
3980      <function>SetViewport()</function> tells the driver to make the upper
3981      left-hand corner of the visible screen correspond to coordinate
3982      <literal remap="tt">(x,y)</literal> on the framebuffer.  <parameter>flags</parameter>
3983      currently defined are:
3984
3985		<variablelist>
3986		  <varlistentry>
3987		    <term><constant>DGA_FLIP_IMMEDIATE</constant></term>
3988		    <listitem><para>
3989	    The viewport change should occur immediately.
3990		      </para></listitem></varlistentry>
3991
3992		  <varlistentry>
3993		    <term><constant>DGA_FLIP_RETRACE</constant></term>
3994		    <listitem><para>
3995	    The viewport change should occur at the
3996            vertical retrace, but this function should
3997            return sooner if possible.
3998			</para></listitem></varlistentry>
3999		</variablelist>
4000	      </para>
4001
4002	      <para>
4003      The <literal remap="tt">(x,y)</literal> locations will be passed as the client
4004      specified them, however, the driver is expected to round these
4005      locations down to the next supported location as specified by the
4006      <structfield>xViewportStep</structfield> and <structfield>yViewportStep</structfield>
4007      for the current mode.
4008	      </para>
4009	    </blockquote></para></blockquote>
4010
4011	<blockquote><para>
4012	    <programlisting>
4013    int GetViewport (pScrn);
4014	    </programlisting>
4015	    <blockquote><para>
4016      <function>GetViewport()</function> gets the current page flip status.
4017      Set bits in the returned int correspond to viewport change requests
4018      still pending.  For instance, set bit zero if the last SetViewport
4019      request is still pending, bit one if the one before that is still
4020      pending, etc.
4021	      </para>
4022
4023	    </blockquote></para></blockquote>
4024
4025	<blockquote><para>
4026	    <programlisting>
4027    void Sync (pScrn);
4028	    </programlisting>
4029	    <blockquote><para>
4030      This function should ensure that any graphics accelerator operations
4031      have finished.  This function should not return until the graphics
4032      accelerator is idle.
4033	      </para>
4034
4035	    </blockquote></para></blockquote>
4036
4037	<blockquote><para>
4038	    <programlisting>
4039    void FillRect (pScrn, x, y, w, h, color);
4040	    </programlisting>
4041	    <blockquote><para>
4042      This optional function should fill a rectangle
4043      <parameter>w&nbsp;&times;&nbsp;h</parameter> located at
4044      <parameter>(x,y)</parameter> in the given color.
4045	      </para>
4046
4047	    </blockquote></para></blockquote>
4048
4049	<blockquote><para>
4050	    <programlisting>
4051    void BlitRect (pScrn, srcx, srcy, w, h, dstx, dsty);
4052	    </programlisting>
4053	    <blockquote><para>
4054      This optional function should copy an area
4055      <parameter>w&nbsp;&times;&nbsp;h</parameter> located at
4056      <parameter>(srcx,srcy)</parameter> to location <parameter>(dstx,dsty)</parameter>.
4057      This function will need to handle copy directions as appropriate.
4058	      </para>
4059
4060	    </blockquote></para></blockquote>
4061
4062	<blockquote><para>
4063	    <programlisting>
4064    void BlitTransRect (pScrn, srcx, srcy, w, h, dstx, dsty, color);
4065	    </programlisting>
4066	    <blockquote><para>
4067      This optional function is the same as BlitRect except that pixels
4068      in the source corresponding to the color key <parameter>color</parameter>
4069      should be skipped.
4070	      </para>
4071
4072	    </blockquote></para></blockquote>
4073    </para></blockquote>
4074
4075  </sect1>
4076
4077  <sect1>
4078    <title>The XFree86 X Video Extension (Xv) Device Dependent Layer</title>
4079
4080    <para>
4081XFree86 offers the X Video Extension which allows clients to treat video
4082as any another primitive and <quote>Put</quote> video into drawables.  By default,
4083the extension reports no video adaptors as being available since the
4084DDX layer has not been initialized.  The driver can initialize the DDX
4085layer by filling out one or more <literal remap="tt">XF86VideoAdaptorRecs</literal>
4086as described later in this document and passing a list of
4087<literal remap="tt">XF86VideoAdaptorPtr</literal> pointers to the following function:
4088
4089      <programlisting>
4090    Bool xf86XVScreenInit(ScreenPtr pScreen,
4091                          XF86VideoAdaptorPtr *adaptPtrs,
4092                          int num);
4093      </programlisting>
4094    </para>
4095
4096    <para>
4097After doing this, the extension will report video adaptors as being
4098available, providing the data in their respective
4099<literal remap="tt">XF86VideoAdaptorRecs</literal> was valid.
4100<function>xf86XVScreenInit()</function> <emphasis>copies</emphasis> data from the structure
4101passed to it so the driver may free it after the initialization.  At
4102the moment, the DDX only supports rendering into Window drawables.
4103Pixmap rendering will be supported after a sufficient survey of suitable
4104hardware is completed.
4105    </para>
4106
4107    <para>
4108The <structname>XF86VideoAdaptorRec</structname>:
4109
4110      <programlisting>
4111typedef struct {
4112	unsigned int type;
4113	int flags;
4114	char *name;
4115	int nEncodings;
4116	XF86VideoEncodingPtr pEncodings;
4117	int nFormats;
4118	XF86VideoFormatPtr pFormats;
4119	int nPorts;
4120	DevUnion *pPortPrivates;
4121	int nAttributes;
4122	XF86AttributePtr pAttributes;
4123	int nImages;
4124	XF86ImagePtr pImages;
4125	PutVideoFuncPtr PutVideo;
4126	PutStillFuncPtr PutStill;
4127	GetVideoFuncPtr GetVideo;
4128	GetStillFuncPtr GetStill;
4129	StopVideoFuncPtr StopVideo;
4130	SetPortAttributeFuncPtr SetPortAttribute;
4131	GetPortAttributeFuncPtr GetPortAttribute;
4132	QueryBestSizeFuncPtr QueryBestSize;
4133	PutImageFuncPtr PutImage;
4134	QueryImageAttributesFuncPtr QueryImageAttributes;
4135} XF86VideoAdaptorRec, *XF86VideoAdaptorPtr;
4136      </programlisting></para>
4137
4138    <para>
4139Each adaptor will have its own XF86VideoAdaptorRec.  The fields are
4140as follows:
4141
4142      <variablelist>
4143	<varlistentry>
4144	  <term><structfield>type</structfield></term>
4145	  <listitem><para>
4146	This can be any of the following flags OR'd together.
4147
4148	      <variablelist>
4149		<varlistentry>
4150		  <term><constant>XvInputMask</constant>
4151		    <constant>XvOutputMask</constant></term>
4152		  <listitem><para>
4153	    These refer to the target drawable and are similar to a Window's
4154	    class. <literal remap="tt">XvInputMask</literal> indicates that the adaptor
4155	    can put video into a drawable.  <literal remap="tt">XvOutputMask</literal>
4156	    indicates that the adaptor can get video from a drawable.
4157		    </para></listitem></varlistentry>
4158
4159		<varlistentry>
4160		  <term><constant>XvVideoMask</constant>
4161		    <constant>XvStillMask</constant>
4162		    <constant>XvImageMask</constant></term>
4163		  <listitem><para>
4164	    These indicate that the adaptor supports video, still or
4165	    image primitives respectively.
4166		    </para></listitem></varlistentry>
4167
4168		<varlistentry>
4169		  <term><constant>XvWindowMask</constant>
4170		    <constant>XvPixmapMask</constant></term>
4171		  <listitem><para>
4172	    These indicate the types of drawables the adaptor is capable
4173	    of rendering into.  At the moment, Pixmap rendering is not
4174	    supported and the <constant>XvPixmapMask</constant> flag is ignored.
4175		    </para></listitem></varlistentry>
4176	      </variablelist>
4177
4178	    </para></listitem></varlistentry>
4179
4180	<varlistentry>
4181	  <term><structfield>flags</structfield></term>
4182	  <listitem><para>
4183	Currently, the following flags are defined:
4184
4185	      <variablelist>
4186		<varlistentry>
4187		  <term><constant>VIDEO_OVERLAID_STILLS</constant></term>
4188		  <listitem><para>
4189	   Implementing PutStill for hardware that does video as an
4190	   overlay can be awkward since it's unclear how long to leave
4191	   the video up for.  When this flag is set, StopVideo will be
4192	   called whenever the destination gets clipped or moved so that
4193	   the still can be left up until then.
4194		    </para></listitem>
4195
4196		</varlistentry>
4197
4198		<varlistentry>
4199		  <term><constant>VIDEO_OVERLAID_IMAGES</constant></term>
4200		  <listitem><para>
4201	   Same as <constant>VIDEO_OVERLAID_STILLS</constant> but for images.
4202		    </para></listitem>
4203		</varlistentry>
4204
4205		<varlistentry>
4206		  <term><constant>VIDEO_CLIP_TO_VIEWPORT</constant></term>
4207		  <listitem><para>
4208	    Indicates that the clip region passed to the driver functions
4209	    should be clipped to the visible portion of the screen in the
4210	    case where the viewport is smaller than the virtual desktop.
4211		    </para></listitem></varlistentry>
4212
4213	      </variablelist>
4214	    </para></listitem></varlistentry>
4215
4216	<varlistentry>
4217	  <term><structfield>name</structfield></term>
4218	  <listitem><para>
4219	The name of the adaptor.
4220	    </para></listitem></varlistentry>
4221
4222
4223	<varlistentry>
4224	  <term><structfield>nEncodings</structfield>
4225	    <structfield>pEncodings</structfield></term>
4226	  <listitem><para>
4227	The number of encodings the adaptor is capable of and pointer
4228	to the <structname>XF86VideoEncodingRec</structname> array.  The
4229	<structname>XF86VideoEncodingRec</structname> is described later on.
4230	For drivers that only support XvImages there should be an encoding
4231	named "XV_IMAGE" and the width and height should specify
4232	the maximum size source image supported.
4233	  </para></listitem></varlistentry>
4234
4235
4236	<varlistentry>
4237	  <term><structfield>nFormats</structfield>
4238	      <structfield>pFormats</structfield></term>
4239	  <listitem><para>
4240	The number of formats the adaptor is capable of and pointer to
4241	the <structname>XF86VideoFormatRec</structname> array.  The
4242	<structname>XF86VideoFormatRec</structname> is described later on.
4243	    </para></listitem></varlistentry>
4244
4245
4246	<varlistentry>
4247	  <term><structfield>nPorts</structfield>
4248	    <structfield>pPortPrivates</structfield></term>
4249	  <listitem><para>
4250	The number of ports is the number of separate data streams which
4251	the adaptor can handle simultaneously.  If you have more than
4252	one port, the adaptor is expected to be able to render into more
4253	than one window at a time.  <structfield>pPortPrivates</structfield> is
4254	an array of pointers or ints - one for each port.  A port's
4255	private data will be passed to the driver any time the port is
4256	requested to do something like put the video or stop the video.
4257	In the case where there may be many ports, this enables the
4258	driver to know which port the request is intended for.  Most
4259	commonly, this will contain a pointer to the data structure
4260	containing information about the port.  In Xv, all ports on
4261	a particular adaptor are expected to be identical in their
4262	functionality.
4263	    </para></listitem></varlistentry>
4264
4265
4266	<varlistentry>
4267	  <term><structfield>nAttributes</structfield>
4268	    <structfield>pAttributes</structfield></term>
4269	  <listitem><para>
4270	The number of attributes recognized by the adaptor and a pointer to
4271	the array of <structname>XF86AttributeRecs</structname>.  The
4272	<structname>XF86AttributeRec</structname> is described later on.
4273	    </para></listitem></varlistentry>
4274
4275
4276	<varlistentry>
4277	  <term><structfield>nImages</structfield>
4278	    <structfield>pImages</structfield></term>
4279	  <listitem><para>
4280	The number of <structname>XF86ImageRecs</structname> supported by the adaptor
4281	and a pointer to the array of <structname>XF86ImageRecs</structname>. The
4282	<structname>XF86ImageRec</structname> is described later on.
4283	    </para></listitem></varlistentry>
4284
4285
4286	<varlistentry>
4287	  <term><structfield>
4288     PutVideo PutStill GetVideo GetStill StopVideo
4289     SetPortAttribute GetPortAttribute QueryBestSize PutImage
4290     QueryImageAttributes
4291	    </structfield></term>
4292	  <listitem><para>
4293	These functions define the DDX-&gt;driver interface.  In each
4294	case, the pointer <parameter>data</parameter> is passed to the driver.
4295	This is the port private for that port as described above.  All
4296	fields are required except under the following conditions:
4297
4298	      <orderedlist>
4299		<listitem><para>
4300	     <structfield>PutVideo</structfield>, <structfield>PutStill</structfield> and
4301	     the image routines <structfield>PutImage</structfield> and
4302	     <structfield>QueryImageAttributes</structfield> are not required when the
4303	     adaptor type does not contain <constant>XvInputMask</constant>.
4304		  </para></listitem>
4305
4306		<listitem><para>
4307	     <structfield>GetVideo</structfield> and <structfield>GetStill</structfield>
4308	     are not required when the adaptor type does not contain
4309	     <constant>XvOutputMask</constant>.
4310		  </para></listitem>
4311
4312		<listitem><para>
4313	     <structfield>GetVideo</structfield> and <structfield>PutVideo</structfield>
4314	     are not required when the adaptor type does not contain
4315	     <constant>XvVideoMask</constant>.
4316		  </para></listitem>
4317
4318		<listitem><para>
4319	     <structfield>GetStill</structfield> and <structfield>PutStill</structfield>
4320	     are not required when the adaptor type does not contain
4321	     <constant>XvStillMask</constant>.
4322		  </para></listitem>
4323
4324		<listitem><para>
4325	     <structfield>PutImage</structfield> and <structfield>QueryImageAttributes</structfield>
4326	     are not required when the adaptor type does not contain
4327	     <constant>XvImageMask</constant>.
4328		  </para></listitem>
4329
4330	      </orderedlist>
4331
4332	    </para>
4333
4334	    <para>
4335	With the exception of <structfield>QueryImageAttributes</structfield>, these
4336	functions should return <constant>Success</constant> if the operation was
4337	completed successfully.  They can return <constant>XvBadAlloc</constant>
4338	otherwise. <structfield>QueryImageAttributes</structfield> returns the size
4339	of the XvImage queried.
4340	    </para>
4341
4342	    <para>
4343	<literal remap="tt">ClipBoxes</literal> is an <literal remap="tt">X-Y</literal>
4344	banded region identical to those used throughout the server.
4345	The clipBoxes represent the visible portions of the area determined
4346	by <literal remap="tt">drw_x</literal>, <literal remap="tt">drw_y</literal>,
4347	<literal remap="tt">drw_w</literal> and <literal remap="tt">drw_h</literal> in the Get/Put
4348	function.  The boxes are in screen coordinates, are guaranteed
4349	not to overlap and an empty region will never be passed.
4350
4351	    </para></listitem></varlistentry>
4352      </variablelist>
4353
4354      <blockquote><para>
4355	  <programlisting>
4356    typedef  int (* PutVideoFuncPtr)( ScrnInfoPtr pScrn,
4357                   short vid_x, short vid_y, short drw_x, short drw_y,
4358                   short vid_w, short vid_h, short drw_w, short drw_h,
4359                   RegionPtr clipBoxes, pointer data );
4360	  </programlisting>
4361	      <blockquote><para>
4362	  This indicates that the driver should take a subsection
4363	  <parameter>vid_w</parameter> by <parameter>vid_h</parameter> at location
4364	  <parameter>(vid_x,vid_y)</parameter> from the video stream and direct
4365	  it into the rectangle <parameter>drw_w</parameter> by <parameter>drw_h</parameter>
4366	  at location <parameter>(drw_x,drw_y)</parameter> on the screen, scaling as
4367	  necessary.  Due to the large variations in capabilities of
4368	  the various hardware expected to be used with this extension,
4369	  it is not expected that all hardware will be able to do this
4370	  exactly as described.  In that case the driver should just do
4371	  <quote>the best it can,</quote> scaling as closely to the target rectangle
4372	  as it can without rendering outside of it.  In the worst case,
4373	  the driver can opt to just not turn on the video.
4374	      </para>
4375
4376	    </blockquote></para></blockquote>
4377
4378	  <blockquote><para>
4379	      <programlisting>
4380    typedef  int (* PutStillFuncPtr)( ScrnInfoPtr pScrn,
4381                   short vid_x, short vid_y, short drw_x, short drw_y,
4382                   short vid_w, short vid_h, short drw_w, short drw_h,
4383                   RegionPtr clipBoxes, pointer data );
4384	      </programlisting>
4385	      <blockquote><para>
4386	  This is same as <structfield>PutVideo</structfield> except that the driver
4387	  should place only one frame from the stream on the screen.
4388	      </para>
4389
4390	    </blockquote></para></blockquote>
4391
4392	  <blockquote><para>
4393	      <programlisting>
4394    typedef int (* GetVideoFuncPtr)( ScrnInfoPtr pScrn,
4395                  short vid_x, short vid_y, short drw_x, short drw_y,
4396                  short vid_w, short vid_h, short drw_w, short drw_h,
4397                  RegionPtr clipBoxes, pointer data );
4398	      </programlisting>
4399	      <blockquote><para>
4400	  This is same as <structfield>PutVideo</structfield> except that the driver
4401	  gets video from the screen and outputs it.  The driver should
4402	  do the best it can to get the requested dimensions correct
4403	  without reading from an area larger than requested.
4404	      </para>
4405
4406	    </blockquote></para></blockquote>
4407
4408	  <blockquote><para>
4409	      <programlisting>
4410    typedef int (* GetStillFuncPtr)( ScrnInfoPtr pScrn,
4411                  short vid_x, short vid_y, short drw_x, short drw_y,
4412                  short vid_w, short vid_h, short drw_w, short drw_h,
4413                  RegionPtr clipBoxes, pointer data );
4414	      </programlisting>
4415	      <blockquote><para>
4416	  This is the same as <literal remap="tt">GetVideo</literal> except that the
4417	  driver should place only one frame from the screen into the
4418	  output stream.
4419	      </para>
4420
4421	    </blockquote></para></blockquote>
4422
4423	  <blockquote><para>
4424	      <programlisting>
4425    typedef void (* StopVideoFuncPtr)(ScrnInfoPtr pScrn,
4426                                      pointer data, Bool cleanup);
4427	      </programlisting>
4428	      <blockquote><para>
4429	  This indicates the driver should stop displaying the video.
4430	  This is used to stop both input and output video.  The
4431	  <parameter>cleanup</parameter> field indicates that the video is
4432	  being stopped because the client requested it to stop or
4433	  because the server is exiting the current VT.  In that case
4434	  the driver should deallocate any offscreen memory areas (if
4435	  there are any) being used to put the video to the screen.  If
4436	  <parameter>cleanup</parameter> is not set, the video is being stopped
4437	  temporarily due to clipping or moving of the window, etc...
4438	  and video will likely be restarted soon so the driver should
4439	  not deallocate any offscreen areas associated with that port.
4440	      </para>
4441
4442	    </blockquote></para></blockquote>
4443	  <blockquote><para>
4444	      <programlisting>
4445    typedef int (* SetPortAttributeFuncPtr)(ScrnInfoPtr pScrn,
4446                                Atom attribute,INT32 value, pointer data);
4447	      </programlisting>
4448
4449	      <programlisting>
4450    typedef int (* GetPortAttributeFuncPtr)(ScrnInfoPtr pScrn,
4451                                Atom attribute,INT32 *value, pointer data);
4452	      </programlisting>
4453	      <blockquote><para>
4454	  A port may have particular attributes such as hue,
4455	  saturation, brightness or contrast.  Xv clients set and
4456	  get these attribute values by sending attribute strings
4457	  (Atoms) to the server.  Such requests end up at these
4458	  driver functions.  It is recommended that the driver provide
4459	  at least the following attributes mentioned in the Xv client
4460	  library docs:
4461		<literallayout><constant>
4462		XV_ENCODING
4463		XV_HUE
4464		XV_SATURATION
4465		XV_BRIGHTNESS
4466		XV_CONTRAST
4467		  </constant></literallayout>
4468	  but the driver may recognize as many atoms as it wishes.  If
4469	  a requested attribute is unknown by the driver it should return
4470	  <constant>BadMatch</constant>.  <constant>XV_ENCODING</constant> is the
4471	  attribute intended to let the client specify which video
4472	  encoding the particular port should be using (see the description
4473	  of <structname>XF86VideoEncodingRec</structname> below).  If the
4474	  requested encoding is unsupported, the driver should return
4475	  <constant>XvBadEncoding</constant>.  If the value lies outside the
4476	  advertised range <constant>BadValue</constant> may be returned.
4477	  <constant>Success</constant> should be returned otherwise.
4478	      </para>
4479
4480	    </blockquote></para></blockquote>
4481
4482	  <blockquote><para>
4483	    <programlisting>
4484    typedef void (* QueryBestSizeFuncPtr)(ScrnInfoPtr pScrn,
4485                   Bool motion, short vid_w, short vid_h,
4486                   short drw_w, short drw_h,
4487                   unsigned int *p_w, unsigned int *p_h, pointer data);
4488	    </programlisting>
4489	    <blockquote><para>
4490	   <function>QueryBestSize</function> provides the client with a way
4491	   to query what the destination dimensions would end up being
4492	   if they were to request that an area
4493	   <parameter>vid_w</parameter> by <parameter>vid_h</parameter> from the video
4494	   stream be scaled to rectangle of
4495	   <parameter>drw_w</parameter> by <parameter>drw_h</parameter> on the screen.
4496	   Since it is not expected that all hardware will be able to
4497	   get the target dimensions exactly, it is important that the
4498	   driver provide this function.
4499	      </para>
4500
4501	    </blockquote></para></blockquote>
4502
4503	<blockquote><para>
4504	    <programlisting>
4505    typedef  int (* PutImageFuncPtr)( ScrnInfoPtr pScrn,
4506                   short src_x, short src_y, short drw_x, short drw_y,
4507                   short src_w, short src_h, short drw_w, short drw_h,
4508                   int image, char *buf, short width, short height,
4509                   Bool sync, RegionPtr clipBoxes, pointer data );
4510	      </programlisting>
4511	      <blockquote><para>
4512	  This is similar to <structfield>PutStill</structfield> except that the
4513	  source of the video is not a port but the data stored in a system
4514	  memory buffer at <parameter>buf</parameter>.  The data is in the format
4515	  indicated by the <parameter>image</parameter> descriptor and represents a
4516	  source of size <parameter>width</parameter> by <parameter>height</parameter>.
4517	  If <parameter>sync</parameter> is TRUE the driver should not return
4518	  from this function until it is through reading the data
4519	  from <parameter>buf</parameter>.  Returning when <parameter>sync</parameter>
4520	  is TRUE indicates that it is safe for the data at <parameter>buf</parameter>
4521	  to be replaced, freed, or modified.
4522	      </para>
4523
4524	    </blockquote></para></blockquote>
4525
4526	<blockquote><para>
4527	    <programlisting>
4528    typedef  int (* QueryImageAttributesFuncPtr)( ScrnInfoPtr pScrn,
4529                                int image, short *width, short *height,
4530                                int *pitches, int *offsets);
4531	    </programlisting>
4532	    <blockquote><para>
4533	  This function is called to let the driver specify how data for
4534	  a particular <parameter>image</parameter> of size <parameter>width</parameter>
4535	  by <parameter>height</parameter> should be stored.  Sometimes only
4536	  the size and corrected width and height are needed.  In that
4537	  case <parameter>pitches</parameter> and <parameter>offsets</parameter> are
4538	  NULL.  The size of the memory required for the image is returned
4539	  by this function.  The <parameter>width</parameter> and
4540	  <parameter>height</parameter> of the requested image can be altered by
4541	  the driver to reflect format limitations (such as component
4542	  sampling periods that are larger than one).  If
4543	  <parameter>pitches</parameter> and <parameter>offsets</parameter> are not NULL,
4544	  these will be arrays with as many elements in them as there
4545	  are planes in the <parameter>image</parameter> format.  The driver
4546	  should specify the pitch (in bytes) of each scanline in the
4547	  particular plane as well as the offset to that plane (in bytes)
4548	  from the beginning of the image.
4549	      </para>
4550
4551	  </blockquote></para></blockquote>
4552    </para>
4553
4554    <para>
4555The XF86VideoEncodingRec:
4556
4557      <blockquote><para>
4558	  <programlisting>
4559typedef struct {
4560	int id;
4561	char *name;
4562	unsigned short width, height;
4563	XvRationalRec rate;
4564} XF86VideoEncodingRec, *XF86VideoEncodingPtr;
4565
4566	  </programlisting>
4567	  <blockquote><para>
4568   The <structname>XF86VideoEncodingRec</structname> specifies what encodings
4569   the adaptor can support.  Most of this data is just informational
4570   and for the client's benefit, and is what will be reported by
4571   <function>XvQueryEncodings</function>.  The <parameter>id</parameter> field is
4572   expected to be a unique identifier to allow the client to request a
4573   certain encoding via the <constant>XV_ENCODING</constant> attribute string.
4574
4575	    </para>
4576	  </blockquote></para></blockquote>
4577    </para>
4578
4579    <para>
4580The XF86VideoFormatRec:
4581
4582      <blockquote><para>
4583	  <programlisting>
4584typedef struct {
4585	char  depth;
4586	short class;
4587} XF86VideoFormatRec, *XF86VideoFormatPtr;
4588
4589	  </programlisting>
4590	  <blockquote><para>
4591    This specifies what visuals the video is viewable in.
4592    <parameter>depth</parameter> is the depth of the visual (not bpp).
4593    <parameter>class</parameter> is the visual class such as
4594    <constant>TrueColor</constant>, <constant>DirectColor</constant> or
4595    <constant>PseudoColor</constant>.  Initialization of an adaptor will fail
4596    if none of the visuals on that screen are supported.
4597	    </para>
4598
4599	  </blockquote></para></blockquote>
4600    </para>
4601
4602    <para>
4603The XF86AttributeRec:
4604
4605      <blockquote><para>
4606	  <programlisting>
4607typedef struct {
4608	int   flags;
4609	int   min_value;
4610	int   max_value;
4611	char  *name;
4612} XF86AttributeListRec, *XF86AttributeListPtr;
4613
4614	  </programlisting>
4615	  <blockquote><para>
4616   Each adaptor may have an array of these advertising the attributes
4617   for its ports.  Currently defined flags are <literal remap="tt">XvGettable</literal>
4618   and <literal remap="tt">XvSettable</literal> which may be OR'd together indicating that
4619   attribute is <quote>gettable</quote> or <quote>settable</quote> by the client.  The
4620   <literal remap="tt">min</literal> and <literal remap="tt">max</literal> field specify the valid range
4621   for the value.  <literal remap="tt">Name</literal> is a text string describing the
4622   attribute by name.
4623	    </para>
4624
4625	  </blockquote></para></blockquote>
4626
4627    </para>
4628
4629    <para>
4630The XF86ImageRec:
4631
4632	<blockquote><para>
4633	    <programlisting>
4634typedef struct {
4635	int id;
4636	int type;
4637	int byte_order;
4638	char guid[16];
4639	int bits_per_pixel;
4640	int format;
4641	int num_planes;
4642
4643	/* for RGB formats */
4644	int depth;
4645	unsigned int red_mask;
4646	unsigned int green_mask;
4647	unsigned int blue_mask;
4648
4649	/* for YUV formats */
4650	unsigned int y_sample_bits;
4651	unsigned int u_sample_bits;
4652	unsigned int v_sample_bits;
4653	unsigned int horz_y_period;
4654	unsigned int horz_u_period;
4655	unsigned int horz_v_period;
4656	unsigned int vert_y_period;
4657	unsigned int vert_u_period;
4658	unsigned int vert_v_period;
4659	char component_order[32];
4660	int scanline_order;
4661} XF86ImageRec, *XF86ImagePtr;
4662
4663	    </programlisting>
4664	    <blockquote><para>
4665   XF86ImageRec describes how video source data is laid out in memory.
4666   The fields are as follows:
4667
4668		<variablelist>
4669		  <varlistentry>
4670		    <term><structfield>id</structfield></term>
4671		    <listitem><para>
4672	This is a unique descriptor for the format.  It is often good to
4673        set this value to the FOURCC for the format when applicable.
4674		      </para></listitem></varlistentry>
4675
4676		  <varlistentry>
4677		    <term><structfield>type</structfield></term>
4678		    <listitem><para>
4679	This is <constant>XvRGB</constant> or <constant>XvYUV</constant>.
4680		      </para></listitem></varlistentry>
4681
4682		  <varlistentry>
4683		    <term><structfield>byte_order</structfield></term>
4684		    <listitem><para>
4685	This is <constant>LSBFirst</constant> or <constant>MSBFirst</constant>.
4686		      </para></listitem></varlistentry>
4687
4688		  <varlistentry>
4689		    <term><structfield>guid</structfield></term>
4690		    <listitem><para>
4691	This is the Globally Unique IDentifier for the format.  When
4692	not applicable, all characters should be NULL.
4693		      </para></listitem></varlistentry>
4694
4695		  <varlistentry>
4696		    <term><structfield>bits_per_pixel</structfield></term>
4697		    <listitem><para>
4698	The number of bits taken up (but not necessarily used) by each
4699	pixel.  Note that for some planar formats which have fractional
4700	bits per pixel (such as IF09) this number may be rounded _down_.
4701		      </para></listitem></varlistentry>
4702
4703		  <varlistentry>
4704		    <term><structfield>format</structfield></term>
4705		    <listitem><para>
4706	This is <constant>XvPlanar</constant> or <constant>XvPacked</constant>.
4707		      </para></listitem></varlistentry>
4708
4709		  <varlistentry>
4710		    <term><structfield>num_planes</structfield></term>
4711		    <listitem><para>
4712	The number of planes in planar formats.  This should be set to
4713	one for packed formats.
4714		      </para></listitem></varlistentry>
4715
4716		  <varlistentry>
4717		    <term><structfield>depth</structfield></term>
4718		    <listitem><para>
4719	The significant bits per pixel in RGB formats (analogous to the
4720	depth of a pixmap format).
4721		      </para></listitem></varlistentry>
4722
4723		  <varlistentry>
4724		    <term><structfield>red_mask</structfield></term>
4725		    <term><structfield>green_mask</structfield></term>
4726		    <term><structfield>blue_mask</structfield></term>
4727		    <listitem><para>
4728	The red, green and blue bitmasks for packed RGB formats.
4729		      </para></listitem></varlistentry>
4730
4731		  <varlistentry>
4732		    <term><structfield>y_sample_bits</structfield></term>
4733		    <term><structfield>u_sample_bits</structfield></term>
4734		    <term><structfield>v_sample_bits</structfield></term>
4735		    <listitem><para>
4736	The y, u and v sample sizes (in bits).
4737		      </para></listitem></varlistentry>
4738
4739		  <varlistentry>
4740		    <term><structfield>horz_y_period</structfield></term>
4741		    <term><structfield>horz_u_period</structfield></term>
4742		    <term><structfield>horz_v_period</structfield></term>
4743		    <listitem><para>
4744	The y, u and v sampling periods in the horizontal direction.
4745		      </para></listitem></varlistentry>
4746
4747		  <varlistentry>
4748		    <term><structfield>vert_y_period</structfield></term>
4749		    <term><structfield>vert_u_period</structfield></term>
4750		    <term><structfield>vert_v_period</structfield></term>
4751		    <listitem><para>
4752	The y, u and v sampling periods in the vertical direction.
4753		      </para></listitem></varlistentry>
4754
4755		  <varlistentry>
4756		    <term><structfield>component_order</structfield></term>
4757		    <listitem><para>
4758	Uppercase ascii characters representing the order that
4759	samples are stored within packed formats.  For planar formats
4760	this represents the ordering of the planes.  Unused characters
4761	in the 32 byte string should be set to NULL.
4762		      </para></listitem></varlistentry>
4763
4764		  <varlistentry>
4765		    <term><structfield>scanline_order</structfield></term>
4766		    <listitem><para>
4767	This is <constant>XvTopToBottom</constant> or <constant>XvBottomToTop</constant>.
4768		      </para></listitem></varlistentry>
4769
4770		  </variablelist>
4771	    </para>
4772
4773	    <para>
4774  Since some formats (particular some planar YUV formats) may not
4775be completely defined by the parameters above, the guid, when
4776available, should provide the most accurate description of the
4777format.
4778	    </para>
4779
4780	  </blockquote></para></blockquote>
4781    </para>
4782  </sect1>
4783
4784  <sect1>
4785    <title>The Loader</title>
4786
4787    <para>
4788This section describes the interfaces to the module loader.  The loader
4789interfaces can be divided into two groups: those that are only available to
4790the XFree86 common layer, and those that are also available to modules.
4791    </para>
4792
4793    <sect2>
4794      <title>Loader Overview</title>
4795
4796      <para>
4797The loader is capable of loading modules in a range of object formats,
4798and knowledge of these formats is built in to the loader.  Knowledge of
4799new object formats can be added to the loader in a straightforward
4800manner.  This makes it possible to provide OS-independent modules (for
4801a given CPU architecture type).  In addition to this, the loader can
4802load modules via the OS-provided <function>dlopen(3)</function> service where
4803available.  Such modules are not platform independent, and the semantics
4804of <function>dlopen()</function> on most systems results in significant
4805limitations in the use of modules of this type.  Support for
4806<function>dlopen()</function> modules in the loader is primarily for
4807experimental and development purposes.
4808      </para>
4809
4810      <para>
4811Symbols exported by the loader (on behalf of the core X server) to
4812modules are determined at compile time.  Only those symbols explicitly
4813exported are available to modules.  All external symbols of loaded
4814modules are exported to other modules, and to the core X server.  The
4815loader can be requested to check for unresolved symbols at any time,
4816and the action to be taken for unresolved symbols can be controlled by
4817the caller of the loader.  Typically the caller identifies which symbols
4818can safely remain unresolved and which cannot.
4819      </para>
4820
4821      <para>
4822NOTE:  Now that ISO-C allows pointers to functions and pointers to data to
4823have different internal representations, some of the following interfaces
4824will need to be revisited.
4825      </para>
4826    </sect2>
4827
4828    <sect2>
4829      <title>Semi-private Loader Interface</title>
4830
4831      <para>
4832The following is the semi-private loader interface that is available to the
4833XFree86 common layer.
4834      </para>
4835
4836      <blockquote><para>
4837	  <programlisting>
4838    void LoaderInit(void);
4839	  </programlisting>
4840	  <blockquote><para>
4841    The <function>LoaderInit()</function> function initialises the loader,
4842    and it must be called once before calling any other loader functions.
4843    This function initialises the tables of exported symbols, and anything
4844    else that might need to be initialised.
4845	    </para>
4846
4847	  </blockquote></para></blockquote>
4848
4849      <blockquote><para>
4850	  <programlisting>
4851    void LoaderSetPath(const char *path);
4852	  </programlisting>
4853	  <blockquote><para>
4854    The <function>LoaderSetPath()</function> function initialises a default
4855    module search path.  This must be called if calls to other functions
4856    are to be made without explicitly specifying a module search path.
4857    The search path <parameter>path</parameter> must be a string of one or more
4858    comma separated absolute paths.  Modules are expected to be located
4859    below these paths, possibly in subdirectories of these paths.
4860	    </para>
4861
4862	  </blockquote></para></blockquote>
4863
4864      <blockquote><para>
4865	  <programlisting>
4866    pointer LoadModule(const char *module, pointer options,
4867                       const XF86ModReqInfo * modreq, int *errmaj);
4868	  </programlisting>
4869	  <blockquote><para>
4870    The <function>LoadModule()</function> function loads the module called
4871    <parameter>module</parameter>.  The return value is a module handle, and
4872    may be used in future calls to the loader that require a reference
4873    to a loaded module.  The module name <parameter>module</parameter> is
4874    normally the module's canonical name, which doesn't contain any
4875    directory path information, or any object/library file prefixes of
4876    suffixes.  Currently a full pathname and/or filename is also accepted.
4877    This might change.  The other parameters are:
4878
4879	      <variablelist>
4880
4881		<varlistentry>
4882		  <term><parameter>options</parameter></term>
4883		  <listitem><para>
4884		  An optional parameter that is passed to the newly
4885		  loaded module's <literal remap="tt">SetupProc</literal> function
4886		  (if it has one).  This argument is normally a
4887		  <constant>NULL</constant> terminated list of
4888		  <structname>Options</structname>, and must be interpreted that
4889		  way by modules loaded directly by the XFree86 common
4890		  layer.  However, it may be used for application-specific
4891		  parameter passing in other situations.
4892		    </para></listitem></varlistentry>
4893
4894
4895		<varlistentry>
4896		  <term><parameter>modreq</parameter></term>
4897		  <listitem><para>
4898		  An optional <structname>XF86ModReqInfo*</structname> containing
4899		  version/ABI/vendor information to requirements to
4900		  check the newly loaded module against.  The main
4901		  purpose of this is to allow the loader to verify that
4902		  a module of the correct type/version before running
4903		  its <function>SetupProc</function> function.
4904		      </para>
4905
4906		    <para>
4907		  The <literal remap="tt">XF86ModReqInfo</literal> struct is defined
4908		  as follows:
4909		      <programlisting>
4910typedef struct {
4911	CARD8        majorversion;
4912	CARD8        minorversion;
4913	CARD16       patchlevel;
4914	const char * abiclass;
4915	CARD32       abiversion;
4916	const char * moduleclass;
4917} XF86ModReqInfo;
4918			</programlisting>
4919
4920		  The information here is compared against the equivalent
4921		  information in the module's
4922		  <structname>XF86ModuleVersionInfo</structname> record (which
4923		  is described below).  The values in comments above
4924		  indicate <quote>don't care</quote> settings for each of the fields.
4925		  The comparisons made are as follows:
4926
4927		      <variablelist>
4928			<varlistentry>
4929			  <term><structfield>majorversion</structfield></term>
4930			  <listitem><para>
4931				   Must match the module's majorversion
4932				   exactly.
4933			    </para></listitem></varlistentry>
4934
4935			<varlistentry>
4936			  <term><structfield>minorversion</structfield></term>
4937			  <listitem><para>
4938				   The module's minor version must be
4939				   no less than this value.  This
4940				   comparison is only made if
4941				   <structfield>majorversion</structfield>
4942                                   matches.
4943			    </para></listitem></varlistentry>
4944
4945			<varlistentry>
4946			  <term><structfield>patchlevel</structfield></term>
4947			  <listitem><para>
4948				   The module's patchlevel must be no
4949				   less than this value.  This comparison
4950				   is only made if
4951				   <structfield>minorversion</structfield>
4952				   matches.
4953			    </para></listitem></varlistentry>
4954
4955			<varlistentry>
4956			  <term><structfield>abiclass</structfield></term>
4957			  <listitem><para>
4958				   String must match the module's abiclass
4959				   string.
4960			    </para></listitem></varlistentry>
4961
4962			<varlistentry>
4963			  <term><structfield>abiversion</structfield></term>
4964			  <listitem><para>
4965				   Must be consistent with the module's
4966				   abiversion (major equal, minor no
4967				   older).
4968			    </para></listitem></varlistentry>
4969
4970			<varlistentry>
4971			  <term><structfield>moduleclass</structfield></term>
4972			  <listitem><para>
4973				   String must match the module's
4974				   moduleclass string.
4975			    </para></listitem></varlistentry>
4976
4977		      </variablelist>
4978		    </para></listitem></varlistentry>
4979
4980		<varlistentry>
4981		  <term><parameter>errmaj</parameter></term>
4982		  <listitem><para>
4983		  An optional pointer to a variable holding the major
4984		  part or the error code.  When provided,
4985		  <parameter>*errmaj</parameter> is filled in when
4986		  <function>LoadModule()</function> fails.
4987		    </para></listitem></varlistentry>
4988
4989	      </variablelist>
4990
4991	    </para></blockquote>
4992	</para></blockquote>
4993
4994      <blockquote><para>
4995	  <programlisting>
4996    void UnloadModule(pointer mod);
4997	  </programlisting>
4998	  <blockquote><para>
4999    This function unloads the module referred to by the handle mod.
5000    All child modules are also unloaded recursively.  This function must
5001    not be used to directly unload modules that are child modules (i.e.,
5002    those that have been loaded with the <function>LoadSubModule()</function>
5003    described below).
5004	    </para>
5005
5006	  </blockquote></para></blockquote>
5007    </sect2>
5008
5009    <sect2>
5010      <title>Module Requirements</title>
5011
5012      <para>
5013Modules must provide information about themselves to the loader, and
5014may optionally provide entry points for "setup" and "teardown" functions
5015(those two functions are referred to here as <function>SetupProc</function>
5016and <function>TearDownProc</function>).
5017      </para>
5018
5019      <para>
5020The module information is contained in the
5021<structname>XF86ModuleVersionInfo</structname> struct, which is defined as follows:
5022
5023	<programlisting>
5024typedef struct {
5025    const char * modname;      /* name of module, e.g. "foo" */
5026    const char * vendor;       /* vendor specific string */
5027    CARD32       _modinfo1_;   /* constant MODINFOSTRING1/2 to find */
5028    CARD32       _modinfo2_;   /* infoarea with a binary editor/sign tool */
5029    CARD32       xf86version;  /* contains XF86_VERSION_CURRENT */
5030    CARD8        majorversion; /* module-specific major version */
5031    CARD8        minorversion; /* module-specific minor version */
5032    CARD16       patchlevel;   /* module-specific patch level */
5033    const char * abiclass;     /* ABI class that the module uses */
5034    CARD32       abiversion;   /* ABI version */
5035    const char * moduleclass;  /* module class */
5036    CARD32       checksum[4];  /* contains a digital signature of the */
5037                               /* version info structure */
5038} XF86ModuleVersionInfo;
5039	</programlisting>
5040
5041The fields are used as follows:
5042
5043	<variablelist>
5044	  <varlistentry>
5045	    <term><structfield>modname</structfield></term>
5046	    <listitem><para>
5047		The module's name.  This field is currently only for
5048		informational purposes, but the loader may be modified
5049		in future to require it to match the module's canonical
5050                name.
5051	      </para></listitem></varlistentry>
5052
5053
5054	  <varlistentry>
5055	    <term><structfield>vendor</structfield></term>
5056	    <listitem><para>
5057		The module vendor.  This field is for informational purposes
5058                only.
5059	      </para></listitem></varlistentry>
5060
5061
5062	  <varlistentry>
5063	    <term><structfield>_modinfo1_</structfield></term>
5064	    <listitem><para>
5065		This field holds the first part of a signature that can
5066                be used to locate this structure in the binary.  It should
5067		always be initialised to <constant>MODINFOSTRING1</constant>.
5068	      </para></listitem></varlistentry>
5069
5070
5071	  <varlistentry>
5072	    <term><structfield>_modinfo2_</structfield></term>
5073	    <listitem><para>
5074		This field holds the second part of a signature that can
5075                be used to locate this structure in the binary.  It should
5076		always be initialised to <constant>MODINFOSTRING2</constant>.
5077	      </para></listitem></varlistentry>
5078
5079
5080	  <varlistentry>
5081	    <term><structfield>xf86version</structfield></term>
5082	    <listitem><para>
5083		The XFree86 version against which the module was compiled.
5084                This is mostly for informational/diagnostic purposes.  It
5085		should be initialised to <constant>XF86_VERSION_CURRENT</constant>, which is
5086		defined in <filename>xf86Version.h</filename>.
5087	      </para></listitem></varlistentry>
5088
5089
5090	  <varlistentry>
5091	    <term><structfield>majorversion</structfield></term>
5092	    <listitem><para>
5093		The module-specific major version.  For modules where this
5094		version is used for more than simply informational
5095		purposes, the major version should only change (be
5096		incremented) when ABI incompatibilities are introduced,
5097		or ABI components are removed.
5098	      </para></listitem></varlistentry>
5099
5100
5101	  <varlistentry>
5102	    <term><structfield>minorversion</structfield></term>
5103	    <listitem><para>
5104		The module-specific minor version.  For modules where this
5105		version is used for more than simply informational
5106		purposes, the minor version should only change (be
5107		incremented) when ABI additions are made in a backward
5108		compatible way.  It should be reset to zero when the major
5109                version is increased.
5110	      </para></listitem></varlistentry>
5111
5112
5113	  <varlistentry>
5114	    <term><structfield>patchlevel</structfield></term>
5115	    <listitem><para>
5116		The module-specific patch level.  The patch level should
5117		increase with new revisions of the module where there
5118		are no ABI changes, and it should be reset to zero when
5119		the minor version is increased.
5120	      </para></listitem></varlistentry>
5121
5122
5123	  <varlistentry>
5124	    <term><structfield>abiclass</structfield></term>
5125	    <listitem><para>
5126		The ABI class that the module requires.  The class is
5127                specified as a string for easy extensibility.  It should
5128		indicate which (if any) of the X server's built-in ABI
5129		classes that the module relies on, or a third-party ABI
5130                if appropriate.  Built-in ABI classes currently defined are:
5131
5132		<variablelist>
5133		  <varlistentry>
5134		    <term><constant>ABI_CLASS_NONE</constant></term>
5135		    <listitem><para>no class
5136		      </para></listitem></varlistentry>
5137		  <varlistentry>
5138		    <term><constant>ABI_CLASS_ANSIC</constant></term>
5139		    <listitem><para>only requires the ANSI C interfaces
5140		      </para></listitem></varlistentry>
5141		  <varlistentry>
5142		    <term><constant>ABI_CLASS_VIDEODRV</constant></term>
5143		    <listitem><para>requires the video driver ABI
5144		      </para></listitem></varlistentry>
5145		  <varlistentry>
5146		    <term><constant>ABI_CLASS_XINPUT</constant></term>
5147		    <listitem><para>requires the XInput driver ABI
5148		      </para></listitem></varlistentry>
5149		  <varlistentry>
5150		    <term><constant>ABI_CLASS_EXTENSION</constant></term>
5151		    <listitem><para>requires the extension module ABI
5152		      </para></listitem></varlistentry>
5153		</variablelist>
5154
5155	      </para></listitem></varlistentry>
5156
5157	  <varlistentry>
5158	    <term><structfield>abiversion</structfield></term>
5159	    <listitem><para>
5160		The version of abiclass that the module requires.  The
5161		version consists of major and minor components.  The
5162		major version must match and the minor version must be
5163		no newer than that provided by the server or parent
5164		module.  Version identifiers for the built-in classes
5165		currently defined are:
5166
5167		<literallayout><constant>
5168                   ABI_ANSIC_VERSION
5169                   ABI_VIDEODRV_VERSION
5170                   ABI_XINPUT_VERSION
5171                   ABI_EXTENSION_VERSION
5172		  </constant></literallayout>
5173
5174	      </para></listitem></varlistentry>
5175
5176	  <varlistentry>
5177	    <term><structfield>moduleclass</structfield></term>
5178	    <listitem><para>
5179		This is similar to the abiclass field, except that it
5180		defines the type of module rather than the ABI it
5181		requires.  For example, although all video drivers require
5182		the video driver ABI, not all modules that require the
5183		video driver ABI are video drivers.  This distinction
5184		can be made with the moduleclass.  Currently pre-defined
5185		module classes are:
5186
5187		<literallayout><constant>
5188                   MOD_CLASS_NONE
5189                   MOD_CLASS_VIDEODRV
5190                   MOD_CLASS_XINPUT
5191                   MOD_CLASS_EXTENSION
5192		  </constant></literallayout>
5193
5194	      </para></listitem></varlistentry>
5195
5196	  <varlistentry>
5197	    <term><structfield>checksum</structfield></term>
5198	    <listitem><para>
5199		Not currently used.
5200	      </para></listitem></varlistentry>
5201
5202	</variablelist>
5203      </para>
5204
5205      <para>
5206The module version information, and the optional <function>SetupProc</function>
5207and <function>TearDownProc</function> entry points are found by the loader
5208by locating a data object in the module called "modnameModuleData",
5209where "modname" is the canonical name of the module.  Modules must
5210contain such a data object, and it must be declared with global scope,
5211be compile-time initialised, and is of the following type:
5212
5213	<programlisting>
5214typedef struct {
5215    XF86ModuleVersionInfo *     vers;
5216    ModuleSetupProc             setup;
5217    ModuleTearDownProc          teardown;
5218} XF86ModuleData;
5219	</programlisting>
5220      </para>
5221
5222      <para>
5223The vers parameter must be initialised to a pointer to a correctly
5224initialised <structname>XF86ModuleVersionInfo</structname> struct.  The other
5225two parameter are optional, and should be initialised to
5226<constant>NULL</constant> when not required.  The other parameters are defined
5227as
5228
5229	<blockquote><para>
5230	    <programlisting>
5231    typedef pointer (*ModuleSetupProc)(pointer, pointer, int *, int *);
5232
5233    typedef void (*ModuleTearDownProc)(pointer);
5234
5235    pointer SetupProc(pointer module, pointer options,
5236                      int *errmaj, int *errmin);
5237	    </programlisting>
5238	    <blockquote><para>
5239    When defined, this function is called by the loader after successfully
5240    loading a module.  module is a handle for the newly loaded module,
5241    and maybe used by the <function>SetupProc</function> if it calls other
5242    loader functions that require a reference to it.   The remaining
5243    arguments are those that were passed to the
5244    <function>LoadModule()</function> (or <function>LoadSubModule()</function>),
5245    and are described above. When the <function>SetupProc</function> is
5246    successful it must return a non-<constant>NULL</constant> value.  The
5247    loader checks this, and if it is <constant>NULL</constant> it unloads
5248    the module and reports the failure to the caller of
5249    <function>LoadModule()</function>.  If the <function>SetupProc</function>
5250    does things that need to be undone when the module is unloaded,
5251    it should define a <function>TearDownProc</function>, and return a
5252    pointer that the <function>TearDownProc</function> can use to undo what
5253    has been done.
5254	      </para>
5255
5256	      <para>
5257    When a module is loaded multiple times, the <function>SetupProc</function>
5258    is called once for each time it is loaded.
5259	      </para>
5260
5261	    </blockquote></para></blockquote>
5262
5263	<blockquote><para>
5264	    <programlisting>
5265    void TearDownProc(pointer tearDownData);
5266	    </programlisting>
5267	    <blockquote><para>
5268    When defined, this function is called when the loader unloads a
5269    module.  The <parameter>tearDownData</parameter> parameter is the return
5270    value of the <function>SetupProc()</function> that was called when the
5271    module was loaded.  The purpose of this function is to clean up
5272    before the module is unloaded (for example, by freeing allocated
5273    resources).
5274	      </para>
5275
5276	    </blockquote></para></blockquote>
5277      </para>
5278    </sect2>
5279
5280    <sect2>
5281      <title>Public Loader Interface</title>
5282
5283      <para>
5284The following is the Loader interface that is available to any part of
5285the server, and may also be used from within modules.
5286      </para>
5287
5288      <blockquote><para>
5289	  <programlisting>
5290    pointer LoadSubModule(pointer parent, const char *module,
5291                          const char **subdirlist, const char **patternlist,
5292                          pointer options, const XF86ModReqInfo * modreq,
5293                          int *errmaj, int *errmin);
5294	  </programlisting>
5295	  <blockquote><para>
5296    This function is like the <function>LoadModule()</function> function
5297    described above, except that the module loaded is registered as a
5298    child of the calling module.  The <parameter>parent</parameter> parameter
5299    is the calling module's handle.  Modules loaded with this function
5300    are automatically unloaded when the parent module is unloaded.
5301	    </para>
5302
5303	  </blockquote></para></blockquote>
5304
5305      <blockquote><para>
5306	  <programlisting>
5307    void UnloadSubModule(pointer module);
5308	  </programlisting>
5309	  <blockquote><para>
5310    This function unloads the module with handle <parameter>module</parameter>.
5311    If that module itself has children, they are also unloaded.  It is
5312    like <function>UnloadModule()</function>, except that it is safe to use
5313    for unloading child modules.
5314	    </para>
5315
5316	  </blockquote></para></blockquote>
5317
5318      <blockquote><para>
5319	  <programlisting>
5320    pointer LoaderSymbol(const char *symbol);
5321	  </programlisting>
5322	<blockquote><para>
5323    This function returns the address of the symbol with name
5324    <parameter>symbol</parameter>.  This may be used to locate a module entry
5325    point with a known name.
5326	    </para>
5327
5328	  </blockquote></para></blockquote>
5329
5330      <blockquote><para>
5331	  <programlisting>
5332    char **LoaderlistDirs(const char **subdirlist,
5333                          const char **patternlist);
5334	  </programlisting>
5335	  <blockquote><para>
5336    This function returns a <constant>NULL</constant> terminated list of
5337    canonical modules names for modules found in the default module
5338    search path.  The <parameter>subdirlist</parameter> and
5339    <parameter>patternlist</parameter> parameters are as described above, and
5340    can be used to control the locations and names that are searched.
5341    If no modules are found, the return value is <constant>NULL</constant>.
5342    The returned list should be freed by calling
5343    <function>LoaderFreeDirList()</function> when it is no longer needed.
5344	    </para>
5345
5346	  </blockquote></para></blockquote>
5347
5348      <blockquote><para>
5349	  <programlisting>
5350    void LoaderFreeDirList(char **list);
5351	  </programlisting>
5352	  <blockquote><para>
5353    This function frees a module list created by
5354    <function>LoaderlistDirs()</function>.
5355	    </para>
5356
5357	  </blockquote></para></blockquote>
5358
5359      <blockquote><para>
5360	  <programlisting>
5361    void LoaderReqSymLists(const char **list0, ...);
5362	  </programlisting>
5363	  <blockquote><para>
5364    This function allows the registration of required symbols with the
5365    loader.  It is normally used by a caller of
5366    <function>LoadSubModule()</function>.  If any symbols registered in this
5367    way are found to be unresolved when
5368    <function>LoaderCheckUnresolved()</function> is called then
5369    <function>LoaderCheckUnresolved()</function> will report a failure.
5370    The function takes one or more <constant>NULL</constant> terminated
5371    lists of symbols.  The end of the argument list is indicated by a
5372    <constant>NULL</constant> argument.
5373	    </para>
5374
5375	  </blockquote></para></blockquote>
5376
5377      <blockquote><para>
5378	  <programlisting>
5379    void LoaderReqSymbols(const char *sym0, ...);
5380	  </programlisting>
5381	<blockquote><para>
5382    This function is like <function>LoaderReqSymLists()</function> except
5383    that its arguments are symbols rather than lists of symbols.  This
5384    function is more convenient when single functions are to be registered,
5385    especially when the single function might depend on runtime factors.
5386    The end of the argument list is indicated by a <constant>NULL</constant>
5387    argument.
5388	    </para>
5389
5390	  </blockquote></para></blockquote>
5391
5392      <blockquote><para>
5393	  <programlisting>
5394    void LoaderRefSymLists(const char **list0, ...);
5395	  </programlisting>
5396	  <blockquote><para>
5397    This function allows the registration of possibly unresolved symbols
5398    with the loader.  When <function>LoaderCheckUnresolved()</function> is
5399    run it won't generate warnings for symbols registered in this way
5400    unless they were also registered as required symbols.
5401    The function takes one or more <constant>NULL</constant> terminated
5402    lists of symbols.  The end of the argument list is indicated by a
5403    <constant>NULL</constant> argument.
5404	    </para>
5405
5406	  </blockquote></para></blockquote>
5407
5408      <blockquote><para>
5409	  <programlisting>
5410    void LoaderRefSymbols(const char *sym0, ...);
5411	  </programlisting>
5412	  <blockquote><para>
5413    This function is like <function>LoaderRefSymLists()</function> except
5414    that its arguments are symbols rather than lists of symbols.  This
5415    function is more convenient when single functions are to be registered,
5416    especially when the single function might depend on runtime factors.
5417    The end of the argument list is indicated by a <constant>NULL</constant>
5418    argument.
5419	    </para>
5420
5421	  </blockquote></para></blockquote>
5422
5423      <blockquote><para>
5424	  <programlisting>
5425    int LoaderCheckUnresolved(int delayflag);
5426	  </programlisting>
5427	  <blockquote><para>
5428    This function checks for unresolved symbols.  It generates warnings
5429    for unresolved symbols that have not been registered with
5430    <function>LoaderRefSymLists()</function>, and maps them to a dummy
5431    function.  This behaviour may change in future.  If unresolved
5432    symbols are found that have been registered with
5433    <function>LoaderReqSymLists()</function> or
5434    <function>LoaderReqSymbols()</function> then this function returns a
5435    non-zero value.  If none of these symbols are unresolved the return
5436    value is zero, indicating success.
5437	    </para>
5438
5439	    <para>
5440    The <parameter>delayflag</parameter> parameter should normally be set to
5441    <constant>LD_RESOLV_IFDONE</constant>.
5442	    </para>
5443
5444	  </blockquote></para></blockquote>
5445
5446      <blockquote><para>
5447	  <programlisting>
5448    LoaderErrorMsg(const char *name, const char *modname,
5449                   int errmaj, int errmin);
5450	  </programlisting>
5451	  <blockquote><para>
5452    This function prints an error message that includes the text <quote>Failed
5453    to load module</quote>, the module name <parameter>modname</parameter>, a message
5454    specific to the <parameter>errmaj</parameter> value, and the value if
5455    <parameter>errmin</parameter>.  If <parameter>name</parameter> is
5456    non-<constant>NULL</constant>, it is printed as an identifying prefix
5457    to the message (followed by a <quote>:</quote>).
5458	    </para>
5459
5460	  </blockquote></para></blockquote>
5461    </sect2>
5462
5463    <sect2>
5464      <title>Special Registration Functions</title>
5465
5466      <para>
5467The loader contains some functions for registering some classes of modules.
5468These may be moved out of the loader at some point.
5469      </para>
5470
5471      <blockquote><para>
5472	  <programlisting>
5473    void LoadExtensionList(const ExtensionModule ext[], int size, Bool builtin);
5474	  </programlisting>
5475	  <blockquote><para>
5476    This registers the entry points for the extension array identified by
5477    <parameter>ext</parameter>.  The <structname>ExtensionModule</structname> struct is
5478    defined as:
5479
5480	      <programlisting>
5481typedef struct {
5482    InitExtension       initFunc;
5483    char *              name;
5484    Bool                *disablePtr;
5485} ExtensionModule;
5486	      </programlisting>
5487	    </para>
5488
5489	  </blockquote></para></blockquote>
5490
5491    </sect2>
5492
5493  </sect1>
5494
5495  <sect1>
5496    <title>Helper Functions</title>
5497
5498    <para>
5499This section describe <quote>helper</quote> functions that video driver
5500might find useful.  While video drivers are not required to use any of
5501these to be considered <quote>compliant</quote>, the use of appropriate helpers is
5502strongly encouraged to improve the consistency of driver behaviour.
5503    </para>
5504
5505    <sect2>
5506      <title>Functions for printing messages</title>
5507
5508      <blockquote><para>
5509	  <programlisting>
5510    ErrorF(const char *format, ...);
5511	  </programlisting>
5512	  <blockquote><para>
5513      This is the basic function for writing to the error log (typically
5514      stderr and/or a log file).  Video drivers should usually avoid
5515      using this directly in favour of the more specialised functions
5516      described below.  This function is useful for printing messages
5517      while debugging a driver.
5518	    </para>
5519
5520	  </blockquote></para></blockquote>
5521
5522      <blockquote><para>
5523	  <programlisting>
5524    FatalError(const char *format, ...);
5525	  </programlisting>
5526	  <blockquote><para>
5527      This prints a message and causes the Xserver to abort.  It should
5528      rarely be used within a video driver, as most error conditions
5529      should be flagged by the return values of the driver functions.
5530      This allows the higher layers to decide how to proceed.  In rare
5531      cases, this can be used within a driver if a fatal unexpected
5532      condition is found.
5533	    </para>
5534
5535	  </blockquote></para></blockquote>
5536
5537      <blockquote><para>
5538	  <programlisting>
5539    xf86ErrorF(const char *format, ...);
5540	  </programlisting>
5541	  <blockquote><para>
5542      This is like <function>ErrorF()</function>, except that the message is
5543      only printed when the Xserver's verbosity level is set to the
5544      default (<constant>1</constant>) or higher.  It means that the messages
5545      are not printed when the server is started with the
5546      <option>-quiet</option> flag.  Typically this function would only be
5547      used for continuing messages started with one of the more specialised
5548      functions described below.
5549	    </para>
5550
5551	  </blockquote></para></blockquote>
5552
5553      <blockquote><para>
5554	  <programlisting>
5555    xf86ErrorFVerb(int verb, const char *format, ...);
5556	  </programlisting>
5557	  <blockquote><para>
5558      Like <function>xf86ErrorF()</function>, except the minimum verbosity
5559      level for which the message is to be printed is given explicitly.
5560      Passing a <parameter>verb</parameter> value of zero means the message
5561      is always printed.  A value higher than <constant>1</constant> can be
5562      used for information would normally not be needed, but which might
5563      be useful when diagnosing problems.
5564	    </para>
5565
5566	  </blockquote></para></blockquote>
5567
5568
5569      <blockquote><para>
5570	  <programlisting>
5571    xf86Msg(MessageType type, const char *format, ...);
5572	  </programlisting>
5573	  <blockquote><para>
5574      This is like <function>xf86ErrorF()</function>, except that the message
5575      is prefixed with a marker determined by the value of
5576      <parameter>type</parameter>.  The marker is used to indicate the type of
5577      message (warning, error, probed value, config value, etc).  Note
5578      the <varname>xf86Verbose</varname> value is ignored for messages of
5579      type <constant>X_ERROR</constant>.
5580	    </para>
5581
5582	    <para>
5583      The marker values are:
5584
5585	      <variablelist>
5586		<varlistentry>
5587		  <term><constant>X_PROBED</constant></term>
5588		  <listitem><para>Value was probed.
5589		    </para></listitem></varlistentry>
5590		<varlistentry>
5591		  <term><constant>X_CONFIG</constant></term>
5592		  <listitem><para>Value was given in the config file.
5593		    </para></listitem></varlistentry>
5594		<varlistentry>
5595		  <term><constant>X_DEFAULT</constant></term>
5596		  <listitem><para>Value is a default.
5597		    </para></listitem></varlistentry>
5598		<varlistentry>
5599		  <term><constant>X_CMDLINE</constant></term>
5600		  <listitem><para>Value was given on the command line.
5601		    </para></listitem></varlistentry>
5602		<varlistentry>
5603		  <term><constant>X_NOTICE</constant></term>
5604		  <listitem><para>Notice.
5605		    </para></listitem></varlistentry>
5606		<varlistentry>
5607		  <term><constant>X_ERROR</constant></term>
5608		  <listitem><para>Error message.
5609		    </para></listitem></varlistentry>
5610		<varlistentry>
5611		  <term><constant>X_WARNING</constant></term>
5612		  <listitem><para>Warning message.
5613		    </para></listitem></varlistentry>
5614		<varlistentry>
5615		  <term><constant>X_INFO</constant></term>
5616		  <listitem><para>Informational message.
5617		    </para></listitem></varlistentry>
5618		<varlistentry>
5619		  <term><constant>X_NONE</constant></term>
5620		  <listitem><para>No prefix.
5621		    </para></listitem></varlistentry>
5622		<varlistentry>
5623		  <term><constant>X_NOT_IMPLEMENTED</constant></term>
5624		  <listitem><para>The message relates to functionality
5625		      that is not yetimplemented.
5626		    </para></listitem></varlistentry>
5627		</variablelist>
5628
5629	    </para>
5630
5631	  </blockquote></para></blockquote>
5632
5633      <blockquote><para>
5634	  <programlisting>
5635    xf86MsgVerb(MessageType type, int verb, const char *format, ...);
5636	  </programlisting>
5637	  <blockquote><para>
5638      Like <function>xf86Msg()</function>, but with the verbosity level given
5639      explicitly.
5640	    </para>
5641
5642	  </blockquote></para></blockquote>
5643
5644      <blockquote><para>
5645	  <programlisting>
5646    xf86DrvMsg(int scrnIndex, MessageType type, const char *format, ...);
5647	  </programlisting>
5648	  <blockquote><para>
5649      This is like <function>xf86Msg()</function> except that the driver's
5650      name (the <structfield>name</structfield> field of the
5651      <structname>ScrnInfoRec</structname>) followed by the
5652      <parameter>scrnIndex</parameter> in parentheses is printed following the
5653      prefix.  This should be used by video drivers in most cases as it
5654      clearly indicates which driver/screen the message is for.  If
5655      <parameter>scrnIndex</parameter> is negative, this function behaves
5656      exactly like <function>xf86Msg()</function>.
5657	    </para>
5658
5659	    <para>
5660      NOTE: This function can only be used after the
5661      <structname>ScrnInfoRec</structname> and its <structfield>name</structfield> field
5662      have been allocated.  Normally, this means that it can not be
5663      used before the END of the <function>ChipProbe()</function> function.
5664      Prior to that, use <function>xf86Msg()</function>, providing the
5665      driver's name explicitly.  No screen number can be supplied at
5666      that point.
5667	    </para>
5668
5669	  </blockquote></para></blockquote>
5670
5671      <blockquote><para>
5672	  <programlisting>
5673    xf86DrvMsgVerb(int scrnIndex, MessageType type, int verb,
5674                                const char *format, ...);
5675	  </programlisting>
5676	  <blockquote><para>
5677      Like <function>xf86DrvMsg()</function>, but with the verbosity level
5678      given explicitly.
5679	    </para>
5680
5681	  </blockquote></para></blockquote>
5682    </sect2>
5683
5684
5685    <sect2>
5686      <title>Functions for setting values based on command line and config file</title>
5687
5688      <blockquote><para>
5689	  <programlisting>
5690    Bool xf86SetDepthBpp(ScrnInfoPtr scrp, int depth, int bpp,
5691
5692                                int fbbpp, int depth24flags);
5693	  </programlisting>
5694	  <blockquote><para>
5695      This function sets the <structfield>depth</structfield>, <structfield>pixmapBPP</structfield> and <structfield>bitsPerPixel</structfield> fields
5696      of the <structname>ScrnInfoRec</structname>.  It also determines the defaults for display-wide
5697      attributes and pixmap formats the screen will support, and finds
5698      the Display subsection that matches the depth/bpp.  This function
5699      should normally be called very early from the
5700      <function>ChipPreInit()</function> function.
5701	    </para>
5702
5703	    <para>
5704      It requires that the <structfield>confScreen</structfield> field of the <structname>ScrnInfoRec</structname> be
5705      initialised prior to calling it.  This is done by the XFree86
5706      common layer prior to calling <function>ChipPreInit()</function>.
5707	    </para>
5708
5709	    <para>
5710      The parameters passed are:
5711
5712	      <variablelist>
5713		<varlistentry>
5714		  <term><parameter>depth</parameter></term>
5715		  <listitem><para>
5716		driver's preferred default depth if no other is given.
5717                If zero, use the overall server default.
5718		    </para></listitem></varlistentry>
5719
5720		<varlistentry>
5721		  <term><parameter>bpp</parameter></term>
5722		  <listitem><para>
5723		Same, but for the pixmap bpp.
5724		    </para></listitem></varlistentry>
5725
5726		<varlistentry>
5727		  <term><parameter>fbbpp</parameter></term>
5728		  <listitem><para>
5729		Same, but for the framebuffer bpp.
5730		    </para></listitem></varlistentry>
5731
5732		<varlistentry>
5733		  <term><parameter>depth24flags</parameter></term>
5734		  <listitem><para>
5735		Flags that indicate the level of 24/32bpp support
5736                and whether conversion between different framebuffer
5737                and pixmap formats is supported.  The flags for this
5738                argument are defined as follows, and multiple flags
5739                may be ORed together:
5740
5741		      <variablelist>
5742			<varlistentry>
5743			  <term><constant>NoDepth24Support</constant></term>
5744			  <listitem><para>No depth 24 formats supported
5745			    </para></listitem></varlistentry>
5746			<varlistentry>
5747			  <term><constant>Support24bppFb</constant></term>
5748			  <listitem><para>24bpp framebuffer supported
5749			    </para></listitem></varlistentry>
5750			<varlistentry>
5751			  <term><constant>Support32bppFb</constant></term>
5752			  <listitem><para>32bpp framebuffer supported
5753			    </para></listitem></varlistentry>
5754			<varlistentry>
5755			  <term><constant>SupportConvert24to32</constant></term>
5756			  <listitem><para>Can convert 24bpp pixmap to 32bpp fb
5757			    </para></listitem></varlistentry>
5758			<varlistentry>
5759			  <term><constant>SupportConvert32to24</constant></term>
5760			  <listitem><para>Can convert 32bpp pixmap to 24bpp fb
5761			    </para></listitem></varlistentry>
5762			<varlistentry>
5763			  <term><constant>ForceConvert24to32</constant></term>
5764			  <listitem><para>Force 24bpp pixmap to 32bpp fb conversion
5765			    </para></listitem></varlistentry>
5766			<varlistentry>
5767			  <term><constant>ForceConvert32to24</constant></term>
5768			  <listitem><para>Force 32bpp pixmap to 24bpp fb conversion
5769			    </para></listitem></varlistentry>
5770		      </variablelist>
5771		    </para></listitem></varlistentry>
5772
5773	      </variablelist>
5774	    </para>
5775
5776	    <para>
5777      It uses the command line, config file, and default values in the
5778      correct order of precedence to determine the depth and bpp values.
5779      It is up to the driver to check the results to see that it supports
5780      them.  If not the <function>ChipPreInit()</function> function should
5781      return <constant>FALSE</constant>.
5782	    </para>
5783
5784	    <para>
5785      If only one of depth/bpp is given, the other is set to a reasonable
5786      (and consistent) default.
5787	    </para>
5788
5789	    <para>
5790      If a driver finds that the initial <parameter>depth24flags</parameter>
5791      it uses later results in a fb format that requires more video
5792      memory than is available it may call this function a second time
5793      with a different <parameter>depth24flags</parameter> setting.
5794	    </para>
5795
5796	    <para>
5797      On success, the return value is <constant>TRUE</constant>.  On failure
5798      it prints an error message and returns <constant>FALSE</constant>.
5799	    </para>
5800
5801	    <para>
5802      The following fields of the <structname>ScrnInfoRec</structname> are
5803      initialised by this function:
5804
5805	      <blockquote><para>
5806<structfield>depth</structfield>, <structfield>bitsPerPixel</structfield>,
5807<structfield>display</structfield>, <structfield>imageByteOrder</structfield>,
5808<structfield>bitmapScanlinePad</structfield>,
5809<structfield>bitmapScanlineUnit</structfield>, <structfield>bitmapBitOrder</structfield>,
5810<structfield>numFormats</structfield>, <structfield>formats</structfield>,
5811<structfield>fbFormat</structfield>.
5812		</para></blockquote>
5813	    </para>
5814
5815	  </blockquote></para></blockquote>
5816
5817      <blockquote><para>
5818	  <programlisting>
5819    void xf86PrintDepthBpp(scrnInfoPtr scrp);
5820	  </programlisting>
5821	  <blockquote><para>
5822      This function can be used to print out the depth and bpp settings.
5823      It should be called after the final call to
5824      <function>xf86SetDepthBpp()</function>.
5825	    </para>
5826
5827	  </blockquote></para></blockquote>
5828
5829      <blockquote><para>
5830	  <programlisting>
5831    Bool xf86SetWeight(ScrnInfoPtr scrp, rgb weight, rgb mask);
5832	  </programlisting>
5833	  <blockquote><para>
5834      This function sets the <structfield>weight</structfield>, <structfield>mask</structfield>,
5835      <structfield>offset</structfield> and <structfield>rgbBits</structfield> fields of the
5836      <structname>ScrnInfoRec</structname>.  It would normally be called fairly
5837      early in the <function>ChipPreInit()</function> function for
5838      depths&nbsp;&gt;&nbsp;8bpp.
5839	    </para>
5840
5841	    <para>
5842      It requires that the <structfield>depth</structfield> and
5843      <structfield>display</structfield> fields of the <structname>ScrnInfoRec</structname>
5844      be initialised prior to calling it.
5845	    </para>
5846
5847	    <para>
5848      The parameters passed are:
5849
5850	      <variablelist>
5851		<varlistentry>
5852		  <term><parameter>weight</parameter></term>
5853		  <listitem><para>
5854		driver's preferred default weight if no other is given.
5855                If zero, use the overall server default.
5856
5857		    </para></listitem></varlistentry>
5858
5859		<varlistentry>
5860		  <term><parameter>mask</parameter></term>
5861		  <listitem><para>
5862		Same, but for mask.
5863
5864		    </para></listitem></varlistentry>
5865	      </variablelist>
5866	    </para>
5867
5868	    <para>
5869      It uses the command line, config file, and default values in the
5870      correct order of precedence to determine the weight value.  It
5871      derives the mask and offset values from the weight and the defaults.
5872      It is up to the driver to check the results to see that it supports
5873      them.  If not the <function>ChipPreInit()</function> function should
5874      return <constant>FALSE</constant>.
5875	    </para>
5876
5877	    <para>
5878      On success, this function prints a message showing the weight
5879      values selected, and returns <constant>TRUE</constant>.
5880	    </para>
5881
5882	    <para>
5883      On failure it prints an error message and returns <constant>FALSE</constant>.
5884	    </para>
5885
5886	    <para>
5887      The following fields of the <structname>ScrnInfoRec</structname> are
5888      initialised by this function:
5889
5890	      <blockquote><para>
5891		  <structfield>weight</structfield>,
5892		  <structfield>mask</structfield>,
5893		  <structfield>offset</structfield>.
5894		</para></blockquote>
5895	    </para>
5896
5897	  </blockquote></para></blockquote>
5898
5899      <blockquote><para>
5900	  <programlisting>
5901    Bool xf86SetDefaultVisual(ScrnInfoPtr scrp, int visual);
5902	  </programlisting>
5903	  <blockquote><para>
5904      This function sets the <structfield>defaultVisual</structfield> field of the
5905      <structname>ScrnInfoRec</structname>.  It would normally be called fairly
5906      early from the <function>ChipPreInit()</function> function.
5907	    </para>
5908
5909	    <para>
5910      It requires that the <structfield>depth</structfield> and
5911      <structfield>display</structfield> fields of the <structname>ScrnInfoRec</structname>
5912      be initialised prior to calling it.
5913	    </para>
5914
5915	    <para>
5916      The parameters passed are:
5917
5918	      <variablelist>
5919		<varlistentry>
5920		  <term><parameter>visual</parameter></term>
5921		  <listitem><para>
5922		driver's preferred default visual if no other is given.
5923		If <constant>-1</constant>, use the overall server default.
5924		    </para></listitem></varlistentry>
5925
5926	      </variablelist>
5927	    </para>
5928
5929	    <para>
5930      It uses the command line, config file, and default values in the
5931      correct order of precedence to determine the default visual value.
5932      It is up to the driver to check the result to see that it supports
5933      it.  If not the <function>ChipPreInit()</function> function should
5934      return <constant>FALSE</constant>.
5935	    </para>
5936
5937	    <para>
5938      On success, this function prints a message showing the default visual
5939      selected, and returns <constant>TRUE</constant>.
5940	    </para>
5941
5942	    <para>
5943      On failure it prints an error message and returns <constant>FALSE</constant>.
5944	    </para>
5945
5946	  </blockquote></para></blockquote>
5947
5948      <blockquote><para>
5949	  <programlisting>
5950    Bool xf86SetGamma(ScrnInfoPtr scrp, Gamma gamma);
5951	  </programlisting>
5952	  <blockquote><para>
5953      This function sets the <structfield>gamma</structfield> field of the
5954      <structname>ScrnInfoRec</structname>.  It would normally be called fairly
5955      early from the <function>ChipPreInit()</function> function in cases
5956      where the driver supports gamma correction.
5957	    </para>
5958
5959	    <para>
5960      It requires that the <structfield>monitor</structfield> field of the
5961      <structname>ScrnInfoRec</structname> be initialised prior to calling it.
5962	    </para>
5963
5964	    <para>
5965      The parameters passed are:
5966
5967	      <variablelist>
5968		<varlistentry>
5969		  <term><parameter>gamma</parameter></term>
5970		  <listitem><para>
5971		driver's preferred default gamma if no other is given.
5972		If zero (<code>&lt; 0.01</code>), use the overall server
5973		default.
5974		    </para></listitem></varlistentry>
5975
5976	      </variablelist>
5977	    </para>
5978
5979	    <para>
5980      It uses the command line, config file, and default values in the
5981      correct order of precedence to determine the gamma value.  It is
5982      up to the driver to check the results to see that it supports
5983      them.  If not the <function>ChipPreInit()</function> function should
5984      return <constant>FALSE</constant>.
5985	    </para>
5986
5987	    <para>
5988      On success, this function prints a message showing the gamma
5989      value selected, and returns <constant>TRUE</constant>.
5990	    </para>
5991
5992	    <para>
5993      On failure it prints an error message and returns <constant>FALSE</constant>.
5994	    </para>
5995
5996	  </blockquote></para></blockquote>
5997
5998
5999      <blockquote><para>
6000	  <programlisting>
6001    void xf86SetDpi(ScrnInfoPtr pScrn, int x, int y);
6002	  </programlisting>
6003	  <blockquote><para>
6004      This function sets the <structfield>xDpi</structfield> and <structfield>yDpi</structfield>
6005      fields of the <structname>ScrnInfoRec</structname>.  The driver can specify
6006      preferred defaults by setting <parameter>x</parameter> and <parameter>y</parameter>
6007      to non-zero values.  The <option>-dpi</option> command line option
6008      overrides all other settings.  Otherwise, if the
6009      <emphasis>DisplaySize</emphasis> entry is present in the screen's &k.monitor;
6010      config file section, it is used together with the virtual size to
6011      calculate the dpi values.  This function should be called after
6012      all the mode resolution has been done.
6013	    </para>
6014
6015	  </blockquote></para></blockquote>
6016
6017      <blockquote><para>
6018	  <programlisting>
6019    void xf86SetBlackWhitePixels(ScrnInfoPtr pScrn);
6020	  </programlisting>
6021	  <blockquote><para>
6022      This functions sets the <structfield>blackPixel</structfield> and
6023      <structfield>whitePixel</structfield> fields of the <structname>ScrnInfoRec</structname>
6024      according to whether or not the <option>-flipPixels</option> command
6025      line options is present.
6026	    </para>
6027
6028	  </blockquote></para></blockquote>
6029
6030      <blockquote><para>
6031	  <programlisting>
6032    const char *xf86GetVisualName(int visual);
6033	  </programlisting>
6034	  <blockquote><para>
6035      Returns a printable string with the visual name matching the
6036      numerical visual class provided.  If the value is outside the
6037      range of valid visual classes, <constant>NULL</constant> is returned.
6038	    </para>
6039
6040	  </blockquote></para></blockquote>
6041
6042    </sect2>
6043
6044    <sect2>
6045      <title>Primary Mode functions</title>
6046
6047      <para>
6048The primary mode helper functions are those which would normally be
6049used by a driver, unless it has unusual requirements which cannot
6050be catered for the by the helpers.
6051      </para>
6052
6053      <blockquote><para>
6054	  <programlisting>
6055    int xf86ValidateModes(ScrnInfoPtr scrp, DisplayModePtr availModes,
6056                          char **modeNames, ClockRangePtr clockRanges,
6057                          int *linePitches, int minPitch, int maxPitch,
6058                          int pitchInc, int minHeight, int maxHeight,
6059                          int virtualX, int virtualY,
6060                          unsigned long apertureSize,
6061                          LookupModeFlags strategy);
6062	  </programlisting>
6063	  <blockquote><para>
6064      This function basically selects the set of modes to use based on
6065      those available and the various constraints.  It also sets some
6066      other related parameters.  It is normally called near the end of
6067      the <function>ChipPreInit()</function> function.
6068	    </para>
6069
6070	    <para>
6071      The parameters passed to the function are:
6072
6073	      <variablelist>
6074		<varlistentry>
6075		  <term><parameter>availModes</parameter></term>
6076		  <listitem><para>
6077		List of modes available for the monitor.
6078		    </para></listitem></varlistentry>
6079
6080		<varlistentry>
6081		  <term><parameter>modeNames</parameter></term>
6082		  <listitem><para>
6083		List of mode names that the screen is requesting.
6084		    </para></listitem></varlistentry>
6085
6086		<varlistentry>
6087		  <term><parameter>clockRanges</parameter></term>
6088		  <listitem><para>
6089		A list of clock ranges allowed by the driver.  Each
6090		range includes whether interlaced or multiscan modes
6091		are supported for that range.  See below for more on
6092		<parameter>clockRanges</parameter>.
6093		    </para></listitem></varlistentry>
6094
6095		<varlistentry>
6096		  <term><parameter>linePitches</parameter></term>
6097		  <listitem><para>
6098		List of line pitches supported by the driver.
6099		This is optional and should be <constant>NULL</constant> when
6100		not used.
6101		    </para></listitem></varlistentry>
6102
6103		<varlistentry>
6104		  <term><parameter>minPitch</parameter></term>
6105		  <listitem><para>
6106		Minimum line pitch supported by the driver.  This must
6107		be supplied when <parameter>linePitches</parameter> is
6108		<constant>NULL</constant>, and is ignored otherwise.
6109		    </para></listitem></varlistentry>
6110
6111		<varlistentry>
6112		  <term><parameter>maxPitch</parameter></term>
6113		  <listitem><para>
6114		Maximum line pitch supported by the driver.  This is
6115		required when <parameter>minPitch</parameter> is required.
6116		    </para></listitem></varlistentry>
6117
6118		<varlistentry>
6119		  <term><parameter>pitchInc</parameter></term>
6120		  <listitem><para>
6121		Granularity of horizontal pitch values as supported by
6122		the chipset.  This is expressed in bits.  This must be
6123		supplied.
6124		    </para></listitem></varlistentry>
6125
6126		<varlistentry>
6127		  <term><parameter>minHeight</parameter></term>
6128		  <listitem><para>
6129		minimum virtual height allowed.  If zero, no limit is
6130		imposed.
6131		    </para></listitem></varlistentry>
6132
6133		<varlistentry>
6134		  <term><parameter>maxHeight</parameter></term>
6135		  <listitem><para>
6136		maximum virtual height allowed.  If zero, no limit is
6137		imposed.
6138		    </para></listitem></varlistentry>
6139
6140		<varlistentry>
6141		  <term><parameter>virtualX</parameter></term>
6142		  <listitem><para>
6143		If greater than zero, this is the virtual width value
6144		that will be used.  Otherwise, the virtual width is
6145		chosen to be the smallest that can accommodate the modes
6146		selected.
6147		    </para></listitem></varlistentry>
6148
6149		<varlistentry>
6150		  <term><parameter>virtualY</parameter></term>
6151		  <listitem><para>
6152		If greater than zero, this is the virtual height value
6153		that will be used.  Otherwise, the virtual height is
6154		chosen to be the smallest that can accommodate the modes
6155		selected.
6156		    </para></listitem></varlistentry>
6157
6158		<varlistentry>
6159		  <term><parameter>apertureSize</parameter></term>
6160		  <listitem><para>
6161		The size (in bytes) of the aperture used to access video
6162		memory.
6163		    </para></listitem></varlistentry>
6164
6165		<varlistentry>
6166		  <term><parameter>strategy</parameter></term>
6167		  <listitem><para>
6168		The strategy to use when choosing from multiple modes
6169		with the same name.  The options are:
6170
6171		      <variablelist>
6172			<varlistentry>
6173			  <term><constant>LOOKUP_DEFAULT</constant></term>
6174			  <listitem><para>???
6175			    </para></listitem></varlistentry>
6176			<varlistentry>
6177			  <term><constant>LOOKUP_BEST_REFRESH</constant></term>
6178			  <listitem><para>mode with best refresh rate
6179			    </para></listitem></varlistentry>
6180			<varlistentry>
6181			  <term><constant>LOOKUP_CLOSEST_CLOCK</constant></term>
6182			  <listitem><para>mode with closest matching clock
6183			    </para></listitem></varlistentry>
6184			<varlistentry>
6185			  <term><constant>LOOKUP_LIST_ORDER</constant></term>
6186			  <listitem><para>first usable mode in list
6187			    </para></listitem></varlistentry>
6188		      </variablelist>
6189
6190		The following options can also be combined (OR'ed) with
6191		one of the above:
6192
6193		      <variablelist>
6194			<varlistentry>
6195			  <term><constant>LOOKUP_CLKDIV2</constant></term>
6196			  <listitem><para>Allow halved clocks
6197			    </para></listitem></varlistentry>
6198			<varlistentry>
6199			  <term><constant>LOOKUP_OPTIONAL_TOLERANCES</constant></term>
6200			  <listitem><para>
6201		      Allow missing horizontal sync and/or vertical refresh
6202		      ranges in the xorg.conf Monitor section
6203			    </para></listitem></varlistentry>
6204		      </variablelist>
6205
6206		<constant>LOOKUP_OPTIONAL_TOLERANCES</constant> should only be
6207		specified when the driver can ensure all modes it generates
6208		can sync on, or at least not damage, the monitor or digital
6209		flat panel.  Horizontal sync and/or vertical refresh ranges
6210		specified by the user will still be honoured (and acted upon).
6211
6212		    </para></listitem></varlistentry>
6213	      </variablelist>
6214	    </para>
6215
6216	    <para>
6217      This function requires that the following fields of the
6218      <structname>ScrnInfoRec</structname> are initialised prior to calling it:
6219
6220	      <variablelist>
6221		<varlistentry>
6222		  <term><structfield>clock[]</structfield></term>
6223		  <listitem><para>
6224		      List of discrete clocks (when non-programmable)
6225		    </para></listitem></varlistentry>
6226		<varlistentry>
6227		  <term><structfield>numClocks</structfield></term>
6228		  <listitem><para>
6229		      Number of discrete clocks (when non-programmable)
6230		    </para></listitem></varlistentry>
6231		<varlistentry>
6232		  <term><structfield>progClock</structfield></term>
6233		  <listitem><para>
6234		      Whether the clock is programmable or not
6235		    </para></listitem></varlistentry>
6236		<varlistentry>
6237		  <term><structfield>monitor</structfield></term>
6238		  <listitem><para>
6239		      Pointer to the applicable xorg.conf monitor section
6240		    </para></listitem></varlistentry>
6241		<varlistentry>
6242		  <term><structfield>fdFormat</structfield></term>
6243		  <listitem><para>
6244		      Format of the screen buffer
6245		    </para></listitem></varlistentry>
6246		<varlistentry>
6247		  <term><structfield>videoRam</structfield></term>
6248		  <listitem><para>
6249		      total video memory size (in bytes)
6250		    </para></listitem></varlistentry>
6251		<varlistentry>
6252		  <term><structfield>xInc</structfield></term>
6253		  <listitem><para>
6254		      Horizontal timing increment in pixels (defaults to 8)
6255		    </para></listitem></varlistentry>
6256	      </variablelist>
6257	    </para>
6258
6259	    <para>
6260      This function fills in the following <structname>ScrnInfoRec</structname>
6261      fields:
6262
6263	      <variablelist>
6264		<varlistentry>
6265		  <term><structfield>modePool</structfield></term>
6266		  <listitem><para>
6267		A subset of the modes available to the monitor which
6268                are compatible with the driver.
6269		    </para></listitem></varlistentry>
6270
6271		<varlistentry>
6272		  <term><structfield>modes</structfield></term>
6273		  <listitem><para>
6274		One mode entry for each of the requested modes, with
6275                the status field of each filled in to indicate if
6276                the mode has been accepted or not.  This list of
6277                modes is a circular list.
6278		    </para></listitem></varlistentry>
6279
6280		<varlistentry>
6281		  <term><structfield>virtualX</structfield></term>
6282		  <listitem><para>
6283		The resulting virtual width.
6284		    </para></listitem></varlistentry>
6285
6286		<varlistentry>
6287		  <term><structfield>virtualY</structfield></term>
6288		  <listitem><para>
6289		The resulting virtual height.
6290		    </para></listitem></varlistentry>
6291
6292		<varlistentry>
6293		  <term><structfield>displayWidth</structfield></term>
6294		  <listitem><para>
6295		The resulting line pitch.
6296		    </para></listitem></varlistentry>
6297
6298	      </variablelist>
6299	    </para>
6300
6301	    <para>
6302      The first stage of this function checks that the
6303      <parameter>virtualX</parameter> and <parameter>virtualY</parameter> values
6304      supplied (if greater than zero) are consistent with the line pitch
6305      and <parameter>maxHeight</parameter> limitations.  If not, an error
6306      message is printed, and the return value is <constant>-1</constant>.
6307	    </para>
6308
6309	    <para>
6310      The second stage sets up the mode pool, eliminating immediately
6311      any modes that exceed the driver's line pitch limits, and also
6312      the virtual width and height limits (if greater than zero).  For
6313      each mode removed an informational message is printed at verbosity
6314      level <constant>2</constant>.  If the mode pool ends up being empty,
6315      a warning message is printed, and the return value is
6316      <constant>0</constant>.
6317	    </para>
6318
6319	    <para>
6320      The final stage is to lookup each mode name, and fill in the remaining
6321      parameters.  If an error condition is encountered, a message is
6322      printed, and the return value is <constant>-1</constant>.  Otherwise,
6323      the return value is the number of valid modes found
6324      (<constant>0</constant> if none are found).
6325	    </para>
6326
6327	    <para>
6328      Even if the supplied mode names include duplicates, no two names will
6329      ever match the same mode.  Furthermore, if the supplied mode names do not
6330      yield a valid mode (including the case where no names are passed at all),
6331      the function will continue looking through the mode pool until it finds
6332      a mode that survives all checks, or until the mode pool is exhausted.
6333	    </para>
6334
6335	    <para>
6336      A message is only printed by this function when a fundamental
6337      problem is found.  It is intended that this function may be called
6338      more than once if there is more than one set of constraints that
6339      the driver can work within.
6340	    </para>
6341
6342	    <para>
6343      If this function returns <constant>-1</constant>, the
6344      <function>ChipPreInit()</function> function should return
6345      <constant>FALSE</constant>.
6346	    </para>
6347
6348	    <para>
6349      <parameter>clockRanges</parameter> is a linked list of clock ranges
6350      allowed by the driver.  If a mode doesn't fit in any of the defined
6351      <parameter>clockRanges</parameter>, it is rejected.  The first
6352      <literal remap="tt">clockRange</literal> that matches all requirements is used.
6353      This structure needs to be initialized to NULL when allocated.
6354	    </para>
6355
6356	    <para>
6357      <parameter>clockRanges</parameter> contains the following fields:
6358
6359	      <variablelist>
6360		<varlistentry>
6361		  <term><structfield>minClock</structfield></term>
6362		  <term><structfield>maxClock</structfield></term>
6363		  <listitem><para>
6364		The lower and upper mode clock bounds for which the rest
6365		of the <structname>clockRange</structname> parameters apply.
6366		Since these are the mode clocks, they are not scaled
6367		with the <structfield>ClockMulFactor</structfield> and
6368		<structfield>ClockDivFactor</structfield>.  It is up to the driver
6369		to adjust these values if they depend on the clock
6370		scaling factors.
6371		    </para></listitem></varlistentry>
6372
6373		<varlistentry>
6374		  <term><structfield>clockIndex</structfield></term>
6375		  <listitem><para>
6376		(not used yet) <constant>-1</constant> for programmable clocks
6377		    </para></listitem></varlistentry>
6378
6379		<varlistentry>
6380		  <term><structfield>interlaceAllowed</structfield></term>
6381		  <listitem><para>
6382		<constant>TRUE</constant> if interlacing is allowed for this
6383		range
6384		    </para></listitem></varlistentry>
6385
6386		<varlistentry>
6387		  <term><structfield>doubleScanAllowed</structfield></term>
6388		  <listitem><para>
6389		<constant>TRUE</constant> if doublescan or multiscan is allowed
6390		for this range
6391		    </para></listitem></varlistentry>
6392
6393		<varlistentry>
6394		  <term><structfield>ClockMulFactor</structfield></term>
6395		  <term><structfield>ClockDivFactor</structfield></term>
6396		  <listitem><para>
6397		Scaling factors that are applied to the mode clocks ONLY
6398		before selecting a clock index (when there is no
6399		programmable clock) or a <structfield>SynthClock</structfield>
6400		value.  This is useful for drivers that support pixel
6401		multiplexing or that need to scale the clocks because
6402		of hardware restrictions (like sending 24bpp data to an
6403		8 bit RAMDAC using a tripled clock).
6404		    </para>
6405
6406		    <para>
6407		Note that these parameters describe what must be done
6408		to the mode clock to achieve the data transport clock
6409		between graphics controller and RAMDAC.  For example
6410		for <literal remap="tt">2:1</literal> pixel multiplexing, two pixels
6411		are sent to the RAMDAC on each clock.  This allows the
6412		RAMDAC clock to be half of the actual pixel clock.
6413		Hence, <code>ClockMulFactor=1</code> and
6414		<code>ClockDivFactor=2</code>.  This means that the
6415		clock used for clock selection (ie, determining the
6416		correct clock index from the list of discrete clocks)
6417		or for the <structfield>SynthClock</structfield> field in case of
6418		a programmable clock is:  (<code>mode-&gt;Clock *
6419		ClockMulFactor) / ClockDivFactor</code>.
6420		    </para></listitem></varlistentry>
6421
6422		<varlistentry>
6423		  <term><structfield>PrivFlags</structfield></term>
6424		  <listitem><para>
6425		This field is copied into the
6426		<literal remap="tt">mode-&gt;PrivFlags</literal> field when this
6427		<literal remap="tt">clockRange</literal> is selected by
6428		<function>xf86ValidateModes()</function>.  It allows the
6429		driver to find out what clock range was selected, so it
6430		knows it needs to set up pixel multiplexing or any other
6431		range-dependent feature.  This field is purely
6432		driver-defined: it may contain flag bits, an index or
6433		anything else (as long as it is an <literal remap="tt">INT</literal>).
6434		    </para></listitem></varlistentry>
6435	      </variablelist>
6436	    </para>
6437
6438	    <para>
6439      Note that the <structfield>mode-&gt;SynthClock</structfield> field is always
6440      filled in by <function>xf86ValidateModes()</function>: it will contain
6441      the <quote>data transport clock</quote>, which is the clock that will have
6442      to be programmed in the chip when it has a programmable clock, or
6443      the clock that will be picked from the clocks list when it is not
6444      a programmable one.  Thus:
6445
6446	      <programlisting>
6447    mode-&gt;SynthClock = (mode-&gt;Clock * ClockMulFactor) / ClockDivFactor
6448	      </programlisting>
6449	    </para>
6450
6451	  </blockquote></para></blockquote>
6452
6453      <blockquote><para>
6454	  <programlisting>
6455    void xf86PruneDriverModes(ScrnInfoPtr scrp);
6456	  </programlisting>
6457	  <blockquote><para>
6458      This function deletes modes in the modes field of the
6459      <structname>ScrnInfoRec</structname> that have been marked as invalid.
6460      This is normally run after having run
6461      <function>xf86ValidateModes()</function> for the last time.  For each
6462      mode that is deleted, a warning message is printed out indicating
6463      the reason for it being deleted.
6464	    </para>
6465
6466	  </blockquote></para></blockquote>
6467
6468      <blockquote><para>
6469	  <programlisting>
6470    void xf86SetCrtcForModes(ScrnInfoPtr scrp, int adjustFlags);
6471	  </programlisting>
6472	  <blockquote><para>
6473      This function fills in the <structname>Crtc*</structname> fields for all
6474      the modes in the <structfield>modes</structfield> field of the
6475      <structname>ScrnInfoRec</structname>.  The <parameter>adjustFlags</parameter>
6476      parameter determines how the vertical CRTC values are scaled for
6477      interlaced modes.  They are halved if it is
6478      <constant>INTERLACE_HALVE_V</constant>.  The vertical CRTC values are
6479      doubled for doublescan modes, and are further multiplied by the
6480      <literal remap="tt">VScan</literal> value.
6481	    </para>
6482
6483	    <para>
6484      This function is normally called after calling
6485      <function>xf86PruneDriverModes()</function>.
6486	    </para>
6487
6488	  </blockquote></para></blockquote>
6489
6490      <blockquote><para>
6491	  <programlisting>
6492    void xf86PrintModes(ScrnInfoPtr scrp);
6493	  </programlisting>
6494	  <blockquote><para>
6495      This function prints out the virtual size setting, and the line
6496      pitch being used.  It also prints out two lines for each mode being
6497      used.  The first line includes the mode's pixel clock, horizontal sync
6498      rate, refresh rate, and whether it is interlaced, doublescanned and/or
6499      multi-scanned.  The second line is the mode's Modeline.
6500	    </para>
6501
6502	    <para>
6503      This function is normally called after calling
6504      <function>xf86SetCrtcForModes()</function>.
6505	    </para>
6506
6507	  </blockquote></para></blockquote>
6508
6509    </sect2>
6510
6511    <sect2>
6512      <title>Secondary Mode functions</title>
6513
6514      <para>
6515The secondary mode helper functions are functions which are normally
6516used by the primary mode helper functions, and which are not normally
6517called directly by a driver.  If a driver has unusual requirements
6518and needs to do its own mode validation, it might be able to make
6519use of some of these secondary mode helper functions.
6520      </para>
6521
6522      <blockquote><para>
6523	  <programlisting>
6524    const char *xf86ModeStatusToString(ModeStatus status);
6525	  </programlisting>
6526	  <blockquote><para>
6527      This function converts the <parameter>status</parameter> value to a
6528      descriptive printable string.
6529	    </para>
6530
6531	  </blockquote></para></blockquote>
6532
6533      <blockquote><para>
6534	  <programlisting>
6535    void xf86DeleteMode(DisplayModePtr *modeList, DisplayModePtr mode);
6536	  </programlisting>
6537	  <blockquote><para>
6538      This function deletes the <parameter>mode</parameter> given from the
6539      <parameter>modeList</parameter>.  It never prints any messages, so it is
6540      up to the caller to print a message if required.
6541	    </para>
6542
6543	  </blockquote></para></blockquote>
6544    </sect2>
6545
6546    <sect2>
6547      <title>Functions for handling strings and tokens</title>
6548
6549      <para>
6550    Tables associating strings and numerical tokens combined with the
6551    following functions provide a compact way of handling strings from
6552    the config file, and for converting tokens into printable strings.
6553    The table data structure is:
6554
6555	<programlisting>
6556typedef struct {
6557    int                 token;
6558    const char *        name;
6559} SymTabRec, *SymTabPtr;
6560	</programlisting>
6561      </para>
6562
6563      <para>
6564    A table is an initialised array of <structname>SymTabRec</structname>.  The
6565    tokens must be non-negative integers.  Multiple names may be mapped
6566    to a single token.  The table is terminated with an element with a
6567    <structfield>token</structfield> value of <constant>-1</constant> and
6568    <constant>NULL</constant> for the <structfield>name</structfield>.
6569      </para>
6570
6571      <blockquote><para>
6572	  <programlisting>
6573    const char *xf86TokenToString(SymTabPtr table, int token);
6574	  </programlisting>
6575	  <blockquote><para>
6576      This function returns the first string in <parameter>table</parameter>
6577      that matches <parameter>token</parameter>.  If no match is found,
6578      <constant>NULL</constant> is returned (NOTE, older versions of this
6579      function would return the string "unknown" when no match is found).
6580	    </para>
6581
6582	  </blockquote></para></blockquote>
6583
6584      <blockquote><para>
6585	  <programlisting>
6586    int xf86StringToToken(SymTabPtr table, const char *string);
6587	  </programlisting>
6588	  <blockquote><para>
6589      This function returns the first token in <parameter>table</parameter>
6590      that matches <parameter>string</parameter>.  The
6591      <function>xf86NameCmp()</function> function is used to determine the
6592      match.  If no match is found, <constant>-1</constant> is returned.
6593	    </para>
6594
6595	  </blockquote></para></blockquote>
6596
6597    </sect2>
6598
6599    <sect2>
6600      <title>Functions for finding which config file entries to use</title>
6601
6602      <para>
6603    These functions can be used to select the appropriate config file
6604    entries that match the detected hardware.  They are described above
6605    in the <link linkend="probe">Probe</link> and
6606    <link linkend="avail">Available Functions</link> sections.
6607      </para>
6608
6609    </sect2>
6610
6611    <sect2>
6612      <title>Probing discrete clocks on old hardware</title>
6613
6614      <para>
6615    The <function>xf86GetClocks()</function> function may be used to assist
6616    in finding the discrete pixel clock values on older hardware.
6617      </para>
6618
6619      <blockquote><para>
6620	  <programlisting>
6621    void xf86GetClocks(ScrnInfoPtr pScrn, int num,
6622                       Bool (*ClockFunc)(ScrnInfoPtr, int),
6623                       void (*ProtectRegs)(ScrnInfoPtr, Bool),
6624                       void (*BlankScreen)(ScrnInfoPtr, Bool),
6625                       int vertsyncreg, int maskval, int knownclkindex,
6626                       int knownclkvalue);
6627	  </programlisting>
6628	  <blockquote><para>
6629      This function uses a comparative sampling method to measure the
6630      discrete pixel clock values.  The number of discrete clocks to
6631      measure is given by <parameter>num</parameter>.  <parameter>clockFunc</parameter>
6632      is a function that selects the <parameter>n</parameter>'th clock.  It
6633      should also save or restore any state affected by programming the
6634      clocks when the index passed is <constant>CLK_REG_SAVE</constant> or
6635      <constant>CLK_REG_RESTORE</constant>.  <parameter>ProtectRegs</parameter> is
6636      a function that does whatever is required to protect the hardware
6637      state while selecting a new clock.  <parameter>BlankScreen</parameter>
6638      is a function that blanks the screen.  <parameter>vertsyncreg</parameter>
6639      and <parameter>maskval</parameter> are the register and bitmask to
6640      check for the presence of vertical sync pulses.
6641      <parameter>knownclkindex</parameter> and <parameter>knownclkvalue</parameter>
6642      are the index and value of a known clock.  These are the known
6643      references on which the comparative measurements are based.  The
6644      number of clocks probed is set in <structfield>pScrn-&gt;numClocks</structfield>,
6645      and the probed clocks are set in the <structfield>pScrn-&gt;clock[]</structfield>
6646      array.  All of the clock values are in units of kHz.
6647	    </para>
6648
6649	  </blockquote></para></blockquote>
6650
6651      <blockquote><para>
6652	  <programlisting>
6653    void xf86ShowClocks(ScrnInfoPtr scrp, MessageType from);
6654	  </programlisting>
6655	  <blockquote><para>
6656      Print out the pixel clocks <parameter>scrp-&gt;clock[]</parameter>.
6657      <parameter>from</parameter> indicates whether the clocks were probed
6658      or from the config file.
6659	    </para>
6660
6661	  </blockquote></para></blockquote>
6662    </sect2>
6663
6664    <sect2>
6665      <title>Other helper functions</title>
6666
6667      <blockquote><para>
6668	  <programlisting>
6669    Bool xf86IsUnblank(int mode);
6670	  </programlisting>
6671	  <blockquote><para>
6672      Returns <constant>TRUE</constant> when the screen saver mode specified
6673      by <parameter>mode</parameter> requires the screen be unblanked,
6674      and <constant>FALSE</constant> otherwise.  The screen saver modes that
6675      require blanking are <constant>SCREEN_SAVER_ON</constant> and
6676      <constant>SCREEN_SAVER_CYCLE</constant>, and the screen saver modes that
6677      require unblanking are <constant>SCREEN_SAVER_OFF</constant> and
6678      <constant>SCREEN_SAVER_FORCER</constant>.  Drivers may call this helper
6679      from their <function>SaveScreen()</function> function to interpret the
6680      screen saver modes.
6681	    </para>
6682
6683	  </blockquote></para></blockquote>
6684    </sect2>
6685  </sect1>
6686
6687  <sect1>
6688    <title>The vgahw module</title>
6689
6690    <para>
6691The vgahw modules provides an interface for saving, restoring and
6692programming the standard VGA registers, and for handling VGA colourmaps.
6693    </para>
6694
6695    <sect2>
6696      <title>Data Structures</title>
6697
6698      <para>
6699    The public data structures used by the vgahw module are
6700    <structname>vgaRegRec</structname> and <structname>vgaHWRec</structname>.  They are
6701    defined in <filename>vgaHW.h.</filename>
6702      </para>
6703
6704    </sect2>
6705
6706    <sect2>
6707      <title>General vgahw Functions</title>
6708
6709      <blockquote><para>
6710	  <programlisting>
6711    Bool vgaHWGetHWRec(ScrnInfoPtr pScrn);
6712	  </programlisting>
6713	  <blockquote><para>
6714      This function allocates a <structname>vgaHWRec</structname> structure, and
6715      hooks it into the <structname>ScrnInfoRec</structname>'s
6716      <structfield>privates</structfield>.  Like all information hooked into the
6717      <structfield>privates</structfield>, it is persistent, and only needs to be
6718      allocated once per screen.  This function should normally be called
6719      from the driver's <function>ChipPreInit()</function> function.  The
6720      <structname>vgaHWRec</structname> is zero-allocated, and the following
6721      fields are explicitly initialised:
6722
6723	      <variablelist>
6724		<varlistentry>
6725		  <term><structfield>ModeReg.DAC[]</structfield></term>
6726		  <listitem><para>initialised with a default colourmap
6727		    </para></listitem></varlistentry>
6728		<varlistentry>
6729		  <term><structfield>ModeReg.Attribute[0x11]</structfield></term>
6730		  <listitem><para>initialised with the default overscan index
6731		    </para></listitem></varlistentry>
6732		<varlistentry>
6733		  <term><structfield>ShowOverscan</structfield></term>
6734		  <listitem><para>initialised according to the "ShowOverscan" option
6735		    </para></listitem></varlistentry>
6736		<varlistentry>
6737		  <term><structfield>paletteEnabled</structfield></term>
6738		  <listitem><para>initialised to FALSE
6739		    </para></listitem></varlistentry>
6740		<varlistentry>
6741		  <term><structfield>cmapSaved</structfield></term>
6742		  <listitem><para>initialised to FALSE
6743		    </para></listitem></varlistentry>
6744		<varlistentry>
6745		  <term><structfield>pScrn</structfield></term>
6746		  <listitem><para>initialised to pScrn
6747		    </para></listitem></varlistentry>
6748	      </variablelist>
6749	    </para>
6750
6751	    <para>
6752      In addition to the above, <function>vgaHWSetStdFuncs()</function> is
6753      called to initialise the register access function fields with the
6754      standard VGA set of functions.
6755	    </para>
6756
6757	    <para>
6758      Once allocated, a pointer to the <structname>vgaHWRec</structname> can be
6759      obtained from the <literal remap="tt">ScrnInfoPtr</literal> with the
6760      <literal remap="tt">VGAHWPTR(pScrn)</literal> macro.
6761	    </para>
6762
6763	  </blockquote></para></blockquote>
6764
6765      <blockquote><para>
6766	  <programlisting>
6767    void vgaHWFreeHWRec(ScrnInfoPtr pScrn);
6768	  </programlisting>
6769	  <blockquote><para>
6770      This function frees a <structname>vgaHWRec</structname> structure.  It
6771      should be called from a driver's <function>ChipFreeScreen()</function>
6772      function.
6773	    </para>
6774
6775	  </blockquote></para></blockquote>
6776
6777      <blockquote><para>
6778	  <programlisting>
6779    Bool vgaHWSetRegCounts(ScrnInfoPtr pScrn, int numCRTC,
6780                          int numSequencer, int numGraphics, int numAttribute);
6781	  </programlisting>
6782	  <blockquote><para>
6783      This function allows the number of CRTC, Sequencer, Graphics and
6784      Attribute registers to be changed.  This makes it possible for
6785      extended registers to be saved and restored with
6786      <function>vgaHWSave()</function> and <function>vgaHWRestore()</function>.
6787      This function should be called after a <structname>vgaHWRec</structname>
6788      has been allocated with <function>vgaHWGetHWRec()</function>.  The
6789      default values are defined in <filename>vgaHW.h</filename> as follows:
6790
6791	      <programlisting>
6792#define VGA_NUM_CRTC 25
6793#define VGA_NUM_SEQ   5
6794#define VGA_NUM_GFX   9
6795#define VGA_NUM_ATTR 21
6796	      </programlisting>
6797	    </para>
6798
6799	  </blockquote></para></blockquote>
6800
6801      <blockquote><para>
6802	  <programlisting>
6803    Bool vgaHWCopyReg(vgaRegPtr dst, vgaRegPtr src);
6804	  </programlisting>
6805	  <blockquote><para>
6806      This function copies the contents of the VGA saved registers in
6807      <parameter>src</parameter> to <parameter>dst</parameter>.  Note that it isn't
6808      possible to simply do this with <function>memcpy()</function> (or
6809      similar).  This function returns <constant>TRUE</constant> unless there
6810      is a problem allocating space for the <structfield>CRTC</structfield> and
6811      related fields in <parameter>dst</parameter>.
6812	    </para>
6813
6814	  </blockquote></para></blockquote>
6815
6816      <blockquote><para>
6817	  <programlisting>
6818    void vgaHWSetStdFuncs(vgaHWPtr hwp);
6819	  </programlisting>
6820	  <blockquote><para>
6821      This function initialises the register access function fields of
6822      <parameter>hwp</parameter> with the standard VGA set of functions.  This
6823      is called by <function>vgaHWGetHWRec()</function>, so there is usually
6824      no need to call this explicitly.  The register access functions
6825      are described below.  If the registers are shadowed in some other
6826      port I/O space (for example a PCI I/O region), these functions
6827      can be used to access the shadowed registers if
6828      <structfield>hwp-&gt;PIOOffset</structfield> is initialised with
6829      <literal remap="tt">offset</literal>, calculated in such a way that when the
6830      standard VGA I/O port value is added to it the correct offset into
6831      the PIO area results.  This value is initialised to zero in
6832      <function>vgaHWGetHWRec()</function>.  (Note: the PIOOffset functionality
6833      is present in XFree86 4.1.0 and later.)
6834	    </para>
6835
6836	  </blockquote></para></blockquote>
6837
6838      <blockquote><para>
6839	  <programlisting>
6840    void vgaHWSetMmioFuncs(vgaHWPtr hwp, CARD8 *base, int offset);
6841	  </programlisting>
6842	  <blockquote><para>
6843      This function initialised the register access function fields of
6844      hwp with a generic MMIO set of functions.
6845      <structfield>hwp-&gt;MMIOBase</structfield> is initialised with
6846      <parameter>base</parameter>, which must be the virtual address that the
6847      start of MMIO area is mapped to.  <structfield>hwp-&gt;MMIOOffset</structfield>
6848      is initialised with <parameter>offset</parameter>, which must be calculated
6849      in such a way that when the standard VGA I/O port value is added
6850      to it the correct offset into the MMIO area results.  That means
6851      that these functions are only suitable when the VGA I/O ports are
6852      made available in a direct mapping to the MMIO space.  If that is
6853      not the case, the driver will need to provide its own register
6854      access functions.  The register access functions are described
6855      below.
6856	    </para>
6857
6858	  </blockquote></para></blockquote>
6859
6860      <blockquote><para>
6861	  <programlisting>
6862    Bool vgaHWMapMem(ScrnInfoPtr pScrn);
6863	  </programlisting>
6864	  <blockquote><para>
6865      This function maps the VGA memory window.  It requires that the
6866      <structname>vgaHWRec</structname> be allocated.  If a driver requires
6867      non-default <structfield>MapPhys</structfield> or <structfield>MapSize</structfield>
6868      settings (the physical location and size of the VGA memory window)
6869      then those fields of the <structname>vgaHWRec</structname> must be initialised
6870      before calling this function.  Otherwise, this function initialiases
6871      the default values of <constant>0xA0000</constant> for
6872      <structfield>MapPhys</structfield> and <code>(64&nbsp;*&nbsp;1024)</code> for
6873      <structfield>MapSize</structfield>.  This function must be called before
6874      attempting to save or restore the VGA state.  If the driver doesn't
6875      call it explicitly, the <function>vgaHWSave()</function> and
6876      <function>vgaHWRestore()</function> functions may call it if they need
6877      to access the VGA memory (in which case they will also call
6878      <function>vgaHWUnmapMem()</function> to unmap the VGA memory before
6879      exiting).
6880	    </para>
6881
6882	  </blockquote></para></blockquote>
6883
6884      <blockquote><para>
6885	  <programlisting>
6886    void vgaHWUnmapMem(ScrnInfoPtr pScrn);
6887	  </programlisting>
6888	  <blockquote><para>
6889      This function unmaps the VGA memory window.  It must only be called
6890      after the memory has been mapped.  The <structfield>Base</structfield> field
6891      of the <structname>vgaHWRec</structname> field is set to <constant>NULL</constant>
6892      to indicate that the memory is no longer mapped.
6893	    </para>
6894
6895	  </blockquote></para></blockquote>
6896
6897      <blockquote><para>
6898	  <programlisting>
6899    void vgaHWGetIOBase(vgaHWPtr hwp);
6900	  </programlisting>
6901	  <blockquote><para>
6902      This function initialises the <structfield>IOBase</structfield> field of the
6903      <structname>vgaHWRec</structname>.  This function must be called before
6904      using any other functions that access the video hardware.
6905	    </para>
6906
6907	    <para>
6908      A macro <function>VGAHW_GET_IOBASE()</function> is also available in
6909      <filename>vgaHW.h</filename> that returns the I/O base, and this may
6910      be used when the vgahw module is not loaded (for example, in the
6911      <function>ChipProbe()</function> function).
6912	    </para>
6913
6914	  </blockquote></para></blockquote>
6915
6916      <blockquote><para>
6917	  <programlisting>
6918    void vgaHWUnlock(vgaHWPtr hwp);
6919	  </programlisting>
6920	  <blockquote><para>
6921      This function unlocks the VGA <literal remap="tt">CRTC[0-7]</literal> registers,
6922      and must be called before attempting to write to those registers.
6923	    </para>
6924
6925	  </blockquote></para></blockquote>
6926
6927      <blockquote><para>
6928	  <programlisting>
6929    void vgaHWLock(vgaHWPtr hwp);
6930	  </programlisting>
6931	  <blockquote><para>
6932      This function locks the VGA <literal remap="tt">CRTC[0-7]</literal> registers.
6933	    </para>
6934
6935	  </blockquote></para></blockquote>
6936
6937      <blockquote><para>
6938	  <programlisting>
6939    void vgaHWEnable(vgaHWPtr hwp);
6940	  </programlisting>
6941	  <blockquote><para>
6942      This function enables the VGA subsystem.  (Note, this function is
6943      present in XFree86 4.1.0 and later.).
6944	    </para>
6945
6946	  </blockquote></para></blockquote>
6947
6948      <blockquote><para>
6949	  <programlisting>
6950    void vgaHWDisable(vgaHWPtr hwp);
6951	  </programlisting>
6952	  <blockquote><para>
6953      This function disables the VGA subsystem.  (Note, this function is
6954      present in XFree86 4.1.0 and later.).
6955	    </para>
6956
6957	  </blockquote></para></blockquote>
6958
6959      <blockquote><para>
6960	  <programlisting>
6961    void vgaHWSave(ScrnInfoPtr pScrn, vgaRegPtr save, int flags);
6962	  </programlisting>
6963	  <blockquote><para>
6964      This function saves the VGA state.  The state is written to the
6965      <structname>vgaRegRec</structname> pointed to by <parameter>save</parameter>.
6966      <parameter>flags</parameter> is set to one or more of the following flags
6967      ORed together:
6968
6969	      <variablelist>
6970		<varlistentry>
6971		  <term><constant>VGA_SR_MODE</constant></term>
6972		  <listitem><para>the mode setting registers are saved
6973		    </para></listitem></varlistentry>
6974		<varlistentry>
6975		  <term><constant>VGA_SR_FONTS</constant></term>
6976		  <listitem><para>the text mode font/text data is saved
6977		    </para></listitem></varlistentry>
6978		<varlistentry>
6979		  <term><constant>VGA_SR_CMAP</constant></term>
6980		  <listitem><para>the colourmap (LUT) is saved
6981		    </para></listitem></varlistentry>
6982		<varlistentry>
6983		  <term><constant>VGA_SR_ALL</constant></term>
6984		  <listitem><para>all of the above are saved
6985		    </para></listitem></varlistentry>
6986	      </variablelist>
6987	    </para>
6988
6989	    <para>
6990      The <structname>vgaHWRec</structname> and its <structfield>IOBase</structfield> fields
6991      must be initialised before this function is called.  If
6992      <constant>VGA_SR_FONTS</constant> is set in <parameter>flags</parameter>, the
6993      VGA memory window must be mapped.  If it isn't then
6994      <function>vgaHWMapMem()</function> will be called to map it, and
6995      <function>vgaHWUnmapMem()</function> will be called to unmap it
6996      afterwards.  <function>vgaHWSave()</function> uses the three functions
6997      below in the order <function>vgaHWSaveColormap()</function>,
6998      <function>vgaHWSaveMode()</function>, <function>vgaHWSaveFonts()</function> to
6999      carry out the different save phases.  It is undecided at this
7000      stage whether they will remain part of the vgahw module's public
7001      interface or not.
7002	    </para>
7003
7004	  </blockquote></para></blockquote>
7005
7006      <blockquote><para>
7007	  <programlisting>
7008    void vgaHWSaveMode(ScrnInfoPtr pScrn, vgaRegPtr save);
7009	  </programlisting>
7010	  <blockquote><para>
7011      This function saves the VGA mode registers.  They are saved to
7012      the <structname>vgaRegRec</structname> pointed to by <parameter>save</parameter>.
7013      The registers saved are:
7014
7015	      <literallayout>
7016        MiscOut
7017        CRTC[0-0x18]
7018        Attribute[0-0x14]
7019        Graphics[0-8]
7020        Sequencer[0-4]
7021	      </literallayout>
7022	    </para>
7023
7024	    <para>
7025    The number of registers actually saved may be modified by a prior call
7026    to <function>vgaHWSetRegCounts()</function>.
7027	    </para>
7028
7029	  </blockquote></para></blockquote>
7030
7031      <blockquote><para>
7032	  <programlisting>
7033    void vgaHWSaveFonts(ScrnInfoPtr pScrn, vgaRegPtr save);
7034	  </programlisting>
7035	  <blockquote><para>
7036      This function saves the text mode font and text data held in the
7037      video memory.  If called while in a graphics mode, no save is
7038      done.  The VGA memory window must be mapped with
7039      <function>vgaHWMapMem()</function> before to calling this function.
7040	    </para>
7041
7042	    <para>
7043      On some platforms, one or more of the font/text plane saves may be
7044      no-ops.  This is the case when the platform's VC driver already
7045      takes care of this.
7046	    </para>
7047
7048	  </blockquote></para></blockquote>
7049
7050      <blockquote><para>
7051	  <programlisting>
7052    void vgaHWSaveColormap(ScrnInfoPtr pScrn, vgaRegPtr save);
7053	  </programlisting>
7054	  <blockquote><para>
7055      This function saves the VGA colourmap (LUT).  Before saving it, it
7056      attempts to verify that the colourmap is readable.  In rare cases
7057      where it isn't readable, a default colourmap is saved instead.
7058	    </para>
7059
7060	  </blockquote></para></blockquote>
7061
7062      <blockquote><para>
7063	  <programlisting>
7064    void vgaHWRestore(ScrnInfoPtr pScrn, vgaRegPtr restore, int flags);
7065	  </programlisting>
7066	  <blockquote><para>
7067      This function programs the VGA state.  The state programmed is
7068      that contained in the <structname>vgaRegRec</structname> pointed to by
7069      <parameter>restore</parameter>.  <parameter>flags</parameter> is the same
7070      as described above for the <function>vgaHWSave()</function> function.
7071	    </para>
7072
7073	    <para>
7074      The <structname>vgaHWRec</structname> and its <structfield>IOBase</structfield> fields
7075      must be initialised before this function is called.  If
7076      <constant>VGA_SR_FONTS</constant> is set in <parameter>flags</parameter>, the
7077      VGA memory window must be mapped.  If it isn't then
7078      <function>vgaHWMapMem()</function> will be called to map it, and
7079      <function>vgaHWUnmapMem()</function> will be called to unmap it
7080      afterwards.  <function>vgaHWRestore()</function> uses the three functions
7081      below in the order <function>vgaHWRestoreFonts()</function>,
7082      <function>vgaHWRestoreMode()</function>,
7083      <function>vgaHWRestoreColormap()</function> to carry out the different
7084      restore phases.  It is undecided at this stage whether they will
7085      remain part of the vgahw module's public interface or not.
7086	    </para>
7087
7088	  </blockquote></para></blockquote>
7089
7090      <blockquote><para>
7091	  <programlisting>
7092    void vgaHWRestoreMode(ScrnInfoPtr pScrn, vgaRegPtr restore);
7093	  </programlisting>
7094	  <blockquote><para>
7095      This function restores the VGA mode registers.  They are restored
7096      from the data in the <structname>vgaRegRec</structname> pointed to by
7097      <parameter>restore</parameter>.  The registers restored are:
7098
7099	      <literallayout>
7100        MiscOut
7101        CRTC[0-0x18]
7102        Attribute[0-0x14]
7103        Graphics[0-8]
7104        Sequencer[0-4]
7105	      </literallayout>
7106	    </para>
7107
7108	    <para>
7109    The number of registers actually restored may be modified by a prior call
7110    to <function>vgaHWSetRegCounts()</function>.
7111	    </para>
7112
7113	  </blockquote></para></blockquote>
7114
7115      <blockquote><para>
7116	  <programlisting>
7117    void vgaHWRestoreFonts(ScrnInfoPtr pScrn, vgaRegPtr restore);
7118	  </programlisting>
7119	  <blockquote><para>
7120      This function restores the text mode font and text data to the
7121      video memory.  The VGA memory window must be mapped with
7122      <function>vgaHWMapMem()</function> before to calling this function.
7123	    </para>
7124
7125	    <para>
7126      On some platforms, one or more of the font/text plane restores
7127      may be no-ops.  This is the case when the platform's VC driver
7128      already takes care of this.
7129	    </para>
7130
7131	  </blockquote></para></blockquote>
7132
7133      <blockquote><para>
7134	  <programlisting>
7135    void vgaHWRestoreColormap(ScrnInfoPtr pScrn, vgaRegPtr restore);
7136	  </programlisting>
7137	  <blockquote><para>
7138      This function restores the VGA colourmap (LUT).
7139	    </para>
7140
7141	  </blockquote></para></blockquote>
7142
7143      <blockquote><para>
7144	  <programlisting>
7145    void vgaHWInit(ScrnInfoPtr pScrn, DisplayModePtr mode);
7146	  </programlisting>
7147	  <blockquote><para>
7148      This function fills in the <structname>vgaHWRec</structname>'s
7149      <structfield>ModeReg</structfield> field with the values appropriate for
7150      programming the given video mode.  It requires that the
7151      <structname>ScrnInfoRec</structname>'s <structfield>depth</structfield> field is
7152      initialised, which determines how the registers are programmed.
7153	    </para>
7154
7155	  </blockquote></para></blockquote>
7156
7157      <blockquote><para>
7158	  <programlisting>
7159    void vgaHWSeqReset(vgaHWPtr hwp, Bool start);
7160	  </programlisting>
7161	  <blockquote><para>
7162      Do a VGA sequencer reset.  If start is <constant>TRUE</constant>, the
7163      reset is started.  If start is <constant>FALSE</constant>, the reset
7164      is ended.
7165	    </para>
7166
7167	  </blockquote></para></blockquote>
7168
7169      <blockquote><para>
7170	  <programlisting>
7171    void vgaHWProtect(ScrnInfoPtr pScrn, Bool on);
7172	  </programlisting>
7173	  <blockquote><para>
7174      This function protects VGA registers and memory from corruption
7175      during loads.  It is typically called with on set to
7176      <constant>TRUE</constant> before programming, and with on set to
7177      <constant>FALSE</constant> after programming.
7178	    </para>
7179
7180	  </blockquote></para></blockquote>
7181
7182      <blockquote><para>
7183	  <programlisting>
7184    Bool vgaHWSaveScreen(ScreenPtr pScreen, int mode);
7185	  </programlisting>
7186	  <blockquote><para>
7187      This function blanks and unblanks the screen.  It is blanked when
7188      <parameter>mode</parameter> is <constant>SCREEN_SAVER_ON</constant> or
7189      <constant>SCREEN_SAVER_CYCLE</constant>, and unblanked when
7190      <parameter>mode</parameter> is <constant>SCREEN_SAVER_OFF</constant> or
7191      <constant>SCREEN_SAVER_FORCER</constant>.
7192	    </para>
7193
7194	  </blockquote></para></blockquote>
7195
7196      <blockquote><para>
7197	  <programlisting>
7198    void vgaHWBlankScreen(ScrnInfoPtr pScrn, Bool on);
7199	  </programlisting>
7200	  <blockquote><para>
7201      This function blanks and unblanks the screen.  It is blanked when
7202      <parameter>on</parameter> is <constant>FALSE</constant>, and unblanked when
7203      <parameter>on</parameter> is <constant>TRUE</constant>.  This function is
7204      provided for use in cases where the <structname>ScrnInfoRec</structname>
7205      can't be derived from the <structname>ScreenRec</structname> (while probing
7206      for clocks, for example).
7207	    </para>
7208
7209	  </blockquote></para></blockquote>
7210
7211    </sect2>
7212
7213    <sect2>
7214      <title>VGA Colormap Functions</title>
7215
7216      <para>
7217    The vgahw module uses the standard colormap support (see the
7218    <link linkend="cmap">Colormap Handling</link> section.  This is initialised
7219    with the following function:
7220
7221	<blockquote><para>
7222	  <programlisting>
7223    Bool vgaHWHandleColormaps(ScreenPtr pScreen);
7224	    </programlisting>
7225	  </para></blockquote>
7226      </para>
7227
7228    </sect2>
7229
7230    <sect2>
7231      <title>VGA Register Access Functions</title>
7232
7233      <para>
7234    The vgahw module abstracts access to the standard VGA registers by
7235    using a set of functions held in the <structname>vgaHWRec</structname>.  When
7236    the <structname>vgaHWRec</structname> is created these function pointers are
7237    initialised with the set of standard VGA I/O register access functions.
7238    In addition to these, the vgahw module includes a basic set of MMIO
7239    register access functions, and the <structname>vgaHWRec</structname> function
7240    pointers can be initialised to these by calling the
7241    <function>vgaHWSetMmioFuncs()</function> function described above.  Some
7242    drivers/platforms may require a different set of functions for VGA
7243    access.  The access functions are described here.
7244      </para>
7245
7246
7247      <blockquote><para>
7248	  <programlisting>
7249    void writeCrtc(vgaHWPtr hwp, CARD8 index, CARD8 value);
7250	  </programlisting>
7251	  <blockquote><para>
7252      Write <parameter>value</parameter> to CRTC register <parameter>index</parameter>.
7253	    </para>
7254
7255	  </blockquote></para></blockquote>
7256
7257      <blockquote><para>
7258	  <programlisting>
7259    CARD8 readCrtc(vgaHWPtr hwp, CARD8 index);
7260	  </programlisting>
7261	  <blockquote><para>
7262      Return the value read from CRTC register <parameter>index</parameter>.
7263	    </para>
7264
7265	  </blockquote></para></blockquote>
7266
7267      <blockquote><para>
7268	  <programlisting>
7269    void writeGr(vgaHWPtr hwp, CARD8 index, CARD8 value);
7270	  </programlisting>
7271	  <blockquote><para>
7272      Write <parameter>value</parameter> to Graphics Controller register
7273      <parameter>index</parameter>.
7274	    </para>
7275
7276	  </blockquote></para></blockquote>
7277
7278      <blockquote><para>
7279	  <programlisting>
7280    CARD8 readGR(vgaHWPtr hwp, CARD8 index);
7281	  </programlisting>
7282	  <blockquote><para>
7283      Return the value read from Graphics Controller register
7284      <parameter>index</parameter>.
7285	    </para>
7286
7287	  </blockquote></para></blockquote>
7288
7289      <blockquote><para>
7290	  <programlisting>
7291    void writeSeq(vgaHWPtr hwp, CARD8 index, CARD8, value);
7292	  </programlisting>
7293	  <blockquote><para>
7294      Write <parameter>value</parameter> to Sequencer register
7295      <parameter>index</parameter>.
7296	    </para>
7297
7298	  </blockquote></para></blockquote>
7299
7300      <blockquote><para>
7301	  <programlisting>
7302    CARD8 readSeq(vgaHWPtr hwp, CARD8 index);
7303	  </programlisting>
7304	  <blockquote><para>
7305      Return the value read from Sequencer register <parameter>index</parameter>.
7306	    </para>
7307
7308	  </blockquote></para></blockquote>
7309
7310      <blockquote><para>
7311	  <programlisting>
7312    void writeAttr(vgaHWPtr hwp, CARD8 index, CARD8, value);
7313	  </programlisting>
7314	  <blockquote><para>
7315      Write <parameter>value</parameter> to Attribute Controller register
7316      <parameter>index</parameter>.  When writing out the index value this
7317      function should set bit 5 (<constant>0x20</constant>) according to the
7318      setting of <structfield>hwp-&gt;paletteEnabled</structfield> in order to
7319      preserve the palette access state.  It should be cleared when
7320      <structfield>hwp-&gt;paletteEnabled</structfield> is <constant>TRUE</constant>
7321      and set when it is <constant>FALSE</constant>.
7322	    </para>
7323
7324	  </blockquote></para></blockquote>
7325
7326      <blockquote><para>
7327	  <programlisting>
7328    CARD8 readAttr(vgaHWPtr hwp, CARD8 index);
7329	  </programlisting>
7330	  <blockquote><para>
7331      Return the value read from Attribute Controller register
7332      <parameter>index</parameter>.  When writing out the index value this
7333      function should set bit 5 (<constant>0x20</constant>) according to the
7334      setting of <structfield>hwp-&gt;paletteEnabled</structfield> in order to
7335      preserve the palette access state.  It should be cleared when
7336      <structfield>hwp-&gt;paletteEnabled</structfield> is <constant>TRUE</constant>
7337      and set when it is <constant>FALSE</constant>.
7338	    </para>
7339
7340	  </blockquote></para></blockquote>
7341
7342      <blockquote><para>
7343	  <programlisting>
7344    void writeMiscOut(vgaHWPtr hwp, CARD8 value);
7345	  </programlisting>
7346	  <blockquote><para>
7347      Write <quote><parameter>value</parameter></quote> to the Miscellaneous Output register.
7348	    </para>
7349
7350	  </blockquote></para></blockquote>
7351
7352      <blockquote><para>
7353	  <programlisting>
7354    CARD8 readMiscOut(vgwHWPtr hwp);
7355	  </programlisting>
7356	  <blockquote><para>
7357      Return the value read from the Miscellaneous Output register.
7358	    </para>
7359
7360	  </blockquote></para></blockquote>
7361
7362      <blockquote><para>
7363	  <programlisting>
7364    void enablePalette(vgaHWPtr hwp);
7365	  </programlisting>
7366	  <blockquote><para>
7367      Clear the palette address source bit in the Attribute Controller
7368      index register and set <literal remap="tt">hwp-&gt;paletteEnabled</literal> to
7369      <constant>TRUE</constant>.
7370	    </para>
7371
7372	  </blockquote></para></blockquote>
7373
7374      <blockquote><para>
7375	  <programlisting>
7376    void disablePalette(vgaHWPtr hwp);
7377	  </programlisting>
7378	  <blockquote><para>
7379      Set the palette address source bit in the Attribute Controller
7380      index register and set <literal remap="tt">hwp-&gt;paletteEnabled</literal> to
7381      <constant>FALSE</constant>.
7382	    </para>
7383
7384	  </blockquote></para></blockquote>
7385
7386      <blockquote><para>
7387	  <programlisting>
7388    void writeDacMask(vgaHWPtr hwp, CARD8 value);
7389	  </programlisting>
7390	  <blockquote><para>
7391      Write <parameter>value</parameter> to the DAC Mask register.
7392	    </para>
7393
7394	  </blockquote></para></blockquote>
7395
7396      <blockquote><para>
7397	  <programlisting>
7398    CARD8 readDacMask(vgaHWptr hwp);
7399	  </programlisting>
7400	  <blockquote><para>
7401      Return the value read from the DAC Mask register.
7402	    </para>
7403
7404	  </blockquote></para></blockquote>
7405
7406      <blockquote><para>
7407	  <programlisting>
7408    void writeDacReadAddress(vgaHWPtr hwp, CARD8 value);
7409	  </programlisting>
7410	  <blockquote><para>
7411      Write <parameter>value</parameter> to the DAC Read Address register.
7412	    </para>
7413
7414	  </blockquote></para></blockquote>
7415
7416      <blockquote><para>
7417	  <programlisting>
7418    void writeDacWriteAddress(vgaHWPtr hwp, CARD8 value);
7419	  </programlisting>
7420	  <blockquote><para>
7421      Write <parameter>value</parameter> to the DAC Write Address register.
7422	    </para>
7423
7424	  </blockquote></para></blockquote>
7425
7426      <blockquote><para>
7427	  <programlisting>
7428    void writeDacData(vgaHWPtr hwp, CARD8 value);
7429	  </programlisting>
7430	  <blockquote><para>
7431      Write <parameter>value</parameter> to the DAC Data register.
7432	    </para>
7433
7434	  </blockquote></para></blockquote>
7435
7436      <blockquote><para>
7437	  <programlisting>
7438    CARD8 readDacData(vgaHWptr hwp);
7439	  </programlisting>
7440	  <blockquote><para>
7441      Return the value read from the DAC Data register.
7442	    </para>
7443
7444	  </blockquote></para></blockquote>
7445
7446      <blockquote><para>
7447	  <programlisting>
7448    CARD8 readEnable(vgaHWptr hwp);
7449	  </programlisting>
7450	  <blockquote><para>
7451      Return the value read from the VGA Enable register.  (Note: This
7452      function is present in XFree86 4.1.0 and later.)
7453	    </para>
7454
7455	  </blockquote></para></blockquote>
7456
7457      <blockquote><para>
7458	  <programlisting>
7459    void writeEnable(vgaHWPtr hwp, CARD8 value);
7460	  </programlisting>
7461	  <blockquote><para>
7462      Write <parameter>value</parameter> to the VGA Enable register.  (Note: This
7463      function is present in XFree86 4.1.0 and later.)
7464	    </para>
7465
7466	  </blockquote></para></blockquote>
7467    </sect2>
7468  </sect1>
7469
7470  <sect1 id="sample">
7471    <title>Some notes about writing a driver</title>
7472
7473    <note><para>NOTE: some parts of this are not up to date</para></note>
7474
7475    <para>
7476The following is an outline for writing a basic unaccelerated driver
7477for a PCI video card with a linear mapped framebuffer, and which has a
7478VGA core.  It is includes some general information that is relevant to
7479most drivers (even those which don't fit that basic description).
7480    </para>
7481
7482    <para>
7483The information here is based on the initial conversion of the Matrox
7484Millennium driver to the <quote>new design</quote>.  For a fleshing out and sample
7485implementation of some of the bits outlined here, refer to that driver.
7486Note that this is an example only.  The approach used here will not be
7487appropriate for all drivers.
7488    </para>
7489
7490    <para>
7491Each driver must reserve a unique driver name, and a string that is used
7492to prefix all of its externally visible symbols.  This is to avoid name
7493space clashes when loading multiple drivers.  The examples here are for
7494the <quote>ZZZ</quote> driver, which uses the <quote>ZZZ</quote> or <quote>zzz</quote> prefix for its externally
7495visible symbols.
7496    </para>
7497
7498    <sect2>
7499      <title>Include files</title>
7500
7501      <para>
7502  All drivers normally include the following headers:
7503	<literallayout><filename>
7504    "xf86.h"
7505    "xf86_OSproc.h"
7506    "xf86_ansic.h"
7507    "xf86Resources.h"
7508	  </filename></literallayout>
7509  Wherever inb/outb (and related things) are used the following should be
7510  included:
7511	<literallayout><filename>
7512    "compiler.h"
7513	  </filename></literallayout>
7514  Note: in drivers, this must be included after <filename>"xf86_ansic.h"</filename>.
7515      </para>
7516
7517      <para>
7518  Drivers that need to access PCI vendor/device definitions need this:
7519	<literallayout><filename>
7520    "xf86PciInfo.h"
7521	  </filename></literallayout>
7522      </para>
7523
7524      <para>
7525  Drivers that need to access the PCI config space need this:
7526	<literallayout><filename>
7527    "xf86Pci.h"
7528	  </filename></literallayout>
7529      </para>
7530
7531      <para>
7532  Drivers using the mi banking wrapper need:
7533
7534	<literallayout><filename>
7535    "mibank.h"
7536	  </filename></literallayout>
7537      </para>
7538
7539      <para>
7540  Drivers that initialise a SW cursor need this:
7541	<literallayout><filename>
7542    "mipointer.h"
7543	  </filename></literallayout>
7544      </para>
7545
7546      <para>
7547  All drivers using the mi colourmap code need this:
7548	<literallayout><filename>
7549    "micmap.h"
7550	  </filename></literallayout>
7551      </para>
7552
7553      <para>
7554  If a driver uses the vgahw module, it needs this:
7555	<literallayout><filename>
7556    "vgaHW.h"
7557	  </filename></literallayout>
7558      </para>
7559
7560      <para>
7561  Drivers supporting VGA or Hercules monochrome screens need:
7562	<literallayout><filename>
7563    "xf1bpp.h"
7564	  </filename></literallayout>
7565      </para>
7566
7567      <para>
7568  Drivers supporting VGA or EGC 16-colour screens need:
7569	<literallayout><filename>
7570    "xf4bpp.h"
7571	  </filename></literallayout>
7572      </para>
7573
7574      <para>
7575  Drivers using cfb need:
7576	<programlisting>
7577    #define PSZ 8
7578    #include "cfb.h"
7579    #undef PSZ
7580	</programlisting>
7581      </para>
7582
7583      <para>
7584  Drivers supporting bpp 16, 24 or 32 with cfb need one or more of:
7585	<literallayout><filename>
7586    "cfb16.h"
7587    "cfb24.h"
7588    "cfb32.h"
7589	  </filename></literallayout>
7590      </para>
7591
7592      <para>
7593  The driver's own header file:
7594	<literallayout><filename>
7595    "zzz.h"
7596	  </filename></literallayout>
7597      </para>
7598
7599      <para>
7600  Drivers must NOT include the following:
7601
7602	<literallayout><filename>
7603    "xf86Priv.h"
7604    "xf86Privstr.h"
7605    "xf86_libc.h"
7606    "xf86_OSlib.h"
7607    "Xos.h"</filename>
7608    any OS header
7609	</literallayout>
7610      </para>
7611
7612    </sect2>
7613
7614    <sect2>
7615      <title>Data structures and initialisation</title>
7616
7617      <itemizedlist>
7618	<listitem>
7619	  <para>The following macros should be defined:
7620	    <programlisting>
7621#define VERSION &lt;version-as-an-int&gt;
7622#define ZZZ_NAME "ZZZ"         /* the name used to prefix messages */
7623#define ZZZ_DRIVER_NAME "zzz"  /* the driver name as used in config file */
7624#define ZZZ_MAJOR_VERSION &lt;int&gt;
7625#define ZZZ_MINOR_VERSION &lt;int&gt;
7626#define ZZZ_PATCHLEVEL    &lt;int&gt;
7627	    </programlisting>
7628	  </para>
7629	  <para>
7630    NOTE: <constant>ZZZ_DRIVER_NAME</constant> should match the name of the
7631    driver module without things like the "lib" prefix, the "_drv" suffix
7632    or filename extensions.
7633	  </para>
7634	</listitem>
7635
7636	<listitem>
7637	  <para>
7638    A DriverRec must be defined, which includes the functions required
7639    at the pre-probe phase.  The name of this DriverRec must be an
7640    upper-case version of ZZZ_DRIVER_NAME (for the purposes of static
7641    linking).
7642	    <programlisting>
7643DriverRec ZZZ = {
7644    VERSION,
7645    ZZZ_DRIVER_NAME,
7646    ZZZIdentify,
7647    ZZZProbe,
7648    ZZZAvailableOptions,
7649    NULL,
7650    0
7651};
7652	    </programlisting>
7653	  </para>
7654	</listitem>
7655
7656	<listitem>
7657	  <para>Define list of supported chips and their matching ID:
7658	    <programlisting>
7659static SymTabRec ZZZChipsets[] = {
7660    { PCI_CHIP_ZZZ1234, "zzz1234a" },
7661    { PCI_CHIP_ZZZ5678, "zzz5678a" },
7662    { -1,               NULL }
7663};
7664	    </programlisting>
7665	  </para>
7666	  <para>
7667    The token field may be any integer value that the driver may use to
7668    uniquely identify the supported chipsets.  For drivers that support
7669    only PCI devices using the PCI device IDs might be a natural choice,
7670    but this isn't mandatory.  For drivers that support both PCI and other
7671    devices (like ISA), some other ID should probably used.  When other
7672    IDs are used as the tokens it is recommended that the names be
7673    defined as an <type>enum</type> type.
7674	  </para>
7675	</listitem>
7676
7677	<listitem>
7678	  <para>
7679    If the driver uses the <function>xf86MatchPciInstances()</function>
7680    helper (recommended for drivers that support PCI cards) a list that
7681    maps PCI IDs to chip IDs and fixed resources must be defined:
7682	    <programlisting>
7683static PciChipsets ZZZPciChipsets[] = {
7684    { PCI_CHIP_ZZZ1234, PCI_CHIP_ZZZ1234, RES_SHARED_VGA },
7685    { PCI_CHIP_ZZZ5678, PCI_CHIP_ZZZ5678, RES_SHARED_VGA },
7686    { -1,               -1,               RES_UNDEFINED }
7687}
7688	    </programlisting>
7689	  </para>
7690	</listitem>
7691
7692	<listitem>
7693	  <para>
7694    Define the <structname>XF86ModuleVersionInfo</structname> struct for the
7695    driver.  This is required for the dynamically loaded version:
7696	    <programlisting>
7697static XF86ModuleVersionInfo zzzVersRec =
7698{
7699    "zzz",
7700    MODULEVENDORSTRING,
7701    MODINFOSTRING1,
7702    MODINFOSTRING2,
7703    XF86_VERSION_CURRENT,
7704    ZZZ_MAJOR_VERSION, ZZZ_MINOR_VERSION, ZZZ_PATCHLEVEL,
7705    ABI_CLASS_VIDEODRV,
7706    ABI_VIDEODRV_VERSION,
7707    MOD_CLASS_VIDEODRV,
7708    {0,0,0,0}
7709};
7710	    </programlisting>
7711	  </para>
7712	</listitem>
7713
7714	<listitem>
7715	  <para>
7716    Define a data structure to hold the driver's screen-specific data.
7717    This must be used instead of global variables.  This would be defined
7718    in the <filename>"zzz.h"</filename> file, something like:
7719	    <programlisting>
7720typedef struct {
7721    type1  field1;
7722    type2  field2;
7723    int    fooHack;
7724    Bool   pciRetry;
7725    Bool   noAccel;
7726    Bool   hwCursor;
7727    CloseScreenProcPtr CloseScreen;
7728    OptionInfoPtr Options;
7729    ...
7730} ZZZRec, *ZZZPtr;
7731	    </programlisting>
7732	  </para>
7733	</listitem>
7734
7735	<listitem>
7736	  <para>
7737    Define the list of config file Options that the driver accepts.  For
7738    consistency between drivers those in the list of <quote>standard</quote> options
7739    should be used where appropriate before inventing new options.
7740
7741	    <programlisting>
7742typedef enum {
7743    OPTION_FOO_HACK,
7744    OPTION_PCI_RETRY,
7745    OPTION_HW_CURSOR,
7746    OPTION_NOACCEL
7747} ZZZOpts;
7748
7749static const OptionInfoRec ZZZOptions[] = {
7750  { OPTION_FOO_HACK,  "FooHack",   OPTV_INTEGER, {0}, FALSE },
7751  { OPTION_PCI_RETRY, "PciRetry",  OPTV_BOOLEAN, {0}, FALSE },
7752  { OPTION_HW_CURSOR, "HWcursor",  OPTV_BOOLEAN, {0}, FALSE },
7753  { OPTION_NOACCEL,   "NoAccel",   OPTV_BOOLEAN, {0}, FALSE },
7754  { -1,               NULL,        OPTV_NONE,    {0}, FALSE }
7755};
7756	    </programlisting>
7757	  </para>
7758	</listitem>
7759      </itemizedlist>
7760    </sect2>
7761
7762    <sect2>
7763      <title>Functions</title>
7764
7765
7766      <sect3>
7767	<title>SetupProc</title>
7768
7769	<para>
7770    For dynamically loaded modules, a <varname>ModuleData</varname>
7771    variable is required.  It is should be the name of the driver
7772    prepended to "ModuleData".  A <function>Setup()</function> function is
7773    also required, which calls <function>xf86AddDriver()</function> to add
7774    the driver to the main list of drivers.
7775	</para>
7776
7777	<programlisting>
7778static MODULESETUPPROTO(zzzSetup);
7779
7780XF86ModuleData zzzModuleData = { &amp;zzzVersRec, zzzSetup, NULL };
7781
7782static pointer
7783zzzSetup(pointer module, pointer opts, int *errmaj, int *errmin)
7784{
7785    static Bool setupDone = FALSE;
7786
7787    /* This module should be loaded only once, but check to be sure. */
7788
7789    if (!setupDone) {
7790        /*
7791         * Modules that this driver always requires may be loaded
7792         * here  by calling LoadSubModule().
7793         */
7794
7795        setupDone = TRUE;
7796        xf86AddDriver(&amp;MGA, module, 0);
7797
7798        /*
7799         * The return value must be non-NULL on success even though
7800         * there is no TearDownProc.
7801         */
7802        return (pointer)1;
7803    } else {
7804        if (errmaj) *errmaj = LDR_ONCEONLY;
7805        return NULL;
7806    }
7807}
7808	</programlisting>
7809      </sect3>
7810
7811      <sect3>
7812	<title>GetRec, FreeRec</title>
7813
7814	<para>
7815    A function is usually required to allocate the driver's
7816    screen-specific data structure and hook it into the
7817    <structname>ScrnInfoRec</structname>'s <structfield>driverPrivate</structfield> field.
7818    The <structname>ScrnInfoRec</structname>'s <structfield>driverPrivate</structfield> is
7819    initialised to <constant>NULL</constant>, so it is easy to check if the
7820    initialisation has already been done.  After allocating it, initialise
7821    the fields.  By using <function>xnfcalloc()</function> to do the allocation
7822    it is zeroed, and if the allocation fails the server exits.
7823	</para>
7824
7825	<para>
7826    NOTE:
7827    When allocating structures from inside the driver which are defined
7828    on the common level it is important to initialize the structure to
7829    zero.
7830    Only this guarantees that the server remains source compatible to
7831    future changes in common level structures.
7832	</para>
7833
7834	<programlisting>
7835static Bool
7836ZZZGetRec(ScrnInfoPtr pScrn)
7837{
7838    if (pScrn-&gt;driverPrivate != NULL)
7839        return TRUE;
7840    pScrn-&gt;driverPrivate = xnfcalloc(sizeof(ZZZRec), 1);
7841    /* Initialise as required */
7842    ...
7843    return TRUE;
7844}
7845	</programlisting>
7846
7847	<para>
7848    Define a macro in <filename>"zzz.h"</filename> which gets a pointer to
7849    the <structname>ZZZRec</structname> when given <parameter>pScrn</parameter>:
7850
7851	  <programlisting>
7852#define ZZZPTR(p) ((ZZZPtr)((p)-&gt;driverPrivate))
7853	  </programlisting>
7854	</para>
7855
7856	<para>
7857    Define a function to free the above, setting it to <constant>NULL</constant>
7858    once it has been freed:
7859
7860	  <programlisting>
7861static void
7862ZZZFreeRec(ScrnInfoPtr pScrn)
7863{
7864    if (pScrn-&gt;driverPrivate == NULL)
7865        return;
7866    xfree(pScrn-&gt;driverPrivate);
7867    pScrn-&gt;driverPrivate = NULL;
7868}
7869	  </programlisting>
7870	</para>
7871      </sect3>
7872
7873      <sect3>
7874	<title>Identify</title>
7875
7876	<para>
7877    Define the <function>Identify()</function> function.  It is run before
7878    the Probe, and typically prints out an identifying message, which
7879    might include the chipsets it supports.  This function is mandatory:
7880
7881	  <programlisting>
7882static void
7883ZZZIdentify(int flags)
7884{
7885    xf86PrintChipsets(ZZZ_NAME, "driver for ZZZ Tech chipsets",
7886                      ZZZChipsets);
7887}
7888	  </programlisting>
7889	</para>
7890      </sect3>
7891
7892      <sect3>
7893	<title>Probe</title>
7894
7895	<para>
7896    Define the <function>Probe()</function> function.  The purpose of this
7897    is to find all instances of the hardware that the driver supports,
7898    and for the ones not already claimed by another driver, claim the
7899    slot, and allocate a <structname>ScrnInfoRec</structname>.  This should be
7900    a minimal probe, and it should under no circumstances leave the
7901    state of the hardware changed.  Because a device is found, don't
7902    assume that it will be used.  Don't do any initialisations other
7903    than the required <structname>ScrnInfoRec</structname> initialisations.
7904    Don't allocate any new data structures.
7905	</para>
7906
7907	<para>
7908    This function is mandatory.
7909	</para>
7910
7911	<para>
7912    NOTE: The <function>xf86DrvMsg()</function> functions cannot be used from
7913    the Probe.
7914	</para>
7915
7916	<programlisting>
7917static Bool
7918ZZZProbe(DriverPtr drv, int flags)
7919{
7920    Bool foundScreen = FALSE;
7921    int numDevSections, numUsed;
7922    GDevPtr *devSections;
7923    int *usedChips;
7924    int i;
7925
7926    /*
7927     * Find the config file Device sections that match this
7928     * driver, and return if there are none.
7929     */
7930    if ((numDevSections = xf86MatchDevice(ZZZ_DRIVER_NAME,
7931                                          &amp;devSections)) &lt;= 0) {
7932        return FALSE;
7933    }
7934
7935    /*
7936     * Since this is a PCI card, "probing" just amounts to checking
7937     * the PCI data that the server has already collected.  If there
7938     * is none, return.
7939     *
7940     * Although the config file is allowed to override things, it
7941     * is reasonable to not allow it to override the detection
7942     * of no PCI video cards.
7943     *
7944     * The provided xf86MatchPciInstances() helper takes care of
7945     * the details.
7946     */
7947    /* test if PCI bus present */
7948    if (xf86GetPciVideoInfo()) {
7949
7950        numUsed = xf86MatchPciInstances(ZZZ_NAME, PCI_VENDOR_ZZZ,
7951                            ZZZChipsets, ZZZPciChipsets, devSections,
7952                            numDevSections, drv, &amp;usedChips);
7953
7954        for (i = 0; i &lt; numUsed; i++) {
7955            ScrnInfoPtr pScrn = NULL;
7956            if ((pScrn = xf86ConfigPciEntity(pScrn, flags, usedChips[i],
7957                                             ZZZPciChipsets, NULL, NULL,
7958                                             NULL, NULL, NULL))) {
7959               /* Allocate a ScrnInfoRec */
7960               pScrn-&gt;driverVersion = VERSION;
7961               pScrn-&gt;driverName    = ZZZ_DRIVER_NAME;
7962               pScrn-&gt;name          = ZZZ_NAME;
7963               pScrn-&gt;Probe         = ZZZProbe;
7964               pScrn-&gt;PreInit       = ZZZPreInit;
7965               pScrn-&gt;ScreenInit    = ZZZScreenInit;
7966               pScrn-&gt;SwitchMode    = ZZZSwitchMode;
7967               pScrn-&gt;AdjustFrame   = ZZZAdjustFrame;
7968               pScrn-&gt;EnterVT       = ZZZEnterVT;
7969               pScrn-&gt;LeaveVT       = ZZZLeaveVT;
7970               pScrn-&gt;FreeScreen    = ZZZFreeScreen;
7971               pScrn-&gt;ValidMode     = ZZZValidMode;
7972               foundScreen = TRUE;
7973               /* add screen to entity */
7974           }
7975        }
7976        xfree(usedChips);
7977    }
7978
7979    xfree(devSections);
7980    return foundScreen;
7981	</programlisting>
7982      </sect3>
7983
7984      <sect3>
7985	<title>AvailableOptions</title>
7986
7987	<para>
7988    Define the <function>AvailableOptions()</function> function. The purpose
7989    of this is to return the available driver options back to the
7990    -configure option, so that an xorg.conf file can be built and the
7991    user can see which options are available for them to use.
7992	</para>
7993      </sect3>
7994
7995      <sect3>
7996	<title>PreInit</title>
7997
7998	<para>
7999    Define the <function>PreInit()</function> function.  The purpose of
8000    this is to find all the information required to determine if the
8001    configuration is usable, and to initialise those parts of the
8002    <structname>ScrnInfoRec</structname> that can be set once at the beginning
8003    of the first server generation.  The information should be found in
8004    the least intrusive way possible.
8005	</para>
8006
8007	<para>
8008    This function is mandatory.
8009	</para>
8010
8011	<para>
8012    NOTES:
8013	  <orderedlist>
8014	    <listitem><para>
8015	 The <function>PreInit()</function> function is only called once
8016	 during the life of the X server (at the start of the first
8017	 generation).
8018	      </para></listitem>
8019
8020	    <listitem><para>
8021	 Data allocated here must be of the type that persists for
8022	 the life of the X server.  This means that data that hooks into
8023	 the <structname>ScrnInfoRec</structname>'s <structfield>privates</structfield>
8024	 field should be allocated here, but data that hooks into the
8025	 <structname>ScreenRec</structname>'s <structfield>devPrivates</structfield> field
8026	 should not be allocated here.  The <structfield>driverPrivate</structfield>
8027	 field should also be allocated here.
8028	      </para></listitem>
8029
8030	    <listitem><para>
8031	 Although the <structname>ScrnInfoRec</structname> has been allocated
8032	 before this function is called, the <structname>ScreenRec</structname>
8033	 has not been allocated.  That means that things requiring it
8034	 cannot be used in this function.
8035	      </para></listitem>
8036
8037	    <listitem><para>
8038	 Very little of the <structname>ScrnInfoRec</structname> has been
8039	 initialised when this function is called.  It is important to
8040	 get the order of doing things right in this function.
8041	      </para></listitem>
8042
8043	  </orderedlist>
8044	</para>
8045
8046	<programlisting>
8047static Bool
8048ZZZPreInit(ScrnInfoPtr pScrn, int flags)
8049{
8050    /* Fill in the monitor field */
8051    pScrn-&gt;monitor = pScrn-&gt;confScreen-&gt;monitor;
8052
8053    /*
8054     * If using the vgahw module, it will typically be loaded
8055     * here by calling xf86LoadSubModule(pScrn, "vgahw");
8056     */
8057
8058    /*
8059     * Set the depth/bpp.  Use the globally preferred depth/bpp.  If the
8060     * driver has special default depth/bpp requirements, the defaults should
8061     * be specified here explicitly.
8062     * We support both 24bpp and 32bpp framebuffer layouts.
8063     * This sets pScrn-&gt;display also.
8064     */
8065    if (!xf86SetDepthBpp(pScrn, 0, 0, 0,
8066                         Support24bppFb | Support32bppFb)) {
8067        return FALSE;
8068    } else {
8069        if (depth/bpp isn't one we support) {
8070            print error message;
8071            return FALSE;
8072        }
8073    }
8074    /* Print out the depth/bpp that was set */
8075    xf86PrintDepthBpp(pScrn);
8076
8077    /* Set bits per RGB for 8bpp */
8078    if (pScrn-&gt;depth &lt;= 8) {
8079        /* Take into account a dac_6_bit option here */
8080        pScrn-&gt;rgbBits = 6 or 8;
8081    }
8082
8083    /*
8084     * xf86SetWeight() and xf86SetDefaultVisual() must be called
8085     * after pScrn-&gt;display is initialised.
8086     */
8087
8088    /* Set weight/mask/offset for depth &gt; 8 */
8089    if (pScrn-&gt;depth &gt; 8) {
8090        if (!xf86SetWeight(pScrn, defaultWeight, defaultMask)) {
8091            return FALSE;
8092        } else {
8093            if (weight isn't one we support) {
8094                print error message;
8095                return FALSE;
8096            }
8097        }
8098    }
8099
8100    /* Set the default visual. */
8101    if (!xf86SetDefaultVisual(pScrn, -1)) {
8102        return FALSE;
8103    } else {
8104        if (visual isn't one we support) {
8105            print error message;
8106            return FALSE;
8107        }
8108    }
8109
8110    /* If the driver supports gamma correction, set the gamma. */
8111    if (!xf86SetGamma(pScrn, default_gamma)) {
8112        return FALSE;
8113    }
8114
8115    /* This driver uses a programmable clock */
8116    pScrn-&gt;progClock = TRUE;
8117
8118    /* Allocate the ZZZRec driverPrivate */
8119    if (!ZZZGetRec(pScrn)) {
8120        return FALSE;
8121    }
8122
8123    pZzz = ZZZPTR(pScrn);
8124
8125    /* Collect all of the option flags (fill in pScrn-&gt;options) */
8126    xf86CollectOptions(pScrn, NULL);
8127
8128    /*
8129     * Process the options based on the information in ZZZOptions.
8130     * The results are written to pZzz-&gt;Options.  If all of the options
8131     * processing is done within this function a local variable "options"
8132     * can be used instead of pZzz-&gt;Options.
8133     */
8134    if (!(pZzz-&gt;Options = xalloc(sizeof(ZZZOptions))))
8135        return FALSE;
8136    (void)memcpy(pZzz-&gt;Options, ZZZOptions, sizeof(ZZZOptions));
8137    xf86ProcessOptions(pScrn-&gt;scrnIndex, pScrn-&gt;options, pZzz-&gt;Options);
8138
8139    /*
8140     * Set various fields of ScrnInfoRec and/or ZZZRec based on
8141     * the options found.
8142     */
8143    from = X_DEFAULT;
8144    pZzz-&gt;hwCursor = FALSE;
8145    if (xf86IsOptionSet(pZzz-&gt;Options, OPTION_HW_CURSOR)) {
8146        from = X_CONFIG;
8147        pZzz-&gt;hwCursor = TRUE;
8148    }
8149    xf86DrvMsg(pScrn-&gt;scrnIndex, from, "Using %s cursor\n",
8150               pZzz-&gt;hwCursor ? "HW" : "SW");
8151    if (xf86IsOptionSet(pZzz-&gt;Options, OPTION_NOACCEL)) {
8152        pZzz-&gt;noAccel = TRUE;
8153        xf86DrvMsg(pScrn-&gt;scrnIndex, X_CONFIG,
8154                   "Acceleration disabled\n");
8155    } else {
8156        pZzz-&gt;noAccel = FALSE;
8157    }
8158    if (xf86IsOptionSet(pZzz-&gt;Options, OPTION_PCI_RETRY)) {
8159        pZzz-&gt;UsePCIRetry = TRUE;
8160        xf86DrvMsg(pScrn-&gt;scrnIndex, X_CONFIG, "PCI retry enabled\n");
8161    }
8162    pZzz-&gt;fooHack = 0;
8163    if (xf86GetOptValInteger(pZzz-&gt;Options, OPTION_FOO_HACK,
8164                             &amp;pZzz-&gt;fooHack)) {
8165        xf86DrvMsg(pScrn-&gt;scrnIndex, X_CONFIG, "Foo Hack set to %d\n",
8166                   pZzz-&gt;fooHack);
8167    }
8168
8169    /*
8170     * Find the PCI slot(s) that this screen claimed in the probe.
8171     * In this case, exactly one is expected, so complain otherwise.
8172     * Note in this case we're not interested in the card types so
8173     * that parameter is set to NULL.
8174     */
8175    if ((i = xf86GetPciInfoForScreen(pScrn-&gt;scrnIndex, &amp;pciList, NULL))
8176        != 1) {
8177        print error message;
8178        ZZZFreeRec(pScrn);
8179        if (i &gt; 0)
8180            xfree(pciList);
8181        return FALSE;
8182    }
8183    /* Note that pciList should be freed below when no longer needed */
8184
8185    /*
8186     * Determine the chipset, allowing config file chipset and
8187     * chipid values to override the probed information.  The config
8188     * chipset value has precedence over its chipid value if both
8189     * are present.
8190     *
8191     * It isn't necessary to fill in pScrn-&gt;chipset if the driver
8192     * keeps track of the chipset in its ZZZRec.
8193     */
8194
8195    ...
8196
8197    /*
8198     * Determine video memory, fb base address, I/O addresses, etc,
8199     * allowing the config file to override probed values.
8200     *
8201     * Set the appropriate pScrn fields (videoRam is probably the
8202     * most important one that other code might require), and
8203     * print out the settings.
8204     */
8205
8206    ...
8207
8208    /* Initialise a clockRanges list. */
8209
8210    ...
8211
8212    /* Set any other chipset specific things in the ZZZRec */
8213
8214    ...
8215
8216    /* Select valid modes from those available */
8217
8218    i = xf86ValidateModes(pScrn, pScrn-&gt;monitor-&gt;Modes,
8219                          pScrn-&gt;display-&gt;modes, clockRanges,
8220                          NULL, minPitch, maxPitch, rounding,
8221                          minHeight, maxHeight,
8222                          pScrn-&gt;display-&gt;virtualX,
8223                          pScrn-&gt;display-&gt;virtualY,
8224                          pScrn-&gt;videoRam * 1024,
8225                          LOOKUP_BEST_REFRESH);
8226    if (i == -1) {
8227        ZZZFreeRec(pScrn);
8228        return FALSE;
8229    }
8230
8231    /* Prune the modes marked as invalid */
8232
8233    xf86PruneDriverModes(pScrn);
8234
8235    /* If no valid modes, return */
8236
8237    if (i == 0 || pScrn-&gt;modes == NULL) {
8238        print error message;
8239        ZZZFreeRec(pScrn);
8240        return FALSE;
8241    }
8242
8243    /*
8244     * Initialise the CRTC fields for the modes.  This driver expects
8245     * vertical values to be halved for interlaced modes.
8246     */
8247    xf86SetCrtcForModes(pScrn, INTERLACE_HALVE_V);
8248
8249    /* Set the current mode to the first in the list. */
8250    pScrn-&gt;currentMode = pScrn-&gt;modes;
8251
8252    /* Print the list of modes being used. */
8253    xf86PrintModes(pScrn);
8254
8255    /* Set the DPI */
8256    xf86SetDpi(pScrn, 0, 0);
8257
8258    /* Load bpp-specific modules */
8259    switch (pScrn-&gt;bitsPerPixel) {
8260    case 1:
8261        mod = "xf1bpp";
8262        break;
8263    case 4:
8264        mod = "xf4bpp";
8265        break;
8266    case 8:
8267        mod = "cfb";
8268        break;
8269    case 16:
8270        mod = "cfb16";
8271        break;
8272    case 24:
8273        mod = "cfb24";
8274        break;
8275    case 32:
8276        mod = "cfb32";
8277        break;
8278    }
8279    if (mod &amp;&amp; !xf86LoadSubModule(pScrn, mod))
8280        ZZZFreeRec(pScrn);
8281        return FALSE;
8282
8283
8284    /* Done */
8285    return TRUE;
8286}
8287	</programlisting>
8288      </sect3>
8289
8290      <sect3>
8291	<title>MapMem, UnmapMem</title>
8292
8293	<para>
8294    Define functions to map and unmap the video memory and any other
8295    memory apertures required.  These functions are not mandatory, but
8296    it is often useful to have such functions.
8297	</para>
8298
8299	<programlisting>
8300static Bool
8301ZZZMapMem(ScrnInfoPtr pScrn)
8302{
8303    /* Call xf86MapPciMem() to map each PCI memory area */
8304    ...
8305    return TRUE or FALSE;
8306}
8307
8308static Bool
8309ZZZUnmapMem(ScrnInfoPtr pScrn)
8310{
8311    /* Call xf86UnMapVidMem() to unmap each memory area */
8312    ...
8313    return TRUE or FALSE;
8314}
8315	</programlisting>
8316      </sect3>
8317
8318      <sect3>
8319	<title>Save, Restore</title>
8320
8321	<para>
8322    Define functions to save and restore the original video state.  These
8323    functions are not mandatory, but are often useful.
8324	</para>
8325
8326	<programlisting>
8327static void
8328ZZZSave(ScrnInfoPtr pScrn)
8329{
8330    /*
8331     * Save state into per-screen data structures.
8332     * If using the vgahw module, vgaHWSave will typically be
8333     * called here.
8334     */
8335    ...
8336}
8337
8338static void
8339ZZZRestore(ScrnInfoPtr pScrn)
8340{
8341    /*
8342     * Restore state from per-screen data structures.
8343     * If using the vgahw module, vgaHWRestore will typically be
8344     * called here.
8345     */
8346    ...
8347}
8348	</programlisting>
8349      </sect3>
8350
8351      <sect3>
8352	<title>ModeInit</title>
8353
8354	<para>
8355    Define a function to initialise a new video mode.  This function isn't
8356    mandatory, but is often useful.
8357	</para>
8358
8359	<programlisting>
8360static Bool
8361ZZZModeInit(ScrnInfoPtr pScrn, DisplayModePtr mode)
8362{
8363    /*
8364     * Program a video mode.  If using the vgahw module,
8365     * vgaHWInit and vgaRestore will typically be called here.
8366     * Once up to the point where there can't be a failure
8367     * set pScrn-&gt;vtSema to TRUE.
8368     */
8369    ...
8370}
8371	</programlisting>
8372      </sect3>
8373
8374      <sect3>
8375	<title>ScreenInit</title>
8376
8377	<para>
8378    Define the <function>ScreenInit()</function> function.  This is called
8379    at the start of each server generation, and should fill in as much
8380    of the <structname>ScreenRec</structname> as possible as well as any other
8381    data that is initialised once per generation.  It should initialise
8382    the framebuffer layers it is using, and initialise the initial video
8383    mode.
8384	</para>
8385
8386	<para>
8387    This function is mandatory.
8388	</para>
8389
8390	<para>
8391    NOTE: The <structname>ScreenRec</structname> (<parameter>pScreen</parameter>) is
8392	  passed to this driver, but it and the
8393	  <varname>ScrnInfoRecs</varname> are not yet hooked into each
8394	  other.  This means that in this function, and functions it
8395	  calls, one cannot be found from the other.
8396	</para>
8397
8398	<programlisting>
8399static Bool
8400ZZZScreenInit(ScreenPtr pScreen, int argc, char **argv)
8401{
8402    /* Get the ScrnInfoRec */
8403    pScrn = xf86ScreenToScrn(pScreen);
8404
8405    /*
8406     * If using the vgahw module, its data structures and related
8407     * things are typically initialised/mapped here.
8408     */
8409
8410    /* Save the current video state */
8411    ZZZSave(pScrn);
8412
8413    /* Initialise the first mode */
8414    ZZZModeInit(pScrn, pScrn-&gt;currentMode);
8415
8416    /* Set the viewport if supported */
8417
8418    ZZZAdjustFrame(pScrn, pScrn-&gt;frameX0, pScrn-&gt;frameY0);
8419
8420    /*
8421     * Setup the screen's visuals, and initialise the framebuffer
8422     * code.
8423     */
8424
8425    /* Reset the visual list */
8426    miClearVisualTypes();
8427
8428    /*
8429     * Setup the visuals supported.  This driver only supports
8430     * TrueColor for bpp &gt; 8, so the default set of visuals isn't
8431     * acceptable.  To deal with this, call miSetVisualTypes with
8432     * the appropriate visual mask.
8433     */
8434
8435    if (pScrn-&gt;bitsPerPixel &gt; 8) {
8436        if (!miSetVisualTypes(pScrn-&gt;depth, TrueColorMask,
8437                              pScrn-&gt;rgbBits, pScrn-&gt;defaultVisual))
8438            return FALSE;
8439    } else {
8440        if (!miSetVisualTypes(pScrn-&gt;depth,
8441                              miGetDefaultVisualMask(pScrn-&gt;depth),
8442                              pScrn-&gt;rgbBits, pScrn-&gt;defaultVisual))
8443            return FALSE;
8444    }
8445
8446    /*
8447     * Initialise the framebuffer.
8448     */
8449
8450    switch (pScrn-&gt;bitsPerPixel) {
8451    case 1:
8452        ret = xf1bppScreenInit(pScreen, FbBase,
8453                               pScrn-&gt;virtualX, pScrn-&gt;virtualY,
8454                               pScrn-&gt;xDpi, pScrn-&gt;yDpi,
8455                               pScrn-&gt;displayWidth);
8456        break;
8457    case 4:
8458        ret = xf4bppScreenInit(pScreen, FbBase,
8459                               pScrn-&gt;virtualX, pScrn-&gt;virtualY,
8460                               pScrn-&gt;xDpi, pScrn-&gt;yDpi,
8461                               pScrn-&gt;displayWidth);
8462        break;
8463    case 8:
8464        ret = cfbScreenInit(pScreen, FbBase,
8465                            pScrn-&gt;virtualX, pScrn-&gt;virtualY,
8466                            pScrn-&gt;xDpi, pScrn-&gt;yDpi,
8467                            pScrn-&gt;displayWidth);
8468        break;
8469    case 16:
8470        ret = cfb16ScreenInit(pScreen, FbBase,
8471                              pScrn-&gt;virtualX, pScrn-&gt;virtualY,
8472                              pScrn-&gt;xDpi, pScrn-&gt;yDpi,
8473                              pScrn-&gt;displayWidth);
8474        break;
8475    case 24:
8476        ret = cfb24ScreenInit(pScreen, FbBase,
8477                              pScrn-&gt;virtualX, pScrn-&gt;virtualY,
8478                              pScrn-&gt;xDpi, pScrn-&gt;yDpi,
8479                              pScrn-&gt;displayWidth);
8480        break;
8481    case 32:
8482        ret = cfb32ScreenInit(pScreen, FbBase,
8483                              pScrn-&gt;virtualX, pScrn-&gt;virtualY,
8484                              pScrn-&gt;xDpi, pScrn-&gt;yDpi,
8485                              pScrn-&gt;displayWidth);
8486        break;
8487    default:
8488        print a message about an internal error;
8489        ret = FALSE;
8490        break;
8491    }
8492
8493    if (!ret)
8494        return FALSE;
8495
8496    /* Override the default mask/offset settings */
8497    if (pScrn-&gt;bitsPerPixel &gt; 8) {
8498        for (i = 0, visual = pScreen-&gt;visuals;
8499             i &lt; pScreen-&gt;numVisuals; i++, visual++) {
8500            if ((visual-&gt;class | DynamicClass) == DirectColor) {
8501                visual-&gt;offsetRed = pScrn-&gt;offset.red;
8502                visual-&gt;offsetGreen = pScrn-&gt;offset.green;
8503                visual-&gt;offsetBlue = pScrn-&gt;offset.blue;
8504                visual-&gt;redMask = pScrn-&gt;mask.red;
8505                visual-&gt;greenMask = pScrn-&gt;mask.green;
8506                visual-&gt;blueMask = pScrn-&gt;mask.blue;
8507            }
8508        }
8509    }
8510
8511    /*
8512     * If banking is needed, initialise an miBankInfoRec (defined in
8513     * "mibank.h"), and call miInitializeBanking().
8514     */
8515    if (!miInitializeBanking(pScreen, pScrn-&gt;virtualX, pScrn-&gt;virtualY,
8516                                     pScrn-&gt;displayWidth, pBankInfo))
8517        return FALSE;
8518
8519    /*
8520     * Set initial black &amp; white colourmap indices.
8521     */
8522    xf86SetBlackWhitePixels(pScreen);
8523
8524    /*
8525     * Install colourmap functions.
8526     */
8527
8528    ...
8529
8530    /*
8531     * Initialise cursor functions.  This example is for the mi
8532     * software cursor.
8533     */
8534    miDCInitialize(pScreen, xf86GetPointerScreenFuncs());
8535
8536    /* Initialise the default colourmap */
8537    switch (pScrn-&gt;depth) {
8538    case 1:
8539        if (!xf1bppCreateDefColormap(pScreen))
8540            return FALSE;
8541        break;
8542    case 4:
8543        if (!xf4bppCreateDefColormap(pScreen))
8544            return FALSE;
8545        break;
8546    default:
8547        if (!cfbCreateDefColormap(pScreen))
8548            return FALSE;
8549        break;
8550    }
8551
8552    /*
8553     * Wrap the CloseScreen vector and set SaveScreen.
8554     */
8555    ZZZPTR(pScrn)-&gt;CloseScreen = pScreen-&gt;CloseScreen;
8556    pScreen-&gt;CloseScreen = ZZZCloseScreen;
8557    pScreen-&gt;SaveScreen = ZZZSaveScreen;
8558
8559    /* Report any unused options (only for the first generation) */
8560    if (serverGeneration == 1) {
8561        xf86ShowUnusedOptions(pScrn-&gt;scrnIndex, pScrn-&gt;options);
8562    }
8563
8564    /* Done */
8565    return TRUE;
8566}
8567	</programlisting>
8568      </sect3>
8569
8570      <sect3>
8571	<title>SwitchMode</title>
8572
8573	<para>
8574    Define the <function>SwitchMode()</function> function if mode switching
8575    is supported by the driver.
8576	</para>
8577
8578	<programlisting>
8579static Bool
8580ZZZSwitchMode(ScrnInfoPtr pScrn, DisplayModePtr mode)
8581{
8582    return ZZZModeInit(pScrn, mode);
8583}
8584	</programlisting>
8585      </sect3>
8586
8587      <sect3>
8588	<title>AdjustFrame</title>
8589
8590	<para>
8591    Define the <function>AdjustFrame()</function> function if the driver
8592    supports this.
8593	</para>
8594
8595	<programlisting>
8596static void
8597ZZZAdjustFrame(ScrnInfoPtr pScrn, int x, int y)
8598{
8599    /* Adjust the viewport */
8600}
8601	</programlisting>
8602      </sect3>
8603
8604      <sect3>
8605	<title>EnterVT, LeaveVT</title>
8606
8607	<para>
8608    Define the <function>EnterVT()</function> and <function>LeaveVT()</function>
8609    functions.
8610	</para>
8611
8612	<para>
8613    These functions are mandatory.
8614	</para>
8615
8616	<programlisting>
8617static Bool
8618ZZZEnterVT(ScrnInfoPtr pScrn)
8619{
8620    return ZZZModeInit(pScrn, pScrn-&gt;currentMode);
8621}
8622
8623static void
8624ZZZLeaveVT(ScrnInfoPtr pScrn)
8625{
8626    ZZZRestore(pScrn);
8627}
8628	</programlisting>
8629      </sect3>
8630
8631      <sect3>
8632	<title>CloseScreen</title>
8633
8634	<para>
8635    Define the <function>CloseScreen()</function> function:
8636	</para>
8637
8638	<para>
8639    This function is mandatory.  Note that it unwraps the previously
8640    wrapped <structfield>pScreen-&gt;CloseScreen</structfield>, and finishes by
8641    calling it.
8642	</para>
8643
8644	<programlisting>
8645static Bool
8646ZZZCloseScreen(ScreenPtr pScreen)
8647{
8648    ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
8649    if (pScrn-&gt;vtSema) {
8650        ZZZRestore(pScrn);
8651        ZZZUnmapMem(pScrn);
8652    }
8653    pScrn-&gt;vtSema = FALSE;
8654    pScreen-&gt;CloseScreen = ZZZPTR(pScrn)-&gt;CloseScreen;
8655    return (*pScreen-&gt;CloseScreen)(pScreen);
8656}
8657	</programlisting>
8658      </sect3>
8659
8660      <sect3>
8661	<title>SaveScreen</title>
8662
8663	<para>
8664    Define the <function>SaveScreen()</function> function (the screen
8665    blanking function).  When using the vgahw module, this will typically
8666    be:
8667
8668	  <programlisting>
8669static Bool
8670ZZZSaveScreen(ScreenPtr pScreen, int mode)
8671{
8672    return vgaHWSaveScreen(pScreen, mode);
8673}
8674	  </programlisting>
8675	</para>
8676
8677	<para>
8678    This function is mandatory.  Before modifying any hardware register
8679    directly this function needs to make sure that the Xserver is active
8680    by checking if <parameter>pScrn</parameter> is non-NULL and for
8681    <literal remap="tt">pScrn-&gt;vtSema == TRUE</literal>.
8682	</para>
8683      </sect3>
8684
8685      <sect3>
8686	<title>FreeScreen</title>
8687
8688	<para>
8689    Define the <function>FreeScreen()</function> function.  This function
8690    is optional.  It should be defined if the <structname>ScrnInfoRec</structname>
8691    <structfield>driverPrivate</structfield> field is used so that it can be freed
8692    when a screen is deleted by the common layer for reasons possibly
8693    beyond the driver's control.  This function is not used in during
8694    normal (error free) operation.  The per-generation data is freed by
8695    the <function>CloseScreen()</function> function.
8696	</para>
8697
8698	<programlisting>
8699static void
8700ZZZFreeScreen(ScrnInfoPtr pScrn)
8701{
8702    /*
8703     * If the vgahw module is used vgaHWFreeHWRec() would be called
8704     * here.
8705     */
8706    ZZZFreeRec(pScrn);
8707}
8708
8709	</programlisting>
8710
8711      </sect3>
8712
8713    </sect2>
8714
8715  </sect1>
8716
8717</article>
8718