xref: /plan9/sys/src/cmd/gs/src/gdevn533.c (revision 593dc095aefb2a85c828727bbfa9da139a49bdf4)
1 /* Copyright (C) 1989, 1990, 1991, 1993, 1996 Aladdin Enterprises.  All rights reserved.
2 
3   This software is provided AS-IS with no warranty, either express or
4   implied.
5 
6   This software is distributed under license and may not be copied,
7   modified or distributed except as expressly authorized under the terms
8   of the license contained in the file LICENSE in this distribution.
9 
10   For more information about licensing, please refer to
11   http://www.ghostscript.com/licensing/. For information on
12   commercial licensing, go to http://www.artifex.com/licensing/ or
13   contact Artifex Software, Inc., 101 Lucas Valley Road #110,
14   San Rafael, CA  94903, U.S.A., +1(415)492-9861.
15 */
16 
17 /* $Id: gdevn533.c,v 1.6 2004/08/10 13:02:36 stefan Exp $*/
18 /* Sony NWP-533 driver for GhostScript */
19 #include "gdevprn.h"
20 #define prn_dev ((gx_device_printer *)dev) /* needed in 5.31 et seq */
21 #include <sys/ioctl.h>
22 #include <newsiop/lbp.h>
23 
24 /***
25  *** Note: this driver was contributed by a user, Tero Kivinen:
26  ***       please contact kivinen@joker.cs.hut.fi if you have questions.
27  ***/
28 
29 #define A4_PAPER 1
30 
31 #ifdef A4_PAPER
32 #define PAPER_XDOTS A4_XDOTS
33 #define PAPER_YDOTS A4_YDOTS
34 #else
35 #define PAPER_XDOTS B4_XDOTS
36 #define PAPER_YDOTS B4_YDOTS
37 #endif
38 
39 #ifndef TRUE
40 #define TRUE 1
41 #endif
42 #ifndef FALSE
43 #define FALSE 0
44 #endif
45 
46 /* The device descriptor */
47 private dev_proc_open_device(nwp533_open);
48 private dev_proc_print_page(nwp533_print_page);
49 private dev_proc_close_device(nwp533_close);
50 private gx_device_procs nwp533_procs =
51   prn_procs(nwp533_open, gdev_prn_output_page, nwp533_close);
52 
53 const gx_device_printer far_data gs_nwp533_device =
54   prn_device(nwp533_procs, "nwp533",
55 	PAPER_XDOTS * 10.0 / DPI,	/* width_10ths */
56 	PAPER_YDOTS * 10.0 / DPI,	/* height_10ths */
57 	DPI,				/* x_dpi */
58 	DPI,				/* y_dpi */
59 	0,0,0,0,			/* margins */
60 	1, nwp533_print_page);
61 
62 /* return True if should retry - False if should quit */
63 private int
analyze_error(int printer_file)64 analyze_error(int printer_file)
65 {
66   struct lbp_stat status;
67   char *detail = NULL, *old_detail = NULL;
68   int waiting = TRUE;
69   int retry_after_return = TRUE;
70 
71   if(ioctl(printer_file, LBIOCRESET, 0) < 0)
72     {
73       perror("ioctl(LBIOCRESET)");
74       return FALSE;
75     }
76   if (ioctl(printer_file, LBIOCSTATUS, &status) < 0)
77     {
78       perror("ioctl(LBIOCSTATUS)");
79       return FALSE;
80     }
81 
82   do
83     {
84       /* Is there an error */
85       if(status.stat[0] & (ST0_CALL | ST0_REPRINT_REQ | ST0_WAIT | ST0_PAUSE))
86 	{
87 	  if(status.stat[1] & ST1_NO_CARTRIGE)/* mispelled? */
88 	    detail = "No cartridge - waiting";
89 	  else if(status.stat[1] & ST1_NO_PAPER)
90 	    detail = "Out of paper - waiting";
91 	  else if(status.stat[1] & ST1_JAM)
92 	    detail = "Paper jam - waiting";
93 	  else if(status.stat[1] & ST1_OPEN)
94 	    detail = "Door open - waiting";
95 	  else if(status.stat[1] & ST1_TEST)
96 	    detail = "Test printing - waiting";
97 	  else {
98 	    waiting = FALSE;
99 	    retry_after_return = FALSE;
100 
101 	    if(status.stat[2] & ST2_FIXER)
102 	      detail = "Fixer trouble - quiting";
103 	    else if(status.stat[2] & ST2_SCANNER)
104 	      detail = "Scanner trouble - quiting";
105 	    else if(status.stat[2] & ST2_MOTOR)
106 	      detail = "Scanner motor trouble - quiting";
107 	    else if(status.stat[5] & ST5_NO_TONER)
108 	      detail = "No toner - quiting";
109 	  }
110 	}
111       else
112 	{
113 	  waiting = FALSE;
114 	}
115       if(detail != NULL && detail != old_detail)
116 	{
117 	  perror(detail);
118 	  old_detail = detail;
119 	}
120       if(waiting)
121 	{
122 	  ioctl(1, LBIOCRESET, 0);
123 	  sleep(5);
124 	  ioctl(1, LBIOCSTATUS, &status);
125 	}
126     }
127   while(waiting);
128   return retry_after_return;
129 }
130 
131 private int
nwp533_open(gx_device * dev)132 nwp533_open(gx_device *dev)
133 {
134   gx_device_printer *pdev = (gx_device_printer *) dev;
135 
136   if (pdev->fname[0] == '\0')
137     {
138       strcpy(pdev->fname, "/dev/lbp");
139     }
140   return gdev_prn_open(dev);
141 }
142 
143 private int
nwp533_close(gx_device * dev)144 nwp533_close(gx_device *dev)
145 {
146   if (((gx_device_printer *) dev)->file != NULL)
147     {
148       int printer_file;
149 
150       printer_file = fileno(((gx_device_printer *) dev)->file);
151     restart2:
152       if(ioctl(printer_file, LBIOCSTOP, 0) < 0)
153 	{
154 	  if(analyze_error(printer_file))
155 	    goto restart2;
156 	  perror("Waiting for device");
157 	  return_error(gs_error_ioerror);
158 	}
159     }
160   return gdev_prn_close(dev);
161 }
162 
163 /* Send the page to the printer. */
164 private int
nwp533_print_page(gx_device_printer * dev,FILE * prn_stream)165 nwp533_print_page(gx_device_printer *dev, FILE *prn_stream)
166 {
167   int lnum;
168   int line_size = gdev_mem_bytes_per_scan_line(dev);
169   byte *in;
170   int printer_file;
171   printer_file = fileno(prn_stream);
172 
173   if (line_size % 4 != 0)
174     {
175       line_size += 4 - (line_size % 4);
176     }
177   in = (byte *) gs_malloc(dev->memory, line_size, 1, "nwp533_output_page(in)");
178  restart:
179   if(ioctl(printer_file, LBIOCSTOP, 0) < 0)
180     {
181       if(analyze_error(printer_file))
182 	goto restart;
183       perror("Waiting for device");
184       return_error(gs_error_ioerror);
185     }
186   lseek(printer_file, 0, 0);
187 
188   for ( lnum = 0; lnum < dev->height; lnum++)
189     {
190       gdev_prn_copy_scan_lines(prn_dev, lnum, in, line_size);
191       if(write(printer_file, in, line_size) != line_size)
192 	{
193 	  perror("Writting to output");
194 	  return_error(gs_error_ioerror);
195 	}
196     }
197  retry:
198   if(ioctl(printer_file, LBIOCSTART, 0) < 0)
199     {
200       if(analyze_error(printer_file))
201 	goto retry;
202       perror("Starting print");
203       return_error(gs_error_ioerror);
204     }
205   gs_free(dev->memory, in, line_size, 1, "nwp533_output_page(in)");
206 
207   return 0;
208 }
209