aboutsummaryrefslogtreecommitdiff
path: root/backend/epkowa_ip.c
blob: 763a34a8350108d9544cfeb09608f191b7cbe2cc (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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
/* epkowa_ip.c -- for scanners that don't speak EPSON ESC/I natively
   Copyright (C) 2005--2008  Olaf Meeuwissen
   Copyright (C) 2009  SEIKO EPSON CORPORATION

   This file is part of the EPKOWA SANE backend.
   It defines a wrapper around the backend's "Interpreter" interface.

   The EPKOWA SANE backend 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 FITNESS
   FOR A PARTICULAR PURPOSE or MERCHANTABILITY.
   See the GNU General Public License for more details.

   You should have received a verbatim copy of the GNU General Public
   License along with this program; if not, write to:

       Free Software Foundation, Inc.
       59 Temple Place, Suite 330
       Boston, MA  02111-1307  USA

   Linking the EPKOWA SANE backend statically or dynamically with
   other modules is making a combined work based on the EPKOWA SANE
   backend.  Thus, the terms and conditions of the GNU General Public
   License cover the whole combination.

   As a special exception, the copyright holders of the EPKOWA SANE
   backend give you permission to link the EPKOWA SANE backend with
   independent modules that communicate with the EPKOWA SANE backend
   solely through the "Interpreter" interface, regardless of the
   license terms of these independent modules, and to copy and
   distribute the resulting combined work under terms of your choice,
   provided that every copy of the combined work is accompanied by a
   complete copy of the source code of the EPKOWA SANE backend (the
   version of the EPKOWA SANE backend used to produce the combined
   work), being distributed under the terms of the GNU General Public
   License plus this exception.  An independent module is a module
   which is not derived from or based on the EPKOWA SANE backend.

   Note that people who make modified versions of the EPKOWA SANE
   backend are not obligated to grant this special exception for their
   modified versions; it is their choice whether to do so.  The GNU
   General Public License gives permission to release a modified
   version without this exception; this exception also makes it
   possible to release a modified version which carries forward this
   exception.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

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

#include "epkowa_ip.h"
#include "cfg-obj.h"

#include "sane/sanei_usb.h"

#define TICK_INTERVAL	60	/* seconds */
#define TICK_TIME_OUT	15	/* intervals */

// FIXME: this ought to be part of the interpreter_type struct so that
//        every instance can have its own.
device_type *g_epson = NULL;


/* default member function implementors */

static SANE_Status _dtor (device_type *);
static int _open  (device_type *);
static int _close (device_type *);
static ssize_t _recv (device_type *, void *, size_t, SANE_Status *);
static ssize_t _send (device_type *, const void *, size_t, SANE_Status *);

static SANE_Status _free (device_type *);
static SANE_Status _ftor0 (device_type *, SANE_Parameters *,
			   SANE_Byte *, SANE_Byte *);
static SANE_Status _ftor1 (device_type *, SANE_Parameters *,
			   int, int, int, int);

/* private functions */

static void *_load (const char *, device_type *);


/* callback functions */

static ssize_t usb_read (void *buffer, size_t size);
static ssize_t usb_write (void *buffer, size_t size);
static ssize_t usb_ctrl (size_t request_type, size_t request,
                         size_t value, size_t index,
                         size_t size, void *buffer);


/*! Creates an interpreter object for a \a device if both necessary
    and available.  The object is accessible through the \a device's
    \c interpreter field once successfully created.
 */
SANE_Status
create_interpreter (device_type *device, unsigned int usb_product_id)
{
  void *cfg  = cfg_init (NULL, NULL);
  list *seen = cfg_seen (cfg, CFG_KEY_INTERPRETER);
  const cfg_interpreter_info *next = NULL;

  if (!device)			/* sanity check */
  {
    return SANE_STATUS_INVAL;
  }

  if (device->interpreter)	/* we've been here before */
  {
    if (device != device->interpreter->_device)
    {				/* internal inconsistency! */
      return SANE_STATUS_INVAL;
    }
    return SANE_STATUS_GOOD;
  }

  if (seen)
    {
      list_entry *cur = seen->cur;
      list_reset (seen);
      while ((next = list_next (seen))
             && usb_product_id != next->product)
        {
          /* condition does all the processing */
        }
      seen->cur = cur;
    }
  if (!next) return SANE_STATUS_GOOD;

  /* If we are still here, we need to set up an interpreter and load
     the corresponding module.  */

  device->interpreter = t_malloc (1, struct interpreter_type);

  if (!device->interpreter)
  {
    return SANE_STATUS_NO_MEM;
  }

  device->interpreter->_device = device;
  device->interpreter->_module = _load (next->library, device);

  if (!device->interpreter->_module)
  {
    delete (device->interpreter);
    return SANE_STATUS_INVAL;
  }

  device->interpreter->_tick_count = -1;
  device->interpreter->_table  = NULL;
  device->interpreter->_buffer = NULL;

  device->interpreter->dtor  = _dtor;
  device->interpreter->open  = _open;
  device->interpreter->close = _close;
  device->interpreter->recv  = _recv;
  device->interpreter->send  = _send;

  device->interpreter->free  = _free;
  device->interpreter->ftor0 = _ftor0;
  device->interpreter->ftor1 = _ftor1;

  return SANE_STATUS_GOOD;
}


/*! Destroys a \a device's interpreter object.
 */
SANE_Status
_dtor (device_type *device)
{
  if (!device || !device->interpreter)
  {
    return SANE_STATUS_INVAL;
  }

  device->interpreter->close (device);
  device->interpreter->free (device);

#ifdef USE_ALARM
  alarm (0);
#endif

  lt_dlclose (device->interpreter->_module);
  device->interpreter->_module = 0;

  delete (device->interpreter);

  return SANE_STATUS_GOOD;
}

/*! Frees several temporary buffers.
 */
SANE_Status
_free (device_type *device)
{
  if (!device || !device->interpreter)
  {
    return SANE_STATUS_INVAL;
  }

  delete (device->interpreter->_table);
  delete (device->interpreter->_buffer);

  return SANE_STATUS_GOOD;
}

/*! Performs whatever actions are needed when opening an interpreter
    "controlled" device.  One of these actions is disarming an alarm
    used to control whether the device can go into power saving mode
    or not.  */
int
_open  (device_type *device)
{
  struct interpreter_type *di;

  if (!device || !device->interpreter)
  {
    return -1;
  }
  di = device->interpreter;

#ifdef USE_ALARM
  alarm (0);
  log_info ("alarm (%d)", 0);
#endif
  di->_tick_count = -1;

  g_epson = device;

  if (device->fd < 0
      || !(di->_init
           ? di->_init (device->fd, usb_read, usb_write)
           : di->_init_with_ctrl (device->fd, usb_read,
                                  usb_write, usb_ctrl)))
  {
    err_fatal ("failed to initialize interpreter");
    g_epson = NULL;
    return -1;
  }

  return device->fd;
}

int
_close (device_type *device)
{
  if (!device || !device->interpreter)
  {
    return -1;
  }

  device->interpreter->_fini ();

  device->interpreter->_tick_count = 0;
#ifdef USE_ALARM
  alarm (TICK_INTERVAL);
  log_info ("alarm (%d)", TICK_INTERVAL);
#endif

  g_epson = NULL;

  return device->fd;
}

ssize_t
_send (device_type *device, const void *buffer, size_t size,
       SANE_Status *status)
{
  if (!status)
  {
    return -1;
  }

  if (!device || !device->interpreter)
  {
    *status = SANE_STATUS_INVAL;
    return -1;
  }

  /* ASSUMPTION: Interpreter does NOT change buffer's content.
   */
  if (device->interpreter->_write ((void *)buffer, size))
  {
    *status = SANE_STATUS_GOOD;
  }
  else
  {
    *status = SANE_STATUS_INVAL;
  }

  return size;
}

ssize_t
_recv (device_type *device, void *buffer, size_t size,
       SANE_Status *status)
{
  if (!status)
  {
    return -1;
  }

  if (!device || !device->interpreter)
  {
    *status = SANE_STATUS_INVAL;
    return -1;
  }

  if (device->interpreter->_read (buffer, size))
  {
    *status = SANE_STATUS_GOOD;
  }
  else
  {
    *status = SANE_STATUS_INVAL;
  }

  return size;
}

/*! Callback for use by the interpreter.  */
ssize_t
usb_read (void *buffer, size_t size)
{
  size_t n = size;

  if (!g_epson || g_epson->fd < 0)
  {
    return 0;
  }

  if (SANE_STATUS_GOOD == sanei_usb_read_bulk (g_epson->fd, buffer, &n))
  {
    if (size != n)
      err_minor ("Did not read number of bytes requested");
    return n;
  }
  else
  {
    return 0;
  }
}

/*! Callback for use by the interpreter.  */
ssize_t
usb_write (void *buffer, size_t size)
{
  size_t n = size;

  if (!g_epson || g_epson->fd < 0)
  {
    return 0;
  }

  if (SANE_STATUS_GOOD == sanei_usb_write_bulk (g_epson->fd, buffer, &n))
  {
    if (size != n)
      err_minor ("Did not read number of bytes requested");
    return n;
  }
  else
    return 0;
}

/*! Callback for use by the interpreter.  */
ssize_t
usb_ctrl (size_t request_type, size_t request, size_t value,
          size_t index, size_t size, void *buffer)
{
  size_t n = size;

  if (!g_epson || g_epson->fd < 0)
  {
    return 0;
  }

  if (SANE_STATUS_GOOD == sanei_usb_control_msg (g_epson->fd, request_type,
                                                 request, value, index,
                                                 &n, buffer))
  {
    if (size != n)
      err_minor ("Did not read number of bytes requested");
    return n;
  }
  else
    return 0;
}

#ifdef USE_ALARM
void
timer_event (int sig)
{
  sig = sig;

  log_call ();

  if (!g_epson || !g_epson->interpreter)
    return;

  if ((g_epson && 0 < g_epson->fd) || g_epson->interpreter->_tick_count == -1)
  {
    return;
  }

  g_epson->interpreter->_tick_count++;

  if (TICK_TIME_OUT - 1 <= g_epson->interpreter->_tick_count)
  {
    g_epson->interpreter->_power ();
    g_epson->interpreter->_tick_count = -1;
    return;
  }

  alarm (TICK_INTERVAL);
  log_info ("alarm (%d)", TICK_INTERVAL);
}
#endif

/*! Loads the interpreter module.  */
/*!
 */
void *
_load (const char *name, device_type *device)
{
  void *handle = NULL;
  struct interpreter_type *di = device->interpreter;

  /* FIXME: should set the search path to a list of directories based
     on the regular load path with the PACKAGE attached to each of the
     individual directories.  Should _not_ look in the regular load
     path's directories.  */
  {
    const char *path = lt_dlgetsearchpath ();
    if (!(path && strstr (path, PKGLIBDIR)))
    {
      lt_dladdsearchdir (PKGLIBDIR);
    }
    handle = lt_dlopenext (name);
  }
  if (!handle)
  {
    err_fatal ("%s", lt_dlerror());
    return NULL;
  }

  di->_init_with_ctrl = lt_dlsym (handle, "int_init_with_ctrl");
  if (di->_init_with_ctrl)
  {
    di->_init = NULL;
  }
  else
  {
    di->_init = lt_dlsym (handle, "int_init");
  }
  di->_fini  = lt_dlsym (handle, "int_fini");
  di->_read  = lt_dlsym (handle, "int_read");
  di->_write = lt_dlsym (handle, "int_write");
  di->_power = lt_dlsym (handle, "int_power_saving_mode");
  di->_s_0   = lt_dlsym (handle, "function_s_0");
  di->_s_1   = lt_dlsym (handle, "function_s_1");

  if (!
      (  (di->_init || di->_init_with_ctrl)
         && di->_fini
         && di->_read
         && di->_write
         && di->_s_0
         && di->_s_1)
    )
    {
      err_fatal ("failed to find all required interpreter API");
      di->_init_with_ctrl = NULL;
      di->_init  = NULL;
      di->_fini  = NULL;
      di->_read  = NULL;
      di->_write = NULL;
      di->_power = NULL;
      di->_s_0   = NULL;
      di->_s_1   = NULL;
      lt_dlclose (handle);
      return NULL;
    }

  /* FIXME: if we are still here, we should set up the alarm stuff
     here because _load is really a sort of ctor.  Note that open
     should disarm the alarm and close should re-arm it.  Our dtor
     should take care of disarming the alarm permanently.  */
  /* FIXME: add error handling */
  {				/* set up alarm for power saving */
#ifdef USE_ALARM
    struct sigaction act;

    /* FIXME: set act.sa_sigaction instead because that can hold a
       void (*) (int, siginfo_t *, void *) so we can pass arguments
       such as device->interpreter->_device(!) to it, perhaps.  */
    act.sa_handler = timer_event;
    sigemptyset (&act.sa_mask);
    act.sa_flags = 0;

    sigaction (SIGALRM, &act, 0);

    alarm (TICK_INTERVAL);
    log_info ("alarm (%d)", TICK_INTERVAL);
#endif
    di->_tick_count = 0;
  }
  return handle;
}

/*
 This functor is called by sane_read only.
 */
SANE_Status
_ftor0 (device_type *device, SANE_Parameters *params,
	SANE_Byte *ptr, SANE_Byte *end)
{
  if (!device || !device->interpreter || !params)
  {
    return SANE_STATUS_INVAL;
  }

  if (params->depth != 1
      && device->interpreter->_table && device->interpreter->_buffer)
  {
    int i, row;

    row = (end - ptr) / params->bytes_per_line;

    for (i = 0; i < row; ++i)
    {
      memcpy (device->interpreter->_buffer, ptr + i * params->bytes_per_line,
	      params->bytes_per_line);
      device->interpreter->_s_1 (device->interpreter->_buffer,
				 ptr + i * params->bytes_per_line,
				 params->pixels_per_line,
				 params->format == SANE_FRAME_RGB,
				 device->interpreter->_table);
    }
  }
  return SANE_STATUS_GOOD;
}

/*
  This functor is called by sane_start only.
 */
SANE_Status
_ftor1 (device_type *device, SANE_Parameters *params,
	int depth, int left, int x_dpi, int optical_res)
{
  if (!device || !device->interpreter || !params)
  {
    return SANE_STATUS_INVAL;
  }

  device->interpreter->free (device);

  if (depth != 1)
  {
    device->interpreter->_table =
      t_malloc (params->pixels_per_line, double);

    if (!device->interpreter->_table)
    {
      return SANE_STATUS_NO_MEM;
    }

    if (device->interpreter->_s_0 (left, params->pixels_per_line,
				   x_dpi, optical_res,
				   device->interpreter->_table))
    {
      device->interpreter->_buffer
        = t_malloc (params->bytes_per_line, SANE_Byte);

      if (!device->interpreter->_buffer)
      {
	delete (device->interpreter->_table);
	return SANE_STATUS_NO_MEM;
      }
    }
    else
    {
      delete (device->interpreter->_table);
    }
  }
  return SANE_STATUS_GOOD;
}