#!/bin/sh -e

# 05_tty_count_build_failure.dpatch by Steve Kowalik and Bas Zoetekouw

# DP: Fix building when tty->count is an atomic_t, rather than int.

. debian/patches/patch-opts

if [ $# -ne 1 ]; then
    echo >&2 "`basename $0`: script expects -patch|-unpatch as argument"
    exit 1
fi

case "$1" in
       -patch) patch $patch_opts -p1 < $0;;
       -unpatch) patch $patch_opts -p1 -R < $0;;
        *)
                echo >&2 "`basename $0`: script expects -patch|-unpatch as argument"
                exit 1;;
esac

exit 0

@DPATCH@

--- alsa-driver/configure.in~	10 Jun 2003 23:41:11 -0000	1.8
+++ alsa-driver/configure.in	12 Jun 2003 00:00:42 -0000
@@ -1197,6 +1197,29 @@
 CHECK_KERNEL_CONFIG(CONFIG_X86_PC9800, [PC9800 support in kernel])
 AC_SUBST(CONFIG_X86_PC9800)
 
+dnl Check if tty_type.count is an atomic_t
+AC_MSG_CHECKING(if tty_struct.count is an atomic_t)
+tty_count_is_atomic="0"
+ac_save_CFLAGS="$CFLAGS"
+CFLAGS="$CFLAGS -I$CONFIG_SND_KERNELDIR/include"
+AC_TRY_COMPILE([
+#define __KERNEL__
+#include <linux/tty.h>
+],[
+struct tty_struct foobar;
+foobar.count.counter=0;
+exit(0);
+],
+    AC_MSG_RESULT("yes");tty_count_is_atomic="1",
+    AC_MSG_RESULT("no");tty_count_is_atomic="0",
+    AC_MSG_RESULT("unknown");tty_count_is_atomic="0"
+)
+CFLAGS=$ac_save_CFLAGS
+CONFIG_TTY_COUNT_ATOMIC=$tty_count_is_atomic
+if test "$CONFIG_TTY_COUNT_ATOMIC" = "1"; then
+  AC_DEFINE(CONFIG_TTY_COUNT_ATOMIC)
+fi
+
 dnl Check for which cards to compile driver for...
 ALSA_TOPLEVEL_INIT
 ALSA_TOPLEVEL_SELECT
--- alsa-driver/include/config.h.in~	30 May 2003 12:27:03 -0000	1.1.1.4
+++ alsa-driver/include/config.h.in	11 Jun 2003 23:53:44 -0000
@@ -33,6 +33,7 @@
 #undef CONFIG_HAVE_STRLCPY
 #undef CONFIG_HAVE_SNPRINTF
 #undef CONFIG_HAVE_VMALLOC_TO_PAGE
+#undef CONFIG_TTY_COUNT_ATOMIC
 
 /* 2.2 kernels */
 #undef CONFIG_OLD_KILL_FASYNC
--- alsa-driver/drivers/serialmidi.c~	22 Feb 2003 09:37:11 -0000	1.1.1.1
+++ alsa-driver/drivers/serialmidi.c	29 May 2003 10:11:50 -0000
@@ -34,6 +34,8 @@
 #include <sound/initval.h>
 #include <linux/delay.h>
 
+#include "config.h"
+
 #define SNDRV_SERIAL_MAX_OUTS		16 /* min 16 */
 #define TX_BUF_SIZE			256
 
@@ -155,7 +157,13 @@
 		retval = -EIO;
 		goto __end;
 	}
+/* in 2.4.21-rc1-ac3, tty->count is an atomic_t
+ * instead of an int */
+#ifdef CONFIG_TTY_COUNT_ATOMIC
+	if (tty->count.counter > 1) {
+#else
 	if (tty->count > 1) {
+#endif
 		snd_printk(KERN_ERR "tty %s is already used", serial->sdev);
 		retval = -EBUSY;
 		goto __end;
