]> git.ipfire.org Git - thirdparty/cups.git/blob - backend/scsi-linux.c
Load cups into easysw/current.
[thirdparty/cups.git] / backend / scsi-linux.c
1 /*
2 * "$Id: scsi-linux.c 4703 2005-09-26 19:33:58Z mike $"
3 *
4 * Linux 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 <scsi/sg.h>
58
59
60 /*
61 * We currently only support the Linux 2.4 generic SCSI interface.
62 */
63
64 #ifndef SG_DXFER_TO_DEV
65 /*
66 * Dummy functions that do nothing on unsupported platforms...
67 */
68 void list_devices(void) {}
69 int print_device(const char *resource, int fd, int copies) { return (1); }
70 #else
71
72
73 /*
74 * 'list_devices()' - List the available SCSI printer devices.
75 */
76
77 void
78 list_devices(void)
79 {
80 puts("direct scsi \"Unknown\" \"SCSI Printer\"");
81 }
82
83
84 /*
85 * 'print_device()' - Print a file to a SCSI device.
86 */
87
88 int /* O - Print status */
89 print_device(const char *resource, /* I - SCSI device */
90 int fd, /* I - File to print */
91 int copies) /* I - Number of copies to print */
92 {
93 int scsi_fd; /* SCSI file descriptor */
94 char buffer[8192]; /* Data buffer */
95 int bytes; /* Number of bytes */
96 int try; /* Current try */
97 sg_io_hdr_t scsi_req; /* SCSI request */
98 unsigned char scsi_cmd[6], /* SCSI command data */
99 scsi_sense[32]; /* SCSI sense data */
100 # if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
101 struct sigaction action; /* Actions for POSIX signals */
102 # endif /* HAVE_SIGACTION && !HAVE_SIGSET */
103
104
105 /*
106 * Make sure we have a valid resource name...
107 */
108
109 if (strncmp(resource, "/dev/sg", 7) != 0)
110 {
111 fprintf(stderr, "ERROR: Bad SCSI device file \"%s\"!\n", resource);
112 return (CUPS_BACKEND_STOP);
113 }
114
115 /*
116 * Open the SCSI device file...
117 */
118
119 do
120 {
121 if ((scsi_fd = open(resource, O_RDWR | O_EXCL)) == -1)
122 {
123 if (getenv("CLASS") != NULL)
124 {
125 /*
126 * If the CLASS environment variable is set, the job was submitted
127 * to a class and not to a specific queue. In this case, we want
128 * to abort immediately so that the job can be requeued on the next
129 * available printer in the class.
130 */
131
132 fputs("INFO: Unable to open SCSI device, queuing on next printer in class...\n",
133 stderr);
134
135 /*
136 * Sleep 5 seconds to keep the job from requeuing too rapidly...
137 */
138
139 sleep(5);
140
141 return (CUPS_BACKEND_FAILED);
142 }
143
144 if (errno != EAGAIN && errno != EBUSY)
145 {
146 fprintf(stderr, "ERROR: Unable to open SCSI device \"%s\" - %s\n",
147 resource, strerror(errno));
148 return (CUPS_BACKEND_FAILED);
149 }
150 else
151 {
152 fprintf(stderr, "INFO: SCSI device \"%s\" busy; retrying...\n",
153 resource);
154 sleep(30);
155 }
156 }
157 }
158 while (scsi_fd == -1);
159
160 /*
161 * Now that we are "connected" to the port, ignore SIGTERM so that we
162 * can finish out any page data the driver sends (e.g. to eject the
163 * current page... Only ignore SIGTERM if we are printing data from
164 * stdin (otherwise you can't cancel raw jobs...)
165 */
166
167 if (fd != 0)
168 {
169 # ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
170 sigset(SIGTERM, SIG_IGN);
171 # elif defined(HAVE_SIGACTION)
172 memset(&action, 0, sizeof(action));
173
174 sigemptyset(&action.sa_mask);
175 action.sa_handler = SIG_IGN;
176 sigaction(SIGTERM, &action, NULL);
177 # else
178 signal(SIGTERM, SIG_IGN);
179 # endif /* HAVE_SIGSET */
180 }
181
182 /*
183 * Copy the print file to the device...
184 */
185
186 while (copies > 0)
187 {
188 if (fd != 0)
189 lseek(fd, 0, SEEK_SET);
190
191 while ((bytes = read(fd, buffer, sizeof(buffer))) > 0)
192 {
193 memset(&scsi_req, 0, sizeof(scsi_req));
194
195 scsi_req.interface_id = 'S';
196 scsi_req.dxfer_direction = SG_DXFER_TO_DEV;
197 scsi_req.cmd_len = 6;
198 scsi_req.mx_sb_len = sizeof(scsi_sense);
199 scsi_req.iovec_count = 0;
200 scsi_req.dxfer_len = bytes;
201 scsi_req.dxferp = buffer;
202 scsi_req.cmdp = scsi_cmd;
203 scsi_req.sbp = scsi_sense;
204 scsi_req.timeout = 60 * 1000;
205
206 scsi_cmd[0] = 0x0a; /* Group 0 print command */
207 scsi_cmd[1] = 0x00;
208 scsi_cmd[2] = bytes / 65536;
209 scsi_cmd[3] = bytes / 256;
210 scsi_cmd[4] = bytes;
211 scsi_cmd[5] = 0x00;
212
213 for (try = 0; try < 10; try ++)
214 if (ioctl(scsi_fd, SG_IO, &scsi_req) < 0 ||
215 scsi_req.status != 0)
216 {
217 fprintf(stderr, "WARNING: SCSI command timed out (%d); retrying...\n",
218 scsi_req.status);
219 sleep(try + 1);
220 }
221 else
222 break;
223
224 if (try >= 10)
225 {
226 fprintf(stderr, "ERROR: Unable to send print data (%d)\n",
227 scsi_req.status);
228 close(scsi_fd);
229 return (CUPS_BACKEND_FAILED);
230 }
231 }
232
233 copies --;
234 }
235
236 /*
237 * Close the device and return...
238 */
239
240 close(fd);
241
242 return (CUPS_BACKEND_OK);
243 }
244 #endif /* !SG_DXFER_TO_DEV */
245
246
247 /*
248 * End of "$Id: scsi-linux.c 4703 2005-09-26 19:33:58Z mike $".
249 */