#!/bin/sh
UNSUPPORTED=1
#CARDS=$(lspci -n|gawk '{sub("Class","")} $2 == "0300:" {print$3}')
CARDS=$(lspci -n|perl -e 'while(<>){(push(@cards,$_)) if (s/^.+?\s*0300\:\s+(.+?)\s+.*$/$1/)} print join(" ",@cards);')
for card in $CARDS; do
 VENDOR=${card%:*}
 DEVICE=${card#*:}
 case $VENDOR in
  1002)
   case $DEVICE in
    3150|3152|3154|3e50|3e54|4144|4146|4147|4148|4149|4150|4151|4152|4153|4154|4155|4242|4966|4967|4a48|4a49|4a4a|4a4b|4a4c|4a4d|4a4e|4a4f|4a50|4a54|4b48|4b49|4b4a|4b4b|4b4c|4c64|4c66|4c67|4e44|4e45|4e46|4e47|4e48|4e49|4e4a|4e4b|4e50|4e51|4e52|4e54|4e56|5148|514c|514d|5460|5461|5462|5464|5548|5549|554a|554b|554d|554e|554f|5550|5551|564a|564b|5652|5653|5834|5835|5954|5955|5960|5961|5962|5964|5974|5975|5a41|5a42|5a61|5a62|5b60|5b62|5b63|5b64|5c61|5c63|5d48|5d49|5d4a|5d4d|5d4f|5d52|5d57|5e48|5e49|5e4a|5e4b|5e4c|5e4d|5e4f|7834) UNSUPPORTED=0 ;;
    *) UNSUPPORTED=1 ;;
   esac
   ;;
 esac
done
if [ "$UNSUPPORTED" == "1" ]; then
 echo Error: No ATI RADEON/FIREGL adapter found.
 exit 3
fi
if test -e /etc/X11/XF86Config-4 -a -z "`grep -s -e radeon -e fglrx -e ati -e vesa -e fbdev /etc/X11/XF86Config-4|grep Driver`"; then
 echo Error: Bad /etc/X11/XF86Config-4 file.
 exit 4
fi
if test -e /etc/X11/xorg.conf -a -z "`grep -s -e radeon -e fglrx -e ati -e vesa -e fbdev /etc/X11/xorg.conf|grep Driver`"; then
 echo Error: Bad /etc/X11/xorg.conf file.
 exit 5
fi
if test $(id -u) != 0; then
 echo Error: You must be root to run this script!
 exit 6
fi
LAPTOP=""
[ "$(find /proc/acpi/battery/ -mindepth 1 -type d 2>/dev/null)" ] && LAPTOP="yes"
INSTALLED=""
[ -d /KNOPPIX ] || INSTALLED="yes"
if [ -n "$INSTALLED" ]; then
 if test -n "$DISPLAY"; then
  echo Error: Can\`t be run within X! Use a text console \(ALT-CTRL-F1\)
  exit 7
 fi
else
 if grep -q unionfs /proc/mounts; then
  echo Unionfs found.
 else
  echo Activating Unionfs now.
  mkdir -p /tmp/union
  mount -t unionfs -o dirs=/tmp/union/:/KNOPPIX /none /KNOPPIX/ 2>&1 >/dev/null
 fi
fi
if X -version 2>&1|grep -q -e "X Window System Version 6.9" -e "X Window System Version 7.0"; then
 echo Error: X Window System Version 6.9 or 7.0 not yet supported by this script.
 echo Hint: Use install-fglrx-debian.sh instead.
 rm -f /usr/local/bin/install-r300-cvs-debian.sh
 if ! [ -x /usr/local/bin/install-fglrx-debian.sh ]; then
  mkdir -p /usr/local/bin/
  cd /usr/local/bin/
  rm -f install-fglrx-debian.sh
  wget -N http://kanotix.com/files/install-fglrx-debian.sh
  cd -
  chmod +x /usr/local/bin/install-fglrx-debian.sh
 fi
 if [ -x /usr/local/bin/install-fglrx-debian.sh ]; then
  exec /usr/local/bin/install-fglrx-debian.sh "$@"
 else

if false; then
 echo Error: X Window System Version 6.9 or 7.0 not yet supported by ATI binary driver.
 echo Hint: Use install-r300-cvs-debian.sh instead.
 if ! [ -x /usr/local/bin/install-r300-cvs-debian.sh ]; then
  mkdir -p /usr/local/bin/
  cd /usr/local/bin/
  rm -f install-r300-cvs-debian.sh
  wget -N http://kanotix.com/files/install-r300-cvs-debian.sh
  cd -
  chmod +x /usr/local/bin/install-r300-cvs-debian.sh
 fi
 if [ -x /usr/local/bin/install-r300-cvs-debian.sh ]; then
  echo Do you want to run r300 cvs install now?
  echo It will use about 600 MB hd space and work only for 95xx to 98xx cards.
  echo Sources will be fetched and compiled, this will take some time.
  echo Not all apps/games will run with that driver!
  echo If you want to try it, please confirm with: Yes
  read x
  if [ "$x" == "Yes" ]; then
   exec /usr/local/bin/install-r300-cvs-debian.sh
  fi
 fi
fi

 X_CONF=XF86Config-4
 if which Xorg >/dev/null; then
  [ -e /etc/X11/xorg.conf ] && X_CONF=xorg.conf
 fi	 
 if grep -q fglrx /etc/X11/$X_CONF; then
  [ "$(uname -m)" == "x86_64" ] && X86=64
  dpkg-divert --package fglrx${X86}-6-8-0 --remove /usr/X11R6/lib/libGL.so.1.2 &>/dev/null
  dpkg --purge fglrx${X86}-6-8-0 &>/dev/null
  echo Error: No working driver installed for your card.
  echo Trying to uninstall fgrlx driver.
  echo "Press Enter to continue (Ctrl-C to stop)."
  read
  rm -rf /lib/modules/fglrx
  apt-get update
  apt-get install --reinstall --force-yes -y xlibmesa-gl
  if [ -n "$INSTALLED" ]; then
  DM=`cat /etc/X11/default-display-manager 2>/dev/null`
  DM=`basename $DM 2>/dev/null`
  if test -z "$DM"; then
   test -e /etc/init.d/kdm && /etc/init.d/kdm stop
   else
   test -e /etc/init.d/$DM && /etc/init.d/$DM stop
  fi
  fi
  rm -f /lib/modules/`uname -r`/kernel/drivers/char/drm/fglrx.*o
  rmmod radeon drm &>/dev/null
  rmmod fglrx &>/dev/null
  if test -e /etc/X11/$X_CONF.1st; then
   rm /etc/X11/$X_CONF
   cp /etc/X11/$X_CONF.1st /etc/X11/$X_CONF
  fi
  perl -pi -e 's/^[\s]*Load\s*"GLcore"/\tLoad  "GLcore"/;
            s/^[\s]*#*[\s]*Load\s*"dri"/\tLoad  "dri"/;
            s/^[\s]*#*[\s]*Load\s*"glx"/\tLoad  "glx"/;
            s/^[\s]*#*[\s]*Option\s*"Composite".*/#\tOption\t"Composite"\t"1"/;
            s/^[\s]*Driver\s*"fbdev"/\tDriver      "ati"/g;
            s/^[\s]*Driver\s*"fglrx"/\tDriver      "ati"/g;
            s/^[\s]*Driver\s*"vesa"/\tDriver      "ati"/g;
            s/^[\s]*Driver\s*"radeon"/\tDriver      "ati"/g;
            s/DefaultColorDepth.*/DefaultColorDepth 16/;
            s/^[\s]*Option\s*"sw_cursor"/\t#Option     "sw_cursor"/;
            s/^.*UseInternalAGPGART.*\n?//g;
            s/^.*VideoOverlay.*\n?//g;
            s/^.*OpenGLOverlay.*\n?//g;
            s/^.*MonitorLayout.*\n?//g;
            s/^.*drm.*\n?//g;
            s/^.*XaaNoScanlineImageWriteRect.*\n?//g;
            s/^.*XaaNoScanlineCPUToScreenColorExpandFill.*\n?//g' \
            /etc/X11/$X_CONF
  if [ -n "$INSTALLED" ]; then
   if test -z "$DM"; then
    test -e /etc/init.d/kdm && /etc/init.d/kdm start
   else
    test -e /etc/init.d/$DM && /etc/init.d/$DM start
   fi
  else
   cp /etc/X11/$X_CONF /etc/X11/$X_CONF.ati
   nohup sh -c "/etc/init.d/xsession stop; sleep 5; rmmod radeon drm; modprobe fglrx; cp /etc/X11/$X_CONF.ati /etc/X11/$X_CONF; /etc/init.d/xsession start"
  fi
  exit 8
 fi
 exit 9
fi
fi
cd /usr/src
KVER=$(uname -r)
KV=${KVER:0:3}
if [ "$KV" == "2.4" ]; then
if ! test -e linux-drm-4.3.0-kernelsource.tar.gz; then
 wget -Nc http://www.xfree86.org/~alanh/linux-drm-4.3.0-kernelsource.tar.gz
fi
rm -rf drm/
tar zxf linux-drm-4.3.0-kernelsource.tar.gz &>/dev/null || (
 rm -f linux-drm-4.3.0-kernelsource.tar.gz
 echo linux-drm-4.3.0-kernelsource.tar.gz could not be extracted
 echo and has been deleted. Please restart this script.
 exit 2
) || exit $?
fi
VER=8.23.7-1
#VER=8.22.5-1
#VER=8.21.7-1
#VER=8.20.8-1
#VER=8.19.10-1
#VER=8.18.8-1
#VER=8.18.6-1
#VER=8.16.20-1
#VER=8.14.13-1
#8.13.3-1 only for Radeon Xpress 200 Series
#VER=8.13.3-1
#VER=8.12.10-1
#VER=8.10.19-1
#VER=8.8.25-1
#VER=3.14.6
#VER=3.14.1
#VER=3.12.0
#VER=3.11.1
#VER=3.9.0
#VER=3.7.6
#VER=3.7.0
#VER=3.2.8
#lspci|grep VGA|grep -q 9800 && VER=3.9.0
#[ "$KV" == "2.6" ] && VER=3.9.0
DVER=4.7
[ "$VER" == "8.23.7-1" ] && DVER=8.23.7-2
[ "$VER" == "8.22.5-1" ] && DVER=8.22.5-2
[ "$VER" == "8.21.7-1" ] && DVER=8.21.7-2
[ "$VER" == "8.20.8-1" ] && DVER=8.20.8-2
[ "$VER" == "8.19.10-1" ] && DVER=8.19.10-2
[ "$VER" == "8.18.8-1" ] && DVER=8.18.8-2
[ "$VER" == "8.18.6-1" ] && DVER=8.18.6-2
[ "$VER" == "8.16.20-1" ] && DVER=8.16.20-2
[ "$VER" == "8.14.13-1" ] && DVER=8.14.13-2
[ "$VER" == "8.13.3-1" ] && DVER=8.13.3-2
[ "$VER" == "8.12.10-1" ] && DVER=8.12.10-2
[ "$VER" == "8.10.19-1" ] && DVER=8.10.19-2
[ "$VER" == "8.8.25-1" ] && DVER=8.8.25-2
[ "$VER" == "3.14.1" -o "$VER" == "3.14.6" ] && DVER=4.14
[ "$VER" == "3.12.0" ] && DVER=4.12
[ "$VER" == "3.11.1" ] && DVER=4.11
[ "$VER" == "3.9.0" ] && DVER=4.9
[ "$VER" == "3.2.8" ] && DVER=4.2
CPU=$(uname -m)
if [ "$CPU" == "x86_64" ]; then
 ARCH="amd64"
else
 CPU="i386"
 ARCH="i386"
fi
[ "$VER" == "3.2.8" -a "$ARCH" == "i386" ] && ARCH=i586
GLC=-glc22
[ "$VER" == "3.7.6" -o "$VER" == "3.9.0" -o "$VER" == "3.11.1" -o "$VER" == "3.12.0" -o "$VER" == "3.14.1" -o "$VER" == "3.14.6" -o "$VER" == "8.8.25-1" -o "$VER" == "8.10.19-1" -o "$VER" == "8.12.10-1" -o "$VER" == "8.13.3-1" -o "$VER" == "8.14.13-1" -o "$VER" == "8.16.20-1" -o "$VER" == "8.18.6-1" -o "$VER" == "8.18.8-1" -o "$VER" == "8.19.10-1" -o "$VER" == "8.20.8-1" -o "$VER" == "8.21.7-1" -o "$VER" == "8.22.5-1" -o "$VER" == "8.23.7-1" ] && unset GLC
X=-4.3.0-
[ "$VER" == "8.8.25-1" -o "$VER" == "8.10.19-1" -o "$VER" == "8.12.10-1" -o "$VER" == "8.13.3-1" -o "$VER" == "8.14.13-1" -o "$VER" == "8.16.20-1" -o "$VER" == "8.18.6-1" -o "$VER" == "8.18.8-1" -o "$VER" == "8.19.10-1" -o "$VER" == "8.20.8-1" -o "$VER" == "8.21.7-1" -o "$VER" == "8.22.5-1" -o "$VER" == "8.23.7-1" ] && X=_4_3_0-
X_CONF=XF86Config-4
if which Xorg >/dev/null; then
 if [ "$VER" == "8.8.25-1" -o "$VER" == "8.10.19-1" -o "$VER" == "8.12.10-1" -o "$VER" == "8.13.3-1" -o "$VER" == "8.14.13-1" -o "$VER" == "8.16.20-1" -o "$VER" == "8.18.6-1" -o "$VER" == "8.18.8-1" -o "$VER" == "8.19.10-1" -o "$VER" == "8.20.8-1" -o "$VER" == "8.21.7-1" -o "$VER" == "8.22.5-1" -o "$VER" == "8.23.7-1" ]; then
  X=_6_8_0-
  [ -e /etc/X11/xorg.conf ] && X_CONF=xorg.conf
 fi
fi
X_DEB=_4.3.0-
[ "$VER" == "8.8.25-1" -a "$X" == "_4_3_0-" ] && X_DEB=-4-3-0_
[ "$VER" == "8.8.25-1" -a "$X" == "_6_8_0-" ] && X_DEB=-6-8-0_
[ "$VER" == "8.10.19-1" -a "$X" == "_4_3_0-" ] && X_DEB=-4-3-0_
[ "$VER" == "8.10.19-1" -a "$X" == "_6_8_0-" ] && X_DEB=-6-8-0_
[ "$VER" == "8.12.10-1" -a "$X" == "_4_3_0-" ] && X_DEB=-4-3-0_
[ "$VER" == "8.12.10-1" -a "$X" == "_6_8_0-" ] && X_DEB=-6-8-0_
[ "$VER" == "8.13.3-1" -a "$X" == "_4_3_0-" ] && X_DEB=-4-3-0_
[ "$VER" == "8.13.3-1" -a "$X" == "_6_8_0-" ] && X_DEB=-6-8-0_
[ "$VER" == "8.14.13-1" -a "$X" == "_4_3_0-" ] && X_DEB=-4-3-0_
[ "$VER" == "8.14.13-1" -a "$X" == "_6_8_0-" ] && X_DEB=-6-8-0_
[ "$VER" == "8.16.20-1" -a "$X" == "_4_3_0-" ] && X_DEB=-4-3-0_
[ "$VER" == "8.16.20-1" -a "$X" == "_6_8_0-" ] && X_DEB=-6-8-0_
[ "$VER" == "8.18.6-1" -a "$X" == "_4_3_0-" ] && X_DEB=-4-3-0_
[ "$VER" == "8.18.6-1" -a "$X" == "_6_8_0-" ] && X_DEB=-6-8-0_
[ "$VER" == "8.18.8-1" -a "$X" == "_4_3_0-" ] && X_DEB=-4-3-0_
[ "$VER" == "8.18.8-1" -a "$X" == "_6_8_0-" ] && X_DEB=-6-8-0_
[ "$VER" == "8.19.10-1" -a "$X" == "_4_3_0-" ] && X_DEB=-4-3-0_
[ "$VER" == "8.19.10-1" -a "$X" == "_6_8_0-" ] && X_DEB=-6-8-0_
[ "$VER" == "8.20.8-1" -a "$X" == "_4_3_0-" ] && X_DEB=-4-3-0_
[ "$VER" == "8.20.8-1" -a "$X" == "_6_8_0-" ] && X_DEB=-6-8-0_
[ "$VER" == "8.21.7-1" -a "$X" == "_4_3_0-" ] && X_DEB=-4-3-0_
[ "$VER" == "8.21.7-1" -a "$X" == "_6_8_0-" ] && X_DEB=-6-8-0_
[ "$VER" == "8.22.5-1" -a "$X" == "_4_3_0-" ] && X_DEB=-4-3-0_
[ "$VER" == "8.22.5-1" -a "$X" == "_6_8_0-" ] && X_DEB=-6-8-0_
[ "$VER" == "8.23.7-1" -a "$X" == "_4_3_0-" ] && X_DEB=-4-3-0_
[ "$VER" == "8.23.7-1" -a "$X" == "_6_8_0-" ] && X_DEB=-6-8-0_

unset X86
[ "$CPU" == "x86_64" ] && X86=64
unset BIT
[ "$CPU" == "x86_64" -a "$VER" == "8.14.13-1" ] && BIT="64bit/"
[ "$CPU" == "x86_64" -a "$VER" == "8.16.20-1" ] && BIT="64bit/"
[ "$CPU" == "x86_64" -a "$VER" == "8.18.6-1" ] && BIT="64bit/"
[ "$CPU" == "x86_64" -a "$VER" == "8.18.8-1" ] && BIT="64bit/"
[ "$CPU" == "x86_64" -a "$VER" == "8.19.10-1" ] && BIT="64bit/"
[ "$CPU" == "x86_64" -a "$VER" == "8.20.8-1" ] && BIT="64bit/"
[ "$CPU" == "x86_64" -a "$VER" == "8.21.7-1" ] && BIT="64bit/"
[ "$CPU" == "x86_64" -a "$VER" == "8.22.5-1" ] && BIT="64bit/"
[ "$CPU" == "x86_64" -a "$VER" == "8.23.7-1" ] && BIT="64bit/"
DRIVER=fglrx${X86}${GLC}${X}${VER}.${CPU}.rpm
DRIVER_DEB=fglrx${X86}${GLC}${X_DEB}${DVER}_${ARCH}.deb
if ! test -e $DRIVER; then
 wget --referer=ati.com -Nc http://www2.ati.com/drivers/linux/${BIT}${DRIVER}
fi
alien $DRIVER &>/dev/null || (
 rm -f $DRIVER
 echo $DRIVER could not be converted
 echo and has been deleted. Please restart this script.
 exit 1
) || exit $?
GL=/usr/X11R6/lib/libGL.so.1.2
if [ "$CPU" == "x86_64" -o true ]; then
 rm -f $GL.1st
else
 if ! test -e $GL.1st; then
  test -e $GL && cp $GL $GL.1st
 fi
fi
dpkg --purge fglrx${X86}-4-3-0 &>/dev/null
dpkg --purge fglrx${X86}-6-8-0 &>/dev/null
dpkg --purge fglrx-glc22 &>/dev/null
dpkg --purge fglrx &>/dev/null
for package in $(dpkg-divert --list|grep "diversion of $GL"|awk '{print $7}'|grep -v fglrx-driver); do 
 dpkg-divert --package $package --remove $GL
done
rm -f $GL $GL.distrib
OLD_FGLRX=$(COLUMNS=200 dpkg -l|awk '/fglrx/ {print $2}')
[ -n "$OLD_FGLRX" ] && dpkg --purge $OLD_FGLRX
if [ -d /usr/lib/opengl ]; then
 rm -rf /usr/lib/opengl
 ldconfig
fi
rm -rf /lib/modules/fglrx
[ "$CPU" == "x86_64" ] && rm -rf /usr/X11R6/lib64
dpkg -i --force-overwrite $DRIVER_DEB
#dpkg-divert --package xlibmesa-gl $GL
dpkg-divert --package fglrx${X86}${GLC}${X_DEB/_} $GL
if [ "$CPU" == "x86_64" ]; then
 cp -a /usr/X11R6/lib64/* /usr/X11R6/lib/
 rm -rf /usr/X11R6/lib64
 ln -s lib /usr/X11R6/lib64
else
 mv $GL.1st $GL 2>/dev/null
fi
chmod 755 /usr/X11R6/lib/modules /usr/X11R6/lib/modules/dri
rm -f $DRIVER_DEB
ldconfig
if test -f /etc/modutils/1alsa; then
 mv /lib/modules/extra/alsa /lib/modules/`uname -r`/
 rm -f /etc/modutils/1alsa
 update-modules
fi
if [ -n "$INSTALLED" ]; then
DM=`cat /etc/X11/default-display-manager 2>/dev/null`
DM=`basename $DM 2>/dev/null`
if test -z "$DM"; then
 test -e /etc/init.d/kdm && /etc/init.d/kdm stop
else
 test -e /etc/init.d/$DM && /etc/init.d/$DM stop
fi
fi
rmmod radeon drm &>/dev/null
rmmod fglrx &>/dev/null
rm -f /lib/modules/`uname -r`/kernel/drivers/char/drm/fglrx.*o
#rmmod agpgart &>/dev/null
perl -pi -e 's/^[\s]*radeon/#radeon/' /etc/modules
perl -pi -e 's/^[\s]*fglrx/#fglrx/' /etc/modules
#perl -pi -e 's/^[\s]*agpgart/#agpgart/' /etc/modules
#perl -pi -e 's/.*agpgart.*\n*//' /etc/modules
#perl -pi -e 's/\n?$/\n/' /etc/modules
#modprobe agpgart && echo agpgart >> /etc/modules
cd /lib/modules/fglrx/build_mod
if test -n "$(lspci|grep KT400)" -a "$VER" != "3.9.0"; then
patch <<EOF
--- agpgart_be.c.old	2003-09-22 04:43:30.000000000 +0200
+++ agpgart_be.c	2003-10-13 18:07:10.000000000 +0200
@@ -6070,7 +6070,7 @@
         VIA_APOLLO_KT400,
         "Via",
         "Apollo KT400",
-        via_generic_setup }, // AGP v2
+        via_kt400_setup }, // AGP v3
     { PCI_DEVICE_ID_VIA_8754, // == PCI_DEVICE_ID_VIA_P4X333,
         PCI_VENDOR_ID_VIA,
         VIA_APOLLO_P4X400,

EOF
fi
[ "$VER" == "3.2.8" ] && if test "$KV" == "2.6"; then
patch <<EOF
--- firegl_public.c.old	2003-12-03 15:28:44.000000000 +0100
+++ firegl_public.c	2003-12-03 15:29:36.000000000 +0100
@@ -22,6 +22,10 @@
  *                                                                          *
  ****************************************************************************/

+#ifndef VMALLOC_VMADDR
+#define VMALLOC_VMADDR(x) ((unsigned long)(x))
+#endif
+
 #ifdef __KERNEL__

 #ifndef MODULE

EOF
fi
[ "$KV" == "2.4" ] && perl -pi -e 's/^drmincludes.*/drmincludes=\/usr\/src\/drm/' make.sh
[ "$KV" == "2.6" ] && [ "$VER" == "3.7.6" -o "$VER" == "3.9.0" ] && if [ "$(uname -r)" == "2.6.6-up" -o "$(uname -r|grep -E '2.6.7|2.6.8')" != "" ]; then
cd /usr/src
wget -Nc http://www.kaseorg.com/fglrx-3.7.6-regparm.patch
cd /lib/modules/fglrx/build_mod
patch < /usr/src/fglrx-3.7.6-regparm.patch
# http://www.rage3d.com/board/showthread.php?s=&threadid=33737573
# Patch to build 3.7.6 with 2.6.6-mm2 and recent
patch <<EOF
diff -uprN build_mod-mreg_new/agpgart_be.c build_mod-mreg_new-mm/agpgart_be.c
--- build_mod-mreg_new/agpgart_be.c     2004-05-15 03:45:48.000000000 +0300
+++ build_mod-mreg_new-mm/agpgart_be.c  2004-05-15 03:18:30.000000000 +0300
@@ -1402,7 +1402,8 @@ unsigned long agp_generic_alloc_page(voi
     }
 #endif

-    atomic_inc(&page->count);
+    //atomic_inc(&page->count);
+    get_page(page);
     set_bit(PG_locked, &page->flags);
     atomic_inc(&agp_bridge.current_memory_agp);

@@ -4413,7 +4414,8 @@ static unsigned long ali_alloc_page(void
     if (page == NULL)
         return 0;

-    atomic_inc(&page->count);
+    //atomic_inc(&page->count);
+    get_page(page);
     set_bit(PG_locked, &page->flags);
     atomic_inc(&agp_bridge.current_memory_agp);

diff -uprN build_mod-mreg_new/firegl_public.c build_mod-mreg_new-mm/firegl_public.c
--- build_mod-mreg_new/firegl_public.c  2004-05-15 03:45:06.000000000 +0300
+++ build_mod-mreg_new-mm/firegl_public.c       2004-05-15 03:25:13.000000000 +0300
@@ -2066,7 +2066,8 @@ static vm_nopage_ret_t vm_shm_nopage(str
     pMmPage = virt_to_page(kaddr);
 #endif /* LINUX_VERSION_CODE < 0x020400 */

-    atomic_inc(&(pMmPage->count));  /* inc usage count of page */
+    //atomic_inc(&(pMmPage->count));  /* inc usage count of page */
+    get_page(pMmPage);

 #if LINUX_VERSION_CODE >= 0x020400
   //  __KE_DEBUG3("vm-address 0x%08lx => kernel-page-address 0x%p\n",
   
EOF
fi
[ "$KV" == "2.6" ] && [ "$VER" == "3.11.1" -o "$VER" == "3.12.0" -o "$VER" == "3.14.1" -o "$VER" == "3.14.6" -o "$VER" == "8.8.25-1" ] && if [ "$(uname -r|grep -E '2.6.10|2.6.11')" != "" ]; then
#cd /lib/modules/fglrx
# http://www.rage3d.com/board/showthread.php?t=33798874
patch -p0 <<EOF
--- agpgart_be.c 2005-01-06 9:05:02.000000000 +0100
+++ agpgart_be.c 2005-01-06 9:19:16.000000000 +0100

@@ -255,6 +255,12 @@
 }
 #endif
 
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,10) 
+#define firegl_pci_find_class(class,from) pci_get_class(class,from)
+#else
+#define firegl_pci_find_class(class,from) pci_find_class(class,from)
+#endif
+
 int agp_backend_acquire(void)
 {
 	if (agp_bridge.type == NOT_SUPPORTED) {
@@ -718,7 +724,7 @@
 	 *        AGP devices and collect their data.
 	 */
 
-	while ((device = pci_find_class(PCI_CLASS_DISPLAY_VGA << 8,
+	while ((device = firegl_pci_find_class(PCI_CLASS_DISPLAY_VGA << 8,
 					device)) != NULL) {
 		pci_read_config_dword(device, 0x04, &scratch);
 
@@ -794,6 +800,13 @@
 				command &= ~0x00000001;
 		}
 	}
+
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,10) 
+            // the reference count has been increased in agp_backend_initialize.
+        if (device)
+            pci_dev_put(device); 
+#endif
+
 	/*
 	 * PASS2: Figure out the 4X/2X/1X setting and enable the
 	 *        target (our motherboard chipset).
@@ -839,8 +852,9 @@
 	 *        command registers.
 	 */
 
-	while ((device = pci_find_class(PCI_CLASS_DISPLAY_VGA << 8,
+	while ((device = firegl_pci_find_class(PCI_CLASS_DISPLAY_VGA << 8,
 					device)) != NULL) {
+
 		pci_read_config_dword(device, 0x04, &scratch);
 
 		if (!(scratch & 0x00100000))
@@ -871,6 +885,12 @@
         }
 	}
 
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,10) 
+            // the reference count has been increased in agp_backend_initialize.
+        if (device)
+            pci_dev_put(device); 
+#endif
+
     return 0; /* success */
 }
 
@@ -5119,8 +5139,9 @@
 	 *        AGP devices and collect their data.
 	 */
 
-	while ((device = pci_find_class(PCI_CLASS_DISPLAY_VGA << 8,
+	while ((device = firegl_pci_find_class(PCI_CLASS_DISPLAY_VGA << 8,
 					device)) != NULL) {
+
 		pci_read_config_dword(device, 0x04, &scratch);
 
 		if (!(scratch & 0x00100000))
@@ -5187,6 +5208,13 @@
 				command &= ~0x00000001;
 		}
 	}
+
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,10) 
+            // the reference count has been increased in agp_backend_initialize.
+        if (device)
+            pci_dev_put(device); 
+#endif
+
 	/*
 	 * PASS2: Figure out the 4X/2X/1X setting and enable the
 	 *        target (our motherboard chipset).
@@ -5217,8 +5245,9 @@
 	 *        command registers.
 	 */
 
-	while ((device = pci_find_class(PCI_CLASS_DISPLAY_VGA << 8,
+	while ((device = firegl_pci_find_class(PCI_CLASS_DISPLAY_VGA << 8,
 					device)) != NULL) {
+
 		pci_read_config_dword(device, 0x04, &scratch);
 
 		if (!(scratch & 0x00100000))
@@ -5249,6 +5278,12 @@
         }
 	}
 
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,10) 
+            // the reference count has been increased in agp_backend_initialize.
+        if (device)
+            pci_dev_put(device); 
+#endif
+
     return(0); /* success */
 }
 
@@ -6494,10 +6529,10 @@
     // locate host bridge device
 #ifdef __x86_64__
     do {
-        dev = pci_find_class(PCI_CLASS_BRIDGE_HOST << 8, dev);
+        dev = firegl_pci_find_class(PCI_CLASS_BRIDGE_HOST << 8, dev);
     } while((dev) && !agp_check_supported_device(dev));
 #else
-    if ((dev = pci_find_class(PCI_CLASS_BRIDGE_HOST << 8, NULL)) == NULL)
+    if ((dev = firegl_pci_find_class(PCI_CLASS_BRIDGE_HOST << 8, NULL)) == NULL)
         return -ENODEV;
 #endif
 
@@ -7040,8 +7075,12 @@
                     &agp_bridge.mode);
 				return hp_zx1_setup(dev);
 			}
-			dev = pci_find_class(PCI_CLASS_BRIDGE_HOST << 8, dev);
+			dev = firegl_pci_find_class(PCI_CLASS_BRIDGE_HOST << 8, dev);
 		} while (dev);
+
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,10) 
+                if(dev) pci_dev_put(dev);
+#endif
 		return -ENODEV;
 	}
 #endif	/* __ia64__ */
@@ -7462,6 +7501,11 @@
 	agp_bridge.free_gatt_table();
 	vfree(agp_bridge.key_list);
 
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,10)
+        // decrease the reference count.
+        pci_dev_put(agp_bridge.dev);
+#endif
+
 	if (agp_bridge.needs_scratch_page == TRUE) {
 		agp_bridge.scratch_page &= ~(0x00000fff);
 		agp_bridge.agp_destroy_page((unsigned long)


--- firegl_public.c 2005-01-06 9:05:02.000000000 +0100
+++ firegl_public.c 2005-01-06 9:19:16.000000000 +0100
@@ -88,6 +88,7 @@
 #include <linux/smp_lock.h>
 // newer SuSE kernels need this
 #include <linux/highmem.h>
+#include <linux/pagemap.h> // for lock_page and unlock_page
 
 #if defined(__ia64__)
 #include <linux/vmalloc.h>
 
EOF
#cd build_mod
fi
[ "$KV" == "2.6" ] && [ "$VER" == "8.8.25-1" -o "$VER" == "8.10.19-1" -o "$VER" == "8.12.10-1" -o "$VER" == "8.13.3-1" ] && if [ "$(uname -r|grep -E '2.6.11')" != "" ]; then
#cd /lib/modules/fglrx
# http://rage3d.com/board/showthread.php?t=33802462
patch -p1 <<EOF
diff -Nur fglrx-xorg.orig/firegl_public.c fglrx-xorg/firegl_public.c
--- fglrx-xorg.orig/firegl_public.c 2005-01-24 01:42:09.000000000 +0100
+++ fglrx-xorg/firegl_public.c 2005-01-24 01:28:21.000000000 +0100
@@ -1659,13 +1659,16 @@
 {
     unsigned long pte_linear;
     pgd_t* pgd_p;
+    pud_t* pud_p;
     pmd_t* pmd_p;
     pte_t* pte_p;
     pte_t  pte;
 
     pte_linear = VMALLOC_VMADDR(virtual_addr);  // convert to pte linear address (x86 => nop)
     pgd_p = pgd_offset(mm, pte_linear);
-    pmd_p = pmd_offset(pgd_p, pte_linear);
+    pud_p = pud_offset(pgd_p, pte_linear);
+    pmd_p = pmd_offset(pud_p, pte_linear);
+
 #ifndef FGL_ATOMIC_PTE
 #if LINUX_VERSION_CODE > 0x020500
     pte_p = pte_offset_kernel(pmd_p, pte_linear);
@@ -2085,6 +2088,7 @@
                                                    unsigned long address)
 {
     pgd_t* pgd_p;
+    pud_t* pud_p;
     pmd_t* pmd_p;
     pte_t* pte_p;
     pte_t  pte;
@@ -2185,7 +2189,8 @@
         /* alternatively we could generate a NOPAGE_OOM "out of memory" */
     }
     /*  locate medium level page table (x86 => nop) */
-    pmd_p = pmd_offset(pgd_p, pte_linear);
+    pud_p = pud_offset(pgd_p, pte_linear);
+    pmd_p = pmd_offset(pud_p, pte_linear);
     if (!pmd_present(*pmd_p))
     {
         __KE_ERROR("FATAL ERROR: User queue buffer not present! (pmd)\n");
@@ -2549,13 +2554,15 @@
 {
     unsigned long pte_linear;
     pgd_t* pgd_p;
+    pud_t* pud_p;
     pmd_t* pmd_p;
     pte_t* pte_p;
     pte_t  pte;
 
     pte_linear = VMALLOC_VMADDR(virtual_addr);  // convert to pte linear address (x86 => nop)
     pgd_p = pgd_offset(vma->vm_mm, pte_linear);
-    pmd_p = pmd_offset(pgd_p, pte_linear);
+    pud_p = pud_offset(pgd_p, pte_linear);
+    pmd_p = pmd_offset(pud_p, pte_linear);
 #ifndef FGL_ATOMIC_PTE
 #if LINUX_VERSION_CODE > 0x020500
     pte_p = pte_offset_kernel(pmd_p, pte_linear);
@@ -2704,13 +2711,13 @@
 #endif /* __ia64__ */
                 vma->vm_flags |= VM_IO; /* not in core dump */
             }
-            if (remap_page_range(FGL_VMA_API_PASS
+            if (remap_pfn_range(FGL_VMA_API_PASS
                                  vma->vm_start,
-                                 __ke_vm_offset(vma),
+                                 __ke_vm_offset(vma) >> PAGE_SHIFT,
                                  vma->vm_end - vma->vm_start,
                                  vma->vm_page_prot))
             {
-                __KE_DEBUG("remap_page_range failed\n");
+                __KE_DEBUG("remap_pfn_range failed\n");
                 return -EAGAIN;
             }
             vma->vm_flags |= VM_SHM | VM_RESERVED; /* Don't swap */
@@ -2771,13 +2778,13 @@
 			{
				if (__ke_vm_offset(vma) >= __pa(high_memory))
					vma->vm_flags |= VM_IO; /* not in core dump */
-				if (remap_page_range(FGL_VMA_API_PASS
+				if (remap_pfn_range(FGL_VMA_API_PASS
									 vma->vm_start,
-									 __ke_vm_offset(vma),
+									 __ke_vm_offset(vma) >> PAGE_SHIFT,
									 vma->vm_end - vma->vm_start,
									 vma->vm_page_prot))
 				{
-					__KE_DEBUG("remap_page_range failed\n");
+					__KE_DEBUG("remap_pfn_range failed\n");
					return -EAGAIN;
 				}
 #ifdef __x86_64__
@@ -2808,13 +2815,13 @@
 			{
				if (__ke_vm_offset(vma) >= __pa(high_memory))
					vma->vm_flags |= VM_IO; /* not in core dump */
-				if (remap_page_range(FGL_VMA_API_PASS
+				if (remap_pfn_range(FGL_VMA_API_PASS
									 vma->vm_start,
-									 __ke_vm_offset(vma),
+									 __ke_vm_offset(vma) >> PAGE_SHIFT,
									 vma->vm_end - vma->vm_start,
									 vma->vm_page_prot))
 				{
-					__KE_DEBUG("remap_page_range failed\n");
+					__KE_DEBUG("remap_pfn_range failed\n");
					return -EAGAIN;
 				}
 #ifdef __x86_64__
@@ -2858,6 +2865,37 @@
 
 #if LINUX_VERSION_CODE >= 0x020400
 
+#if LINUX_VERSION_CODE >= 0x02060b
+
+typedef struct {
+       void                    (*free_memory)(struct agp_memory *);
+       struct agp_memory *     (*allocate_memory)(size_t, u32);
+       int                     (*bind_memory)(struct agp_memory *, off_t);
+       int                     (*unbind_memory)(struct agp_memory *);
+       void                    (*enable)(u32);
+       int                     (*acquire)(void);
+       void                    (*release)(void);
+       int                     (*copy_info)(struct agp_kern_info *);
+} drm_agp_t;
+
+static const drm_agp_t drm_agp = {
+       &agp_free_memory,
+       &agp_allocate_memory,
+       &agp_bind_memory,
+       &agp_unbind_memory,
+       &agp_enable,
+       &agp_backend_acquire,
+       &agp_backend_release,
+       &agp_copy_info
+};
+#undef DRM_AGP_MODULE_GET
+#undef DRM_AGP_MODULE_PUT
+
+#define DRM_AGP_MODULE_GET      &drm_agp
+#define DRM_AGP_MODULE_PUT 
+
+#endif
+
 static const drm_agp_t  *drm_agp_module_stub = NULL;
 
 #define AGP_FUNCTIONS  8
 
EOF
#cd build_mod
fi
[ "$KV" == "2.6" ] && [ "$VER" == "8.14.13-1" ] && if [ "$(uname -r|grep -E '2.6.12|2.6.13')" != "" ]; then
# http://ati.cchtml.com/show_bug.cgi?id=136
patch -p0 <<EOF
diff -Nru /lib/modules/fglrx.orig/build_mod/agpgart_be.c /lib/modules/fglrx/build_mod/agpgart_be.c
--- /lib/modules/fglrx.orig/build_mod/agpgart_be.c	2005-06-08 21:04:39.000000000 +0200
+++ /lib/modules/fglrx/build_mod/agpgart_be.c	2005-06-29 17:55:32.000000000 +0200
@@ -7067,7 +7067,7 @@
 			if ( agp_bridge.capndx )
             {
 				printk(KERN_INFO PFX "Detected HP ZX1 AGP "
-				       "chipset at %s\n", dev->slot_name);
+				       "chipset at %s\n", pci_name(dev));
 				agp_bridge.type = HP_ZX1;
 				agp_bridge.dev = dev;
               	/* fill in the mode register */
@@ -7133,7 +7133,7 @@
                 if( cap_ptr )
                 {
     				printk(KERN_INFO PFX "Detected a Via Apollo KT400 chipset in AGP v3 mode "
-	    			       "at %s\n", dev->slot_name);
+	    			       "at %s\n", pci_name(dev));
 
                     agp_bridge.type = VIA_APOLLO_KT400;
     				agp_bridge.dev = dev;
@@ -7153,12 +7153,12 @@
                 if( cap_ptr )
                 {
     				printk(KERN_DEBUG PFX "Detected a Via Apollo KT400 chipset in AGP v2 compat mode "
-	    			       "at %s\n", dev->slot_name);
+	    			       "at %s\n", pci_name(dev));
                 }
                 else
                 {
     				printk(KERN_ERR PFX "Detected a Via Apollo KT400 chipset but no AGPv2/v3 caps found "
-	    			       "at %s\n", dev->slot_name);
+	    			       "at %s\n", pci_name(dev));
                 }
             }
             break;
@@ -7184,7 +7184,7 @@
                 if( cap_ptr )
                 {
                     printk(KERN_INFO PFX "Detected a Via P4X400 chipset in AGP v3 mode "
-                           "at %s\n", dev->slot_name);
+                           "at %s\n", pci_name(dev));
 
                     agp_bridge.type = VIA_APOLLO_P4X400;
                     agp_bridge.dev = dev;
@@ -7204,7 +7204,7 @@
                 if( cap_ptr )
                 {
                     printk(KERN_INFO PFX "Detected a Via P4X400 chipset with AGP v3.x caps in AGP v2 compatibility mode "
-                           "at %s\n", dev->slot_name);
+                           "at %s\n", pci_name(dev));
 
                     agp_bridge.type = VIA_APOLLO_P4X400;
                     agp_bridge.dev = dev;
@@ -7224,12 +7224,12 @@
                 if( cap_ptr )
                 {
                     printk(KERN_DEBUG PFX "Detected a Via P4X400 chipset with AGP v2 caps in true AGP v2 mode "
-                           "at %s\n", dev->slot_name);
+                           "at %s\n", pci_name(dev));
                 }
                 else
                 {
                     printk(KERN_ERR PFX "Detected a Via P4X400 chipset but no AGPv2/v3 caps found "
-                           "at %s\n", dev->slot_name);
+                           "at %s\n", pci_name(dev));
                 }
             }
             break;
diff -Nru /lib/modules/fglrx.orig/build_mod/firegl_public.c /lib/modules/fglrx/build_mod/firegl_public.c
--- /lib/modules/fglrx.orig/build_mod/firegl_public.c	2005-06-08 21:04:39.000000000 +0200
+++ /lib/modules/fglrx/build_mod/firegl_public.c	2005-06-29 17:55:32.000000000 +0200
@@ -116,7 +116,11 @@
 #endif
 
 #ifdef __x86_64__
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
+#include "linux/ioctl32.h"
+#else
 #include "asm/ioctl32.h"
+#endif
 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,2)
 #include "linux/syscalls.h"
 #endif
@@ -340,17 +344,17 @@
 struct firegl_drm_stub_info_t *firegl_stub_pointer = NULL;
 #define inter_module_put(x)
 #define inter_module_unregister(x)
-#define inter_module_get(x)             firegl_stub_pointer
+#define inter_module_get_request(x,y)             firegl_stub_pointer
 #define inter_module_register(x,y,z)    do { firegl_stub_pointer = z; } while (0)
 /* This is a kludge for backward compatibility that is only useful in DRM(stub_open) */
 #define fops_put(fops)      MOD_DEC_USE_COUNT
 #define fops_get(fops)      (fops); MOD_INC_USE_COUNT
 #endif // LINUX_VERSION_CODE < 0x020400
 
-#define DRM_MODULE_GET          (firegl_drm_stub_info_t *)inter_module_get("drm")
+#define DRM_MODULE_GET          (firegl_drm_stub_info_t *)inter_module_get_request("drm","drm")
 #define DRM_MODULE_PUT          inter_module_put("drm")
 
-#define DRM_AGP_MODULE_GET      (drm_agp_t *)inter_module_get("drm_agp")
+#define DRM_AGP_MODULE_GET      (drm_agp_t *)inter_module_get_request("drm_agp","drm_agp")
 #define DRM_AGP_MODULE_PUT      inter_module_put("drm_agp")
 
 unsigned long ATI_API_CALL __ke_cpu_to_le32(unsigned long _u)
@@ -997,8 +1001,16 @@
     return high_memory;
 }
 
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
+/* Saved pci_dev pointer for the new agpgart API */
+static struct pci_dev *fglrx_pci_dev;
+#endif
+
 int ATI_API_CALL __ke_pci_enable_device(__ke_pci_dev_t* dev)
 {
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
+    fglrx_pci_dev = (struct pci_dev *)dev;
+#endif
     return (pci_enable_device( (struct pci_dev*)(void *)dev ));
 }
 
@@ -2860,6 +2872,68 @@
 	int			(*copy_info)(struct agp_kern_info *);
 } drm_agp_t;
 
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
+/*
+ * Kernel 2.6.12 has new agpgart API with support for multiple AGP bridges, but
+ * the fglrx core does not know about this yet.  For now, just emulate the old
+ * API.
+ */
+
+static struct agp_bridge_data *fglrx_agp_bridge;
+
+static int fglrx_compat_agp_backend_acquire(void)
+{
+	fglrx_agp_bridge = agp_backend_acquire(fglrx_pci_dev);
+	if (!fglrx_agp_bridge)
+		return -ENODEV;
+	return 0;
+}
+
+static void fglrx_compat_agp_backend_release(void)
+{
+	agp_backend_release(fglrx_agp_bridge);
+}
+
+static struct agp_memory *fglrx_compat_agp_allocate_memory(size_t page_count,
+							   u32 type)
+{
+	return agp_allocate_memory(fglrx_agp_bridge, page_count, type);
+}
+
+static void fglrx_compat_agp_enable(u32 mode)
+{
+	agp_enable(fglrx_agp_bridge, mode);
+}
+
+static int fglrx_compat_agp_copy_info(struct agp_kern_info *info)
+{
+	if (!fglrx_agp_bridge) {
+		fglrx_agp_bridge = agp_find_bridge(fglrx_pci_dev);
+		if (!fglrx_agp_bridge) {
+			memset(info, 0, sizeof(struct agp_kern_info));
+			info->chipset = NOT_SUPPORTED;
+			return -ENODEV;
+		}
+	}
+	return agp_copy_info(fglrx_agp_bridge, info);
+}
+
+static const drm_agp_t drm_agp = {
+	&agp_free_memory,
+	&fglrx_compat_agp_allocate_memory,
+	&agp_bind_memory,
+	&agp_unbind_memory,
+	&fglrx_compat_agp_enable,
+	&fglrx_compat_agp_backend_acquire,
+	&fglrx_compat_agp_backend_release,
+	&fglrx_compat_agp_copy_info
+};
+
+#else /* LINUX_VERSION_CODE < KERNEL_VERSION(2,6,12) */
+/*
+ * For 2.6.11 we can just use the agpgart functions directly.
+ */
+
 static const drm_agp_t drm_agp = {
 	&agp_free_memory,
 	&agp_allocate_memory,
@@ -2870,6 +2944,9 @@
 	&agp_backend_release,
 	&agp_copy_info
 };
+
+#endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2,6,12) */
+
 #undef DRM_AGP_MODULE_GET
 #undef DRM_AGP_MODULE_PUT
 
EOF
fi
CC=gcc-$(perl -pe '($_)=/gcc.[Vv]ersion\s(4\.\d|3\.\d|2\.\d+)/;' /proc/version)
[ "$KV" == "2.6" ] && [ "$VER" == "8.14.13-1" ] && [ "${CC:4:1}" == "4" ] && if [ "$(uname -r|grep -E '2.6.12|2.6.13')" != "" ]; then
perl -pi -e "s/(GCC_VER_MAJ\s*= ).*/\${1}3/" /lib/modules/fglrx/build_mod/2.6.x/Makefile
# http://www.openjlab.org/fglrx-linux-2.6.12-gcc4.diff
patch -p1 <<EOF
diff -urN fglrx-vanilla/firegl_public.c fglrx/firegl_public.c
--- fglrx-vanilla/firegl_public.c	2005-06-12 20:56:30.000000000 +0200
+++ fglrx/firegl_public.c	2005-06-30 00:19:24.000000000 +0200
@@ -167,7 +167,7 @@
 
 #if !defined(__ia64__)
 // the macros do use errno variable
-static int errno;
+int errno;
 #endif // __ia64__
 
 // int mlock(const void *addr, size_t len);
diff -urN fglrx/agp_backend.h fglrx-vanilla/agp_backend.h
--- fglrx/agp_backend.h	2005-06-12 20:56:04.000000000 +0200
+++ fglrx-vanilla/agp_backend.h	2005-06-30 00:37:34.000000000 +0200
@@ -89,7 +89,7 @@
 #define _X(_x) __fgl_##_x
 extern int _X(agp_init)(void);
 extern void _X(agp_cleanup)(void);
-extern int _X(agp_try_unsupported);
+static int _X(agp_try_unsupported);
 #else /* Original AGPGART module */
 #define STANDALONE_AGPGART
 #define _X(_x) _x
diff -urN fglrx/agpgart_be.c fglrx-vanilla/agpgart_be.c
--- fglrx/agpgart_be.c	2005-06-30 00:38:38.000000000 +0200
+++ fglrx-vanilla/agpgart_be.c	2005-06-30 00:38:02.000000000 +0200
@@ -176,7 +176,7 @@
 #ifdef STANDALONE_AGPGART
 static int agp_try_unsupported __initdata = 0;
 #else /* !STANDALONE_AGPGART */
-int agp_try_unsupported = 0;
+static int agp_try_unsupported = 0;
 #endif /* !STANDALONE_AGPGART */
 
 int agp_memory_reserved;

EOF
fi
[ "$KV" == "2.6" ] && [ "$VER" == "8.13.3-1" ] && if [ "$(uname -r|grep -E '2.6.11|2.6.12')" != "" ]; then
 perl -pi -e s/pci_find_class/pci_get_class/g /lib/modules/fglrx/build_mod/agpgart_be.c
fi
[ "$VER" == "8.16.20-1" -a "$CPU" == "x86_64" ] && if [ "$(uname -r|grep -E '2.6.12|2.6.13')" != "" ]; then
 perl -pi -e 's|asm(/ioctl32.h)|linux\1|' /lib/modules/fglrx/build_mod/firegl_public.c
fi
[ "$KV" == "2.6" ] && [ "$VER" == "8.16.20-1" -o "$VER" == "8.18.6-1" -o "$VER" == "8.18.8-1" ] && if [ "$(uname -r|grep -E '2.6.14|2.6.15|2.6.16')" != "" ]; then
 perl -pi -e 's|verify_area|access_ok|' /lib/modules/fglrx/build_mod/firegl_public.c /lib/modules/fglrx/build_mod/drm_os_linux.h
fi
[ "$KV" == "2.6" ] && [ "$VER" == "8.16.20-1" -o "$VER" == "8.18.6-1" -o "$VER" == "8.18.8-1" -o "$VER" == "8.19.10-1" -o "$VER" == "8.20.8-1" -o "$VER" == "8.21.7-1" ] && if [ "$(uname -r|grep -E '2.6.15|2.6.16')" != "" ]; then
patch <<EOF
--- firegl_public.c.old	2005-12-09 19:21:33.000000000 +0100
+++ firegl_public.c	2005-12-18 14:44:15.000000000 +0100
@@ -2621,7 +2621,7 @@
 
     pMmPage = virt_to_page(kaddr);
 
-#if 0
+#if LINUX_VERSION_CODE >= 0x02060f
     // WARNING WARNINIG WARNNING WARNNING WARNNING WARNNING WARNNING WARNNING
     // Don't increment page usage count, cause ctx pages are allocated
     // with drm_alloc_pages, which marks all pages as reserved. Reserved

EOF
fi
chmod +x make.sh
if test -e /usr/bin/$CC; then
 perl -pi -e "s/^\s*(make)\s(.*)/\1 CC=$CC \2/" make.sh
 [ "${CC:4:1}" == "3" ] && perl -pi -e "s/(GCC_VER_MAJ\s*= ).*/\${1}${CC:4:1}/" /lib/modules/fglrx/build_mod/2.6.x/Makefile
 perl -pi -e "s/(\sGCC_MAJOR=\").*(\".*)/\${1}${CC:4:1}\2/" make.sh
 perl -pi -e "s/(KERNEL_GCC=\").*(\".*)/\${1}${CC#gcc-}\2/" make.sh
 perl -pi -e "s/(GCC_AVAILABLE=\").*(\".*)/\${1}${CC#gcc-}\2/" make.sh
 CC=$CC ./make.sh
else
 ./make.sh
fi
cd ..
chmod +x make_install.sh
./make_install.sh
if ! test -e /etc/X11/$X_CONF.1st; then
 cp /etc/X11/$X_CONF /etc/X11/$X_CONF.1st
fi
if test -e /usr/lib/libqt.so.2; then
 [ -e /usr/X11R6/bin/fireglcontrol.qtstatic.gcc2.96.bz2 ] && bzcat /usr/X11R6/bin/fireglcontrol.qtstatic.gcc2.96.bz2 > /usr/X11R6/bin/fireglcontrol
else
 [ -e /usr/X11R6/bin/fireglcontrol.qt3.gcc3.2.bz2 ] && bzcat /usr/X11R6/bin/fireglcontrol.qt3.gcc3.2.bz2 > /usr/X11R6/bin/fireglcontrol
fi
[ -f /usr/X11R6/bin/fireglcontrol ] && chmod 755 /usr/X11R6/bin/fireglcontrol
#fglrxconfig
perl -p -e 's/^[\s]*Load\s*"GLcore"/#\tLoad  "GLcore"/;
            s/^[\s]*#*[\s]*Load\s*"dri"/\tLoad  "dri"/;
            s/^[\s]*#*[\s]*Load\s*"glx"/\tLoad  "glx"/;
	    s/^[\s]*#*[\s]*Option\s*"Composite".*/#\tOption\t"Composite"\t"1"/;
            s/^[\s]*Driver\s*"radeon"/\tDriver      "fglrx"/g;
            s/^[\s]*Driver\s*"fbdev"/\tDriver      "fglrx"/g;
            s/^[\s]*Driver\s*"vesa"/\tDriver      "fglrx"/g;
	    s/^[\s]*Driver\s*"ati"/\tDriver      "fglrx"/g;
            s/DefaultColorDepth.*/DefaultColorDepth 24/;
            s/^[\s]*Option\s*"sw_cursor"/\t#Option     "sw_cursor"/;
	    s/^([\s]*Load\s*"extmod")/#\1\n\tSubSection  "extmod"\n\t  Option  "omit xfree86-dga"\n\tEndSubSection/;
	    s/^.*UseInternalAGPGART.*\n?//g;
	    s/^.*VideoOverlay.*\n?//g;
	    s/^.*OpenGLOverlay.*\n?//g;
	    s/^.*MonitorLayout.*\n?//g;
	    s/^([\s]*Driver\s*"fglrx")/\1\n\tOption      "UseInternalAGPGART" "yes"\n\tOption      "VideoOverlay" "on"\n\tOption      "OpenGLOverlay" "off"\n\tOption      "MonitorLayout" "AUTO, AUTO"/g' \
            < /etc/X11/$X_CONF.1st > /etc/X11/$X_CONF
lsmod|grep -q agpgart && perl -pi -e 's/^([\s]*Option\s*"UseInternalAGPGART").*/\1 "no"/g' /etc/X11/$X_CONF
[ "$CPU" == "x86_64" ] && perl -pi -e 's/^([\s]*Option\s*"UseInternalAGPGART").*/\1 "no"/g' /etc/X11/$X_CONF
[ "$LAPTOP" ] && perl -pi -e 's/^([\s]*Option\s*"MonitorLayout").*/\1 "LVDS, AUTO"/g' /etc/X11/$X_CONF
[ "$VER" == "8.16.20-1" -o "$VER" == "8.18.6-1" -o "$VER" == "8.18.8-1" -o "$VER" == "8.19.10-1" -o "$VER" == "8.20.8-1" -o "$VER" == "8.21.7-1" -o "$1" == "-r" ] && perl -pi -e 's/^.*Mode.ine.*\n?//g' /etc/X11/$X_CONF
sync
# added for 3.14.6 and above
if ! grep -q /dev/shm /proc/mounts; then
perl -pi -e 's|\n?$|\n|' /etc/fstab
perl -pi -e 's|.*tmpfs.*[\n]*||' /etc/fstab
echo tmpfs /dev/shm tmpfs defaults 0 0 >> /etc/fstab
mount /dev/shm
fi
if [ -n "$INSTALLED" ]; then
if test -z "$DM"; then
 test -e /etc/init.d/kdm && /etc/init.d/kdm start
else
 test -e /etc/init.d/$DM && /etc/init.d/$DM start
fi
else
 cp /etc/X11/$X_CONF /etc/X11/$X_CONF.ati
 nohup sh -c "/etc/init.d/xsession stop; sleep 5; rmmod radeon drm; modprobe fglrx; cp /etc/X11/$X_CONF.ati /etc/X11/$X_CONF; /etc/init.d/xsession start"
fi
