1 /*
2  * epsonds.c - Epson ESC/I-2 driver.
3  *
4  * Copyright (C) 2015 Tower Technologies
5  * Author: Alessandro Zummo <a.zummo@towertech.it>
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, version 2.
12  */
13 
14 #ifndef epsonds_h
15 #define epsonds_h
16 
17 #undef BACKEND_NAME
18 #define BACKEND_NAME epsonds
19 #define DEBUG_NOT_STATIC
20 
21 #define mode_params epsonds_mode_params
22 #define source_list epsonds_source_list
23 
24 #ifdef HAVE_SYS_IOCTL_H
25 #include <sys/ioctl.h>
26 #endif
27 
28 #ifdef HAVE_STDDEF_H
29 #include <stddef.h>
30 #endif
31 
32 #ifdef HAVE_STDLIB_H
33 #include <stdlib.h>
34 #endif
35 
36 #ifdef NEED_SYS_TYPES_H
37 #include <sys/types.h>
38 #endif
39 
40 #include <string.h> /* for memset and memcpy */
41 #include <stdio.h>
42 
43 #include "sane/sane.h"
44 #include "sane/sanei_backend.h"
45 #include "sane/sanei_debug.h"
46 #include "sane/sanei_usb.h"
47 #include "sane/sanei_jpeg.h"
48 
49 #define EPSONDS_CONFIG_FILE "epsonds.conf"
50 
51 #ifndef PATH_MAX
52 #define PATH_MAX (1024)
53 #endif
54 
55 #ifndef XtNumber
56 #define XtNumber(x)  (sizeof(x) / sizeof(x[0]))
57 #define XtOffset(p_type, field)  ((size_t)&(((p_type)NULL)->field))
58 #define XtOffsetOf(s_type, field)  XtOffset(s_type*, field)
59 #endif
60 
61 #define ACK	0x06
62 #define NAK	0x15
63 #define	FS	0x1C
64 
65 #define FBF_STR SANE_I18N("Flatbed")
66 #define TPU_STR SANE_I18N("Transparency Unit")
67 #define ADF_STR SANE_I18N("Automatic Document Feeder")
68 
69 enum {
70 	OPT_NUM_OPTS = 0,
71 	OPT_MODE_GROUP,
72 	OPT_MODE,
73 	OPT_DEPTH,
74 	OPT_RESOLUTION,
75 	OPT_GEOMETRY_GROUP,
76 	OPT_TL_X,
77 	OPT_TL_Y,
78 	OPT_BR_X,
79 	OPT_BR_Y,
80 	OPT_EQU_GROUP,
81 	OPT_SOURCE,
82 	OPT_EJECT,
83 	OPT_LOAD,
84 	OPT_ADF_MODE,
85 	OPT_ADF_SKEW,
86 	NUM_OPTIONS
87 };
88 
89 typedef enum
90 {	/* hardware connection to the scanner */
91 	SANE_EPSONDS_NODEV,	/* default, no HW specified yet */
92 	SANE_EPSONDS_USB,	/* USB interface */
93 	SANE_EPSONDS_NET	/* network interface */
94 } epsonds_conn_type;
95 
96 /* hardware description */
97 
98 struct epsonds_device
99 {
100 	struct epsonds_device *next;
101 
102 	epsonds_conn_type connection;
103 
104 	char *name;
105 	char *model;
106 
107 	unsigned int model_id;
108 
109 	SANE_Device sane;
110 	SANE_Range *x_range;
111 	SANE_Range *y_range;
112 	SANE_Range dpi_range;
113 	SANE_Byte alignment;
114 
115 
116 	SANE_Int *res_list;		/* list of resolutions */
117 	SANE_Int *depth_list;
118 	SANE_Int max_depth;		/* max. color depth */
119 
120 	SANE_Bool has_raw;		/* supports RAW format */
121 
122 	SANE_Bool has_fb;		/* flatbed */
123 	SANE_Range fbf_x_range;	        /* x range */
124 	SANE_Range fbf_y_range;	        /* y range */
125 	SANE_Byte fbf_alignment;	/* left, center, right */
126 	SANE_Bool fbf_has_skew;		/* supports skew correction */
127 
128 	SANE_Bool has_adf;		/* adf */
129 	SANE_Range adf_x_range;	        /* x range */
130 	SANE_Range adf_y_range;	        /* y range */
131 	SANE_Bool adf_is_duplex;	/* supports duplex mode */
132 	SANE_Bool adf_singlepass;	/* supports single pass duplex */
133 	SANE_Bool adf_has_skew;		/* supports skew correction */
134 	SANE_Bool adf_has_load;		/* supports load command */
135 	SANE_Bool adf_has_eject;	/* supports eject command */
136 	SANE_Byte adf_alignment;	/* left, center, right */
137 	SANE_Byte adf_has_dfd;		/* supports double feed detection */
138 
139 	SANE_Bool has_tpu;		/* tpu */
140 	SANE_Range tpu_x_range;	        /* transparency unit x range */
141 	SANE_Range tpu_y_range;	        /* transparency unit y range */
142 };
143 
144 typedef struct epsonds_device epsonds_device;
145 
146 typedef struct ring_buffer
147 {
148 	SANE_Byte *ring, *wp, *rp, *end;
149 	SANE_Int fill, size;
150 
151 } ring_buffer;
152 
153 /* an instance of a scanner */
154 
155 struct epsonds_scanner
156 {
157 	struct epsonds_scanner *next;
158 	struct epsonds_device *hw;
159 
160 	int fd;
161 
162 	SANE_Option_Descriptor opt[NUM_OPTIONS];
163 	Option_Value val[NUM_OPTIONS];
164 	SANE_Parameters params;
165 
166 	size_t bsz;		/* transfer buffer size */
167 	SANE_Byte *buf, *line_buffer;
168 	ring_buffer *current, front, back;
169 
170 	SANE_Bool eof, scanning, canceling, locked, backside, mode_jpeg;
171 
172 	SANE_Int left, top, pages, dummy;
173 
174 	/* jpeg stuff */
175 
176 	djpeg_dest_ptr jdst;
177 	struct jpeg_decompress_struct jpeg_cinfo;
178 	struct jpeg_error_mgr jpeg_err;
179 	SANE_Bool jpeg_header_seen;
180 
181 	/* network buffers */
182 	unsigned char *netbuf, *netptr;
183 	size_t netlen;
184 };
185 
186 typedef struct epsonds_scanner epsonds_scanner;
187 
188 struct mode_param
189 {
190 	int color;
191 	int flags;
192 	int dropout_mask;
193 	int depth;
194 };
195 
196 enum {
197 	MODE_BINARY, MODE_GRAY, MODE_COLOR
198 };
199 
200 #endif
201