1 /*
2   Copyright (C) 2000 by Adrian Perez Jorge
3 
4   This program is free software; you can redistribute it and/or
5   modify it under the terms of the GNU General Public License
6   as published by the Free Software Foundation; either version 2
7   of the License, or (at your option) any later version.
8 
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   GNU General Public License for more details.
13 
14   You should have received a copy of the GNU General Public License
15   along with this program.  If not, see <https://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef _HP4200_H
19 #define _HP4200_H
20 
21 #include <sys/types.h>
22 
23 #define MCLKDIV_SCALING 2
24 #define NUM_REGISTERS 0x80
25 
26 #define min(a, b) (((a) < (b)) ? (a) : (b))
27 #define max(a, b) (((a) > (b)) ? (a) : (b))
28 
29 
30 /*--------------------------------------------------------------------------*/
31 
32 #define DBG_error0  0
33 #define DBG_error   1
34 #define DBG_sense   2
35 #define DBG_warning 3
36 #define DBG_inquiry 4
37 #define DBG_info    5
38 #define DBG_info2   6
39 #define DBG_proc    7
40 #define DBG_read    8
41 #define DBG_sane_init   10
42 #define DBG_sane_proc   11
43 #define DBG_sane_info   12
44 #define DBG_sane_option 13
45 
46 /*--------------------------------------------------------------------------*/
47 
48 enum HP4200_Option
49 {
50   OPT_NUM_OPTS = 0,
51 
52   OPT_RES,
53 
54   OPT_TL_X,			/* top-left x */
55   OPT_TL_Y,			/* top-left y */
56   OPT_BR_X,			/* bottom-right x */
57   OPT_BR_Y,			/* bottom-right y */
58 
59   OPT_BACKTRACK,
60 
61   OPT_GAMMA_VECTOR_R,
62   OPT_GAMMA_VECTOR_G,
63   OPT_GAMMA_VECTOR_B,
64 
65   OPT_PREVIEW,
66 
67   NUM_OPTIONS			/* must come last */
68 };
69 
70 /* already declared in the sane includes...
71 typedef union
72 {
73 	SANE_Word w;
74 	SANE_Bool b;
75 	SANE_Fixed f;
76 	SANE_Word *wa;
77 }
78 Option_Value;
79 */
80 
81 enum ScannerModels
82 {
83   HP4200
84 };
85 
86 typedef struct HP4200_Device
87 {
88   struct HP4200_Device *next;
89   SANE_Device dev;
90   SANE_Handle handle;
91 }
92 HP4200_Device;
93 
94 struct _scanner_buffer_t
95 {
96   unsigned char *buffer;	/* buffer memory space */
97   int size;			/* size of the buffer */
98   int num_bytes;		/* number of bytes left (to read) */
99   unsigned char *data_ptr;	/* cursor in buffer */
100 };
101 
102 typedef struct _scanner_buffer_t scanner_buffer_t;
103 
104 struct _ciclic_buffer_t
105 {
106   int good_bytes;		/* number of valid bytes of the image */
107   int num_lines;		/* number of lines of the ciclic buffer */
108   int size;			/* size in bytes of the buffer space */
109   unsigned char *buffer;	/* pointer to the buffer space */
110   unsigned char **buffer_ptrs;	/* pointers to the beginning of each
111 				   line in the buffer space */
112   int can_consume;		/* num of bytes the ciclic buf can consume */
113   int current_line;		/* current scanned line */
114   int first_good_line;		/* number of lines to fill the ``pipeline'' */
115   unsigned char *buffer_position;	/* pointer to the first byte that
116 					   can be copied */
117   int pixel_position;		/* pixel position in current line */
118 
119   /* color indexes for the proper line in the ciclic buffer */
120   int red_idx;
121   int green_idx;
122   int blue_idx;
123 };
124 
125 typedef struct _ciclic_buffer_t ciclic_buffer_t;
126 
127 struct _hardware_parameters_t
128 {
129   unsigned int SRAM_size;
130   unsigned char SRAM_bandwidth;
131   unsigned long crystal_frequency;
132   unsigned int min_pixel_data_buffer_limit;
133   unsigned int motor_full_steps_per_inch;
134   float motor_max_speed;
135   unsigned int scan_bar_max_speed;
136   unsigned int start_of_scanning_area;
137   unsigned int calibration_strip_height;
138   unsigned int scan_area_width;
139   double scan_area_length;	/* in inches */
140   unsigned int sensor_num_pixels;
141   unsigned int sensor_pixel_start;
142   unsigned int sensor_pixel_end;
143   int sensor_cds_state;		/* 0 == off, 1 == on */
144   int sensor_signal_polarity;	/* 0 == ??, 1 == ?? */
145   int sensor_max_integration_time;
146   int sensor_line_separation;
147   int sensor_type;
148   unsigned int sensor_resolution;
149   int sensor_control_signals_polarity;
150   int sensor_control_signals_state;
151   int sensor_control_pixel_rate_timing;
152   int sensor_control_line_rate_timing;
153   unsigned int sensor_black_clamp_timing;	/* ??? */
154   unsigned int sensor_CIS_timing;
155   int sensor_toshiba_timing;
156   int sensor_color_modes;	/* bitmask telling color modes supported */
157   int illumination_mode;
158   int motor_control_mode;
159   int motor_paper_sense_mode;
160   int motor_pause_reverse_mode;
161   int misc_io_mode;
162   int num_tr_pulses;
163   int guard_band_duration;
164   int pulse_duration;
165   int fsteps_25_speed;
166   int fsteps_50_speed;
167   int steps_to_reverse;
168   struct
169   {
170     int red;
171     int green;
172     int blue;
173   }
174   target_value;
175   unsigned short home_sensor;
176 };
177 
178 typedef struct _hardware_parameters_t hardware_parameters_t;
179 
180 struct _user_parameters_t
181 {
182   unsigned int image_width;
183   unsigned int lines_to_scan;
184   unsigned int horizontal_resolution;
185   unsigned int vertical_resolution;
186   int hres_reduction_method;	/* interpolation/??? */
187   int vres_reduction_method;
188   SANE_Bool color;		/* color/grayscale */
189   int bpp;
190   int scan_mode;		/* preview/full scan */
191   SANE_Bool no_reverse;
192   SANE_Word gamma[3][1024];	/* gamma table for rgb */
193 };
194 
195 typedef struct _user_parameters_t user_parameters_t;
196 
197 struct _measured_parameters_t
198 {
199   unsigned int datalink_bandwidth;
200   struct
201   {
202     int red;
203     int green;
204     int blue;
205   }
206   coarse_calibration_data;
207   struct
208   {
209     int *pRedOffset;
210     int *pGreenOffset;
211     int *pBlueOffset;
212     int *pRedGain;
213     int *pGreenGain;
214     int *pBlueGain;
215   }
216   fine_calibration_data;
217   int max_integration_time;
218   int color_mode;
219 };
220 
221 typedef struct _measured_parameters_t measured_parameters_t;
222 
223 struct _runtime_parameters_t
224 {
225   long num_bytes_left_to_scan;
226   int status_bytes;		/* number of status bytes per line */
227   int image_line_size;		/* line size in bytes without status bytes */
228   int scanner_line_size;	/* line size in bytes including the
229 				   status bytes */
230   int first_pixel;		/* first pixel in the line to be scanned */
231   int steps_to_skip;
232 };
233 
234 typedef struct _runtime_parameters_t runtime_parameters_t;
235 
236 struct _HP4200_Scanner
237 {
238   struct _HP4200_Scanner *next;
239 
240   SANE_Option_Descriptor opt[NUM_OPTIONS];
241   Option_Value val[NUM_OPTIONS];
242 
243   SANE_Bool scanning;
244   SANE_Bool aborted_by_user;
245 
246   SANE_Parameters params;
247 
248   HP4200_Device *dev;
249   hardware_parameters_t hw_parms;
250   user_parameters_t user_parms;
251   measured_parameters_t msrd_parms;
252   unsigned int regs[NUM_REGISTERS];
253   int mclk;
254   float mclk_div;
255 
256   int fd;
257 
258   ciclic_buffer_t ciclic_buffer;
259   scanner_buffer_t scanner_buffer;
260   runtime_parameters_t runtime_parms;
261 };
262 
263 typedef struct _HP4200_Scanner HP4200_Scanner;
264 
265 #endif /* !_HP4200_H */
266