]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
f7faf1f5 | 2 | * "$Id: scsi-linux.c 5241 2006-03-07 22:07:44Z mike $" |
ef416fc2 | 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 | ||
757d2cad | 119 | fputs("STATE: +connecting-to-device\n", stderr); |
120 | ||
ef416fc2 | 121 | do |
122 | { | |
123 | if ((scsi_fd = open(resource, O_RDWR | O_EXCL)) == -1) | |
124 | { | |
125 | if (getenv("CLASS") != NULL) | |
126 | { | |
127 | /* | |
128 | * If the CLASS environment variable is set, the job was submitted | |
129 | * to a class and not to a specific queue. In this case, we want | |
130 | * to abort immediately so that the job can be requeued on the next | |
131 | * available printer in the class. | |
132 | */ | |
133 | ||
134 | fputs("INFO: Unable to open SCSI device, queuing on next printer in class...\n", | |
135 | stderr); | |
136 | ||
137 | /* | |
138 | * Sleep 5 seconds to keep the job from requeuing too rapidly... | |
139 | */ | |
140 | ||
141 | sleep(5); | |
142 | ||
143 | return (CUPS_BACKEND_FAILED); | |
144 | } | |
145 | ||
146 | if (errno != EAGAIN && errno != EBUSY) | |
147 | { | |
148 | fprintf(stderr, "ERROR: Unable to open SCSI device \"%s\" - %s\n", | |
149 | resource, strerror(errno)); | |
150 | return (CUPS_BACKEND_FAILED); | |
151 | } | |
152 | else | |
153 | { | |
154 | fprintf(stderr, "INFO: SCSI device \"%s\" busy; retrying...\n", | |
155 | resource); | |
156 | sleep(30); | |
157 | } | |
158 | } | |
159 | } | |
160 | while (scsi_fd == -1); | |
161 | ||
757d2cad | 162 | fputs("STATE: -connecting-to-device\n", stderr); |
163 | ||
ef416fc2 | 164 | /* |
165 | * Now that we are "connected" to the port, ignore SIGTERM so that we | |
166 | * can finish out any page data the driver sends (e.g. to eject the | |
167 | * current page... Only ignore SIGTERM if we are printing data from | |
168 | * stdin (otherwise you can't cancel raw jobs...) | |
169 | */ | |
170 | ||
171 | if (fd != 0) | |
172 | { | |
173 | # ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */ | |
174 | sigset(SIGTERM, SIG_IGN); | |
175 | # elif defined(HAVE_SIGACTION) | |
176 | memset(&action, 0, sizeof(action)); | |
177 | ||
178 | sigemptyset(&action.sa_mask); | |
179 | action.sa_handler = SIG_IGN; | |
180 | sigaction(SIGTERM, &action, NULL); | |
181 | # else | |
182 | signal(SIGTERM, SIG_IGN); | |
183 | # endif /* HAVE_SIGSET */ | |
184 | } | |
185 | ||
186 | /* | |
187 | * Copy the print file to the device... | |
188 | */ | |
189 | ||
190 | while (copies > 0) | |
191 | { | |
192 | if (fd != 0) | |
193 | lseek(fd, 0, SEEK_SET); | |
194 | ||
195 | while ((bytes = read(fd, buffer, sizeof(buffer))) > 0) | |
196 | { | |
197 | memset(&scsi_req, 0, sizeof(scsi_req)); | |
198 | ||
199 | scsi_req.interface_id = 'S'; | |
200 | scsi_req.dxfer_direction = SG_DXFER_TO_DEV; | |
201 | scsi_req.cmd_len = 6; | |
202 | scsi_req.mx_sb_len = sizeof(scsi_sense); | |
203 | scsi_req.iovec_count = 0; | |
204 | scsi_req.dxfer_len = bytes; | |
205 | scsi_req.dxferp = buffer; | |
206 | scsi_req.cmdp = scsi_cmd; | |
207 | scsi_req.sbp = scsi_sense; | |
208 | scsi_req.timeout = 60 * 1000; | |
209 | ||
210 | scsi_cmd[0] = 0x0a; /* Group 0 print command */ | |
211 | scsi_cmd[1] = 0x00; | |
212 | scsi_cmd[2] = bytes / 65536; | |
213 | scsi_cmd[3] = bytes / 256; | |
214 | scsi_cmd[4] = bytes; | |
215 | scsi_cmd[5] = 0x00; | |
216 | ||
217 | for (try = 0; try < 10; try ++) | |
218 | if (ioctl(scsi_fd, SG_IO, &scsi_req) < 0 || | |
219 | scsi_req.status != 0) | |
220 | { | |
221 | fprintf(stderr, "WARNING: SCSI command timed out (%d); retrying...\n", | |
222 | scsi_req.status); | |
223 | sleep(try + 1); | |
224 | } | |
225 | else | |
226 | break; | |
227 | ||
228 | if (try >= 10) | |
229 | { | |
230 | fprintf(stderr, "ERROR: Unable to send print data (%d)\n", | |
231 | scsi_req.status); | |
232 | close(scsi_fd); | |
233 | return (CUPS_BACKEND_FAILED); | |
234 | } | |
235 | } | |
236 | ||
237 | copies --; | |
238 | } | |
239 | ||
240 | /* | |
241 | * Close the device and return... | |
242 | */ | |
243 | ||
244 | close(fd); | |
245 | ||
246 | return (CUPS_BACKEND_OK); | |
247 | } | |
248 | #endif /* !SG_DXFER_TO_DEV */ | |
249 | ||
250 | ||
251 | /* | |
f7faf1f5 | 252 | * End of "$Id: scsi-linux.c 5241 2006-03-07 22:07:44Z mike $". |
ef416fc2 | 253 | */ |