2df06f
--- cups-1.2.10/backend/runloop.c.usb-paperout	2006-12-06 20:10:16.000000000 +0000
2df06f
+++ cups-1.2.10/backend/runloop.c	2007-04-25 18:00:59.000000000 +0100
2df06f
@@ -40,6 +40,14 @@
2df06f
 #endif /* __hpux */
2df06f
 
2df06f
 
2df06f
+#ifdef __linux
2df06f
+#include <sys ioctl.h="">
2df06f
+#include <linux lp.h="">
2df06f
+#include <time.h>
2df06f
+int linux_usb_paperout_hack = 0;
2df06f
+#endif /* __linux */
2df06f
+
2df06f
+
2df06f
 /*
2df06f
  * 'backendRunLoop()' - Read and write print and back-channel data.
2df06f
  */
2df06f
@@ -64,6 +72,9 @@
2df06f
 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
2df06f
   struct sigaction action;		/* Actions for POSIX signals */
2df06f
 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
2df06f
+#ifdef __linux
2df06f
+  time_t	last_write = 0;		/* Last time write() succeeded */
2df06f
+#endif /* __linux */
2df06f
 
2df06f
 
2df06f
   fprintf(stderr, "DEBUG: backendRunLoop(print_fd=%d, device_fd=%d, use_bc=%d)\n",
2df06f
@@ -227,6 +238,9 @@
2df06f
       }
2df06f
       else
2df06f
       {
2df06f
+#ifdef __linux
2df06f
+	last_write = time (NULL);
2df06f
+#endif /* __linux */
2df06f
         if (paperout)
2df06f
 	{
2df06f
 	  fputs("STATE: -media-empty-error\n", stderr);
2df06f
@@ -247,7 +261,42 @@
2df06f
 	total_bytes += bytes;
2df06f
       }
2df06f
     }
2df06f
+
2df06f
+#ifdef __linux
2df06f
+    if (linux_usb_paperout_hack)
2df06f
+    {
2df06f
+      time_t now;
2df06f
+      if (!paperout &&
2df06f
+	  ((now = time(NULL)) - last_write) > 5)
2df06f
+      {
2df06f
+	unsigned int status;
2df06f
+	if (ioctl (device_fd, LPGETSTATUS, &status) == 0 &&
2df06f
+	    (status & LP_POUTPA))
2df06f
+	{
2df06f
+	  fputs("ERROR: Out of paper!\n", stderr);
2df06f
+	  fputs("STATE: +media-empty-error\n", stderr);
2df06f
+	  paperout = 1;
2df06f
+	}
2df06f
+
2df06f
+	/* Don't check status for another 5s. */
2df06f
+	last_write = now;
2df06f
+      }
2df06f
+    }
2df06f
+#endif /* __linux */
2df06f
+  }
2df06f
+
2df06f
+#ifdef __linux
2df06f
+  if (linux_usb_paperout_hack)
2df06f
+  {
2df06f
+    /* Wait for the last write() to finish. */
2df06f
+    do
2df06f
+    {
2df06f
+      FD_ZERO(&output);
2df06f
+      FD_SET(device_fd, &output);
2df06f
+    } while (select(device_fd + 1, NULL, &output, NULL, NULL) < 0 &&
2df06f
+	     errno == EINTR);
2df06f
   }
2df06f
+#endif /* __linux */
2df06f
 
2df06f
  /*
2df06f
   * Return with success...
2df06f
--- cups-1.2.10/backend/usb-unix.c.usb-paperout	2007-04-25 17:50:06.000000000 +0100
2df06f
+++ cups-1.2.10/backend/usb-unix.c	2007-04-25 17:50:06.000000000 +0100
2df06f
@@ -39,6 +39,11 @@
2df06f
 #include "ieee1284.c"
2df06f
 #include <sys select.h="">
2df06f
 
2df06f
+#ifdef __linux
2df06f
+#include <unistd.h>
2df06f
+#include <fcntl.h>
2df06f
+extern int linux_usb_paperout_hack;
2df06f
+#endif /* __linux */
2df06f
 
2df06f
 /*
2df06f
  * Local functions...
2df06f
@@ -70,6 +75,11 @@
2df06f
   (void)argc;
2df06f
   (void)argv;
2df06f
 
2df06f
+#ifdef __linux
2df06f
+  /* Get the runloop to check for USB paper-out condition. */
2df06f
+  linux_usb_paperout_hack = 1;
2df06f
+#endif /* __linux */
2df06f
+
2df06f
  /*
2df06f
   * Open the USB port device...
2df06f
   */
2df06f
@@ -150,6 +160,12 @@
2df06f
 
2df06f
   tcsetattr(device_fd, TCSANOW, &opts);
2df06f
 
2df06f
+#ifdef __linux
2df06f
+  /* Put the file descriptor into non-blocking mode so that we
2df06f
+   * get a chance to detect paper-out. */
2df06f
+  fcntl (device_fd, F_SETFL, fcntl (device_fd, F_GETFL) | O_NONBLOCK);
2df06f
+#endif /* __linux */
2df06f
+
2df06f
  /*
2df06f
   * Finally, send the print file...
2df06f
   */