1 /* sane - Scanner Access Now Easy.
2
3 This file is part of the SANE package.
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>.
17
18 As a special exception, the authors of SANE give permission for
19 additional uses of the libraries contained in this release of SANE.
20
21 The exception is that, if you link a SANE library with other files
22 to produce an executable, this does not by itself cause the
23 resulting executable to be covered by the GNU General Public
24 License. Your use of that executable is in no way restricted on
25 account of linking the SANE library code into it.
26
27 This exception does not, however, invalidate any other reasons why
28 the executable file might be covered by the GNU General Public
29 License.
30
31 If you submit changes to SANE to the maintainers to be included in
32 a subsequent release, you agree by submitting the changes that
33 those changes may be distributed with this exception intact.
34
35 If you write modifications of your own for SANE, it is your choice
36 whether to permit this exception to apply to your modifications.
37 If you do not wish that, delete this exception notice. */
38
39 /*
40 */
41
42 #include <time.h>
43
44
45 /* SCSI commands that the Ibm scanners understand: */
46 #define IBM_SCSI_TEST_UNIT_READY 0x00
47 #define IBM_SCSI_SET_WINDOW 0x24
48 #define IBM_SCSI_GET_WINDOW 0x25
49 #define IBM_SCSI_READ_SCANNED_DATA 0x28
50 #define IBM_SCSI_INQUIRY 0x12
51 #define IBM_SCSI_MODE_SELECT 0x15
52 #define IBM_SCSI_START_SCAN 0x1b
53 #define IBM_SCSI_MODE_SENSE 0x1a
54 #define IBM_SCSI_GET_BUFFER_STATUS 0x34
55 #define IBM_SCSI_OBJECT_POSITION 0x31
56
57 /* How long do we wait for scanner to have data for us */
58 #define MAX_WAITING_TIME 15
59
60 /* for object_position command */
61 #define OBJECT_POSITION_UNLOAD 0
62 #define OBJECT_POSITION_LOAD 1
63
64 struct scsi_window_cmd {
65 SANE_Byte opcode;
66 SANE_Byte byte2;
67 SANE_Byte reserved[4];
68 SANE_Byte len[3];
69 SANE_Byte control;
70 };
71
72 struct scsi_mode_select_cmd {
73 SANE_Byte opcode;
74 SANE_Byte byte2;
75 #define SMS_SP 0x01
76 #define SMS_PF 0x10
77 SANE_Byte page_code; /* for mode_sense, reserved for mode_select */
78 SANE_Byte unused[1];
79 SANE_Byte len;
80 SANE_Byte control;
81 };
82
83 struct scsi_mode_header {
84 SANE_Byte data_length; /* Sense data length */
85 SANE_Byte medium_type;
86 SANE_Byte dev_spec;
87 SANE_Byte blk_desc_len;
88 };
89
90 /* next struct introduced by mf */
91 struct scsi_object_position_cmd {
92 SANE_Byte opcode;
93 SANE_Byte position_func;
94 SANE_Byte count[3];
95 SANE_Byte res[3];
96 SANE_Byte control;
97 SANE_Byte res2;
98 };
99
100 struct scsi_get_buffer_status_cmd {
101 SANE_Byte opcode;
102 SANE_Byte byte2;
103 SANE_Byte res[5];
104 SANE_Byte len[2];
105 SANE_Byte control;
106 };
107
108 struct scsi_status_desc {
109 SANE_Byte window_id;
110 SANE_Byte byte2;
111 SANE_Byte available[3];
112 SANE_Byte filled[3];
113 };
114
115 struct scsi_status_data {
116 SANE_Byte len[3];
117 SANE_Byte byte4;
118 struct scsi_status_desc desc;
119 };
120
121 struct scsi_start_scan_cmd {
122 SANE_Byte opcode;
123 SANE_Byte byte2;
124 SANE_Byte unused[2];
125 SANE_Byte len;
126 SANE_Byte control;
127 };
128
129 struct scsi_read_scanner_cmd {
130 SANE_Byte opcode;
131 SANE_Byte byte2;
132 SANE_Byte data_type;
133 SANE_Byte byte3;
134 SANE_Byte data_type_qualifier[2];
135 SANE_Byte len[3];
136 SANE_Byte control;
137 };
138
139 static SANE_Status
test_unit_ready(int fd)140 test_unit_ready (int fd)
141 {
142 static SANE_Byte cmd[6];
143 SANE_Status status;
144 DBG (11, ">> test_unit_ready\n");
145
146 cmd[0] = IBM_SCSI_TEST_UNIT_READY;
147 memset (cmd, 0, sizeof (cmd));
148 status = sanei_scsi_cmd (fd, cmd, sizeof (cmd), 0, 0);
149
150 DBG (11, "<< test_unit_ready\n");
151 return (status);
152 }
153
154 static SANE_Status
inquiry(int fd,void * buf,size_t * buf_size)155 inquiry (int fd, void *buf, size_t * buf_size)
156 {
157 static SANE_Byte cmd[6];
158 SANE_Status status;
159 DBG (11, ">> inquiry\n");
160
161 memset (cmd, 0, sizeof (cmd));
162 cmd[0] = IBM_SCSI_INQUIRY;
163 cmd[4] = *buf_size;
164 status = sanei_scsi_cmd (fd, cmd, sizeof (cmd), buf, buf_size);
165
166 DBG (11, "<< inquiry\n");
167 return (status);
168 }
169
170 static SANE_Status
mode_select(int fd,struct mode_pages * mp)171 mode_select (int fd, struct mode_pages *mp)
172 {
173 static struct {
174 struct scsi_mode_select_cmd cmd;
175 struct scsi_mode_header smh;
176 struct mode_pages mp;
177 } select_cmd;
178 SANE_Status status;
179 DBG (11, ">> mode_select\n");
180
181 memset (&select_cmd, 0, sizeof (select_cmd));
182 select_cmd.cmd.opcode = IBM_SCSI_MODE_SELECT;
183 select_cmd.cmd.byte2 |= SMS_PF;
184 select_cmd.cmd.len = sizeof(select_cmd.smh) + sizeof(select_cmd.mp);
185 /* next line by mf */
186 /* select_cmd.cmd.page_code= 20; */
187 memcpy (&select_cmd.mp, mp, sizeof(*mp));
188 status = sanei_scsi_cmd (fd, &select_cmd, sizeof (select_cmd), 0, 0);
189
190 DBG (11, "<< mode_select\n");
191 return (status);
192 }
193
194 #if 0
195 static SANE_Status
196 mode_sense (int fd, struct mode_pages *mp, SANE_Byte page_code)
197 {
198 static struct scsi_mode_select_cmd cmd; /* no type, we can reuse it for sensing */
199 static struct {
200 struct scsi_mode_header smh;
201 struct mode_pages mp;
202 } select_data;
203 static size_t select_size = sizeof(select_data);
204 SANE_Status status;
205 DBG (11, ">> mode_sense\n");
206
207 memset (&cmd, 0, sizeof (cmd));
208 cmd.opcode = IBM_SCSI_MODE_SENSE;
209 cmd.page_code = page_code;
210 cmd.len = sizeof(select_data);
211 status = sanei_scsi_cmd (fd, &cmd, sizeof (cmd), &select_data, &select_size);
212 memcpy (mp, &select_data.mp, sizeof(*mp));
213
214 DBG (11, "<< mode_sense\n");
215 return (status);
216 }
217 #endif
218
219 static SANE_Status
trigger_scan(int fd)220 trigger_scan (int fd)
221 {
222 static struct scsi_start_scan_cmd cmd;
223 static char window_id_list[1] = { '\0' }; /* scan start data out */
224 static size_t wl_size = 1;
225 SANE_Status status;
226 DBG (11, ">> trigger scan\n");
227
228 memset (&cmd, 0, sizeof (cmd));
229 cmd.opcode = IBM_SCSI_START_SCAN;
230 cmd.len = wl_size;
231 /* next line by mf */
232 /* cmd.unused[0] = 1; */
233 if (wl_size)
234 status = sanei_scsi_cmd (fd, &cmd, sizeof (cmd), &window_id_list, &wl_size);
235 else
236 status = sanei_scsi_cmd (fd, &cmd, sizeof (cmd), 0, 0);
237
238 DBG (11, "<< trigger scan\n");
239 return (status);
240 }
241
242 static SANE_Status
set_window(int fd,struct ibm_window_data * iwd)243 set_window (int fd, struct ibm_window_data *iwd)
244 {
245
246 static struct {
247 struct scsi_window_cmd cmd;
248 struct ibm_window_data iwd;
249 } win;
250
251 SANE_Status status;
252 DBG (11, ">> set_window\n");
253
254 memset (&win, 0, sizeof (win));
255 win.cmd.opcode = IBM_SCSI_SET_WINDOW;
256 _lto3b(sizeof(*iwd), win.cmd.len);
257 memcpy (&win.iwd, iwd, sizeof(*iwd));
258 status = sanei_scsi_cmd (fd, &win, sizeof (win), 0, 0);
259
260 DBG (11, "<< set_window\n");
261 return (status);
262 }
263
264 static SANE_Status
get_window(int fd,struct ibm_window_data * iwd)265 get_window (int fd, struct ibm_window_data *iwd)
266 {
267
268 static struct scsi_window_cmd cmd;
269 static size_t iwd_size;
270 SANE_Status status;
271
272 iwd_size = sizeof(*iwd);
273 DBG (11, ">> get_window datalen = %lu\n", (unsigned long) iwd_size);
274
275 memset (&cmd, 0, sizeof (cmd));
276 cmd.opcode = IBM_SCSI_GET_WINDOW;
277 #if 1 /* it was if 0 */
278 cmd.byte2 |= (SANE_Byte)0x01; /* set Single bit to get one window desc. */
279 #endif
280 _lto3b(iwd_size, cmd.len);
281 status = sanei_scsi_cmd (fd, &cmd, sizeof (cmd), iwd, &iwd_size);
282
283 DBG (11, "<< get_window, datalen = %lu\n", (unsigned long) iwd_size);
284 return (status);
285 }
286
287 static SANE_Status
read_data(int fd,void * buf,size_t * buf_size)288 read_data (int fd, void *buf, size_t * buf_size)
289 {
290 static struct scsi_read_scanner_cmd cmd;
291 SANE_Status status;
292 DBG (11, ">> read_data %lu\n", (unsigned long) *buf_size);
293
294 memset (&cmd, 0, sizeof (cmd));
295 cmd.opcode = IBM_SCSI_READ_SCANNED_DATA;
296 _lto3b(*buf_size, cmd.len);
297 status = sanei_scsi_cmd (fd, &cmd, sizeof (cmd), buf, buf_size);
298
299 DBG (11, "<< read_data %lu\n", (unsigned long) *buf_size);
300 return (status);
301 }
302
303 static SANE_Status
object_position(int fd,int load)304 object_position (int fd, int load)
305 {
306 static struct scsi_object_position_cmd cmd;
307 SANE_Status status;
308 DBG (11, ">> object_position\n");
309
310 #if 0
311 /* At least the Ricoh 420 doesn't like that command */
312 DBG (11, "object_position: ignored\n");
313 return SANE_STATUS_GOOD;
314 #endif
315
316 memset (&cmd, 0, sizeof (cmd));
317 cmd.opcode = IBM_SCSI_OBJECT_POSITION;
318 if (load)
319 cmd.position_func = OBJECT_POSITION_LOAD;
320 else
321 cmd.position_func = OBJECT_POSITION_UNLOAD;
322 _lto3b(1, cmd.count);
323 status = sanei_scsi_cmd (fd, &cmd, sizeof (cmd), 0, 0);
324
325 DBG (11, "<< object_position\n");
326 return (status);
327 }
328
329 static SANE_Status
get_data_status(int fd,struct scsi_status_desc * dbs)330 get_data_status (int fd, struct scsi_status_desc *dbs)
331 {
332 static struct scsi_get_buffer_status_cmd cmd;
333 static struct scsi_status_data ssd;
334 size_t ssd_size = sizeof(ssd);
335 SANE_Status status;
336 DBG (11, ">> get_data_status %lu\n", (unsigned long) ssd_size);
337
338 memset (&cmd, 0, sizeof (cmd));
339 cmd.opcode = IBM_SCSI_GET_BUFFER_STATUS;
340 _lto2b(ssd_size, cmd.len);
341 status = sanei_scsi_cmd (fd, &cmd, sizeof (cmd), &ssd, &ssd_size);
342
343 memcpy (dbs, &ssd.desc, sizeof(*dbs));
344 if (status == SANE_STATUS_GOOD &&
345 ((unsigned int) _3btol(ssd.len) <= sizeof(*dbs) || _3btol(ssd.desc.filled) == 0)) {
346 DBG (11, "get_data_status: busy\n");
347 status = SANE_STATUS_DEVICE_BUSY;
348 }
349
350 DBG (11, "<< get_data_status %lu\n", (unsigned long) ssd_size);
351 return (status);
352 }
353
354 #if 0
355 static SANE_Status
356 ibm_wait_ready_tur (int fd)
357 {
358 struct timeval now, start;
359 SANE_Status status;
360
361 gettimeofday (&start, 0);
362
363 while (1)
364 {
365 DBG(3, "scsi_wait_ready: sending TEST_UNIT_READY\n");
366
367 status = sanei_scsi_cmd (fd, test_unit_ready, sizeof (test_unit_ready),
368 0, 0);
369 switch (status)
370 {
371 default:
372 /* Ignore errors while waiting for scanner to become ready.
373 Some SCSI drivers return EIO while the scanner is
374 returning to the home position. */
375 DBG(1, "scsi_wait_ready: test unit ready failed (%s)\n",
376 sane_strstatus (status));
377 /* fall through */
378 case SANE_STATUS_DEVICE_BUSY:
379 gettimeofday (&now, 0);
380 if (now.tv_sec - start.tv_sec >= MAX_WAITING_TIME)
381 {
382 DBG(1, "ibm_wait_ready: timed out after %lu seconds\n",
383 (u_long) (now.tv_sec - start.tv_sec));
384 return SANE_STATUS_INVAL;
385 }
386 usleep (100000); /* retry after 100ms */
387 break;
388
389 case SANE_STATUS_GOOD:
390 return status;
391 }
392 }
393 return SANE_STATUS_INVAL;
394 }
395 #endif
396
397 static SANE_Status
ibm_wait_ready(Ibm_Scanner * s)398 ibm_wait_ready (Ibm_Scanner * s)
399 {
400 struct scsi_status_desc dbs;
401 time_t now, start;
402 SANE_Status status;
403
404 start = time(NULL);
405
406 while (1)
407 {
408 status = get_data_status (s->fd, &dbs);
409
410 switch (status)
411 {
412 default:
413 /* Ignore errors while waiting for scanner to become ready.
414 Some SCSI drivers return EIO while the scanner is
415 returning to the home position. */
416 DBG(1, "scsi_wait_ready: get datat status failed (%s)\n",
417 sane_strstatus (status));
418 /* fall through */
419 case SANE_STATUS_DEVICE_BUSY:
420 now = time(NULL);
421 if (now - start >= MAX_WAITING_TIME)
422 {
423 DBG(1, "ibm_wait_ready: timed out after %lu seconds\n",
424 (u_long) (now - start));
425 return SANE_STATUS_INVAL;
426 }
427 break;
428
429 case SANE_STATUS_GOOD:
430 DBG(11, "ibm_wait_ready: %d bytes ready\n", _3btol(dbs.filled));
431 return status;
432 break;
433 }
434 usleep (1000000); /* retry after 100ms */
435 }
436 return SANE_STATUS_INVAL;
437 }
438