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