1141cc406Sopenharmony_ci/* SANE - Scanner Access Now Easy.
2141cc406Sopenharmony_ci
3141cc406Sopenharmony_ci   Copyright (C) 2011-2020 Rolf Bensch <rolf at bensch hyphen online dot de>
4141cc406Sopenharmony_ci   Copyright (C) 2006-2007 Wittawat Yamwong <wittawat@web.de>
5141cc406Sopenharmony_ci
6141cc406Sopenharmony_ci   This file is part of the SANE package.
7141cc406Sopenharmony_ci
8141cc406Sopenharmony_ci   This program is free software; you can redistribute it and/or
9141cc406Sopenharmony_ci   modify it under the terms of the GNU General Public License as
10141cc406Sopenharmony_ci   published by the Free Software Foundation; either version 2 of the
11141cc406Sopenharmony_ci   License, or (at your option) any later version.
12141cc406Sopenharmony_ci
13141cc406Sopenharmony_ci   This program is distributed in the hope that it will be useful, but
14141cc406Sopenharmony_ci   WITHOUT ANY WARRANTY; without even the implied warranty of
15141cc406Sopenharmony_ci   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16141cc406Sopenharmony_ci   General Public License for more details.
17141cc406Sopenharmony_ci
18141cc406Sopenharmony_ci   You should have received a copy of the GNU General Public License
19141cc406Sopenharmony_ci   along with this program.  If not, see <https://www.gnu.org/licenses/>.
20141cc406Sopenharmony_ci
21141cc406Sopenharmony_ci   As a special exception, the authors of SANE give permission for
22141cc406Sopenharmony_ci   additional uses of the libraries contained in this release of SANE.
23141cc406Sopenharmony_ci
24141cc406Sopenharmony_ci   The exception is that, if you link a SANE library with other files
25141cc406Sopenharmony_ci   to produce an executable, this does not by itself cause the
26141cc406Sopenharmony_ci   resulting executable to be covered by the GNU General Public
27141cc406Sopenharmony_ci   License.  Your use of that executable is in no way restricted on
28141cc406Sopenharmony_ci   account of linking the SANE library code into it.
29141cc406Sopenharmony_ci
30141cc406Sopenharmony_ci   This exception does not, however, invalidate any other reasons why
31141cc406Sopenharmony_ci   the executable file might be covered by the GNU General Public
32141cc406Sopenharmony_ci   License.
33141cc406Sopenharmony_ci
34141cc406Sopenharmony_ci   If you submit changes to SANE to the maintainers to be included in
35141cc406Sopenharmony_ci   a subsequent release, you agree by submitting the changes that
36141cc406Sopenharmony_ci   those changes may be distributed with this exception intact.
37141cc406Sopenharmony_ci
38141cc406Sopenharmony_ci   If you write modifications of your own for SANE, it is your choice
39141cc406Sopenharmony_ci   whether to permit this exception to apply to your modifications.
40141cc406Sopenharmony_ci   If you do not wish that, delete this exception notice.
41141cc406Sopenharmony_ci */
42141cc406Sopenharmony_ci#ifndef PIXMA_COMMON_H
43141cc406Sopenharmony_ci#define PIXMA_COMMON_H
44141cc406Sopenharmony_ci
45141cc406Sopenharmony_ci
46141cc406Sopenharmony_ci#include <time.h>		/* time_t */
47141cc406Sopenharmony_ci#include "pixma.h"
48141cc406Sopenharmony_ci
49141cc406Sopenharmony_ci
50141cc406Sopenharmony_ci/*! \defgroup subdriver Subdriver Interface
51141cc406Sopenharmony_ci *  \brief Subdriver interface. */
52141cc406Sopenharmony_ci
53141cc406Sopenharmony_ci/*! \defgroup debug Debug utilities
54141cc406Sopenharmony_ci *  \brief Debug utilities. */
55141cc406Sopenharmony_ci
56141cc406Sopenharmony_ci#ifdef NDEBUG
57141cc406Sopenharmony_ci# define PDBG(x)     do {} while(0)
58141cc406Sopenharmony_ci# define PASSERT(x)  do {} while(0)
59141cc406Sopenharmony_ci#else
60141cc406Sopenharmony_ci# define PDBG(x) x
61141cc406Sopenharmony_ci# define PASSERT(x) do {				\
62141cc406Sopenharmony_ci    if (!(x))						\
63141cc406Sopenharmony_ci	pixma_dbg(1, "ASSERT failed:%s:%d: "	\
64141cc406Sopenharmony_ci		     #x "\n", __FILE__, __LINE__);	\
65141cc406Sopenharmony_ci  } while(0)
66141cc406Sopenharmony_ci#endif
67141cc406Sopenharmony_ci
68141cc406Sopenharmony_ci
69141cc406Sopenharmony_ci#define PIXMA_STATUS_OK       0x0606
70141cc406Sopenharmony_ci#define PIXMA_STATUS_FAILED   0x1515
71141cc406Sopenharmony_ci#define PIXMA_STATUS_BUSY     0x1414
72141cc406Sopenharmony_ci
73141cc406Sopenharmony_ci#define PIXMA_MAX_ID_LEN 30
74141cc406Sopenharmony_ci
75141cc406Sopenharmony_ci/* These may have been defined elsewhere */
76141cc406Sopenharmony_ci#ifndef MIN
77141cc406Sopenharmony_ci#define MIN(x,y) (((x) < (y)) ? (x):(y))
78141cc406Sopenharmony_ci#endif
79141cc406Sopenharmony_ci#ifndef MAX
80141cc406Sopenharmony_ci#define MAX(x,y) (((x) < (y)) ? (y):(x))
81141cc406Sopenharmony_ci#endif
82141cc406Sopenharmony_ci#define ALIGN_SUP(x,n) (((x) + (n) - 1) / (n) * (n))
83141cc406Sopenharmony_ci#define ALIGN_INF(x,n) (((x) / (n)) * (n))
84141cc406Sopenharmony_ci
85141cc406Sopenharmony_cistruct pixma_io_t;
86141cc406Sopenharmony_ci
87141cc406Sopenharmony_cistruct pixma_limits_t
88141cc406Sopenharmony_ci{
89141cc406Sopenharmony_ci  unsigned xdpi, ydpi;
90141cc406Sopenharmony_ci  unsigned width, height;
91141cc406Sopenharmony_ci};
92141cc406Sopenharmony_ci
93141cc406Sopenharmony_cistruct pixma_cmdbuf_t
94141cc406Sopenharmony_ci{
95141cc406Sopenharmony_ci  unsigned cmd_header_len, res_header_len, cmd_len_field_ofs;
96141cc406Sopenharmony_ci  unsigned expected_reslen, cmdlen;
97141cc406Sopenharmony_ci  int reslen;
98141cc406Sopenharmony_ci  unsigned size;
99141cc406Sopenharmony_ci  uint8_t *buf;
100141cc406Sopenharmony_ci};
101141cc406Sopenharmony_ci
102141cc406Sopenharmony_cistruct pixma_imagebuf_t
103141cc406Sopenharmony_ci{
104141cc406Sopenharmony_ci  uint8_t *wptr, *wend;
105141cc406Sopenharmony_ci  const uint8_t *rptr, *rend;
106141cc406Sopenharmony_ci};
107141cc406Sopenharmony_ci
108141cc406Sopenharmony_cistruct pixma_t
109141cc406Sopenharmony_ci{
110141cc406Sopenharmony_ci  pixma_t *next;
111141cc406Sopenharmony_ci  struct pixma_io_t *io;
112141cc406Sopenharmony_ci  const pixma_scan_ops_t *ops;
113141cc406Sopenharmony_ci  pixma_scan_param_t *param;
114141cc406Sopenharmony_ci  const pixma_config_t *cfg;
115141cc406Sopenharmony_ci  char id[PIXMA_MAX_ID_LEN + 1];
116141cc406Sopenharmony_ci  int cancel;			/* NOTE: It can be set in a signal handler. */
117141cc406Sopenharmony_ci  uint32_t events;
118141cc406Sopenharmony_ci  void *subdriver;		/* can be used by model driver. */
119141cc406Sopenharmony_ci  int rec_tmo;                  /* receive timeout [s] */
120141cc406Sopenharmony_ci  pixma_paper_source_t last_source; /* used for calibrate=once */
121141cc406Sopenharmony_ci
122141cc406Sopenharmony_ci  /* private */
123141cc406Sopenharmony_ci  uint64_t cur_image_size;
124141cc406Sopenharmony_ci  pixma_imagebuf_t imagebuf;
125141cc406Sopenharmony_ci  unsigned scanning:1;
126141cc406Sopenharmony_ci  unsigned underrun:1;
127141cc406Sopenharmony_ci};
128141cc406Sopenharmony_ci
129141cc406Sopenharmony_ci/** \addtogroup subdriver
130141cc406Sopenharmony_ci * @{ */
131141cc406Sopenharmony_ci/** Scan operations for subdriver. */
132141cc406Sopenharmony_cistruct pixma_scan_ops_t
133141cc406Sopenharmony_ci{
134141cc406Sopenharmony_ci    /** Allocate a data structure for the subdriver. It is called after the
135141cc406Sopenharmony_ci     *  core driver connected to the scanner. The subdriver should reset the
136141cc406Sopenharmony_ci     *  scanner to a known state in this function. */
137141cc406Sopenharmony_ci  int (*open) (pixma_t *);
138141cc406Sopenharmony_ci
139141cc406Sopenharmony_ci    /** Free resources allocated by the subdriver. Don't forget to send abort
140141cc406Sopenharmony_ci     *  command to the scanner if it is scanning. */
141141cc406Sopenharmony_ci  void (*close) (pixma_t *);
142141cc406Sopenharmony_ci
143141cc406Sopenharmony_ci    /** Setup the scanner for scan parameters defined in \a s->param. */
144141cc406Sopenharmony_ci  int (*scan) (pixma_t * s);
145141cc406Sopenharmony_ci
146141cc406Sopenharmony_ci    /** Fill a buffer with image data. The subdriver has two choices:
147141cc406Sopenharmony_ci     * -# Fill the buffer pointed by ib->wptr directly and leave
148141cc406Sopenharmony_ci     *    ib->rptr and ib->rend untouched. The length of the buffer is
149141cc406Sopenharmony_ci     *    ib->wend - ib->wptr. It must update ib->wptr accordingly.
150141cc406Sopenharmony_ci     * -# Update ib->rptr and ib->rend to point to the beginning and
151141cc406Sopenharmony_ci     *    the end of the internal buffer resp. The length of the buffer
152141cc406Sopenharmony_ci     *    is ib->rend - ib->rptr. This function is called again if
153141cc406Sopenharmony_ci     *    and only if pixma_read_image() has copied the whole buffer.
154141cc406Sopenharmony_ci     *
155141cc406Sopenharmony_ci     * The subdriver must wait until there is at least one byte to read or
156141cc406Sopenharmony_ci     * return 0 for the end of image. */
157141cc406Sopenharmony_ci  int (*fill_buffer) (pixma_t *, pixma_imagebuf_t * ib);
158141cc406Sopenharmony_ci
159141cc406Sopenharmony_ci    /** Cancel the scan operation if necessary and free resources allocated in
160141cc406Sopenharmony_ci     *  scan(). */
161141cc406Sopenharmony_ci  void (*finish_scan) (pixma_t *);
162141cc406Sopenharmony_ci
163141cc406Sopenharmony_ci    /** [Optional] Wait for a user's event, e.g. button event. \a timeout is
164141cc406Sopenharmony_ci     *  in milliseconds. If an event occurred before it's timed out, flags in
165141cc406Sopenharmony_ci     *  \a s->events should be set accordingly.
166141cc406Sopenharmony_ci     *  \see PIXMA_EV_* */
167141cc406Sopenharmony_ci  void (*wait_event) (pixma_t * s, int timeout);
168141cc406Sopenharmony_ci
169141cc406Sopenharmony_ci    /** Check the scan parameters. The parameters can be adjusted if they are
170141cc406Sopenharmony_ci     *  out of range, e.g. width > max_width. */
171141cc406Sopenharmony_ci  int (*check_param) (pixma_t *, pixma_scan_param_t *);
172141cc406Sopenharmony_ci
173141cc406Sopenharmony_ci    /** Read the device status. \see pixma_get_device_status() */
174141cc406Sopenharmony_ci  int (*get_status) (pixma_t *, pixma_device_status_t *);
175141cc406Sopenharmony_ci};
176141cc406Sopenharmony_ci
177141cc406Sopenharmony_ci
178141cc406Sopenharmony_ci/** \name Functions for read and write big-endian integer values */
179141cc406Sopenharmony_ci/**@{*/
180141cc406Sopenharmony_civoid pixma_set_be16 (uint16_t x, uint8_t * buf);
181141cc406Sopenharmony_civoid pixma_set_be32 (uint32_t x, uint8_t * buf);
182141cc406Sopenharmony_ciuint16_t pixma_get_be16 (const uint8_t * buf);
183141cc406Sopenharmony_ciuint32_t pixma_get_be32 (const uint8_t * buf);
184141cc406Sopenharmony_ci/**@}*/
185141cc406Sopenharmony_ci
186141cc406Sopenharmony_ci/** \name Utility functions */
187141cc406Sopenharmony_ci/**@{*/
188141cc406Sopenharmony_ciuint8_t pixma_sum_bytes (const void *data, unsigned len);
189141cc406Sopenharmony_ciint pixma_check_dpi (unsigned dpi, unsigned max);
190141cc406Sopenharmony_civoid pixma_sleep (unsigned long usec);
191141cc406Sopenharmony_civoid pixma_get_time (time_t * sec, uint32_t * usec);
192141cc406Sopenharmony_ciuint8_t * pixma_r_to_ir (uint8_t * gptr, uint8_t * sptr, unsigned w, unsigned c);
193141cc406Sopenharmony_ciuint8_t * pixma_rgb_to_gray (uint8_t * gptr, uint8_t * sptr, unsigned w, unsigned c);
194141cc406Sopenharmony_ciuint8_t * pixma_binarize_line(pixma_scan_param_t *, uint8_t * dst, uint8_t * src, unsigned width, unsigned c);
195141cc406Sopenharmony_ci/**@}*/
196141cc406Sopenharmony_ci
197141cc406Sopenharmony_ci/** \name Command related functions */
198141cc406Sopenharmony_ci/**@{*/
199141cc406Sopenharmony_ciint pixma_cmd_transaction (pixma_t *, const void *cmd, unsigned cmdlen,
200141cc406Sopenharmony_ci			   void *data, unsigned expected_len);
201141cc406Sopenharmony_ciint pixma_check_result (pixma_cmdbuf_t *);
202141cc406Sopenharmony_ciuint8_t *pixma_newcmd (pixma_cmdbuf_t *, unsigned cmd,
203141cc406Sopenharmony_ci		       unsigned dataout, unsigned datain);
204141cc406Sopenharmony_ciint pixma_exec (pixma_t *, pixma_cmdbuf_t *);
205141cc406Sopenharmony_ciint pixma_exec_short_cmd (pixma_t *, pixma_cmdbuf_t *, unsigned cmd);
206141cc406Sopenharmony_ciint pixma_map_status_errno (unsigned status);
207141cc406Sopenharmony_ci#if defined(HAVE_LIBXML2)
208141cc406Sopenharmony_ciint pixma_parse_xml_response(const char *xml_message);
209141cc406Sopenharmony_ci#endif
210141cc406Sopenharmony_ci/**@}*/
211141cc406Sopenharmony_ci
212141cc406Sopenharmony_ci#define pixma_fill_checksum(start, end) do {		\
213141cc406Sopenharmony_ci    *(end) = -pixma_sum_bytes(start, (end)-(start));	\
214141cc406Sopenharmony_ci} while(0)
215141cc406Sopenharmony_ci
216141cc406Sopenharmony_ci/** @} end of group subdriver */
217141cc406Sopenharmony_ci
218141cc406Sopenharmony_ci/** \addtogroup debug
219141cc406Sopenharmony_ci *  @{ */
220141cc406Sopenharmony_civoid pixma_set_debug_level (int level);
221141cc406Sopenharmony_ci#ifndef NDEBUG
222141cc406Sopenharmony_civoid pixma_hexdump (int level, const void *d_, unsigned len);
223141cc406Sopenharmony_ci
224141cc406Sopenharmony_ci/* len:   length of data or error code.
225141cc406Sopenharmony_ci   size:  if >= 0, force to print 'size' bytes.
226141cc406Sopenharmony_ci   max:   maximum number of bytes to print(-1 means no limit). */
227141cc406Sopenharmony_civoid pixma_dump (int level, const char *type, const void *data, int len,
228141cc406Sopenharmony_ci		 int size, int max);
229141cc406Sopenharmony_ci#  define DEBUG_DECLARE_ONLY
230141cc406Sopenharmony_ci#  include "../include/sane/sanei_debug.h"
231141cc406Sopenharmony_ci#endif /* NDEBUG */
232141cc406Sopenharmony_ci/** @} end of group debug */
233141cc406Sopenharmony_ci
234141cc406Sopenharmony_ci#endif
235