1 /*
2  * magicolor.h - SANE library for Magicolor scanners.
3  *
4  * (C) 2010 Reinhold Kainhofer <reinhold@kainhofer.com>
5  *
6  * Based on the epson2 sane backend:
7  * Based on Kazuhiro Sasayama previous
8  * Work on epson.[ch] file from the SANE package.
9  * Please see those files for original copyrights.
10  * Copyright (C) 2006 Tower Technologies
11  * Author: Alessandro Zummo <a.zummo@towertech.it>
12  *
13  * This file is part of the SANE package.
14  *
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License as
17  * published by the Free Software Foundation, version 2.
18  */
19 
20 #ifndef magicolor_h
21 #define magicolor_h
22 
23 #undef BACKEND_NAME
24 #define BACKEND_NAME magicolor
25 #define DEBUG_NOT_STATIC
26 
27 #include <sys/ioctl.h>
28 
29 #ifdef HAVE_STDDEF_H
30 #include <stddef.h>
31 #endif
32 
33 #ifdef HAVE_STDLIB_H
34 #include <stdlib.h>
35 #endif
36 
37 #ifdef NEED_SYS_TYPES_H
38 #include <sys/types.h>
39 #endif
40 
41 #include <stdio.h>
42 
43 #include "../include/sane/sane.h"
44 #include "../include/sane/sanei_debug.h"
45 #include "../include/sane/sanei_backend.h"
46 
47 /* Silence the compiler for unused arguments */
48 #define NOT_USED(x) ( (void)(x) )
49 
50 #define MAGICOLOR_CONFIG_FILE "magicolor.conf"
51 
52 #define NUM_OF_HEX_ELEMENTS (16)        /* number of hex numbers per line for data dump */
53 #define DEVICE_NAME_LEN (16)    /* length of device name in extended status */
54 
55 
56 
57 /* misc constants */
58 
59 #define NET	0x04
60 #define CMD	0x03
61 
62 
63 /* status values */
64 #define STATUS_READY		0x00	/* scanner is ready */
65 #define STATUS_ADF_JAM		0x01	/* ADF paper jam */
66 #define STATUS_OPEN		0x02	/* scanner is open */
67 #define STATUS_NOT_READY	0x03	/* scanner is in use on another interface */
68 
69 #define ADF_LOADED		0x01	/* ADF is loaded */
70 
71 #define MAGICOLOR_CAP_DEFAULT 0
72 
73 #define MAGICOLOR_LEVEL_1690mf	0
74 #define	MAGICOLOR_LEVEL_DEFAULT MAGICOLOR_LEVEL_1690mf
75 #define	MAGICOLOR_LEVEL_NET     MAGICOLOR_LEVEL_1690mf
76 
77 /* Structure holding the command set for a device */
78 struct MagicolorCmd
79 {
80 	const char *level;
81 	unsigned char scanner_cmd;
82 	unsigned char start_scanning;
83 	unsigned char request_error;
84 	unsigned char stop_scanning;
85 	unsigned char request_scan_parameters;
86 	unsigned char set_scan_parameters;
87 	unsigned char request_status;
88 	unsigned char request_data;
89 	unsigned char unknown1;
90 	unsigned char unknown2;
91 
92 	unsigned char net_wrapper_cmd;
93 	unsigned char net_welcome;
94 	unsigned char net_lock;
95 	unsigned char net_lock_ack;
96 	unsigned char net_unlock;
97 };
98 
99 /* Structure holding the device capabilities */
100 struct MagicolorCap
101 {
102 	unsigned int id;
103 	const char *cmds;
104 	const char *model;
105 	const char *OID;
106 	SANE_Int out_ep, in_ep;		/* USB bulk out/in endpoints */
107 
108 	SANE_Int optical_res;		/* optical resolution */
109 	SANE_Range dpi_range;		/* max/min resolutions */
110 
111 	SANE_Int *res_list;		/* list of resolutions */
112 	SANE_Int res_list_size;		/* number of entries in this list */
113 
114 	SANE_Int maxDepth;		/* max. color depth */
115 	SANE_Word *depth_list;		/* list of color depths */
116 
117 	SANE_Range brightness;		/* brightness range */
118 
119 	SANE_Range fbf_x_range;		/* flattbed x range */
120 	SANE_Range fbf_y_range;		/* flattbed y range */
121 
122 	SANE_Bool ADF;			/* ADF is installed */
123 	SANE_Bool adf_duplex;		/* does the ADF handle duplex scanning */
124 	SANE_Range adf_x_range;		/* autom. document feeder x range */
125 	SANE_Range adf_y_range;		/* autom. document feeder y range */
126 };
127 
128 enum {
129 	OPT_NUM_OPTS = 0,
130 	OPT_MODE_GROUP,
131 	OPT_MODE,
132 	OPT_BIT_DEPTH,
133 	OPT_BRIGHTNESS,
134 	OPT_RESOLUTION,
135 	OPT_PREVIEW,
136 	OPT_SOURCE,
137 	OPT_ADF_MODE,
138 	OPT_GEOMETRY_GROUP,
139 	OPT_TL_X,
140 	OPT_TL_Y,
141 	OPT_BR_X,
142 	OPT_BR_Y,
143 	NUM_OPTIONS
144 };
145 
146 typedef enum
147 {	/* hardware connection to the scanner */
148 	SANE_MAGICOLOR_NODEV,	/* default, no HW specified yet */
149 	SANE_MAGICOLOR_USB,	/* USB interface */
150 	SANE_MAGICOLOR_NET	/* network interface */
151 } Magicolor_Connection_Type;
152 
153 
154 /* Structure holding the hardware description */
155 
156 struct Magicolor_Device
157 {
158 	struct Magicolor_Device *next;
159 	int missing;
160 
161 	char *name;
162 	char *model;
163 
164 	SANE_Device sane;
165 
166 	SANE_Range *x_range;	/* x range w/out extension */
167 	SANE_Range *y_range;	/* y range w/out extension */
168 
169 	Magicolor_Connection_Type connection;
170 
171 	struct MagicolorCmd *cmd;
172 	struct MagicolorCap *cap;
173 };
174 
175 typedef struct Magicolor_Device Magicolor_Device;
176 
177 /* Structure holding an instance of a scanner (i.e. scanner has been opened) */
178 struct Magicolor_Scanner
179 {
180 	struct Magicolor_Scanner *next;
181 	struct Magicolor_Device *hw;
182 
183 	int fd;
184 
185 	SANE_Option_Descriptor opt[NUM_OPTIONS];
186 	Option_Value val[NUM_OPTIONS];
187 	SANE_Parameters params;
188 
189 	SANE_Bool eof;
190 	SANE_Byte *buf, *end, *ptr;
191 	SANE_Bool canceling;
192 
193 	SANE_Int left, top;
194 	SANE_Int width, height;
195 
196 	/* image block data */
197 	SANE_Int data_len;
198 	SANE_Int block_len;
199 	SANE_Int last_len;
200 	SANE_Int blocks;
201 	SANE_Int counter;
202 
203 	/* store how many bytes of the current pixel line we have already
204 	 * read in previous read attempts. Since each line will be padded
205 	 * to multiples of 512 bytes, this is needed to know which bytes
206 	 * to ignore */
207 	SANE_Int bytes_read_in_line;
208 	SANE_Byte *line_buffer;
209 	/* How many bytes are scanned per line (multiple of 512 bytes */
210 	SANE_Int scan_bytes_per_line;
211 };
212 
213 typedef struct Magicolor_Scanner Magicolor_Scanner;
214 
215 struct mode_param
216 {
217 	int flags;
218 	int colors;
219 	int depth;
220 };
221 
222 enum {
223 	MODE_BINARY, MODE_GRAY, MODE_COLOR
224 };
225 
226 #endif
227