1 /* Copyright (C) 1989, 1990, 1991, 1993, 1996 artofcode LLC.  All rights reserved.
2 
3   This program is free software; you can redistribute it and/or modify it
4   under the terms of the GNU General Public License as published by the
5   Free Software Foundation; either version 2 of the License, or (at your
6   option) any later version.
7 
8   This program is distributed in the hope that it will be useful, but
9   WITHOUT ANY WARRANTY; without even the implied warranty of
10   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11   General Public License for more details.
12 
13   You should have received a copy of the GNU General Public License along
14   with this program; if not, write to the Free Software Foundation, Inc.,
15   59 Temple Place, Suite 330, Boston, MA, 02111-1307.
16 
17 */
18 
19 /*$Id: gdevn533.c,v 1.3.2.1.2.1 2003/01/17 00:49:01 giles Exp $*/
20 /* Sony NWP-533 driver for GhostScript */
21 #include "gdevprn.h"
22 #define prn_dev ((gx_device_printer *)dev) /* needed in 5.31 et seq */
23 #include <sys/ioctl.h>
24 #include <newsiop/lbp.h>
25 
26 /***
27  *** Note: this driver was contributed by a user, Tero Kivinen:
28  ***       please contact kivinen@joker.cs.hut.fi if you have questions.
29  ***/
30 
31 #define A4_PAPER 1
32 
33 #ifdef A4_PAPER
34 #define PAPER_XDOTS A4_XDOTS
35 #define PAPER_YDOTS A4_YDOTS
36 #else
37 #define PAPER_XDOTS B4_XDOTS
38 #define PAPER_YDOTS B4_YDOTS
39 #endif
40 
41 #ifndef TRUE
42 #define TRUE 1
43 #endif
44 #ifndef FALSE
45 #define FALSE 0
46 #endif
47 
48 /* The device descriptor */
49 private dev_proc_open_device(nwp533_open);
50 private dev_proc_print_page(nwp533_print_page);
51 private dev_proc_close_device(nwp533_close);
52 private gx_device_procs nwp533_procs =
53   prn_procs(nwp533_open, gdev_prn_output_page, nwp533_close);
54 
55 const gx_device_printer far_data gs_nwp533_device =
56   prn_device(nwp533_procs, "nwp533",
57 	PAPER_XDOTS * 10.0 / DPI,	/* width_10ths */
58 	PAPER_YDOTS * 10.0 / DPI,	/* height_10ths */
59 	DPI,				/* x_dpi */
60 	DPI,				/* y_dpi */
61 	0,0,0,0,			/* margins */
62 	1, nwp533_print_page);
63 
64 /* return True if should retry - False if should quit */
65 private int
analyze_error(int printer_file)66 analyze_error(int printer_file)
67 {
68   struct lbp_stat status;
69   char *detail = NULL, *old_detail = NULL;
70   int waiting = TRUE;
71   int retry_after_return = TRUE;
72 
73   if(ioctl(printer_file, LBIOCRESET, 0) < 0)
74     {
75       perror("ioctl(LBIOCRESET)");
76       return FALSE;
77     }
78   if (ioctl(printer_file, LBIOCSTATUS, &status) < 0)
79     {
80       perror("ioctl(LBIOCSTATUS)");
81       return FALSE;
82     }
83 
84   do
85     {
86       /* Is there an error */
87       if(status.stat[0] & (ST0_CALL | ST0_REPRINT_REQ | ST0_WAIT | ST0_PAUSE))
88 	{
89 	  if(status.stat[1] & ST1_NO_CARTRIGE)/* mispelled? */
90 	    detail = "No cartridge - waiting";
91 	  else if(status.stat[1] & ST1_NO_PAPER)
92 	    detail = "Out of paper - waiting";
93 	  else if(status.stat[1] & ST1_JAM)
94 	    detail = "Paper jam - waiting";
95 	  else if(status.stat[1] & ST1_OPEN)
96 	    detail = "Door open - waiting";
97 	  else if(status.stat[1] & ST1_TEST)
98 	    detail = "Test printing - waiting";
99 	  else {
100 	    waiting = FALSE;
101 	    retry_after_return = FALSE;
102 
103 	    if(status.stat[2] & ST2_FIXER)
104 	      detail = "Fixer trouble - quiting";
105 	    else if(status.stat[2] & ST2_SCANNER)
106 	      detail = "Scanner trouble - quiting";
107 	    else if(status.stat[2] & ST2_MOTOR)
108 	      detail = "Scanner motor trouble - quiting";
109 	    else if(status.stat[5] & ST5_NO_TONER)
110 	      detail = "No toner - quiting";
111 	  }
112 	}
113       else
114 	{
115 	  waiting = FALSE;
116 	}
117       if(detail != NULL && detail != old_detail)
118 	{
119 	  perror(detail);
120 	  old_detail = detail;
121 	}
122       if(waiting)
123 	{
124 	  ioctl(1, LBIOCRESET, 0);
125 	  sleep(5);
126 	  ioctl(1, LBIOCSTATUS, &status);
127 	}
128     }
129   while(waiting);
130   return retry_after_return;
131 }
132 
133 private int
nwp533_open(gx_device * dev)134 nwp533_open(gx_device *dev)
135 {
136   gx_device_printer *pdev = (gx_device_printer *) dev;
137 
138   if (pdev->fname[0] == '\0')
139     {
140       strcpy(pdev->fname, "/dev/lbp");
141     }
142   return gdev_prn_open(dev);
143 }
144 
145 private int
nwp533_close(gx_device * dev)146 nwp533_close(gx_device *dev)
147 {
148   if (((gx_device_printer *) dev)->file != NULL)
149     {
150       int printer_file;
151 
152       printer_file = fileno(((gx_device_printer *) dev)->file);
153     restart2:
154       if(ioctl(printer_file, LBIOCSTOP, 0) < 0)
155 	{
156 	  if(analyze_error(printer_file))
157 	    goto restart2;
158 	  perror("Waiting for device");
159 	  return_error(gs_error_ioerror);
160 	}
161     }
162   return gdev_prn_close(dev);
163 }
164 
165 /* Send the page to the printer. */
166 private int
nwp533_print_page(gx_device_printer * dev,FILE * prn_stream)167 nwp533_print_page(gx_device_printer *dev, FILE *prn_stream)
168 {
169   int lnum;
170   int line_size = gdev_mem_bytes_per_scan_line(dev);
171   byte *in;
172   int printer_file;
173   printer_file = fileno(prn_stream);
174 
175   if (line_size % 4 != 0)
176     {
177       line_size += 4 - (line_size % 4);
178     }
179   in = (byte *) gs_malloc(line_size, 1, "nwp533_output_page(in)");
180  restart:
181   if(ioctl(printer_file, LBIOCSTOP, 0) < 0)
182     {
183       if(analyze_error(printer_file))
184 	goto restart;
185       perror("Waiting for device");
186       return_error(gs_error_ioerror);
187     }
188   lseek(printer_file, 0, 0);
189 
190   for ( lnum = 0; lnum < dev->height; lnum++)
191     {
192       gdev_prn_copy_scan_lines(prn_dev, lnum, in, line_size);
193       if(write(printer_file, in, line_size) != line_size)
194 	{
195 	  perror("Writting to output");
196 	  return_error(gs_error_ioerror);
197 	}
198     }
199  retry:
200   if(ioctl(printer_file, LBIOCSTART, 0) < 0)
201     {
202       if(analyze_error(printer_file))
203 	goto retry;
204       perror("Starting print");
205       return_error(gs_error_ioerror);
206     }
207   gs_free(in, line_size, 1, "nwp533_output_page(in)");
208 
209   return 0;
210 }
211