~ubuntu-branches/debian/jessie/sane-backends/jessie

« back to all changes in this revision

Viewing changes to backend/kvs40xx.c

  • Committer: Package Import Robot
  • Author(s): Markus Koschany
  • Date: 2013-07-04 17:41:47 UTC
  • mfrom: (1.1.13)
  • Revision ID: package-import@ubuntu.com-20130704174147-tl5tzd8bwvmctka2
Tags: 1.0.23-1
* QA upload.
* New upstream release.
* This package has been orphaned. Set maintainer to the Debian QA Group.
* Bump compat level to 9 (was 5) and require debhelper >= 9.
* debian/control:
  - sane-utils: Inherit Section: graphics from source package sane-backends.
  - Remove versioned dependencies. They are trivially satisfied.
* Drop the following patches. They are merged upstream now.
  - fix_v4l_build.patch
  - hurd_path_max_fix.patch
  - sane_desc_udev+acl.patch
  - scanimage_man_batch_start.patch
  - udev_usb_suspend.patch
  - xerox_mfp_add_scx_4623fw.patch
  - xerox_mfp_fix_usb_device.patch
* Drop disable_rpath.patch because the RPATH is already removed by chrpath
  in debian/rules.
* Rebase and refresh all other patches against the new upstream relase.
* debian/rules:
  - Simplify debian/rules by using dh sequencer.
  - Build with --parallel and with autotools_dev.
  - Enable all hardening build flags.
  - Install umax_pp with sane-utils.install.
* Build-Depend on libtiff5-dev. Thanks to Michael Terry for the patch.
  (Closes: #681079)
* Build-Depend on libusb-1.0-0-dev and enable libusb1.0 support in
  debian/rules. Thanks to Martin Pitt for the report and Whoopie for the
  patch. (Closes: #687137)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
   Copyright (C) 2009, Panasonic Russia Ltd.
 
3
   Copyright (C) 2010,2011, m. allan noah
 
4
*/
 
5
/*
 
6
   Panasonic KV-S40xx USB-SCSI scanner driver.
 
7
*/
 
8
 
 
9
#include "../include/sane/config.h"
 
10
 
 
11
#include <ctype.h> /*isspace*/
 
12
#include <math.h> /*tan*/
 
13
 
 
14
#include <string.h>
 
15
#include <unistd.h>
 
16
#include <pthread.h>
 
17
#define DEBUG_NOT_STATIC
 
18
#include "../include/sane/sanei_backend.h"
 
19
#include "../include/sane/sane.h"
 
20
#include "../include/sane/saneopts.h"
 
21
#include "../include/sane/sanei.h"
 
22
#include "../include/sane/sanei_config.h"
 
23
#include "../include/sane/sanei_usb.h"
 
24
#include "../include/sane/sanei_scsi.h"
 
25
#include "lassert.h"
 
26
 
 
27
#include "kvs40xx.h"
 
28
 
 
29
#include "sane/sanei_debug.h"
 
30
 
 
31
#define DATA_TAIL 0x200
 
32
 
 
33
struct known_device
 
34
{
 
35
  const SANE_Int id;
 
36
  const SANE_Device scanner;
 
37
};
 
38
 
 
39
static const struct known_device known_devices[] = {
 
40
  {
 
41
   KV_S4085C,
 
42
   {
 
43
    "MATSHITA",
 
44
    "KV-S4085C",
 
45
    "High Speed Color ADF Scanner",
 
46
    "scanner"
 
47
   },
 
48
  },
 
49
  {
 
50
   KV_S4065C,
 
51
   {
 
52
    "MATSHITA",
 
53
    "KV-S4065C",
 
54
    "High Speed Color ADF Scanner",
 
55
    "scanner"
 
56
   },
 
57
  },
 
58
  {
 
59
   KV_S7075C,
 
60
   {
 
61
    "MATSHITA",
 
62
    "KV-S7075C",
 
63
    "High Speed Color ADF Scanner",
 
64
    "scanner"
 
65
   },
 
66
  },
 
67
};
 
68
 
 
69
static inline SANE_Status buf_init(struct buf *b, SANE_Int sz)
 
70
{
 
71
        const int num = sz / BUF_SIZE + 1;
 
72
        b->buf = (u8 **) realloc(b->buf, num * sizeof(u8 *));
 
73
        if (!b->buf)
 
74
                return SANE_STATUS_NO_MEM;
 
75
        memset(b->buf, 0, num * sizeof(void *));
 
76
        b->size = b->head = b->tail = 0;
 
77
        b->sem = 0;
 
78
        b->st = SANE_STATUS_GOOD;
 
79
        pthread_cond_init(&b->cond, NULL);
 
80
        pthread_mutex_init(&b->mu, NULL);
 
81
        return SANE_STATUS_GOOD;
 
82
}
 
83
 
 
84
static inline void buf_deinit(struct buf *b)
 
85
{
 
86
        int i;
 
87
        if (!b->buf)
 
88
                return;
 
89
        for (i = b->head; i < b->tail; i++)
 
90
                if (b->buf[i])
 
91
                        free(b->buf[i]);
 
92
        free(b->buf);
 
93
        b->buf = NULL;
 
94
        b->head = b->tail = 0;
 
95
}
 
96
 
 
97
static inline SANE_Status new_buf(struct buf *b, u8 ** p)
 
98
{
 
99
        b->buf[b->tail] = (u8 *) malloc(BUF_SIZE);
 
100
        if (!b->buf[b->tail])
 
101
                return SANE_STATUS_NO_MEM;
 
102
        *p = b->buf[b->tail];
 
103
        ++b->tail;
 
104
        return SANE_STATUS_GOOD;
 
105
}
 
106
 
 
107
static inline SANE_Status buf_get_err(struct buf *b)
 
108
{
 
109
        return b->size ? SANE_STATUS_GOOD : b->st;
 
110
}
 
111
 
 
112
static inline void buf_set_st(struct buf *b, SANE_Status st)
 
113
{
 
114
        pthread_mutex_lock(&b->mu);
 
115
        b->st = st;
 
116
        if (buf_get_err(b))
 
117
                pthread_cond_signal(&b->cond);
 
118
        pthread_mutex_unlock(&b->mu);
 
119
}
 
120
 
 
121
static inline void buf_cancel(struct buf *b)
 
122
{
 
123
        buf_set_st(b, SANE_STATUS_CANCELLED);
 
124
}
 
125
 
 
126
static inline void push_buf(struct buf *b, SANE_Int sz)
 
127
{
 
128
        pthread_mutex_lock(&b->mu);
 
129
        b->sem++;
 
130
        b->size += sz;
 
131
        pthread_cond_signal(&b->cond);
 
132
        pthread_mutex_unlock(&b->mu);
 
133
}
 
134
 
 
135
static inline u8 *get_buf(struct buf *b, SANE_Int * sz)
 
136
{
 
137
        SANE_Status err = buf_get_err(b);
 
138
        if (err)
 
139
                return NULL;
 
140
 
 
141
        pthread_mutex_lock(&b->mu);
 
142
        while (!b->sem && !buf_get_err(b))
 
143
                pthread_cond_wait(&b->cond, &b->mu);
 
144
        b->sem--;
 
145
        err = buf_get_err(b);
 
146
        if (!err) {
 
147
                *sz = b->size < BUF_SIZE ? b->size : BUF_SIZE;
 
148
                b->size -= *sz;
 
149
        }
 
150
        pthread_mutex_unlock(&b->mu);
 
151
        return err ? NULL : b->buf[b->head];
 
152
}
 
153
 
 
154
static inline void pop_buf(struct buf *b)
 
155
{
 
156
        free(b->buf[b->head]);
 
157
        b->buf[b->head] = NULL;
 
158
        ++b->head;
 
159
}
 
160
 
 
161
SANE_Status
 
162
sane_init (SANE_Int __sane_unused__ * version_code,
 
163
           SANE_Auth_Callback __sane_unused__ authorize)
 
164
{
 
165
  DBG_INIT ();
 
166
  DBG (DBG_INFO, "This is panasonic kvs40xx driver\n");
 
167
 
 
168
  *version_code = SANE_VERSION_CODE (V_MAJOR, V_MINOR, 1);
 
169
 
 
170
  /* Initialize USB */
 
171
  sanei_usb_init ();
 
172
 
 
173
  return SANE_STATUS_GOOD;
 
174
}
 
175
 
 
176
/*
 
177
 * List of available devices, allocated by sane_get_devices, released
 
178
 * by sane_exit()
 
179
 */
 
180
static SANE_Device **devlist = NULL;
 
181
static unsigned curr_scan_dev = 0;
 
182
 
 
183
void
 
184
sane_exit (void)
 
185
{
 
186
  if (devlist)
 
187
    {
 
188
      int i;
 
189
      for (i = 0; devlist[i]; i++)
 
190
        {
 
191
          free ((void *) devlist[i]);
 
192
        }
 
193
      free ((void *) devlist);
 
194
      devlist = NULL;
 
195
    }
 
196
}
 
197
 
 
198
SANE_Status
 
199
attach (SANE_String_Const devname);
 
200
 
 
201
SANE_Status
 
202
attach (SANE_String_Const devname)
 
203
{
 
204
  int i = 0;
 
205
  if (devlist)
 
206
    {
 
207
      for (; devlist[i]; i++);
 
208
      devlist = realloc (devlist, sizeof (SANE_Device *) * (i + 1));
 
209
      if (!devlist)
 
210
        return SANE_STATUS_NO_MEM;
 
211
    }
 
212
  else
 
213
    {
 
214
      devlist = malloc (sizeof (SANE_Device *) * 2);
 
215
      if (!devlist)
 
216
        return SANE_STATUS_NO_MEM;
 
217
    }
 
218
  devlist[i] = malloc (sizeof (SANE_Device));
 
219
  if (!devlist[i])
 
220
    return SANE_STATUS_NO_MEM;
 
221
  memcpy (devlist[i], &known_devices[curr_scan_dev].scanner,
 
222
          sizeof (SANE_Device));
 
223
  devlist[i]->name = strdup (devname);
 
224
  /* terminate device list with NULL entry: */
 
225
  devlist[i + 1] = 0;
 
226
  DBG (DBG_INFO, "%s device attached\n", devname);
 
227
  return SANE_STATUS_GOOD;
 
228
}
 
229
 
 
230
/* Get device list */
 
231
SANE_Status
 
232
sane_get_devices (const SANE_Device *** device_list,
 
233
                  SANE_Bool __sane_unused__ local_only)
 
234
{
 
235
  if (devlist)
 
236
    {
 
237
      int i;
 
238
      for (i = 0; devlist[i]; i++)
 
239
        {
 
240
          free ((void *) devlist[i]);
 
241
        }
 
242
      free ((void *) devlist);
 
243
      devlist = NULL;
 
244
    }
 
245
 
 
246
  for (curr_scan_dev = 0;
 
247
       curr_scan_dev <
 
248
       sizeof (known_devices) / sizeof (known_devices[0]); curr_scan_dev++)
 
249
    {
 
250
      sanei_usb_find_devices (PANASONIC_ID,
 
251
                              known_devices[curr_scan_dev].id, attach);
 
252
    }
 
253
 
 
254
  for (curr_scan_dev = 0;
 
255
       curr_scan_dev <
 
256
       sizeof (known_devices) / sizeof (known_devices[0]); curr_scan_dev++)
 
257
    {
 
258
      sanei_scsi_find_devices (known_devices[curr_scan_dev].
 
259
                               scanner.vendor,
 
260
                               known_devices[curr_scan_dev].
 
261
                               scanner.model, NULL, -1, -1, -1, -1, attach);
 
262
    }
 
263
  if(device_list)
 
264
    *device_list = (const SANE_Device **) devlist;
 
265
  return SANE_STATUS_GOOD;
 
266
}
 
267
 
 
268
/* Open device, return the device handle */
 
269
SANE_Status
 
270
sane_open (SANE_String_Const devname, SANE_Handle * handle)
 
271
{
 
272
  unsigned i, j, id = 0;
 
273
  struct scanner *s;
 
274
  SANE_Int h, bus;
 
275
  SANE_Status st = SANE_STATUS_GOOD;
 
276
  if (!devlist)
 
277
    {
 
278
      st = sane_get_devices (NULL, 0);
 
279
      if (st)
 
280
        return st;
 
281
    }
 
282
  for (i = 0; devlist[i]; i++)
 
283
    {
 
284
      if (!strcmp (devlist[i]->name, devname))
 
285
        break;
 
286
    }
 
287
  if (!devlist[i])
 
288
    return SANE_STATUS_INVAL;
 
289
  for (j = 0; j < sizeof (known_devices) / sizeof (known_devices[0]); j++)
 
290
    {
 
291
      if (!strcmp (devlist[i]->model, known_devices[j].scanner.model))
 
292
        {
 
293
          id = known_devices[j].id;
 
294
          break;
 
295
        }
 
296
    }
 
297
 
 
298
  st = sanei_usb_open (devname, &h);
 
299
 
 
300
  if (st == SANE_STATUS_ACCESS_DENIED)
 
301
    return st;
 
302
  if (st)
 
303
    {
 
304
      st = sanei_scsi_open (devname, &h, kvs40xx_sense_handler, NULL);
 
305
      if (st)
 
306
        {
 
307
          return st;
 
308
        }
 
309
      bus = SCSI;
 
310
    }
 
311
  else
 
312
    {
 
313
      bus = USB;
 
314
      st = sanei_usb_claim_interface (h, 0);
 
315
      if (st)
 
316
        {
 
317
          sanei_usb_close (h);
 
318
          return st;
 
319
        }
 
320
    }
 
321
 
 
322
  s = malloc (sizeof (struct scanner));
 
323
  if (!s)
 
324
    return SANE_STATUS_NO_MEM;
 
325
  memset (s, 0, sizeof (struct scanner));
 
326
  s->buffer = malloc (MAX_READ_DATA_SIZE + BULK_HEADER_SIZE);
 
327
  if (!s->buffer)
 
328
    return SANE_STATUS_NO_MEM;
 
329
 
 
330
  s->file = h;
 
331
  s->bus = bus;
 
332
  s->id = id;
 
333
  strcpy (s->name, devname);
 
334
  *handle = s;
 
335
  for (i = 0; i < 3; i++)
 
336
    {
 
337
      st = kvs40xx_test_unit_ready (s);
 
338
      if (st)
 
339
        {
 
340
          if (s->bus == SCSI)
 
341
            {
 
342
              sanei_scsi_close (s->file);
 
343
              st = sanei_scsi_open (devname, &h, kvs40xx_sense_handler, NULL);
 
344
              if (st)
 
345
                return st;
 
346
            }
 
347
          else
 
348
            {
 
349
              sanei_usb_release_interface (s->file, 0);
 
350
              sanei_usb_close (s->file);
 
351
              st = sanei_usb_open (devname, &h);
 
352
              if (st)
 
353
                return st;
 
354
              st = sanei_usb_claim_interface (h, 0);
 
355
              if (st)
 
356
                {
 
357
                  sanei_usb_close (h);
 
358
                  return st;
 
359
                }
 
360
            }
 
361
          s->file = h;
 
362
        }
 
363
      else
 
364
        break;
 
365
    }
 
366
  if (i == 3)
 
367
    return SANE_STATUS_DEVICE_BUSY;
 
368
 
 
369
  if (id == KV_S4085C || id == KV_S4065C)
 
370
    {
 
371
      char str[16];
 
372
      st = inquiry (s, str);
 
373
      if (st)
 
374
        goto err;
 
375
      if (id == KV_S4085C)
 
376
        s->id = !strcmp (str, "KV-S4085CL") ? KV_S4085CL : KV_S4085CW;
 
377
      else
 
378
        s->id = !strcmp (str, "KV-S4065CL") ? KV_S4065CL : KV_S4065CW;
 
379
    }
 
380
  kvs40xx_init_options (s);
 
381
  st = kvs40xx_set_timeout (s, s->val[FEED_TIMEOUT].w);
 
382
  if (st)
 
383
    goto err;
 
384
 
 
385
  return SANE_STATUS_GOOD;
 
386
err:
 
387
  sane_close (s);
 
388
  return st;
 
389
}
 
390
 
 
391
/* Close device */
 
392
void
 
393
sane_close (SANE_Handle handle)
 
394
{
 
395
  struct scanner *s = (struct scanner *) handle;
 
396
  unsigned i;
 
397
  hopper_down (s);
 
398
  if (s->bus == USB)
 
399
    {
 
400
      sanei_usb_release_interface (s->file, 0);
 
401
      sanei_usb_close (s->file);
 
402
    }
 
403
  else
 
404
    sanei_scsi_close (s->file);
 
405
 
 
406
  for (i = 1; i < NUM_OPTIONS; i++)
 
407
    {
 
408
      if (s->opt[i].type == SANE_TYPE_STRING && s->val[i].s)
 
409
        free (s->val[i].s);
 
410
    }
 
411
 
 
412
  for (i = 0; i < sizeof (s->buf) / sizeof (s->buf[0]); i++)
 
413
    buf_deinit (&s->buf[i]);
 
414
 
 
415
  free (s->buffer);
 
416
  free (s);
 
417
 
 
418
}
 
419
 
 
420
/* Get option descriptor */
 
421
const SANE_Option_Descriptor *
 
422
sane_get_option_descriptor (SANE_Handle handle, SANE_Int option)
 
423
{
 
424
  struct scanner *s = handle;
 
425
 
 
426
  if ((unsigned) option >= NUM_OPTIONS || option < 0)
 
427
    return NULL;
 
428
  return s->opt + option;
 
429
}
 
430
 
 
431
static SANE_Status
 
432
wait_document (struct scanner *s)
 
433
{
 
434
  SANE_Status st;
 
435
  int i;
 
436
  if (!strcmp ("fb", s->val[SOURCE].s))
 
437
    return SANE_STATUS_GOOD;
 
438
  if (!strcmp ("off", s->val[MANUALFEED].s))
 
439
    return kvs40xx_document_exist (s);
 
440
 
 
441
  for (i = 0; i < s->val[FEED_TIMEOUT].w; i++)
 
442
    {
 
443
      st = kvs40xx_document_exist (s);
 
444
      if (st != SANE_STATUS_NO_DOCS)
 
445
        return st;
 
446
      sleep (1);
 
447
    }
 
448
  return SANE_STATUS_NO_DOCS;
 
449
}
 
450
 
 
451
static SANE_Status read_image_duplex(SANE_Handle handle)
 
452
{
 
453
        struct scanner *s = (struct scanner *) handle;
 
454
        SANE_Status st = SANE_STATUS_GOOD;
 
455
        unsigned read, side;
 
456
        int i;
 
457
        struct side {
 
458
                unsigned mx, eof;
 
459
                u8 *p;
 
460
                struct buf *buf;
 
461
        } a[2], *b;
 
462
 
 
463
        for (i = 0; i < 2; i++) {
 
464
                a[i].mx = BUF_SIZE;
 
465
                a[i].eof = 0;
 
466
                a[i].buf = &s->buf[i];
 
467
                st = new_buf(&s->buf[i], &a[i].p);
 
468
                if (st)
 
469
                        goto err;
 
470
        }
 
471
        for (b = &a[0], side = SIDE_FRONT; (!a[0].eof || !a[1].eof);) {
 
472
                pthread_testcancel();
 
473
                if (b->mx == 0) {
 
474
                        push_buf(b->buf, BUF_SIZE);
 
475
                        st = new_buf(b->buf, &b->p);
 
476
                        if (st)
 
477
                                goto err;
 
478
                        b->mx = BUF_SIZE;
 
479
                }
 
480
 
 
481
                st = kvs40xx_read_image_data(s, s->page, side,
 
482
                                             b->p + BUF_SIZE - b->mx, b->mx,
 
483
                                             &read);
 
484
                b->mx -= read;
 
485
                if (st) {
 
486
                        if (st != INCORRECT_LENGTH
 
487
                            && st != SANE_STATUS_EOF)
 
488
                                goto err;
 
489
 
 
490
                        if (st == SANE_STATUS_EOF) {
 
491
                                b->eof = 1;
 
492
                                push_buf(b->buf, BUF_SIZE - b->mx);
 
493
                        }
 
494
                        side ^= SIDE_BACK;
 
495
                        b = &a[side == SIDE_FRONT ? 0 : 1];
 
496
                }
 
497
        }
 
498
 
 
499
      err:
 
500
        for (i = 0; i < 2; i++)
 
501
                buf_set_st(&s->buf[i], st);
 
502
        return st;
 
503
}
 
504
 
 
505
static SANE_Status read_image_simplex(SANE_Handle handle)
 
506
{
 
507
        struct scanner *s = (struct scanner *) handle;
 
508
        SANE_Status st = SANE_STATUS_GOOD;
 
509
 
 
510
        for (; (!st || st == INCORRECT_LENGTH);) {
 
511
                unsigned read, mx;
 
512
                unsigned char *p = NULL;
 
513
                st = new_buf(&s->buf[0], &p);
 
514
                for (read = 0, mx = BUF_SIZE; mx &&
 
515
                     (!st || st == INCORRECT_LENGTH); mx -= read) {
 
516
                        pthread_testcancel();
 
517
                        st = kvs40xx_read_image_data(s, s->page, SIDE_FRONT,
 
518
                                                     p + BUF_SIZE - mx, mx,
 
519
                                                     &read);
 
520
                }
 
521
                push_buf(&s->buf[0], BUF_SIZE - mx);
 
522
        }
 
523
        buf_set_st(&s->buf[0], st);
 
524
        return st;
 
525
}
 
526
 
 
527
static SANE_Status read_data(struct scanner *s)
 
528
{
 
529
        SANE_Status st;
 
530
        int duplex = s->val[DUPLEX].w;
 
531
        s->read = 0;
 
532
        s->side = SIDE_FRONT;
 
533
 
 
534
        st = duplex ? read_image_duplex(s) : read_image_simplex(s);
 
535
        if (st && (st != SANE_STATUS_EOF))
 
536
                goto err;
 
537
 
 
538
        st = kvs40xx_read_picture_element(s, SIDE_FRONT, &s->params);
 
539
        if (st)
 
540
                goto err;
 
541
        if (!s->params.lines) {
 
542
                st = SANE_STATUS_INVAL;
 
543
                goto err;
 
544
        }
 
545
 
 
546
        sane_get_parameters(s, NULL);
 
547
 
 
548
        s->page++;
 
549
        return SANE_STATUS_GOOD;
 
550
      err:
 
551
        s->scanning = 0;
 
552
        return st;
 
553
}
 
554
 
 
555
/* Start scanning */
 
556
SANE_Status
 
557
sane_start (SANE_Handle handle)
 
558
{
 
559
  struct scanner *s = (struct scanner *) handle;
 
560
  SANE_Status st = SANE_STATUS_GOOD;
 
561
  int duplex = s->val[DUPLEX].w, i;
 
562
  unsigned data_avalible;
 
563
  int start = 0;
 
564
 
 
565
  if (s->thread)
 
566
    {
 
567
      pthread_join (s->thread, NULL);
 
568
      s->thread = 0;
 
569
    }
 
570
  if (!s->scanning)
 
571
    {
 
572
      st = kvs40xx_test_unit_ready (s);
 
573
      if (st)
 
574
        return st;
 
575
 
 
576
      st = wait_document (s);
 
577
      if (st)
 
578
        return st;
 
579
 
 
580
      st = kvs40xx_reset_window (s);
 
581
      if (st)
 
582
        return st;
 
583
      st = kvs40xx_set_window (s, SIDE_FRONT);
 
584
 
 
585
      if (st)
 
586
        return st;
 
587
 
 
588
      if (duplex)
 
589
        {
 
590
          st = kvs40xx_set_window (s, SIDE_BACK);
 
591
          if (st)
 
592
            return st;
 
593
        }
 
594
 
 
595
      st = kvs40xx_scan (s);
 
596
      if (st)
 
597
        return st;
 
598
 
 
599
      if (s->val[CROP].b || s->val[LENGTHCTL].b || s->val[LONG_PAPER].b)
 
600
        {
 
601
          unsigned w, h, res = s->val[RESOLUTION].w;
 
602
          SANE_Parameters *p = &s->params;
 
603
          w = 297;              /*A3 */
 
604
          h = 420;
 
605
          p->pixels_per_line = w * res / 25.4 + .5;
 
606
          p->lines = h * res / 25.4 + .5;
 
607
        }
 
608
      else
 
609
        {
 
610
          st = kvs40xx_read_picture_element (s, SIDE_FRONT, &s->params);
 
611
          if (st)
 
612
            return st;
 
613
        }
 
614
 
 
615
      start = 1;
 
616
      s->scanning = 1;
 
617
      s->page = 0;
 
618
      s->read = 0;
 
619
      s->side = SIDE_FRONT;
 
620
      sane_get_parameters (s, NULL);
 
621
    }
 
622
 
 
623
  if (duplex && s->side == SIDE_FRONT && !start)
 
624
    {
 
625
      s->side = SIDE_BACK;
 
626
      s->read = 0;
 
627
      return SANE_STATUS_GOOD;
 
628
    }
 
629
        do {
 
630
                st = get_buffer_status(s, &data_avalible);
 
631
                if (st)
 
632
                        goto err;
 
633
 
 
634
        } while (!data_avalible);
 
635
 
 
636
  for (i = 0; i < (duplex ? 2 : 1); i++)
 
637
    {
 
638
      st = buf_init (&s->buf[i], s->side_size);
 
639
      if (st)
 
640
        goto err;
 
641
    }
 
642
 
 
643
  if (pthread_create (&s->thread, NULL, (void *(*)(void *)) read_data, s))
 
644
    {
 
645
      st = SANE_STATUS_IO_ERROR;
 
646
      goto err;
 
647
    }
 
648
 
 
649
  if (s->val[CROP].b || s->val[LENGTHCTL].b || s->val[LONG_PAPER].b)
 
650
    {
 
651
      pthread_join (s->thread, NULL);
 
652
      s->thread = 0;
 
653
    }
 
654
 
 
655
  return SANE_STATUS_GOOD;
 
656
err:
 
657
  s->scanning = 0;
 
658
  return st;
 
659
}
 
660
 
 
661
SANE_Status
 
662
sane_read(SANE_Handle handle, SANE_Byte * buf,
 
663
          SANE_Int max_len, SANE_Int * len)
 
664
{
 
665
        struct scanner *s = (struct scanner *) handle;
 
666
        int duplex = s->val[DUPLEX].w;
 
667
        struct buf *b = s->side == SIDE_FRONT ? &s->buf[0] : &s->buf[1];
 
668
        SANE_Status err = buf_get_err(b);
 
669
        SANE_Int inbuf = 0;
 
670
        *len = 0;
 
671
 
 
672
        if (!s->scanning)
 
673
                return SANE_STATUS_EOF;
 
674
        if (err)
 
675
                goto out;
 
676
 
 
677
        if (s->read) {
 
678
                *len =
 
679
                    max_len <
 
680
                    (SANE_Int) s->read ? max_len : (SANE_Int) s->read;
 
681
                memcpy(buf, s->data + BUF_SIZE - s->read, *len);
 
682
                s->read -= *len;
 
683
 
 
684
                if (!s->read)
 
685
                        pop_buf(b);
 
686
                goto out;
 
687
        }
 
688
 
 
689
        s->data = get_buf(b, &inbuf);
 
690
        if (!s->data)
 
691
                goto out;
 
692
 
 
693
        *len = max_len < inbuf ? max_len : inbuf;
 
694
        if (*len > BUF_SIZE)
 
695
                *len = BUF_SIZE;
 
696
        memcpy(buf, s->data, *len);
 
697
        s->read = inbuf > BUF_SIZE ? BUF_SIZE - *len : inbuf - *len;
 
698
 
 
699
        if (!s->read)
 
700
                pop_buf(b);
 
701
      out:
 
702
        err = *len ? SANE_STATUS_GOOD : buf_get_err(b);
 
703
        if (err == SANE_STATUS_EOF) {
 
704
                if (strcmp(s->val[FEEDER_MODE].s, SANE_I18N("continuous"))) {
 
705
                        if (!duplex || s->side == SIDE_BACK)
 
706
                                s->scanning = 0;
 
707
                }
 
708
                buf_deinit(b);
 
709
        } else if (err) {
 
710
                unsigned i;
 
711
                for (i = 0; i < sizeof(s->buf) / sizeof(s->buf[0]); i++)
 
712
                        buf_deinit(&s->buf[i]);
 
713
        }
 
714
        return err;
 
715
}
 
716
 
 
717
void
 
718
sane_cancel (SANE_Handle handle)
 
719
{
 
720
  unsigned i;
 
721
  struct scanner *s = (struct scanner *) handle;
 
722
  if (s->scanning && !strcmp (s->val[FEEDER_MODE].s, SANE_I18N ("continuous")))
 
723
    {
 
724
      stop_adf (s);
 
725
    }
 
726
  if (s->thread)
 
727
    {
 
728
      pthread_cancel (s->thread);
 
729
      pthread_join (s->thread, NULL);
 
730
      s->thread = 0;
 
731
    }
 
732
  for (i = 0; i < sizeof (s->buf) / sizeof (s->buf[0]); i++)
 
733
    buf_deinit (&s->buf[i]);
 
734
  s->scanning = 0;
 
735
}
 
736
 
 
737
SANE_Status
 
738
sane_set_io_mode (SANE_Handle __sane_unused__ h, SANE_Bool __sane_unused__ m)
 
739
{
 
740
  return SANE_STATUS_UNSUPPORTED;
 
741
}
 
742
 
 
743
SANE_Status
 
744
sane_get_select_fd (SANE_Handle __sane_unused__ h,
 
745
                    SANE_Int __sane_unused__ * fd)
 
746
{
 
747
  return SANE_STATUS_UNSUPPORTED;
 
748
}