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