summaryrefslogtreecommitdiff
path: root/backend/kvs20xx.c
blob: 8864d7d0344806686f730b940bf131fa7853cec1 (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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
/*
   Copyright (C) 2008, Panasonic Russia Ltd.
   Copyright (C) 2010, m. allan noah
*/
/*
   Panasonic KV-S20xx USB-SCSI scanners.
*/

#define DEBUG_NOT_STATIC
#define BUILD 2

#include "../include/sane/config.h"

#include <string.h>
#include <unistd.h>

#include "../include/sane/sanei_backend.h"
#include "../include/sane/sanei_scsi.h"
#include "../include/sane/sanei_usb.h"
#include "../include/sane/saneopts.h"
#include "../include/sane/sanei_config.h"
#include "../include/lassert.h"

#include "kvs20xx.h"
#include "kvs20xx_cmd.h"

struct known_device
{
  const SANE_Int id;
  const SANE_Device scanner;
};

static const struct known_device known_devices[] = {
  {
    KV_S2025C,
    { "", "MATSHITA", "KV-S2025C", "sheetfed scanner" },
  },
  {
    KV_S2045C,
    { "", "MATSHITA", "KV-S2045C", "sheetfed scanner" },
  },
  {
    KV_S2026C,
    { "", "MATSHITA", "KV-S2026C", "sheetfed scanner" },
  },
  {
    KV_S2046C,
    { "", "MATSHITA", "KV-S2046C", "sheetfed scanner" },
  },
  {
    KV_S2028C,
    { "", "MATSHITA", "KV-S2028C", "sheetfed scanner" },
  },
  {
    KV_S2048C,
    { "", "MATSHITA", "KV-S2048C", "sheetfed scanner" },
  },
};

SANE_Status
sane_init (SANE_Int __sane_unused__ * version_code,
	   SANE_Auth_Callback __sane_unused__ authorize)
{
  DBG_INIT ();
  DBG (DBG_INFO, "This is panasonic kvs20xx driver\n");

  *version_code = SANE_VERSION_CODE (V_MAJOR, V_MINOR, BUILD);

  /* Initialize USB */
  sanei_usb_init ();

  return SANE_STATUS_GOOD;
}

/*
 * List of available devices, allocated by sane_get_devices, released
 * by sane_exit()
 */
static SANE_Device **devlist = NULL;
static unsigned curr_scan_dev = 0;

void
sane_exit (void)
{
  if (devlist)
    {
      int i;
      for (i = 0; devlist[i]; i++)
	{
	  free ((void *) devlist[i]->name);
	  free ((void *) devlist[i]);
	}
      free ((void *) devlist);
      devlist = NULL;
    }
}

static SANE_Status
attach (SANE_String_Const devname)
{
  int i = 0;
  if (devlist)
    {
      for (; devlist[i]; i++);
      devlist = realloc (devlist, sizeof (SANE_Device *) * (i + 1));
      if (!devlist)
	return SANE_STATUS_NO_MEM;
    }
  else
    {
      devlist = malloc (sizeof (SANE_Device *) * 2);
      if (!devlist)
	return SANE_STATUS_NO_MEM;
    }
  devlist[i] = malloc (sizeof (SANE_Device));
  if (!devlist[i])
    return SANE_STATUS_NO_MEM;
  memcpy (devlist[i], &known_devices[curr_scan_dev].scanner,
	  sizeof (SANE_Device));
  devlist[i]->name = strdup (devname);
  /* terminate device list with NULL entry: */
  devlist[i + 1] = 0;
  DBG (DBG_INFO, "%s device attached\n", devname);
  return SANE_STATUS_GOOD;
}

/* Get device list */
SANE_Status
sane_get_devices (const SANE_Device *** device_list,
		  SANE_Bool __sane_unused__ local_only)
{
  if (devlist)
    {
      int i;
      for (i = 0; devlist[i]; i++)
	{
	  free ((void *) devlist[i]->name);
	  free ((void *) devlist[i]);
	}
      free ((void *) devlist);
      devlist = NULL;
    }

  for (curr_scan_dev = 0;
       curr_scan_dev < sizeof (known_devices) / sizeof (known_devices[0]);
       curr_scan_dev++)
    {
      sanei_usb_find_devices (PANASONIC_ID,
			      known_devices[curr_scan_dev].id, attach);
    }
  for (curr_scan_dev = 0;
       curr_scan_dev < sizeof (known_devices) / sizeof (known_devices[0]);
       curr_scan_dev++)
    {
      sanei_scsi_find_devices (known_devices[curr_scan_dev].scanner.vendor,
			       known_devices[curr_scan_dev].scanner.model,
			       NULL, -1, -1, -1, -1, attach);
    }
  if(device_list)
    *device_list = (const SANE_Device **) devlist;
  return SANE_STATUS_GOOD;
}

/* Open device, return the device handle */
SANE_Status
sane_open (SANE_String_Const devname, SANE_Handle * handle)
{
  unsigned i, j, id = 0;
  struct scanner *s;
  SANE_Int h, bus;
  SANE_Status st;
  if (!devlist)
    {
      st = sane_get_devices (NULL, 0);
      if (st)
        return st;
    }
  for (i = 0; devlist[i]; i++)
    {
      if (!strcmp (devlist[i]->name, devname))
	break;
    }
  if (!devlist[i])
    return SANE_STATUS_INVAL;
  for (j = 0; j < sizeof (known_devices) / sizeof (known_devices[0]); j++)
    {
      if (!strcmp (devlist[i]->model, known_devices[j].scanner.model))
	{
	  id = known_devices[j].id;
	  break;
	}
    }

  st = sanei_usb_open (devname, &h);
  if (st == SANE_STATUS_ACCESS_DENIED)
    return st;
  if (st)
    {
      st = sanei_scsi_open (devname, &h, kvs20xx_sense_handler, NULL);
      if (st)
	{
	  return st;
	}
      bus = SCSI;
    }
  else
    {
      bus = USB;
      st = sanei_usb_claim_interface (h, 0);
      if (st)
	{
	  sanei_usb_close (h);
	  return st;
	}
    }

  s = malloc (sizeof (struct scanner));
  if (!s)
    return SANE_STATUS_NO_MEM;
  memset (s, 0, sizeof (struct scanner));
  s->buffer = malloc (MAX_READ_DATA_SIZE + BULK_HEADER_SIZE);
  if (!s->buffer)
    return SANE_STATUS_NO_MEM;
  s->file = h;
  s->bus = bus;
  s->id = id;
  kvs20xx_init_options (s);
  *handle = s;
  for (i = 0; i < 3; i++)
    {
      st = kvs20xx_test_unit_ready (s);
      if (st)
	{
	  if (s->bus == SCSI)
	    {
	      sanei_scsi_close (s->file);
	      st = sanei_scsi_open (devname, &h, kvs20xx_sense_handler, NULL);
	      if (st)
		return st;
	    }
	  else
	    {
	      sanei_usb_release_interface (s->file, 0);
	      sanei_usb_close (s->file);
	      st = sanei_usb_open (devname, &h);
	      if (st)
		return st;
	      st = sanei_usb_claim_interface (h, 0);
	      if (st)
		{
		  sanei_usb_close (h);
		  return st;
		}
	    }
	  s->file = h;
	}
      else
	break;
    }
  if (i == 3)
    return SANE_STATUS_DEVICE_BUSY;

  st = kvs20xx_set_timeout (s, s->val[FEED_TIMEOUT].w);
  if (st)
    {
      sane_close (s);
      return st;
    }

  return SANE_STATUS_GOOD;
}

/* Close device */
void
sane_close (SANE_Handle handle)
{
  struct scanner *s = (struct scanner *) handle;
  int i;
  if (s->bus == USB)
    {
      sanei_usb_release_interface (s->file, 0);
      sanei_usb_close (s->file);
    }
  else
    sanei_scsi_close (s->file);

  for (i = 1; i < NUM_OPTIONS; i++)
    {
      if (s->opt[i].type == SANE_TYPE_STRING && s->val[i].s)
	free (s->val[i].s);
    }
  if (s->data)
    free (s->data);
  free (s->buffer);
  free (s);

}

/* Get option descriptor */
const SANE_Option_Descriptor *
sane_get_option_descriptor (SANE_Handle handle, SANE_Int option)
{
  struct scanner *s = handle;

  if ((unsigned) option >= NUM_OPTIONS || option < 0)
    return NULL;
  return s->opt + option;
}

static SANE_Status
wait_document (struct scanner *s)
{
  SANE_Status st;
  int i;
  if (!strcmp ("off", s->val[MANUALFEED].s))
    return kvs20xx_document_exist (s);

  for (i = 0; i < s->val[FEED_TIMEOUT].w; i++)
    {
      st = kvs20xx_document_exist (s);
      if (st != SANE_STATUS_NO_DOCS)
	return st;
      sleep (1);
    }
  return SANE_STATUS_NO_DOCS;
}

/* Start scanning */
SANE_Status
sane_start (SANE_Handle handle)
{
  struct scanner *s = (struct scanner *) handle;
  SANE_Status st;
  int duplex = s->val[DUPLEX].w;

  if (!s->scanning)
    {
      unsigned dummy_length;
      st = kvs20xx_test_unit_ready (s);
      if (st)
	return st;

      st = wait_document (s);
      if (st)
	return st;

      st = kvs20xx_reset_window (s);
      if (st)
	return st;
      st = kvs20xx_set_window (s, SIDE_FRONT);
      if (st)
	return st;
      if (duplex)
	{
	  st = kvs20xx_set_window (s, SIDE_BACK);
	  if (st)
	    return st;
	}
      st = kvs20xx_scan (s);
      if (st)
	return st;

      st = kvs20xx_read_picture_element (s, SIDE_FRONT, &s->params);
      if (st)
	return st;
      if (duplex)
	{
	  st = get_adjust_data (s, &dummy_length);
	  if (st)
	    return st;
	}
      else
	{
	  dummy_length = 0;
	}
      s->scanning = 1;
      s->page = 0;
      s->read = 0;
      s->side = SIDE_FRONT;
      sane_get_parameters (s, NULL);
      s->saved_dummy_size = s->dummy_size = dummy_length
	? (dummy_length * s->val[RESOLUTION].w / 1200 - 1)
	* s->params.bytes_per_line : 0;
      s->side_size = s->params.lines * s->params.bytes_per_line;

      s->data = realloc (s->data, duplex ? s->side_size * 2 : s->side_size);
      if (!s->data)
	{
	  s->scanning = 0;
	  return SANE_STATUS_NO_MEM;
	}
    }

  if (duplex)
    {
      unsigned side = SIDE_FRONT;
      unsigned read, mx;
      if (s->side == SIDE_FRONT && s->read == s->side_size - s->dummy_size)
	{
	  s->side = SIDE_BACK;
	  s->read = s->dummy_size;
	  s->dummy_size = 0;
	  return SANE_STATUS_GOOD;
	}
      s->read = 0;
      s->dummy_size = s->saved_dummy_size;
      s->side = SIDE_FRONT;
      st = kvs20xx_document_exist (s);
      if (st)
	return st;
      for (mx = s->side_size * 2; !st; mx -= read, side ^= SIDE_BACK)
	st = kvs20xx_read_image_data (s, s->page, side,
				      &s->data[s->side_size * 2 - mx], mx,
				      &read);
    }
  else
    {
      unsigned read, mx;
      s->read = 0;
      st = kvs20xx_document_exist (s);
      if (st)
	return st;
      DBG (DBG_INFO, "start: %d\n", s->page);

      for (mx = s->side_size; !st; mx -= read)
	st = kvs20xx_read_image_data (s, s->page, SIDE_FRONT,
				      &s->data[s->side_size - mx], mx, &read);
    }
  if (st && st != SANE_STATUS_EOF)
    {
      s->scanning = 0;
      return st;
    }
  s->page++;
  return SANE_STATUS_GOOD;
}

inline static void
memcpy24 (u8 * dest, u8 * src, unsigned size, unsigned ls)
{
  unsigned i;
  for (i = 0; i < size; i++)
    {
      dest[i * 3] = src[i];
      dest[i * 3 + 1] = src[i + ls];
      dest[i * 3 + 2] = src[i + 2 * ls];
    }
}

SANE_Status
sane_read (SANE_Handle handle, SANE_Byte * buf,
	   SANE_Int max_len, SANE_Int * len)
{
  struct scanner *s = (struct scanner *) handle;
  int duplex = s->val[DUPLEX].w;
  int color = !strcmp (s->val[MODE].s, SANE_VALUE_SCAN_MODE_COLOR);
  int rest = s->side_size - s->read - s->dummy_size;
  *len = 0;

  if (!s->scanning || !rest)
    {
      if (strcmp (s->val[FEEDER_MODE].s, SANE_I18N ("continuous")))
	{
	  if (!duplex || s->side == SIDE_BACK)
	    s->scanning = 0;
	}
      return SANE_STATUS_EOF;
    }

  *len = max_len < rest ? max_len : rest;
  if (duplex && (s->id == KV_S2025C
		 || s->id == KV_S2026C || s->id == KV_S2028C))
    {
      if (color)
	{
	  unsigned ls = s->params.bytes_per_line;
	  unsigned i, a = s->side == SIDE_FRONT ? 0 : ls / 3;
	  u8 *data;
	  *len = (*len / ls) * ls;
	  for (i = 0, data = s->data + s->read * 2 + a;
	       i < *len / ls; buf += ls, data += 2 * ls, i++)
	    memcpy24 (buf, data, ls / 3, ls * 2 / 3);
	}
      else
	{
	  unsigned ls = s->params.bytes_per_line;
	  unsigned i = s->side == SIDE_FRONT ? 0 : ls;
	  unsigned head = ls - (s->read % ls);
	  unsigned tail = (*len - head) % ls;
	  unsigned lines = (*len - head) / ls;
	  u8 *data = s->data + (s->read / ls) * ls * 2 + i + s->read % ls;
	  assert (data <= s->data + s->side_size * 2);
	  memcpy (buf, data, head);
	  for (i = 0, buf += head, data += head + (head ? ls : 0);
	       i < lines; buf += ls, data += ls * 2, i++)
	    {
	      assert (data <= s->data + s->side_size * 2);
	      memcpy (buf, data, ls);
	    }
	  assert ((data <= s->data + s->side_size * 2) || !tail);
	  memcpy (buf, data, tail);
	}
      s->read += *len;
    }
  else
    {
      if (color)
	{
	  unsigned i, ls = s->params.bytes_per_line;
	  u8 *data = s->data + s->read;
	  *len = (*len / ls) * ls;
	  for (i = 0; i < *len / ls; buf += ls, data += ls, i++)
	    memcpy24 (buf, data, ls / 3, ls / 3);
	}
      else
	{
	  memcpy (buf, s->data + s->read, *len);
	}
      s->read += *len;
    }
  return SANE_STATUS_GOOD;
}

void
sane_cancel (SANE_Handle handle)
{
  struct scanner *s = (struct scanner *) handle;
  s->scanning = 0;
}

SANE_Status
sane_set_io_mode (SANE_Handle __sane_unused__ h, SANE_Bool __sane_unused__ m)
{
  return SANE_STATUS_UNSUPPORTED;
}

SANE_Status
sane_get_select_fd (SANE_Handle __sane_unused__ h,
		    SANE_Int __sane_unused__ * fd)
{
  return SANE_STATUS_UNSUPPORTED;
}