2
Copyright (C) 2009, Panasonic Russia Ltd.
3
Copyright (C) 2010,2011, m. allan noah
6
Panasonic KV-S40xx USB-SCSI scanner driver.
9
#include "../include/sane/config.h"
11
#include <ctype.h> /*isspace*/
12
#include <math.h> /*tan*/
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"
29
#include "sane/sanei_debug.h"
31
#define DATA_TAIL 0x200
36
const SANE_Device scanner;
39
static const struct known_device known_devices[] = {
45
"High Speed Color ADF Scanner",
54
"High Speed Color ADF Scanner",
63
"High Speed Color ADF Scanner",
69
static inline SANE_Status buf_init(struct buf *b, SANE_Int sz)
71
const int num = sz / BUF_SIZE + 1;
72
b->buf = (u8 **) realloc(b->buf, num * sizeof(u8 *));
74
return SANE_STATUS_NO_MEM;
75
memset(b->buf, 0, num * sizeof(void *));
76
b->size = b->head = b->tail = 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;
84
static inline void buf_deinit(struct buf *b)
89
for (i = b->head; i < b->tail; i++)
94
b->head = b->tail = 0;
97
static inline SANE_Status new_buf(struct buf *b, u8 ** p)
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];
104
return SANE_STATUS_GOOD;
107
static inline SANE_Status buf_get_err(struct buf *b)
109
return b->size ? SANE_STATUS_GOOD : b->st;
112
static inline void buf_set_st(struct buf *b, SANE_Status st)
114
pthread_mutex_lock(&b->mu);
117
pthread_cond_signal(&b->cond);
118
pthread_mutex_unlock(&b->mu);
121
static inline void buf_cancel(struct buf *b)
123
buf_set_st(b, SANE_STATUS_CANCELLED);
126
static inline void push_buf(struct buf *b, SANE_Int sz)
128
pthread_mutex_lock(&b->mu);
131
pthread_cond_signal(&b->cond);
132
pthread_mutex_unlock(&b->mu);
135
static inline u8 *get_buf(struct buf *b, SANE_Int * sz)
137
SANE_Status err = buf_get_err(b);
141
pthread_mutex_lock(&b->mu);
142
while (!b->sem && !buf_get_err(b))
143
pthread_cond_wait(&b->cond, &b->mu);
145
err = buf_get_err(b);
147
*sz = b->size < BUF_SIZE ? b->size : BUF_SIZE;
150
pthread_mutex_unlock(&b->mu);
151
return err ? NULL : b->buf[b->head];
154
static inline void pop_buf(struct buf *b)
156
free(b->buf[b->head]);
157
b->buf[b->head] = NULL;
162
sane_init (SANE_Int __sane_unused__ * version_code,
163
SANE_Auth_Callback __sane_unused__ authorize)
166
DBG (DBG_INFO, "This is panasonic kvs40xx driver\n");
168
*version_code = SANE_VERSION_CODE (V_MAJOR, V_MINOR, 1);
173
return SANE_STATUS_GOOD;
177
* List of available devices, allocated by sane_get_devices, released
180
static SANE_Device **devlist = NULL;
181
static unsigned curr_scan_dev = 0;
189
for (i = 0; devlist[i]; i++)
191
free ((void *) devlist[i]);
193
free ((void *) devlist);
199
attach (SANE_String_Const devname);
202
attach (SANE_String_Const devname)
207
for (; devlist[i]; i++);
208
devlist = realloc (devlist, sizeof (SANE_Device *) * (i + 1));
210
return SANE_STATUS_NO_MEM;
214
devlist = malloc (sizeof (SANE_Device *) * 2);
216
return SANE_STATUS_NO_MEM;
218
devlist[i] = malloc (sizeof (SANE_Device));
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: */
226
DBG (DBG_INFO, "%s device attached\n", devname);
227
return SANE_STATUS_GOOD;
230
/* Get device list */
232
sane_get_devices (const SANE_Device *** device_list,
233
SANE_Bool __sane_unused__ local_only)
238
for (i = 0; devlist[i]; i++)
240
free ((void *) devlist[i]);
242
free ((void *) devlist);
246
for (curr_scan_dev = 0;
248
sizeof (known_devices) / sizeof (known_devices[0]); curr_scan_dev++)
250
sanei_usb_find_devices (PANASONIC_ID,
251
known_devices[curr_scan_dev].id, attach);
254
for (curr_scan_dev = 0;
256
sizeof (known_devices) / sizeof (known_devices[0]); curr_scan_dev++)
258
sanei_scsi_find_devices (known_devices[curr_scan_dev].
260
known_devices[curr_scan_dev].
261
scanner.model, NULL, -1, -1, -1, -1, attach);
264
*device_list = (const SANE_Device **) devlist;
265
return SANE_STATUS_GOOD;
268
/* Open device, return the device handle */
270
sane_open (SANE_String_Const devname, SANE_Handle * handle)
272
unsigned i, j, id = 0;
275
SANE_Status st = SANE_STATUS_GOOD;
278
st = sane_get_devices (NULL, 0);
282
for (i = 0; devlist[i]; i++)
284
if (!strcmp (devlist[i]->name, devname))
288
return SANE_STATUS_INVAL;
289
for (j = 0; j < sizeof (known_devices) / sizeof (known_devices[0]); j++)
291
if (!strcmp (devlist[i]->model, known_devices[j].scanner.model))
293
id = known_devices[j].id;
298
st = sanei_usb_open (devname, &h);
300
if (st == SANE_STATUS_ACCESS_DENIED)
304
st = sanei_scsi_open (devname, &h, kvs40xx_sense_handler, NULL);
314
st = sanei_usb_claim_interface (h, 0);
322
s = malloc (sizeof (struct scanner));
324
return SANE_STATUS_NO_MEM;
325
memset (s, 0, sizeof (struct scanner));
326
s->buffer = malloc (MAX_READ_DATA_SIZE + BULK_HEADER_SIZE);
328
return SANE_STATUS_NO_MEM;
333
strcpy (s->name, devname);
335
for (i = 0; i < 3; i++)
337
st = kvs40xx_test_unit_ready (s);
342
sanei_scsi_close (s->file);
343
st = sanei_scsi_open (devname, &h, kvs40xx_sense_handler, NULL);
349
sanei_usb_release_interface (s->file, 0);
350
sanei_usb_close (s->file);
351
st = sanei_usb_open (devname, &h);
354
st = sanei_usb_claim_interface (h, 0);
367
return SANE_STATUS_DEVICE_BUSY;
369
if (id == KV_S4085C || id == KV_S4065C)
372
st = inquiry (s, str);
376
s->id = !strcmp (str, "KV-S4085CL") ? KV_S4085CL : KV_S4085CW;
378
s->id = !strcmp (str, "KV-S4065CL") ? KV_S4065CL : KV_S4065CW;
380
kvs40xx_init_options (s);
381
st = kvs40xx_set_timeout (s, s->val[FEED_TIMEOUT].w);
385
return SANE_STATUS_GOOD;
393
sane_close (SANE_Handle handle)
395
struct scanner *s = (struct scanner *) handle;
400
sanei_usb_release_interface (s->file, 0);
401
sanei_usb_close (s->file);
404
sanei_scsi_close (s->file);
406
for (i = 1; i < NUM_OPTIONS; i++)
408
if (s->opt[i].type == SANE_TYPE_STRING && s->val[i].s)
412
for (i = 0; i < sizeof (s->buf) / sizeof (s->buf[0]); i++)
413
buf_deinit (&s->buf[i]);
420
/* Get option descriptor */
421
const SANE_Option_Descriptor *
422
sane_get_option_descriptor (SANE_Handle handle, SANE_Int option)
424
struct scanner *s = handle;
426
if ((unsigned) option >= NUM_OPTIONS || option < 0)
428
return s->opt + option;
432
wait_document (struct scanner *s)
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);
441
for (i = 0; i < s->val[FEED_TIMEOUT].w; i++)
443
st = kvs40xx_document_exist (s);
444
if (st != SANE_STATUS_NO_DOCS)
448
return SANE_STATUS_NO_DOCS;
451
static SANE_Status read_image_duplex(SANE_Handle handle)
453
struct scanner *s = (struct scanner *) handle;
454
SANE_Status st = SANE_STATUS_GOOD;
463
for (i = 0; i < 2; i++) {
466
a[i].buf = &s->buf[i];
467
st = new_buf(&s->buf[i], &a[i].p);
471
for (b = &a[0], side = SIDE_FRONT; (!a[0].eof || !a[1].eof);) {
472
pthread_testcancel();
474
push_buf(b->buf, BUF_SIZE);
475
st = new_buf(b->buf, &b->p);
481
st = kvs40xx_read_image_data(s, s->page, side,
482
b->p + BUF_SIZE - b->mx, b->mx,
486
if (st != INCORRECT_LENGTH
487
&& st != SANE_STATUS_EOF)
490
if (st == SANE_STATUS_EOF) {
492
push_buf(b->buf, BUF_SIZE - b->mx);
495
b = &a[side == SIDE_FRONT ? 0 : 1];
500
for (i = 0; i < 2; i++)
501
buf_set_st(&s->buf[i], st);
505
static SANE_Status read_image_simplex(SANE_Handle handle)
507
struct scanner *s = (struct scanner *) handle;
508
SANE_Status st = SANE_STATUS_GOOD;
510
for (; (!st || st == INCORRECT_LENGTH);) {
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,
521
push_buf(&s->buf[0], BUF_SIZE - mx);
523
buf_set_st(&s->buf[0], st);
527
static SANE_Status read_data(struct scanner *s)
530
int duplex = s->val[DUPLEX].w;
532
s->side = SIDE_FRONT;
534
st = duplex ? read_image_duplex(s) : read_image_simplex(s);
535
if (st && (st != SANE_STATUS_EOF))
538
st = kvs40xx_read_picture_element(s, SIDE_FRONT, &s->params);
541
if (!s->params.lines) {
542
st = SANE_STATUS_INVAL;
546
sane_get_parameters(s, NULL);
549
return SANE_STATUS_GOOD;
557
sane_start (SANE_Handle handle)
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;
567
pthread_join (s->thread, NULL);
572
st = kvs40xx_test_unit_ready (s);
576
st = wait_document (s);
580
st = kvs40xx_reset_window (s);
583
st = kvs40xx_set_window (s, SIDE_FRONT);
590
st = kvs40xx_set_window (s, SIDE_BACK);
595
st = kvs40xx_scan (s);
599
if (s->val[CROP].b || s->val[LENGTHCTL].b || s->val[LONG_PAPER].b)
601
unsigned w, h, res = s->val[RESOLUTION].w;
602
SANE_Parameters *p = &s->params;
605
p->pixels_per_line = w * res / 25.4 + .5;
606
p->lines = h * res / 25.4 + .5;
610
st = kvs40xx_read_picture_element (s, SIDE_FRONT, &s->params);
619
s->side = SIDE_FRONT;
620
sane_get_parameters (s, NULL);
623
if (duplex && s->side == SIDE_FRONT && !start)
627
return SANE_STATUS_GOOD;
630
st = get_buffer_status(s, &data_avalible);
634
} while (!data_avalible);
636
for (i = 0; i < (duplex ? 2 : 1); i++)
638
st = buf_init (&s->buf[i], s->side_size);
643
if (pthread_create (&s->thread, NULL, (void *(*)(void *)) read_data, s))
645
st = SANE_STATUS_IO_ERROR;
649
if (s->val[CROP].b || s->val[LENGTHCTL].b || s->val[LONG_PAPER].b)
651
pthread_join (s->thread, NULL);
655
return SANE_STATUS_GOOD;
662
sane_read(SANE_Handle handle, SANE_Byte * buf,
663
SANE_Int max_len, SANE_Int * len)
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);
673
return SANE_STATUS_EOF;
680
(SANE_Int) s->read ? max_len : (SANE_Int) s->read;
681
memcpy(buf, s->data + BUF_SIZE - s->read, *len);
689
s->data = get_buf(b, &inbuf);
693
*len = max_len < inbuf ? max_len : inbuf;
696
memcpy(buf, s->data, *len);
697
s->read = inbuf > BUF_SIZE ? BUF_SIZE - *len : inbuf - *len;
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)
711
for (i = 0; i < sizeof(s->buf) / sizeof(s->buf[0]); i++)
712
buf_deinit(&s->buf[i]);
718
sane_cancel (SANE_Handle handle)
721
struct scanner *s = (struct scanner *) handle;
722
if (s->scanning && !strcmp (s->val[FEEDER_MODE].s, SANE_I18N ("continuous")))
728
pthread_cancel (s->thread);
729
pthread_join (s->thread, NULL);
732
for (i = 0; i < sizeof (s->buf) / sizeof (s->buf[0]); i++)
733
buf_deinit (&s->buf[i]);
738
sane_set_io_mode (SANE_Handle __sane_unused__ h, SANE_Bool __sane_unused__ m)
740
return SANE_STATUS_UNSUPPORTED;
744
sane_get_select_fd (SANE_Handle __sane_unused__ h,
745
SANE_Int __sane_unused__ * fd)
747
return SANE_STATUS_UNSUPPORTED;