]> git.ipfire.org Git - thirdparty/cups.git/blob - backend/scsi-irix.c
Import CUPS 1.4svn r7023 into easysw/current.
[thirdparty/cups.git] / backend / scsi-irix.c
1 /*
2 * "$Id: scsi-irix.c 6834 2007-08-22 18:29:25Z mike $"
3 *
4 * IRIX SCSI printer support for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 2007 by Apple Inc.
7 * Copyright 2003-2005 by Easy Software Products, all rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or
10 * without modification, are permitted provided that the
11 * following conditions are met:
12 *
13 * 1. Redistributions of source code must retain the above
14 * copyright notice, this list of conditions and the
15 * following disclaimer.
16 *
17 * 2. Redistributions in binary form must reproduce the
18 * above copyright notice, this list of conditions and
19 * the following disclaimer in the documentation and/or
20 * other materials provided with the distribution.
21 *
22 * 3. All advertising materials mentioning features or use
23 * of this software must display the following
24 * acknowledgement:
25 *
26 * This product includes software developed by Easy
27 * Software Products.
28 *
29 * 4. The name of Easy Software Products may not be used to
30 * endorse or promote products derived from this software
31 * without specific prior written permission.
32 *
33 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS
34 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
35 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
36 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
37 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS
38 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
39 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
41 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
43 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
44 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
45 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
46 * DAMAGE.
47 *
48 * Contents:
49 *
50 * list_devices() - List the available SCSI printer devices.
51 * print_device() - Print a file to a SCSI device.
52 */
53
54 /*
55 * Include necessary headers.
56 */
57
58 #include <bstring.h> /* memcpy() and friends */
59 #include <sys/dsreq.h> /* SCSI interface stuff */
60
61
62 /*
63 * 'list_devices()' - List the available SCSI printer devices.
64 */
65
66 void
67 list_devices(void)
68 {
69 puts("direct scsi \"Unknown\" \"SCSI Printer\"");
70 }
71
72
73 /*
74 * 'print_device()' - Print a file to a SCSI device.
75 */
76
77 int /* O - Print status */
78 print_device(const char *resource, /* I - SCSI device */
79 int fd, /* I - File to print */
80 int copies) /* I - Number of copies to print */
81 {
82 int scsi_fd; /* SCSI file descriptor */
83 char buffer[8192]; /* Data buffer */
84 int bytes; /* Number of bytes */
85 int try; /* Current try */
86 dsreq_t scsi_req; /* SCSI request */
87 char scsi_cmd[6]; /* SCSI command data */
88 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
89 struct sigaction action; /* Actions for POSIX signals */
90 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
91
92
93 /*
94 * Make sure we have a valid resource name...
95 */
96
97 if (strncmp(resource, "/dev/scsi/", 10) != 0)
98 {
99 _cupsLangPrintf(stderr, _("ERROR: Bad SCSI device file \"%s\"!\n"),
100 resource);
101 return (CUPS_BACKEND_STOP);
102 }
103
104 /*
105 * Open the SCSI device file...
106 */
107
108 fputs("STATE: +connecting-to-device\n", stderr);
109
110 do
111 {
112 if ((scsi_fd = open(resource, O_RDWR | O_EXCL)) == -1)
113 {
114 if (getenv("CLASS") != NULL)
115 {
116 /*
117 * If the CLASS environment variable is set, the job was submitted
118 * to a class and not to a specific queue. In this case, we want
119 * to abort immediately so that the job can be requeued on the next
120 * available printer in the class.
121 */
122
123 _cupsLangPuts(stderr,
124 _("INFO: Unable to contact printer, queuing on next "
125 "printer in class...\n"));
126
127 /*
128 * Sleep 5 seconds to keep the job from requeuing too rapidly...
129 */
130
131 sleep(5);
132
133 return (1);
134 }
135
136 if (errno != EAGAIN && errno != EBUSY)
137 {
138 _cupsLangPrintf(stderr,
139 _("ERROR: Unable to open device file \"%s\": %s\n"),
140 resource, strerror(errno));
141 return (CUPS_BACKEND_FAILED);
142 }
143 else
144 {
145 _cupsLangPuts(stderr,
146 _("INFO: Printer busy; will retry in 30 seconds...\n"));
147 sleep(30);
148 }
149 }
150 }
151 while (scsi_fd == -1);
152
153 fputs("STATE: -connecting-to-device\n", stderr);
154
155 /*
156 * Now that we are "connected" to the port, ignore SIGTERM so that we
157 * can finish out any page data the driver sends (e.g. to eject the
158 * current page... Only ignore SIGTERM if we are printing data from
159 * stdin (otherwise you can't cancel raw jobs...)
160 */
161
162 if (fd != 0)
163 {
164 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
165 sigset(SIGTERM, SIG_IGN);
166 #elif defined(HAVE_SIGACTION)
167 memset(&action, 0, sizeof(action));
168
169 sigemptyset(&action.sa_mask);
170 action.sa_handler = SIG_IGN;
171 sigaction(SIGTERM, &action, NULL);
172 #else
173 signal(SIGTERM, SIG_IGN);
174 #endif /* HAVE_SIGSET */
175 }
176
177 /*
178 * Copy the print file to the device...
179 */
180
181 while (copies > 0)
182 {
183 if (fd != 0)
184 lseek(fd, 0, SEEK_SET);
185
186 while ((bytes = read(fd, buffer, sizeof(buffer))) > 0)
187 {
188 memset(&scsi_req, 0, sizeof(scsi_req));
189
190 scsi_req.ds_flags = DSRQ_WRITE;
191 scsi_req.ds_time = 60 * 1000;
192 scsi_req.ds_cmdbuf = scsi_cmd;
193 scsi_req.ds_cmdlen = 6;
194 scsi_req.ds_databuf = buffer;
195 scsi_req.ds_datalen = bytes;
196
197 scsi_cmd[0] = 0x0a; /* Group 0 print command */
198 scsi_cmd[1] = 0x00;
199 scsi_cmd[2] = bytes / 65536;
200 scsi_cmd[3] = bytes / 256;
201 scsi_cmd[4] = bytes;
202 scsi_cmd[5] = 0x00;
203
204 for (try = 0; try < 10; try ++)
205 if (ioctl(scsi_fd, DS_ENTER, &scsi_req) < 0 ||
206 scsi_req.ds_status != 0)
207 {
208 _cupsLangPrintf(stderr,
209 _("WARNING: SCSI command timed out (%d); "
210 "retrying...\n"), scsi_req.ds_status);
211 sleep(try + 1);
212 }
213 else
214 break;
215
216 if (try >= 10)
217 {
218 _cupsLangPrintf(stderr, _("ERROR: Unable to send print data (%d)\n"),
219 scsi_req.ds_status);
220 close(scsi_fd);
221 return (CUPS_BACKEND_FAILED);
222 }
223 }
224
225 copies --;
226 }
227
228 /*
229 * Close the device and return...
230 */
231
232 close(fd);
233
234 return (CUPS_BACKEND_OK);
235 }
236
237
238 /*
239 * End of "$Id: scsi-irix.c 6834 2007-08-22 18:29:25Z mike $".
240 */