summaryrefslogtreecommitdiff
path: root/backend/ricoh-scsi.c
blob: 73bd4fa0130030c8652975aef5d72c89befeb60a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/* sane - Scanner Access Now Easy.
   Copyright (C) 1998 F.W. Dillema (dillema@acm.org)

   This file is part of the SANE package.

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston,
   MA 02111-1307, USA.

   As a special exception, the authors of SANE give permission for
   additional uses of the libraries contained in this release of SANE.

   The exception is that, if you link a SANE library with other files
   to produce an executable, this does not by itself cause the
   resulting executable to be covered by the GNU General Public
   License.  Your use of that executable is in no way restricted on
   account of linking the SANE library code into it.

   This exception does not, however, invalidate any other reasons why
   the executable file might be covered by the GNU General Public
   License.

   If you submit changes to SANE to the maintainers to be included in
   a subsequent release, you agree by submitting the changes that
   those changes may be distributed with this exception intact.

   If you write modifications of your own for SANE, it is your choice
   whether to permit this exception to apply to your modifications.
   If you do not wish that, delete this exception notice. */

/*
	$Id$
	This file implements the low-level scsi-commands.
*/




/* SCSI commands that the Ricoh scanners understand: */
#define RICOH_SCSI_TEST_UNIT_READY	0x00
#define RICOH_SCSI_SET_WINDOW	        0x24
#define RICOH_SCSI_GET_WINDOW	        0x25
#define RICOH_SCSI_READ_SCANNED_DATA	0x28
#define RICOH_SCSI_INQUIRY		0x12
#define RICOH_SCSI_MODE_SELECT		0x15
#define RICOH_SCSI_START_SCAN		0x1b
#define RICOH_SCSI_MODE_SENSE		0x1a
#define RICOH_SCSI_GET_BUFFER_STATUS	0x34
#define RICOH_SCSI_OBJECT_POSITION      0x31

/* How long do we wait for scanner to have data for us */
#define MAX_WAITING_TIME       15

struct scsi_window_cmd {
        SANE_Byte opcode;
        SANE_Byte byte2;
        SANE_Byte reserved[4];
        SANE_Byte len[3];
        SANE_Byte control;
};

struct scsi_mode_select_cmd {
        SANE_Byte opcode;
        SANE_Byte byte2;
#define SMS_SP  0x01
#define SMS_PF  0x10
        SANE_Byte page_code; /* for mode_sense, reserved for mode_select */
        SANE_Byte unused[1];
        SANE_Byte len;
        SANE_Byte control;
};

struct scsi_mode_header {
         SANE_Byte data_length;   /* Sense data length */
         SANE_Byte medium_type;
         SANE_Byte dev_spec;
         SANE_Byte blk_desc_len;
};

struct scsi_get_buffer_status_cmd {
        SANE_Byte opcode;
        SANE_Byte byte2;
        SANE_Byte res[5];
        SANE_Byte len[2];
        SANE_Byte control;
};

struct scsi_status_desc {
        SANE_Byte window_id;
        SANE_Byte byte2;
        SANE_Byte available[3];
        SANE_Byte filled[3];
};

struct scsi_status_data {
        SANE_Byte len[3];
        SANE_Byte byte4;
        struct scsi_status_desc desc;
};

struct scsi_start_scan_cmd {
        SANE_Byte opcode;
        SANE_Byte byte2;
        SANE_Byte unused[2];
        SANE_Byte len;
        SANE_Byte control;
};

struct scsi_read_scanner_cmd {
        SANE_Byte opcode;
        SANE_Byte byte2;
        SANE_Byte data_type;
        SANE_Byte byte3;
        SANE_Byte data_type_qualifier[2];
        SANE_Byte len[3];
        SANE_Byte control;
};

static SANE_Status
test_unit_ready (int fd)
{
  static SANE_Byte cmd[6];
  SANE_Status status;
  DBG (11, ">> test_unit_ready\n");

  cmd[0] = RICOH_SCSI_TEST_UNIT_READY;
  memset (cmd, 0, sizeof (cmd));
  status = sanei_scsi_cmd (fd, cmd, sizeof (cmd), 0, 0);

  DBG (11, "<< test_unit_ready\n");
  return (status);
}

static SANE_Status
inquiry (int fd, void *buf, size_t  * buf_size)
{
  static SANE_Byte cmd[6];
  SANE_Status status;
  DBG (11, ">> inquiry\n");

  memset (cmd, 0, sizeof (cmd));
  cmd[0] = RICOH_SCSI_INQUIRY;
  cmd[4] = *buf_size;
  status = sanei_scsi_cmd (fd, cmd, sizeof (cmd), buf, buf_size);

  DBG (11, "<< inquiry\n");
  return (status);
}

static SANE_Status
mode_select (int fd, struct mode_pages *mp)
{
  static struct {
    struct scsi_mode_select_cmd cmd;
    struct scsi_mode_header smh;
    struct mode_pages mp;
  } select_cmd;
  SANE_Status status;
  DBG (11, ">> mode_select\n");

  memset (&select_cmd, 0, sizeof (select_cmd));
  select_cmd.cmd.opcode = RICOH_SCSI_MODE_SELECT;
  select_cmd.cmd.byte2 |= SMS_PF;
  select_cmd.cmd.len = sizeof(select_cmd.smh) + sizeof(select_cmd.mp);
  memcpy (&select_cmd.mp, mp, sizeof(*mp));
  status = sanei_scsi_cmd (fd, &select_cmd, sizeof (select_cmd), 0, 0);

  DBG (11, "<< mode_select\n");
  return (status);
}

#if 0
static SANE_Status
mode_sense (int fd, struct mode_pages *mp, SANE_Byte page_code)
{
  static struct scsi_mode_select_cmd cmd; /* no type, we can reuse it for sensing */
  static struct {
    struct scsi_mode_header smh;
    struct mode_pages mp;
  } select_data;
  static size_t select_size = sizeof(select_data);
  SANE_Status status;
  DBG (11, ">> mode_sense\n");

  memset (&cmd, 0, sizeof (cmd));
  cmd.opcode = RICOH_SCSI_MODE_SENSE;
  cmd.page_code = page_code;
  cmd.len =  sizeof(select_data);
  status = sanei_scsi_cmd (fd, &cmd, sizeof (cmd), &select_data, &select_size);
  memcpy (mp, &select_data.mp, sizeof(*mp));

  DBG (11, "<< mode_sense\n");
  return (status);
}
#endif

static SANE_Status
trigger_scan (int fd)
{
  static struct scsi_start_scan_cmd cmd;
  static char   window_id_list[1] = { '\0' }; /* scan start data out */
  static size_t wl_size = 1;
  SANE_Status status;
  DBG (11, ">> trigger scan\n");

  memset (&cmd, 0, sizeof (cmd));
  cmd.opcode = RICOH_SCSI_START_SCAN;
  cmd.len = wl_size;
  if (wl_size)
    status = sanei_scsi_cmd (fd, &cmd, sizeof (cmd), &window_id_list, &wl_size);
  else
    status = sanei_scsi_cmd (fd, &cmd, sizeof (cmd), 0, 0);

  DBG (11, "<< trigger scan\n");
  return (status);
}

static SANE_Status
set_window (int fd, struct ricoh_window_data *rwd)
{

  static struct {
    struct scsi_window_cmd cmd;
    struct ricoh_window_data rwd;
  } win;

  SANE_Status status;
  DBG (11, ">> set_window\n");

  memset (&win, 0, sizeof (win));
  win.cmd.opcode = RICOH_SCSI_SET_WINDOW;
  _lto3b(sizeof(*rwd), win.cmd.len);
  memcpy (&win.rwd, rwd, sizeof(*rwd));
  status = sanei_scsi_cmd (fd, &win, sizeof (win), 0, 0);

  DBG (11, "<< set_window\n");
  return (status);
}

static SANE_Status
get_window (int fd, struct ricoh_window_data *rwd)
{

  static struct scsi_window_cmd cmd;
  static size_t rwd_size;
  SANE_Status status;

  rwd_size = sizeof(*rwd);
  DBG (11, ">> get_window datalen = %lu\n", (unsigned long) rwd_size);

  memset (&cmd, 0, sizeof (cmd));
  cmd.opcode = RICOH_SCSI_GET_WINDOW;
#if 0
  cmd.byte2 |= (SANE_Byte)0x01; /* set Single bit to get one window desc. */
#endif
  _lto3b(rwd_size, cmd.len);
  status = sanei_scsi_cmd (fd, &cmd, sizeof (cmd), rwd, &rwd_size);

  DBG (11, "<< get_window, datalen = %lu\n", (unsigned long) rwd_size);
  return (status);
}

static SANE_Status
read_data (int fd, void *buf, size_t * buf_size)
{
  static struct scsi_read_scanner_cmd cmd;
  SANE_Status status;
  DBG (11, ">> read_data %lu\n", (unsigned long) *buf_size);

  memset (&cmd, 0, sizeof (cmd));
  cmd.opcode = RICOH_SCSI_READ_SCANNED_DATA;
  _lto3b(*buf_size, cmd.len);
  status = sanei_scsi_cmd (fd, &cmd, sizeof (cmd), buf, buf_size);

  DBG (11, "<< read_data %lu\n", (unsigned long) *buf_size);
  return (status);
}

static SANE_Status
object_position (int fd)
{
  static SANE_Byte cmd[10];
  SANE_Status status;
  DBG (11, ">> object_position\n");

  memset (cmd, 0, sizeof (cmd));
  cmd[0] = RICOH_SCSI_OBJECT_POSITION;
  status = sanei_scsi_cmd (fd, cmd, sizeof (cmd), 0, 0);

  DBG (11, "<< object_position\n");
  return (status);
}

static SANE_Status
get_data_status (int fd, struct scsi_status_desc *dbs)
{
  static struct scsi_get_buffer_status_cmd cmd;
  static struct scsi_status_data ssd;
  size_t ssd_size = sizeof(ssd);
  SANE_Status status;
  DBG (11, ">> get_data_status %lu\n", (unsigned long) ssd_size);

  memset (&cmd, 0, sizeof (cmd));
  cmd.opcode = RICOH_SCSI_GET_BUFFER_STATUS;
  _lto2b(ssd_size, cmd.len);
  status = sanei_scsi_cmd (fd, &cmd, sizeof (cmd), &ssd, &ssd_size);

  memcpy (dbs, &ssd.desc, sizeof(*dbs));
  if (status == SANE_STATUS_GOOD &&
      (((unsigned int) _3btol(ssd.len)) <= sizeof(*dbs) || _3btol(ssd.desc.filled) == 0)) {
    DBG (11, "get_data_status: busy\n");
    status = SANE_STATUS_DEVICE_BUSY;
  }

  DBG (11, "<< get_data_status %lu\n", (unsigned long) ssd_size);
  return (status);
}

#if 0
static SANE_Status
ricoh_wait_ready_tur (int fd)
{
  struct timeval now, start;
  SANE_Status status;

  gettimeofday (&start, 0);

  while (1)
    {
      DBG(3, "scsi_wait_ready: sending TEST_UNIT_READY\n");

      status = sanei_scsi_cmd (fd, test_unit_ready, sizeof (test_unit_ready),
                               0, 0);
      switch (status)
        {
        default:
          /* Ignore errors while waiting for scanner to become ready.
             Some SCSI drivers return EIO while the scanner is
             returning to the home position.  */
          DBG(1, "scsi_wait_ready: test unit ready failed (%s)\n",
              sane_strstatus (status));
          /* fall through */
        case SANE_STATUS_DEVICE_BUSY:
          gettimeofday (&now, 0);
          if (now.tv_sec - start.tv_sec >= MAX_WAITING_TIME)
            {
              DBG(1, "ricoh_wait_ready: timed out after %lu seconds\n",
                  (u_long) (now.tv_sec - start.tv_sec));
              return SANE_STATUS_INVAL;
            }
          usleep (100000);      /* retry after 100ms */
          break;

        case SANE_STATUS_GOOD:
          return status;
        }
    }
  return SANE_STATUS_INVAL;
}
#endif

static SANE_Status
ricoh_wait_ready (Ricoh_Scanner * s)
{
  struct scsi_status_desc dbs;
  time_t now, start;
  SANE_Status status;

  start = time(NULL);

  while (1)
    {
      status = get_data_status (s->fd, &dbs);

      switch (status)
        {
        default:
          /* Ignore errors while waiting for scanner to become ready.
             Some SCSI drivers return EIO while the scanner is
             returning to the home position.  */
          DBG(1, "scsi_wait_ready: get datat status failed (%s)\n",
              sane_strstatus (status));
          /* fall through */
        case SANE_STATUS_DEVICE_BUSY:
          now = time(NULL);
          if (now - start >= MAX_WAITING_TIME)
            {
              DBG(1, "ricoh_wait_ready: timed out after %lu seconds\n",
                  (u_long) (now - start));
              return SANE_STATUS_INVAL;
            }
          break;

        case SANE_STATUS_GOOD:
	  DBG(11, "ricoh_wait_ready: %d bytes ready\n", _3btol(dbs.filled));
	  return status;
	  break;
	}
      usleep (1000000);      /* retry after 100ms */
    }
  return SANE_STATUS_INVAL;
}