1 /* SANE - Scanner Access Now Easy.
2 
3    Copyright (C) 2011-2020 Rolf Bensch <rolf at bensch hyphen online dot de>
4    Copyright (C) 2007-2008 Nicolas Martin, <nicols-guest at alioth dot debian dot org>
5    Copyright (C) 2006-2007 Wittawat Yamwong <wittawat@web.de>
6 
7    This file is part of the SANE package.
8 
9    This program is free software; you can redistribute it and/or
10    modify it under the terms of the GNU General Public License as
11    published by the Free Software Foundation; either version 2 of the
12    License, or (at your option) any later version.
13 
14    This program is distributed in the hope that it will be useful, but
15    WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17    General Public License for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <https://www.gnu.org/licenses/>.
21 
22    As a special exception, the authors of SANE give permission for
23    additional uses of the libraries contained in this release of SANE.
24 
25    The exception is that, if you link a SANE library with other files
26    to produce an executable, this does not by itself cause the
27    resulting executable to be covered by the GNU General Public
28    License.  Your use of that executable is in no way restricted on
29    account of linking the SANE library code into it.
30 
31    This exception does not, however, invalidate any other reasons why
32    the executable file might be covered by the GNU General Public
33    License.
34 
35    If you submit changes to SANE to the maintainers to be included in
36    a subsequent release, you agree by submitting the changes that
37    those changes may be distributed with this exception intact.
38 
39    If you write modifications of your own for SANE, it is your choice
40    whether to permit this exception to apply to your modifications.
41    If you do not wish that, delete this exception notice.
42  */
43 #ifndef PIXMA_H
44 #define PIXMA_H
45 
46 #include "../include/sane/sane.h"
47 
48 
49 /*!
50  * \mainpage Scanner driver for Canon PIXMA MP series
51  * \section example Sample code for application
52  * \code
53  *    pixma_set_debug_level(level);
54  *    pixma_init();
55  *    nscanners = pixma_find_scanners();
56  *    devnr = choose_scanner(nscanners);
57  *    scanner = pixma_open(devnr);
58  *    setup_param(param);
59  *    pixma_check_scan_param(scanner, param);
60  *    do {
61  *        if (I_need_events &&
62  *            (ev = pixma_wait_event(scanner, timeout)) > 0) {
63  *            handle_event(ev);
64  *        }
65  *        pixma_scan(scanner, param);
66  *        while ((count = pixma_read_image(scanner, buf, len)) > 0) {
67  *            write(buf, count);
68  *            if (error_occured_in_write) {
69  *                pixma_cancel(scanner);
70  *            }
71  *        }
72  *    } while (!enough);
73  *    pixma_close(scanner);
74  *    pixma_cleanup();
75  * \endcode
76  *
77  * <b>Note:</b> pixma_cancel() can be called asynchronously to
78  * interrupt pixma_read_image(). It does not cancel the operation
79  * immediately. pixma_read_image() <em>must</em> be called until it
80  * returns zero or an error (probably \c PIXMA_ECANCELED).
81  *
82  * \section reference Reference
83  * - \subpage API
84  * - \subpage IO
85  * - \subpage subdriver
86  * - \subpage debug
87  */
88 
89 /*!
90  * \defgroup API The driver API
91  * \brief The driver API.
92  *
93  * The return value of functions that returns \c int has the following
94  * meaning if not otherwise specified:
95  *    - >= 0  if succeeded
96  *    - < 0 if failed
97  */
98 
99 #ifdef HAVE_STDINT_H
100 # include <stdint.h>		/* available in ISO C99 */
101 #else
102 # include <sys/types.h>
103 typedef uint8_t uint8_t;
104 typedef uint16_t uint16_t;
105 typedef uint32_t uint32_t;
106 #endif /* HAVE_STDINT_H */
107 
108 #ifdef HAVE_INTTYPES_H
109 # include <inttypes.h>          /* available in ISO C99 */
110 #endif /* HAVE_INTTYPES_H */
111 
112 /** \addtogroup API
113  *  @{ */
114 /** Don't forget to update the backend version in the SANE Backend specification
115  *  file: doc/descriptions/pixma.desc !!!
116  */
117 /** \name Version of the driver */
118 /**@{*/
119 #define PIXMA_VERSION_MAJOR 0
120 #define PIXMA_VERSION_MINOR 28
121 #define PIXMA_VERSION_BUILD 6
122 /**@}*/
123 
124 /** \name Error codes */
125 /**@{*/
126 #define PIXMA_EIO               -1
127 #define PIXMA_ENODEV            -2
128 #define PIXMA_EACCES            -3
129 #define PIXMA_ENOMEM            -4
130 #define PIXMA_EINVAL            -5
131 #define PIXMA_EBUSY             -6
132 #define PIXMA_ECANCELED         -7
133 #define PIXMA_ENOTSUP           -8
134 #define PIXMA_ETIMEDOUT         -9
135 #define PIXMA_EPROTO            -10
136 #define PIXMA_EPAPER_JAMMED     -11
137 #define PIXMA_ECOVER_OPEN       -12
138 #define PIXMA_ENO_PAPER         -13
139 #define PIXMA_EOF               -14
140 /**@}*/
141 
142 /** \name Capabilities for using with pixma_config_t::cap */
143 /**@{*/
144 #define PIXMA_CAP_EASY_RGB     (1 << 0)
145 #define PIXMA_CAP_GRAY         (1 << 1)
146 #define PIXMA_CAP_ADF          (1 << 2)
147 #define PIXMA_CAP_48BIT        (1 << 3)
148 #define PIXMA_CAP_GAMMA_TABLE  (1 << 4)
149 #define PIXMA_CAP_EVENTS       (1 << 5)
150 #define PIXMA_CAP_TPU          (1 << 6)
151 #define PIXMA_CAP_ADFDUP       ((1 << 7) | PIXMA_CAP_ADF)
152 #define PIXMA_CAP_CIS          (0)
153 #define PIXMA_CAP_CCD          (1 << 8)
154 #define PIXMA_CAP_LINEART      (1 << 9)
155 #define PIXMA_CAP_NEGATIVE     (1 << 10)
156 #define PIXMA_CAP_TPUIR        ((1 << 11) | PIXMA_CAP_TPU)
157 #define PIXMA_CAP_ADF_WAIT     (1 << 12)
158 #define PIXMA_CAP_ADF_JPEG     (1 << 13)    /* scanner returns image as jpeg from ADF */
159 #define PIXMA_CAP_JPEG         (1 << 14)    /* scanner always returns image as jpeg */
160 #define PIXMA_CAP_GT_4096      (1 << 15)    /* gamma table has 4096 8-bit values
161                                              * only generation 1 scanners
162                                              * usually gamma table has 1024 16-bit values
163                                              */
164 #define PIXMA_CAP_EXPERIMENT   (1 << 31)
165 /**@}*/
166 
167 /** \name Button events and related information returned by pixma_wait_event() */
168 /**@{*/
169 #define PIXMA_EV_NONE          0
170 #define PIXMA_EV_ACTION_MASK   (0xffffff)
171 #define PIXMA_EV_BUTTON1       (1 << 24)
172 #define PIXMA_EV_BUTTON2       (2 << 24)
173 #define PIXMA_EV_TARGET_MASK   (0x0f)
174 #define PIXMA_EV_ORIGINAL_MASK (0x0f00)
175 #define PIXMA_EV_DPI_MASK      (0x0f0000)
176 #define PIXMA_EV_DOC_MASK      (0xf000)
177 #define PIXMA_EV_STAT_MASK     (0xf00000)
178 #define PIXMA_EV_ORIENT_MASK   (0xf0)
179 
180 #define GET_EV_TARGET(x) (x & PIXMA_EV_TARGET_MASK)
181 #define GET_EV_ORIGINAL(x) ( (x & PIXMA_EV_ORIGINAL_MASK) >> 8 )
182 #define GET_EV_DPI(x) ( (x & PIXMA_EV_DPI_MASK) >> 16 )
183 #define GET_EV_DOC(x) ( (x & PIXMA_EV_DOC_MASK) >> 12 )
184 #define GET_EV_STAT(x) ( (x & PIXMA_EV_STAT_MASK) >> 20 )
185 #define GET_EV_ORIENT(x) ( (x & PIXMA_EV_ORIENT_MASK) >> 4 )
186 
187 /**@}*/
188 /** @} end of API group */
189 
190 #define PIXMA_CONFIG_FILE "pixma.conf"
191 #define MAX_CONF_DEVICES 15
192 
193 struct pixma_t;
194 struct pixma_scan_ops_t;
195 struct pixma_scan_param_t;
196 struct pixma_config_t;
197 struct pixma_cmdbuf_t;
198 struct pixma_imagebuf_t;
199 struct pixma_device_status_t;
200 
201 typedef struct pixma_t pixma_t;
202 typedef struct pixma_scan_ops_t pixma_scan_ops_t;
203 typedef struct pixma_scan_param_t pixma_scan_param_t;
204 typedef struct pixma_config_t pixma_config_t;
205 typedef struct pixma_cmdbuf_t pixma_cmdbuf_t;
206 typedef struct pixma_imagebuf_t pixma_imagebuf_t;
207 typedef struct pixma_device_status_t pixma_device_status_t;
208 
209 
210 /** \addtogroup API
211  *  @{ */
212 /** String index constants */
213 typedef enum pixma_string_index_t
214 {
215   PIXMA_STRING_MODEL,
216   PIXMA_STRING_ID,
217   PIXMA_STRING_LAST
218 } pixma_string_index_t;
219 
220 /** Paper sources */
221 typedef enum pixma_paper_source_t
222 {
223   PIXMA_SOURCE_FLATBED,
224   PIXMA_SOURCE_ADF,
225   PIXMA_SOURCE_TPU,
226   PIXMA_SOURCE_ADFDUP,		/* duplex */
227   PIXMA_SOURCE_NONE
228 } pixma_paper_source_t;
229 
230 /** Scan modes */
231 typedef enum pixma_scan_mode_t
232 {
233   /* standard scan modes */
234   PIXMA_SCAN_MODE_COLOR,
235   PIXMA_SCAN_MODE_GRAY,
236   /* TPU scan modes for negatives */
237   PIXMA_SCAN_MODE_NEGATIVE_COLOR,
238   PIXMA_SCAN_MODE_NEGATIVE_GRAY,
239   /* extended scan modes for 48 bit flatbed scanners */
240   PIXMA_SCAN_MODE_COLOR_48,
241   PIXMA_SCAN_MODE_GRAY_16,
242   /* 1 bit lineart scan mode */
243   PIXMA_SCAN_MODE_LINEART,
244   /* TPUIR scan mode */
245   PIXMA_SCAN_MODE_TPUIR
246 } pixma_scan_mode_t;
247 
248 typedef enum pixma_hardware_status_t
249 {
250   PIXMA_HARDWARE_OK,
251   PIXMA_HARDWARE_ERROR
252 } pixma_hardware_status_t;
253 
254 typedef enum pixma_lamp_status_t
255 {
256   PIXMA_LAMP_OK,
257   PIXMA_LAMP_WARMING_UP,
258   PIXMA_LAMP_OFF,
259   PIXMA_LAMP_ERROR
260 } pixma_lamp_status_t;
261 
262 typedef enum pixma_adf_status_t
263 {
264   PIXMA_ADF_OK,
265   PIXMA_ADF_NO_PAPER,
266   PIXMA_ADF_JAMMED,
267   PIXMA_ADF_COVER_OPEN,
268   PIXMA_ADF_ERROR
269 } pixma_adf_status_t;
270 
271 typedef enum pixma_calibration_status_t
272 {
273   PIXMA_CALIBRATION_OK,
274   PIXMA_CALIBRATION_IN_PROGRESS,
275   PIXMA_CALIBRATION_OFF,
276   PIXMA_CALIBRATION_ERROR
277 } pixma_calibration_status_t;
278 
279 typedef enum pixma_calibrate_option_t
280 {
281   PIXMA_CALIBRATE_ONCE,
282   PIXMA_CALIBRATE_ALWAYS,
283   PIXMA_CALIBRATE_NEVER,
284   PIXMA_CALIBRATE_NUM_OPTS
285 } pixma_calibrate_option_t;
286 
287 /** Device status. */
288 struct pixma_device_status_t
289 {
290   pixma_hardware_status_t hardware;
291   pixma_lamp_status_t lamp;
292   pixma_adf_status_t adf;
293   pixma_calibration_status_t cal;
294 };
295 
296 /** Scan parameters. */
297 struct pixma_scan_param_t
298 {
299     /** Size in bytes of one image line (row).
300      *  line_size >= depth / 8 * channels * w <br>
301      *  This field will be set by pixma_check_scan_param(). */
302   uint64_t line_size;
303 
304     /** Size in bytes of the whole image.
305      *  image_size = line_size * h <br>
306      *  This field will be set by pixma_check_scan_param(). */
307   uint64_t image_size;
308 
309     /** Channels per pixel. 1 = grayscale and lineart, 3 = color */
310   unsigned channels;
311 
312     /** Bits per channels.
313      *   1 =  1 bit B/W lineart (flatbed)
314      *   8 =  8 bit grayscale,
315      *       24 bit color (both flatbed)
316      *  16 = 16 bit grayscale (TPU, flatbed not implemented),
317      *       48 bit color (TPU, flatbed not implemented) */
318   unsigned depth;
319 
320   /*@{ */
321     /** Resolution. Valid values are 75,150,300,600,1200... */
322   unsigned xdpi, ydpi;
323   /*@} */
324 
325   /*! \name Scan area in pixels
326    * (0,0) = top left; positive x extends to the right; positive y to the
327    *  bottom; in pixels.
328    * xs is the offset in x direction of the selected scan range relative
329    * to the range read from the scanner and wx the width in x direction
330    * of the scan line read from scanner. */
331   /*@{ */
332   unsigned x, y, w, h,   xs, wx;
333   /*@} */
334 
335   /** Flag indicating whether the offset correction for TPU scans
336    *  was already performed (to avoid repeated corrections).
337    *  Currently only used in pixma_mp800.c sub-driver */
338   unsigned tpu_offset_added;
339 
340   /* Flag indicating if data from scanner will be in JPEG format */
341   unsigned mode_jpeg;
342 
343   /** Flag indicating whether a software-lineart scan is in progress
344    *  0 = other scan
345    *  1 = software-lineart scan */
346   unsigned software_lineart;
347 
348   /** Threshold for software-lineart scans */
349   unsigned threshold;
350 
351   /** lineart threshold curve for dynamic rasterization */
352   unsigned threshold_curve;
353 
354   /* look up table used in dynamic rasterization */
355   unsigned char lineart_lut[256];
356 
357     /** Gamma table. 4096 entries, 12 bit => 8 bit. If \c NULL, default gamma
358      *  specified by subdriver will be used. */
359   const uint8_t *gamma_table;
360 
361   /** value for auto generated gamma table */
362   double gamma;
363 
364     /** \see #pixma_paper_source_t */
365   pixma_paper_source_t source;
366 
367   /** \see #pixma_scan_mode_t */
368   pixma_scan_mode_t mode;
369 
370   /** \see #pixma_calibrate_option_t */
371   pixma_calibrate_option_t calibrate;
372 
373     /** The current page # in the same ADF scan session, 0 in non ADF */
374   unsigned adf_pageid;
375 
376   /** adf-wait */
377   unsigned adf_wait;
378   unsigned frontend_cancel;
379 };
380 
381 /** PIXMA model information */
382 struct pixma_config_t
383 {
384   /* If you change this structure, don't forget to update the device list in
385    * subdrivers. */
386   const char *name;	   /**< Model name. */
387   const char *model;   /**< Short model */
388   uint16_t vid;		     /**< USB Vendor ID */
389   uint16_t pid;		     /**< USB Product ID */
390   unsigned iface;	     /**< USB Interface number */
391   const pixma_scan_ops_t *ops;	  /**< Subdriver ops */
392   unsigned min_xdpi;   /**< Minimum horizontal resolution[DPI] */
393   unsigned min_xdpi_16;/**< Minimum horizontal resolution[DPI] for 16-bit scans */
394   unsigned xdpi;	     /**< Maximum horizontal resolution[DPI] */
395   unsigned ydpi;	     /**< Maximum vertical resolution[DPI] */
396   unsigned adftpu_min_dpi;    /**< Maximum horizontal resolution[DPI] for adf/tpu
397                                  *  only needed if ADF/TPU has another min. dpi value than 75 dpi */
398   unsigned adftpu_max_dpi;    /**< Maximum vertical resolution[DPI] for adf/tpu
399                                  *  only needed if ADF/TPU has another max. dpi value than xdpi */
400   unsigned tpuir_min_dpi;       /**< Minimum resolution[DPI] for tpu-ir
401                                    *  only needed if TPU-IR has another min. dpi value than 75 dpi */
402   unsigned tpuir_max_dpi;       /**< Maximum resolution[DPI] for tpu-ir
403                                    *  only needed if TPU-IR has another max. dpi value than xdpi */
404   unsigned width;	     /**< Maximum width of scannable area in pixels at 75DPI */
405   unsigned height;	   /**< Maximum height of scannable area in pixels at 75DPI */
406   unsigned cap;		     /**< Capability bitfield \see PIXMA_CAP_* */
407 };
408 
409 
410 /* Defined in pixma_common.c */
411 
412 /** Initialize the driver. It must be called before any other functions
413  *  except pixma_set_debug_level(). */
414 int pixma_init (void);
415 
416 /** Free resources allocated by the driver. */
417 void pixma_cleanup (void);
418 
419 /** Set the debug level.
420  *  \param[in] level the debug level
421  *    - 0 No debug output at all
422  *    - 1 Only errors and warning
423  *    - 2 General information
424  *    - 3 Debugging messages
425  *    - 10 USB traffic dump */
426 void pixma_set_debug_level (int level);
427 
428 /** Find scanners. The device number used in pixma_open(),
429  *  pixma_get_device_model(), pixma_get_device_id() and
430  *  pixma_get_device_config() must be less than the value returned by the last
431  *  call of this function.
432  *
433  *  \return The number of scanners found currently. The return value is
434  *  guaranteed to be valid until the next call to pixma_find_scanners(). */
435 int pixma_find_scanners (const char **conf_devices, SANE_Bool local_only);
436 
437 /** Return the model name of the device \a devnr. */
438 const char *pixma_get_device_model (unsigned devnr);
439 
440 /** Return the unique ID of the device \a devnr. */
441 const char *pixma_get_device_id (unsigned devnr);
442 
443 /** Return the device configuration of the device \a devnr. */
444 const struct pixma_config_t *pixma_get_device_config (unsigned devnr);
445 
446 /** Open a connection to the scanner \a devnr.
447  *  \param[in] devnr The scanner number
448  *  \param[out] handle The device handle
449  *  \see pixma_find_scanners() */
450 int pixma_open (unsigned devnr, pixma_t ** handle);
451 
452 /** Close the connection to the scanner. The scanning process is aborted
453  *  if necessary before the function returns. */
454 void pixma_close (pixma_t * s);
455 
456 /** Initiate an image acquisition process. You must keep \a sp valid until the
457  *  image acquisition process has finished. */
458 int pixma_scan (pixma_t *, pixma_scan_param_t * sp);
459 
460 /** Read a block of image data. It blocks until there is at least one byte
461  *  available or an error occurs.
462  *
463  *  \param[out] buf Pointer to the buffer
464  *  \param[in] len Size of the buffer
465  *
466  *  \retval count Number of bytes written to the buffer or error. Possible
467  *  return value:
468  *     - count = 0 for end of image
469  *     - count = \a len
470  *     - 0 < count < \a len if and only if it is the last block.
471  *     - count < 0 for error  */
472 int pixma_read_image (pixma_t *, void *buf, unsigned len);
473 
474 #if 0
475 /** Read a block of image data and write to \a fd.
476  *  \param[in] fd output file descriptor
477  *  \see pixma_read_image() */
478 int pixma_read_image_write (pixma_t *, int fd);
479 #endif
480 
481 /** Cancel the scanning process. No effect if no scanning process is in
482  *  progress. It can be called asynchronously e.g. within a signal
483  *  handle. pixma_cancel() doesn't abort the operation immediately.  It
484  *  guarantees that the current call or, at the latest, the next call to
485  *  pixma_read_image() will return zero or an error (probably PIXMA_ECANCELED). */
486 void pixma_cancel (pixma_t *);
487 
488 /** Check the scan parameters. This function can change your parameters to
489  *  match the device capability, e.g. adjust width and height to the available
490  *  area.
491  *  \return PIXMA_EINVAL for invalid parameters. */
492 int pixma_check_scan_param (pixma_t *, pixma_scan_param_t *);
493 
494 /** Wait until a scanner button is pressed or it times out. It should not be
495  *  called during image acquisition is in progress.
496  *  \param[in] timeout in milliseconds, less than 0 means forever
497  *  \return
498  *   - \c PIXMA_EV_NONE if it timed out.
499  *   - non-zero value indicates which button was pressed.
500  *  \see PIXMA_EV_*
501  */
502 uint32_t pixma_wait_event (pixma_t *, int timeout);
503 
504 /** Activate connection to scanner */
505 int pixma_activate_connection (pixma_t *);
506 
507 /** De-activate connection to scanner */
508 
509 int pixma_deactivate_connection (pixma_t *);
510 
511 
512 /** Enable or disable background tasks. Currently, the only one task
513  *  is submitting interrupt URB in background.
514  *  \param[in] enabled if not zero, enable background task.
515  *  \see pixma_set_interrupt_mode() */
516 int pixma_enable_background (pixma_t *, int enabled);
517 
518 /** Read the current device status.
519  *  \param[out] status the current device status
520  *  \return 0 if succeeded. Otherwise, failed.
521  */
522 int pixma_get_device_status (pixma_t *, pixma_device_status_t * status);
523 
524 /** Decide whether to run calibration or not.
525  *  Decision takes into account scan_param, source and last_source.
526  *  \return 0x01 for calibration and 0x00 for no calibration
527  */
528 unsigned pixma_calc_calibrate (pixma_t *);
529 
530 const char *pixma_get_string (pixma_t *, pixma_string_index_t);
531 const pixma_config_t *pixma_get_config (pixma_t *);
532 void pixma_fill_gamma_table (double gamma, uint8_t * table, unsigned n);
533 const char *pixma_strerror (int error);
534 
535 /** @} end of API group */
536 
537 #endif
538