1 /*
2 * kodakaio.c - SANE library for Kodak ESP Aio scanners.
3 *
4 * Copyright (C) 2011-2017 Paul Newall
5 *
6 * Based on the Magicolor sane backend:
7 * Based on the epson2 sane backend:
8 * Based on Kazuhiro Sasayama previous
9 * work on epson.[ch] file from the SANE package.
10 * Please see those files for additional copyrights.
11 * Author: Paul Newall
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License as
15 * published by the Free Software Foundation, version 2.
16
17 * Modified 30/12/14 to fix bug where network connection was broken after 30s of idle time.
18 * The connection is now made in sane_start and ended in sane_cancel.
19 * 01/01/13 Now with adf, the scan can be padded to make up the full page length,
20 * or the page can terminate at the end of the paper. This is a selectable option.
21 * 25/11/12 Using avahi now for net autodiscovery. Use configure option --with-avahi to make sure it's enabled
22 * 1/5/17 patched to use local pointer for avahi callback
23 */
24
25 /*
26 Packages to add to a clean ubuntu install
27 libavahi-client-dev
28 libusb-dev
29 libsnmp-dev
30
31 convenient lines to paste
32 export SANE_DEBUG_KODAKAIO=20
33
34 for ubuntu prior to 12.10
35 ./configure --prefix=/usr --sysconfdir=/etc --localstatedir=/var BACKENDS="kodakaio test"
36
37 for ubuntu 12.10
38 ./configure --prefix=/usr --libdir=/usr/lib/i386-linux-gnu --sysconfdir=/etc --localstatedir=/var BACKENDS="kodakaio test"
39
40 for ubuntu 14.10 up to at least 17.04
41 ./configure --prefix=/usr --libdir=/usr/lib/x86_64-linux-gnu --sysconfdir=/etc --localstatedir=/var BACKENDS="kodakaio test"
42
43 If you want to use the test backend, for example with sane-troubleshoot, you should enable it in /etc/sane.d/dll.conf
44
45 */
46
47 /* SANE-FLOW-DIAGRAM Kodakaio commands in [] brackets
48
49 - sane_init() : initialize backend, attach scanners(devicename,0)
50 . - sane_get_devices() : query list of scanner-devices
51 . - sane_open() : open a particular scanner-device and attach_scanner(devicename,&dev)
52 . . - sane_set_io_mode : set blocking-mode
53 . . - sane_get_select_fd : get scanner-fd
54 . . - sane_get_option_descriptor() : get option information
55 . . - sane_control_option() : change option values
56 . .
57 . . - sane_start() : start image acquisition [V,L,F,S,C,D,O,Z] first time or after cancel. [(F),E,G] every time
58 . . - sane_get_parameters() : returns actual scan-parameters
59 . . - sane_read() : read image-data (from pipe)
60 . . - sane_cancel() : cancel operation, kill reader_process [(F), U]
61
62 . - sane_close() : close opened scanner-device, do_cancel, free buffer and handle
63 - sane_exit() : terminate use of backend, free devicename and device-struture
64 */
65 /* FUNCTION-TREE
66 sane_init
67 sane_open
68 device_detect
69 k_dev_init
70 open_scanner
71 close_scanner
72 sane_get_devices
73 init_options
74 (open_scanner - moved to sane_start 27/12/14 )
75 sane_control_option
76 getvalue
77 setvalue
78 search_string_list
79 change_source
80 activateOption
81 deactivateOption
82 sane_start
83 open_scanner
84 k_init_parametersta
85 k_lock_scanner
86 k_hello
87 k_set_scanning_parameters
88 print_params
89 k_start_scan
90 cmd_start_scan
91 print_status
92 k_send
93 kodakaio_txrxack
94 sane_get_parameters
95 print_params
96 sane_read
97 k_read
98 cmd_read_data (reads one block)
99 k_recv
100 cmp_array
101 sane_cancel
102 cmd_cancel_scan
103 close_scanner
104 sane_close
105 (close_scanner - moved to sane_cancel 27/12/14)
106 sane_exit
107 free_devices
108 k_recv
109 kodakaio_net_read
110 dump_hex_buffer_dense
111 k_send
112 sanei_kodakaio_net_write_raw
113 dump_hex_buffer_dense
114 open_scanner
115 sanei_kodakaio_net_open
116 close_scanner
117 k_scan_finish
118 cmd_cancel_scan
119 sanei_kodakaio_net_close or sanei_usb_close
120 detect_usb
121 kodakaio_getNumberOfUSBProductIds
122 attach_one_config - (Passed to sanei_configure_attach)
123 kodakaio_getNumberOfUSBProductIds
124 kodak_network_discovery
125 client_callback
126 browse_callback
127 resolve_callback
128 ProcessAvahiDevice
129 attach_one_net
130 attach_one_net
131 attach
132 device_detect
133 attach_one_usb - (passed to sanei_usb_find_devices)
134 attach
135 device_detect
136 k_lock_scanner
137 kodakaio_txrx
138 k_send
139 k_recv
140 kodakaio_txrxack
141 k_send
142 k_recv
143 cmd_set_color_curve
144 kodakaio_expect_ack
145 k_recv
146 cmd_cancel_scan
147 kodakaio_txrxack
148 cmd_set_scanning_parameters
149 kodakaio_txrxack
150 device_detect
151 k_dev_init
152 */
153
154
155 #define KODAKAIO_VERSION 02
156 #define KODAKAIO_REVISION 7
157 #define KODAKAIO_BUILD 3
158
159 /* for usb (but also used for net though it's not required). */
160 #define MAX_BLOCK_SIZE 32768
161
162 #define SCANNER_READ_TIMEOUT 15
163 /* POLL_ITN_MS sets the individual poll timeout for network discovery */
164 #define POLL_ITN_MS 20
165
166
167 /* debugging levels:
168 In terminal use: export SANE_DEBUG_KODAKAIO=40 to set the level to 40 or whatever
169 level you want.
170 Then you can scan with scanimage or simple-scan from terminal and see debug info
171
172 use these defines to promote certain functions that you are interested in
173 define low values to make detail of a section appear when DBG level is low
174 define a high value eg 99 to get normal behaviour. */
175 #define DBG_READ 99
176 #define DBG_AUTO 99 /* for autodiscovery */
177
178 /*
179 normal levels. This system is a plan rather than a reality
180 *
181 * 127 recv buffer
182 * 125 send buffer
183 * 35 fine-grained status and progress
184 * 30 sane_read
185 * 25 setvalue, getvalue, control_option
186 * 20 low-level (I/O) functions
187 * 15 mid-level functions
188 * 10 high-level functions
189 * 7 open/close/attach
190 * 6 print_params
191 * 5 basic functions
192 * 3 status info and progress
193 * 2 sane api
194 * 1 errors & warnings
195 */
196
197 #include "sane/config.h"
198
199 #include <limits.h>
200 #include <stdio.h>
201 #include <string.h>
202 #include <stdlib.h>
203 #include <ctype.h>
204 #include <fcntl.h>
205 #include <unistd.h>
206 #include <errno.h>
207 #include <sys/time.h>
208 #include <math.h>
209 #include <poll.h>
210 #include <time.h>
211 #include <sys/socket.h>
212
213 #if WITH_AVAHI
214 /* used for auto detecting network printers */
215 #include <assert.h>
216 #include <avahi-client/client.h>
217 #include <avahi-client/lookup.h>
218 #include <avahi-common/simple-watch.h>
219 #include <avahi-common/malloc.h>
220 #include <avahi-common/error.h>
221 #endif
222
223 #include "../include/sane/saneopts.h"
224 #include "../include/sane/sanei_usb.h"
225 #include "../include/sane/sanei_tcp.h"
226 #include "../include/sane/sanei_udp.h"
227 #include "../include/sane/sanei_config.h"
228 #include "../include/sane/sanei_backend.h"
229
230 #include "kodakaio.h"
231
232 /* vendor and product ids that are allowed */
233 #define SANE_KODAKAIO_VENDOR_ID (0x040a)
234
235 #define min(x,y) (((x)<(y))?(x):(y))
236
237 /* I think these timeouts (ms) are defaults, overridden by any timeouts in the kodakaio.conf file */
238 static int K_SNMP_Timeout = 3000; /* used for any auto detection method */
239 static int K_Scan_Data_Timeout = 10000;
240 static int K_Request_Timeout = 5000;
241
242 /* static int bitposn=0; was used to pack bits into bytes in lineart mode */
243
244 /* This file is used to store directly the raster returned by the scanner for debugging
245 If RawScanPath has no length it will not be created */
246 FILE *RawScan = NULL;
247 /* example: char RawScanPath[] = "TestRawScan.pgm"; */
248 char RawScanPath[] = ""; /* empty path means no raw scan file is made */
249
250 /*
251 * Devices supported by this backend
252 */
253
254 /* kodak command strings */
255 static unsigned char KodakEsp_V[] = {0x1b,'S','V',0,0,0,0,0}; /* version?? */
256 static unsigned char KodakEsp_v[] = {0x1b,'s','v',0,0,0,0,0}; /* reply to version?? */
257 static unsigned char KodakEsp_Lock[] = {0x1b,'S','L',0,0,0,0,0}; /* Locks scanner */
258 static unsigned char KodakEsp_UnLock[] = {0x1b,'S','U',0,0,0,0,0}; /* Unlocks scanner */
259 static unsigned char KodakEsp_Ack[] = {0x1b,'S','S',0,0,0,0,0}; /* Acknowledge for all commands */
260 /* the bytes after esc S S 0 may indicate status: S S 0 1 = docs in adf */
261 static unsigned char KodakEsp_F[] = {0x1b,'S','F',0,0,0,0,0}; /* Purpose not known? colour balance?*/
262 static unsigned char KodakEsp_Comp[] = {0x1b,'S','C',3,8,3,0,0}; /* 3,8,3,1,0 does compression. */
263 /* The compression method is unknown */
264 /* static unsigned char KodakEsp_E[] = {0x1b,'S','E',1,0,0,0,0}; NET Purpose not known */
265 /* the extra 1 below could be despeckle option? maybe only for Hero 9.1 but no errors with ESP5250 */
266 static unsigned char KodakEsp_E[] = {0x1b,'S','E',1,1,0,0,0};
267 static unsigned char KodakEsp_Go[] = {0x1b,'S','G',0,0,0,0,0}; /* Starts the scan */
268 /* Other commands are: D (resolution), O (top left), Z (bottom right), R, G, B (curves) */
269
270 /* What is the relationship between these and the ranges in cap? */
271 static SANE_Int kodakaio_resolution_list[] = {75, 150, 300, 600, 1200};
272 static SANE_Int kodakaio_depth_list[] = {1,8}; /* The first value is the number of following entries */
273
274 /* strings to try and match the model ';' separator
275 static unsigned char SupportedMatchString[] = "KODAK ESP;KODAK HERO;KODAK OFFICE HERO;ADVENT WiFi AIO;"; */
276
277 static struct KodakaioCap kodakaio_cap[] = {
278 /* usbid,commandtype, modelname, USBoutEP, USBinEP,
279 opticalres, {dpi range}, pointer to res list, res list size
280 max depth, pointer to depth list,
281 flatbed x range, flatbed y range,
282 adf present, adf duplex,
283 adf x range, adf y range (y range should be set a little shorter than the paper being scanned)
284
285 The following are not used but may be in future
286 commandtype, max depth, pointer to depth list
287 */
288
289 /* list of cap data the first scanner is the default
290 */
291 /* KODAK AIO DEFAULT, */
292 {
293 0x9999, "esp", "KODAK AIO DEFAULT",
294 -1, 0x82, /* USBoutEP, USBinEP (-1 means find one) */
295 600, {75, 600, 0}, kodakaio_resolution_list, 4, /* 600 dpi max, 4 resolutions */
296 8, kodakaio_depth_list, /* color depth max 8, list above */
297 {0, SANE_FIX(8.5 * MM_PER_INCH), 0}, {0, SANE_FIX(11.7 * MM_PER_INCH), 0}, /* FBF x/y ranges */
298 SANE_FALSE, SANE_FALSE, /* ADF, duplex */
299 {0, SANE_FIX(100), 0}, {0, SANE_FIX(100), 0} /* ADF x/y ranges (TODO!) */
300 },
301 /* KODAK ESP 5100, */
302 {
303 0x4025, "esp", "KODAK ESP 5100 AiO",
304 -1, 0x82, /* USBoutEP, USBinEP (-1 means find one) */
305 600, {75, 600, 0}, kodakaio_resolution_list, 4, /* 600 dpi max, 4 resolutions */
306 8, kodakaio_depth_list, /* color depth max 8, list above */
307 {0, SANE_FIX(8.5 * MM_PER_INCH), 0}, {0, SANE_FIX(11.7 * MM_PER_INCH), 0}, /* FBF x/y ranges */
308 SANE_FALSE, SANE_FALSE, /* ADF, duplex */
309 {0, SANE_FIX(8.5 * MM_PER_INCH), 0}, {0, SANE_FIX(14 * MM_PER_INCH), 0} /* 14 ADF x/y ranges */
310 },
311 /* KODAK ESP 5300, */
312 {
313 0x4026, "esp", "KODAK ESP 5300 AiO",
314 -1, 0x82, /* USBoutEP, USBinEP (-1 means find one) */
315 600, {75, 600, 0}, kodakaio_resolution_list, 4, /* 600 dpi max, 4 resolutions */
316 8, kodakaio_depth_list, /* color depth max 8, list above */
317 {0, SANE_FIX(8.5 * MM_PER_INCH), 0}, {0, SANE_FIX(11.7 * MM_PER_INCH), 0}, /* FBF x/y ranges */
318 SANE_FALSE, SANE_FALSE, /* ADF, duplex */
319 {0, SANE_FIX(8.5 * MM_PER_INCH), 0}, {0, SANE_FIX(14 * MM_PER_INCH), 0} /* 14 ADF x/y ranges */
320 },
321 /* KODAK ESP 5500, */
322 {
323 0x4027, "esp", "KODAK ESP 5500 AiO",
324 -1, 0x82, /* USBoutEP, USBinEP (-1 means find one) */
325 600, {75, 600, 0}, kodakaio_resolution_list, 4, /* 600 dpi max, 4 resolutions */
326 8, kodakaio_depth_list, /* color depth max 8, list above */
327 {0, SANE_FIX(8.5 * MM_PER_INCH), 0}, {0, SANE_FIX(11.7 * MM_PER_INCH), 0}, /* FBF x/y ranges */
328 SANE_FALSE, SANE_FALSE, /* ADF, duplex */
329 {0, SANE_FIX(8.5 * MM_PER_INCH), 0}, {0, SANE_FIX(14 * MM_PER_INCH), 0} /* 14 ADF x/y ranges */
330 },
331 /* KODAK ESP 5000, */
332 {
333 0x4028, "esp", "KODAK ESP 5000 Series AiO",
334 -1, 0x82, /* USBoutEP, USBinEP (-1 means find one) */
335 600, {75, 600, 0}, kodakaio_resolution_list, 4, /* 600 dpi max, 4 resolutions */
336 8, kodakaio_depth_list, /* color depth max 8, list above */
337 {0, SANE_FIX(8.5 * MM_PER_INCH), 0}, {0, SANE_FIX(11.7 * MM_PER_INCH), 0}, /* FBF x/y ranges */
338 SANE_FALSE, SANE_FALSE, /* ADF, duplex */
339 {0, SANE_FIX(8.5 * MM_PER_INCH), 0}, {0, SANE_FIX(14 * MM_PER_INCH), 0} /* 14 ADF x/y ranges */
340 },
341 /* KODAK ESP 3300, */
342 {
343 0x4031, "esp", "KODAK ESP 3300 Series AiO",
344 -1, 0x82, /* USBoutEP, USBinEP (-1 means find one) */
345 600, {75, 600, 0}, kodakaio_resolution_list, 4, /* 600 dpi max, 4 resolutions */
346 8, kodakaio_depth_list, /* color depth max 8, list above */
347 {0, SANE_FIX(8.5 * MM_PER_INCH), 0}, {0, SANE_FIX(11.7 * MM_PER_INCH), 0}, /* FBF x/y ranges */
348 SANE_FALSE, SANE_FALSE, /* ADF, duplex */
349 {0, SANE_FIX(8.5 * MM_PER_INCH), 0}, {0, SANE_FIX(14 * MM_PER_INCH), 0} /* 14 ADF x/y ranges */
350 },
351 /* KODAK ESP5, */
352 {
353 0x4032, "esp", "KODAK ESP 5 AiO",
354 -1, 0x82, /* USBoutEP, USBinEP (-1 means find one) */
355 600, {75, 600, 0}, kodakaio_resolution_list, 4, /* 600 dpi max, 4 resolutions */
356 8, kodakaio_depth_list, /* color depth max 8, list above */
357 {0, SANE_FIX(8.5 * MM_PER_INCH), 0}, {0, SANE_FIX(11.7 * MM_PER_INCH), 0}, /* FBF x/y ranges */
358 SANE_FALSE, SANE_FALSE, /* ADF, duplex */
359 {0, SANE_FIX(8.5 * MM_PER_INCH), 0}, {0, SANE_FIX(14 * MM_PER_INCH), 0} /* 14 ADF x/y ranges */
360 },
361 /* KODAK ESP7, */
362 {
363 0x403E, "esp", "KODAK ESP 7 AiO",
364 -1, 0x82, /* USBoutEP, USBinEP (-1 means find one) */
365 600, {75, 600, 0}, kodakaio_resolution_list, 4, /* 600 dpi max, 4 resolutions */
366 8, kodakaio_depth_list, /* color depth max 8, list above */
367 {0, SANE_FIX(8.5 * MM_PER_INCH), 0}, {0, SANE_FIX(11.7 * MM_PER_INCH), 0}, /* FBF x/y ranges */
368 SANE_FALSE, SANE_FALSE, /* ADF, duplex */
369 {0, SANE_FIX(8.5 * MM_PER_INCH), 0}, {0, SANE_FIX(14 * MM_PER_INCH), 0} /* 14 ADF x/y ranges */
370 },
371 /* KODAK ESP9, */
372 {
373 0x403F, "esp", "KODAK ESP 9 AiO",
374 -1, 0x82, /* USBoutEP, USBinEP (-1 means find one) */
375 600, {75, 600, 0}, kodakaio_resolution_list, 4, /* 600 dpi max, 4 resolutions */
376 8, kodakaio_depth_list, /* color depth max 8, list above */
377 {0, SANE_FIX(8.5 * MM_PER_INCH), 0}, {0, SANE_FIX(11.7 * MM_PER_INCH), 0}, /* FBF x/y ranges */
378 SANE_FALSE, SANE_FALSE, /* ADF, duplex */
379 {0, SANE_FIX(8.5 * MM_PER_INCH), 0}, {0, SANE_FIX(14 * MM_PER_INCH), 0} /* 14 ADF x/y ranges */
380 },
381 /* KODAK ESP5210 or 5250, */
382 {
383 0x4041, "esp", "KODAK ESP 5200 Series AiO",
384 -1, 0x82, /* USBoutEP, USBinEP (-1 means find one) */
385 600, {75, 600, 0}, kodakaio_resolution_list, 4, /* 600 dpi max, 4 resolutions */
386 8, kodakaio_depth_list, /* color depth max 8, list above */
387 {0, SANE_FIX(8.5 * MM_PER_INCH), 0}, {0, SANE_FIX(11.7 * MM_PER_INCH), 0}, /* FBF x/y ranges */
388 SANE_FALSE, SANE_FALSE, /* ADF, duplex */
389 {0, SANE_FIX(8.5 * MM_PER_INCH), 0}, {0, SANE_FIX(14 * MM_PER_INCH), 0} /* 14 ADF x/y ranges */
390 },
391 /* KODAK ESP3200 , */
392 {
393 0x4043, "esp", "KODAK ESP 3200 Series AiO",
394 -1, 0x82,
395 600, {75, 600, 0}, kodakaio_resolution_list, 4, /* 600 dpi max, 4 resolutions */
396 8, kodakaio_depth_list, /* color depth max 8, list above */
397 {0, SANE_FIX(8.5 * MM_PER_INCH), 0}, {0, SANE_FIX(11.7 * MM_PER_INCH), 0}, /* FBF x/y ranges */
398 SANE_FALSE, SANE_FALSE, /* ADF, duplex */
399 {0, SANE_FIX(8.5 * MM_PER_INCH), 0}, {0, SANE_FIX(14 * MM_PER_INCH), 0} /* 14 ADF x/y ranges */
400 },
401 /* KODAK ESP4100 , */
402 {
403 0x4053, "esp", "KODAK ESP Office 4100 Series AiO",
404 -1, 0x82,
405 600, {75, 600, 0}, kodakaio_resolution_list, 4, /* 600 dpi max, 4 resolutions */
406 8, kodakaio_depth_list, /* color depth max 8, list above */
407 {0, SANE_FIX(8.5 * MM_PER_INCH), 0}, {0, SANE_FIX(11.7 * MM_PER_INCH), 0}, /* FBF x/y ranges */
408 SANE_FALSE, SANE_FALSE, /* ADF, duplex */
409 {0, SANE_FIX(8.5 * MM_PER_INCH), 0}, {0, SANE_FIX(14 * MM_PER_INCH), 0} /* 14 ADF x/y ranges */
410 },
411 /* KODAK ESP6100 , */
412 {
413 0x4054, "esp", "KODAK ESP Office 6100 Series AiO",
414 -1, 0x82,
415 600, {75, 600, 0}, kodakaio_resolution_list, 4, /* 600 dpi max, 4 resolutions */
416 8, kodakaio_depth_list, /* color depth max 8, list above */
417 {0, SANE_FIX(8.5 * MM_PER_INCH), 0}, {0, SANE_FIX(11.7 * MM_PER_INCH), 0}, /* FBF x/y ranges */
418 SANE_TRUE, SANE_FALSE, /* ADF, duplex */
419 {0, SANE_FIX(8.5 * MM_PER_INCH), 0}, {0, SANE_FIX(14 * MM_PER_INCH), 0} /* 14 ADF x/y ranges */
420 },
421 /* KODAK ESP7200 , */
422 {
423 0x4056, "esp", "KODAK ESP 7200 Series AiO",
424 -1, 0x82,
425 600, {75, 600, 0}, kodakaio_resolution_list, 4, /* 600 dpi max, 4 resolutions */
426 8, kodakaio_depth_list, /* color depth max 8, list above */
427 {0, SANE_FIX(8.5 * MM_PER_INCH), 0}, {0, SANE_FIX(11.7 * MM_PER_INCH), 0}, /* FBF x/y ranges */
428 SANE_FALSE, SANE_FALSE, /* ADF, duplex */
429 {0, SANE_FIX(8.5 * MM_PER_INCH), 0}, {0, SANE_FIX(14 * MM_PER_INCH), 0} /* 14 ADF x/y ranges */
430 },
431 /* KODAK ESP C110 , */
432 {
433 0x4057, "esp", "KODAK ESP C110 AiO",
434 -1, 0x82,
435 600, {75, 600, 0}, kodakaio_resolution_list, 4, /* 600 dpi max, 4 resolutions */
436 8, kodakaio_depth_list, /* color depth max 8, list above */
437 {0, SANE_FIX(8.5 * MM_PER_INCH), 0}, {0, SANE_FIX(11.7 * MM_PER_INCH), 0}, /* FBF x/y ranges */
438 SANE_FALSE, SANE_FALSE, /* ADF, duplex */
439 {0, SANE_FIX(8.5 * MM_PER_INCH), 0}, {0, SANE_FIX(14 * MM_PER_INCH), 0} /* 14 ADF x/y ranges */
440 },
441 /* KODAK ESP C115 , */
442 {
443 0x4058, "esp", "KODAK ESP C115 AiO",
444 -1, 0x82,
445 600, {75, 600, 0}, kodakaio_resolution_list, 4, /* 600 dpi max, 4 resolutions */
446 8, kodakaio_depth_list, /* color depth max 8, list above */
447 {0, SANE_FIX(8.5 * MM_PER_INCH), 0}, {0, SANE_FIX(11.7 * MM_PER_INCH), 0}, /* FBF x/y ranges */
448 SANE_FALSE, SANE_FALSE, /* ADF, duplex */
449 {0, SANE_FIX(8.5 * MM_PER_INCH), 0}, {0, SANE_FIX(14 * MM_PER_INCH), 0} /* 14 ADF x/y ranges */
450 },
451 /* KODAK ESP 2150 , */
452 {
453 0x4059, "esp", "KODAK ESP Office 2150 Series",
454 -1, 0x82,
455 600, {75, 600, 0}, kodakaio_resolution_list, 4, /* 600 dpi max, 4 resolutions */
456 8, kodakaio_depth_list, /* color depth max 8, list above */
457 {0, SANE_FIX(8.5 * MM_PER_INCH), 0}, {0, SANE_FIX(11.7 * MM_PER_INCH), 0}, /* FBF x/y ranges */
458 SANE_TRUE, SANE_FALSE, /* ADF, duplex */
459 {0, SANE_FIX(8.5 * MM_PER_INCH), 0}, {0, SANE_FIX(14 * MM_PER_INCH), 0} /* 14 ADF x/y ranges */
460 },
461 /* KODAK ESP C310 , */
462 {
463 0x405D, "esp", "KODAK ESP C310 AiO",
464 -1, 0x82,
465 600, {75, 600, 0}, kodakaio_resolution_list, 4, /* 600 dpi max, 4 resolutions */
466 8, kodakaio_depth_list, /* color depth max 8, list above */
467 {0, SANE_FIX(8.5 * MM_PER_INCH), 0}, {0, SANE_FIX(11.7 * MM_PER_INCH), 0}, /* FBF x/y ranges */
468 SANE_FALSE, SANE_FALSE, /* ADF, duplex */
469 {0, SANE_FIX(8.5 * MM_PER_INCH), 0}, {0, SANE_FIX(14 * MM_PER_INCH), 0} /* 14 ADF x/y ranges */
470 },
471 /* KODAK ESP C315 , */
472 {
473 0x405E, "esp", "KODAK ESP C315 AiO",
474 -1, 0x82,
475 600, {75, 600, 0}, kodakaio_resolution_list, 4, /* 600 dpi max, 4 resolutions */
476 8, kodakaio_depth_list, /* color depth max 8, list above */
477 {0, SANE_FIX(8.5 * MM_PER_INCH), 0}, {0, SANE_FIX(11.7 * MM_PER_INCH), 0}, /* FBF x/y ranges */
478 SANE_FALSE, SANE_FALSE, /* ADF, duplex */
479 {0, SANE_FIX(8.5 * MM_PER_INCH), 0}, {0, SANE_FIX(14 * MM_PER_INCH), 0} /* 14 ADF x/y ranges */
480 },
481 /* ADVENT AW10, */
482 {
483 0x4060, "esp", "ADVENT WiFi AIO AW10",
484 -1, 0x82,
485 600, {75, 600, 0}, kodakaio_resolution_list, 4, /* 600 dpi max, {from, to, 0} 4 resolutions */
486 8, kodakaio_depth_list, /* color depth max 8, list above */
487 {0, SANE_FIX(8.5 * MM_PER_INCH), 0}, {0, SANE_FIX(11.7 * MM_PER_INCH), 0}, /* FBF x/y ranges */
488 SANE_TRUE, SANE_FALSE, /* ADF, duplex. */
489 {0, SANE_FIX(8.5 * MM_PER_INCH), 0}, {0, SANE_FIX(14 * MM_PER_INCH), 0} /* 14 ADF x/y ranges */
490 },
491 /* KODAK HERO 6.1, */
492 {
493 0x4062, "esp", "KODAK OFFICE HERO 6.1 AiO",
494 -1, 0x82,
495 600, {75, 600, 0}, kodakaio_resolution_list, 4, /* 600 dpi max, {from, to, 0} 4 resolutions */
496 8, kodakaio_depth_list, /* color depth max 8, list above */
497 {0, SANE_FIX(8.5 * MM_PER_INCH), 0}, {0, SANE_FIX(11.7 * MM_PER_INCH), 0}, /* FBF x/y ranges */
498 SANE_TRUE, SANE_FALSE, /* ADF, duplex. */
499 {0, SANE_FIX(8.5 * MM_PER_INCH), 0}, {0, SANE_FIX(14 * MM_PER_INCH), 0} /* 14 ADF x/y ranges */
500 },
501 /* KODAK HERO 7.1, */
502 {
503 0x4063, "esp", "KODAK HERO 7.1 AiO",
504 -1, 0x82,
505 600, {75, 600, 0}, kodakaio_resolution_list, 4, /* 600 dpi max, {from, to, 0} 4 resolutions */
506 8, kodakaio_depth_list, /* color depth max 8, list above */
507 {0, SANE_FIX(8.5 * MM_PER_INCH), 0}, {0, SANE_FIX(11.7 * MM_PER_INCH), 0}, /* FBF x/y ranges */
508 SANE_FALSE, SANE_TRUE, /* ADF, duplex. */
509 {0, SANE_FIX(8.5 * MM_PER_INCH), 0}, {0, SANE_FIX(14 * MM_PER_INCH), 0} /* 14 ADF x/y ranges */
510 },
511 /* KODAK HERO 5.1, */
512 {
513 0x4064, "esp", "KODAK HERO 5.1 AiO",
514 -1, 0x82,
515 600, {75, 600, 0}, kodakaio_resolution_list, 4, /* 600 dpi max, {from, to, 0} 4 resolutions */
516 8, kodakaio_depth_list, /* color depth max 8, list above */
517 {0, SANE_FIX(8.5 * MM_PER_INCH), 0}, {0, SANE_FIX(11.7 * MM_PER_INCH), 0}, /* FBF x/y ranges */
518 SANE_FALSE, SANE_TRUE, /* ADF, duplex.*/
519 {0, SANE_FIX(8.5 * MM_PER_INCH), 0}, {0, SANE_FIX(14 * MM_PER_INCH), 0} /* 14 ADF x/y ranges */
520 },
521 /* KODAK ESP9200 , */
522 {
523 0x4065, "esp", "KODAK ESP 9200 Series AiO",
524 -1, 0x82,
525 600, {75, 600, 0}, kodakaio_resolution_list, 4, /* 600 dpi max, 4 resolutions */
526 8, kodakaio_depth_list, /* color depth max 8, list above */
527 {0, SANE_FIX(8.5 * MM_PER_INCH), 0}, {0, SANE_FIX(11.7 * MM_PER_INCH), 0}, /* FBF x/y ranges */
528 SANE_TRUE, SANE_FALSE, /* ADF, duplex */
529 {0, SANE_FIX(8.5 * MM_PER_INCH), 0}, {0, SANE_FIX(14 * MM_PER_INCH), 0} /* 14 ADF x/y ranges */
530 },
531 /* KODAK ESP2170 , */
532 {
533 0x4066, "esp", "KODAK ESP Office 2170 Series",
534 -1, 0x82,
535 1200, {75, 1200, 0}, kodakaio_resolution_list, 5, /* 1200 dpi optical, {from, to, 0} 5 resolutions */
536 8, kodakaio_depth_list, /* color depth max 8, list above */
537 {0, SANE_FIX(8.5 * MM_PER_INCH), 0}, {0, SANE_FIX(11.7 * MM_PER_INCH), 0}, /* FBF x/y ranges */
538 SANE_TRUE, SANE_FALSE, /* ADF, duplex */
539 {0, SANE_FIX(8.5 * MM_PER_INCH), 0}, {0, SANE_FIX(14 * MM_PER_INCH), 0} /* 14 ADF x/y ranges */
540 },
541 /* KODAK HERO 9.1, */
542 {
543 0x4067, "esp", "KODAK HERO 9.1 AiO",
544 -1, 0x82,
545 1200, {75, 1200, 0}, kodakaio_resolution_list, 5, /* 1200 dpi optical, {from, to, 0} 5 resolutions */
546 8, kodakaio_depth_list, /* color depth max 8, list above */
547 {0, SANE_FIX(8.5 * MM_PER_INCH), 0}, {0, SANE_FIX(11.7 * MM_PER_INCH), 0}, /* FBF x/y ranges */
548 SANE_TRUE, SANE_FALSE, /* ADF, duplex. */
549 {0, SANE_FIX(8.5 * MM_PER_INCH), 0}, {0, SANE_FIX(14 * MM_PER_INCH), 0} /* 14 ADF x/y ranges */
550 },
551 /* KODAK HERO 4.1, */
552 {
553 0x4069, "esp", "KODAK HERO 4.1 AiO",
554 -1, 0x82,
555 600, {75, 600, 0}, kodakaio_resolution_list, 4, /* 600 dpi max, {from, to, 0} 4 resolutions */
556 8, kodakaio_depth_list, /* color depth max 8, list above */
557 {0, SANE_FIX(8.5 * MM_PER_INCH), 0}, {0, SANE_FIX(11.7 * MM_PER_INCH), 0}, /* FBF x/y ranges */
558 SANE_TRUE, SANE_FALSE, /* ADF, duplex.*/
559
560 {0, SANE_FIX(8.5 * MM_PER_INCH), 0}, {0, SANE_FIX(14 * MM_PER_INCH), 0} /* 14 ADF x/y ranges */
561 },
562
563 /* KODAK HERO 3.1, */
564 {
565 0x406D, "esp", "KODAK HERO 3.1 AiO",
566 -1, 0x82,
567 600, {75, 600, 0}, kodakaio_resolution_list, 4, /* 600 dpi max, {from, to, 0} 4 resolutions */
568 8, kodakaio_depth_list, /* color depth max 8, list above */
569 {0, SANE_FIX(8.5 * MM_PER_INCH), 0}, {0, SANE_FIX(11.7 * MM_PER_INCH), 0}, /* FBF x/y ranges */
570 SANE_FALSE, SANE_TRUE, /* ADF, duplex. */
571 {0, SANE_FIX(8.5 * MM_PER_INCH), 0}, {0, SANE_FIX(14 * MM_PER_INCH), 0} /* 14 ADF x/y ranges */
572 },
573 /* spare use for specified usbid */
574 {
575 0, "esp", "specified",
576 -1, 0x82,
577 600, {75, 600, 0}, kodakaio_resolution_list, 4, /* 600 dpi max, {from, to, 0} 4 resolutions */
578 8, kodakaio_depth_list, /* color depth max 8, list above */
579 {0, SANE_FIX(8.5 * MM_PER_INCH), 0}, {0, SANE_FIX(11.7 * MM_PER_INCH), 0}, /* FBF x/y ranges */
580 SANE_FALSE, SANE_TRUE, /* ADF, duplex. */
581 {0, SANE_FIX(8.5 * MM_PER_INCH), 0}, {0, SANE_FIX(14 * MM_PER_INCH), 0} /* 14 ADF x/y ranges */
582 }
583 };
584
585 /****************************************************************************
586 * General configuration parameter definitions ****************************************************************************/
587
588
589 /*
590 * Definition of the mode_param struct, that is used to
591 * specify the valid parameters for the different scan modes.
592 *
593 * The depth variable gets updated when the bit depth is modified.
594 */
595
596 /* could be affecting what data sane delivers */
597 static struct mode_param mode_params[] = {
598 {0x03, 3, 24}, /* Color, 3 colors, 24 bit */
599 {0x02, 1, 8}, /* Grayscale, 1 color, 8 bit */
600 {0x00, 1, 1} /* Lineart, 1 color, 8 bit (was 8 bit) */
601 };
602
603 static SANE_String_Const mode_list[] = {
604 SANE_VALUE_SCAN_MODE_COLOR,
605 SANE_VALUE_SCAN_MODE_GRAY,
606 SANE_VALUE_SCAN_MODE_LINEART,
607 NULL
608 };
609
610 static const SANE_String_Const adf_mode_list[] = {
611 SANE_I18N("Simplex"),
612 SANE_I18N("Duplex"),
613 NULL
614 };
615
616 /* Define the different scan sources */
617 #define FBF_STR SANE_I18N("Flatbed")
618 #define ADF_STR SANE_I18N("Automatic Document Feeder")
619
620 /*
621 * source list need one dummy entry (save device settings is crashing).
622 * NOTE: no const - this list gets created while exploring the capabilities
623 * of the scanner. Here space is reserved for 3 entries + NULL ?
624 */
625
626 static SANE_String_Const source_list[] = {
627 FBF_STR,
628 NULL,
629 NULL,
630 NULL
631 };
632
633 static const SANE_Range percent_range_fixed = {SANE_FIX(0.0), SANE_FIX(100.0), SANE_FIX(1.0)};
634 /*static const SANE_Range percent_range_int = {0, 100, 1};*/
635
636 /* prototypes */
637 static SANE_Status attach_one_usb(SANE_String_Const devname);
638 static SANE_Status attach_one_net(SANE_String_Const devname, unsigned int device);
639 void kodakaio_com_str(unsigned char *buf, char *fmt_buf);
640 int cmparray (unsigned char *array1, unsigned char *array2, size_t len);
641 #if WITH_AVAHI
642 static struct KodakaioCap *get_device_from_identification (const char *ident, const char *vid, const char *pid);
643 void ProcessAvahiDevice(const char *device_id, const char *vid, const char *pid, const char *ip_addr);
644 #endif
645
646
647 /* Some utility functions */
648
649 static size_t
max_string_size(const SANE_String_Const strings[])650 max_string_size(const SANE_String_Const strings[])
651 {
652 /* returns the length of the longest string in an array of strings */
653 size_t size, max_size = 0;
654 int i;
655
656 for (i = 0; strings[i]; i++) {
657 size = strlen(strings[i]) + 1;
658 if (size > max_size)
659 max_size = size;
660 }
661 return max_size;
662 }
663
664
665
666 static void
print_params(const SANE_Parameters params,int level)667 print_params(const SANE_Parameters params, int level)
668 {
669 DBG(level, "formats: binary=?, grey=%d, colour=%d\n",SANE_FRAME_GRAY, SANE_FRAME_RGB );
670 DBG(level, "params.format = %d\n", params.format);
671 DBG(level, "params.last_frame = %d\n", params.last_frame);
672 DBG(level, "params.bytes_per_line = %d\n", params.bytes_per_line);
673 DBG(level, "params.pixels_per_line = %d\n", params.pixels_per_line);
674 DBG(level, "params.lines = %d\n", params.lines);
675 DBG(level, "params.depth = %d\n", params.depth);
676 }
677
678 static void
print_status(KodakAio_Scanner * s,int level)679 print_status(KodakAio_Scanner *s,int level)
680 {
681 DBG(level, "print_status with level %d\n", level);
682 DBG(level, "s->bytes_unread = %d\n", s->bytes_unread);
683 /*
684 DBG(level, "params.last_frame = %d\n", params.last_frame);
685 DBG(level, "params.bytes_per_line = %d\n", params.bytes_per_line);
686 DBG(level, "params.pixels_per_line = %d\n", params.pixels_per_line);
687 DBG(level, "params.lines = %d\n", params.lines);
688 DBG(level, "params.depth = %d\n", params.depth);
689 */
690 }
691
692 /****************************************************************************
693 * Low-level Network communication functions ****************************************************************************/
694
695 static int
kodakaio_net_read(struct KodakAio_Scanner * s,unsigned char * buf,size_t wanted,SANE_Status * status)696 kodakaio_net_read(struct KodakAio_Scanner *s, unsigned char *buf, size_t wanted,
697 SANE_Status * status)
698 /* there seems to be a condition where this returns no error and no data without detecting a timeout
699 That is probably if the scanner disconnected the network connection
700 */
701 {
702 size_t size, read = 0;
703 struct pollfd fds[1];
704 int pollreply;
705
706 *status = SANE_STATUS_GOOD;
707
708 /* poll for data-to-be-read (using K_Request_Timeout) */
709 fds[0].fd = s->fd;
710 fds[0].events = POLLIN;
711 fds[0].revents = 0;
712 if ((pollreply = poll (fds, 1, K_Request_Timeout)) <= 0) {
713 if (pollreply == 0)
714 DBG(1, "net poll timeout\n");
715 else
716 /* pollreply is -ve */
717 DBG(1, "net poll error\n");
718 *status = SANE_STATUS_IO_ERROR;
719 }
720 else if((fds[0].revents & POLLIN) && !(fds[0].revents & (POLLERR | POLLHUP | POLLNVAL))) {
721 while (read < wanted) {
722 DBG(50, "reading: read %lu, wanted %lu\n",read, wanted);
723 size = sanei_tcp_read(s->fd, buf + read, wanted - read);
724 if (size == 0) {
725 DBG(1, "No data read. Scanner may have disconnected\n");
726 break;
727 }
728 read += size;
729 }
730
731 if (read == 0)
732 *status = SANE_STATUS_IO_ERROR;
733
734 DBG(32, "net read %lu bytes:%x,%x,%x,%x,%x,%x,%x,%x\n",(unsigned long)read,buf[0],buf[1],buf[2],buf[3],buf[4],buf[5],buf[6],buf[7]);
735 }
736 else
737 DBG(1, "Unknown problem with poll\n");
738
739 return read;
740 }
741
742
743 static int
sanei_kodakaio_net_write_raw(struct KodakAio_Scanner * s,const unsigned char * buf,size_t buf_size,SANE_Status * status)744 sanei_kodakaio_net_write_raw(struct KodakAio_Scanner *s,
745 const unsigned char *buf, size_t buf_size,
746 SANE_Status *status)
747 {
748 DBG(32, "net write:%x,%x,%x,%x,%x,%x,%x,%x\n",buf[0],buf[1],buf[2],buf[3],buf[4],buf[5],buf[6],buf[7]);
749
750 sanei_tcp_write(s->fd, buf, buf_size);
751 /* TODO: Check whether sending failed... */
752
753 *status = SANE_STATUS_GOOD;
754 return buf_size;
755 }
756
757 static SANE_Status
sanei_kodakaio_net_open(struct KodakAio_Scanner * s)758 sanei_kodakaio_net_open(struct KodakAio_Scanner *s)
759 {
760 struct timeval tv;
761
762 tv.tv_sec = 5;
763 tv.tv_usec = 0;
764
765 DBG(5, "%s\n", __func__);
766
767 setsockopt(s->fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv));
768 return SANE_STATUS_GOOD;
769 }
770
771 static SANE_Status
sanei_kodakaio_net_close(struct KodakAio_Scanner * s)772 sanei_kodakaio_net_close(struct KodakAio_Scanner *s)
773 {
774 NOT_USED(s);
775 /* Does nothing - maybe should close the socket ? */
776 return SANE_STATUS_GOOD;
777 }
778
779
780 /****************************************************************************
781 * Low-level USB communication functions ****************************************************************************/
782
783 static int
kodakaio_getNumberOfUSBProductIds(void)784 kodakaio_getNumberOfUSBProductIds (void)
785 {
786 return sizeof (kodakaio_cap) / sizeof (struct KodakaioCap);
787 }
788
789 /****************************************************************************
790 * low-level communication commands ****************************************************************************/
791
dump_hex_buffer_dense(int level,const unsigned char * buf,size_t buf_size)792 static void dump_hex_buffer_dense (int level, const unsigned char *buf, size_t buf_size)
793 {
794 size_t k;
795 char msg[1024], fmt_buf[1024];
796 memset (&msg[0], 0x00, 1024);
797 memset (&fmt_buf[0], 0x00, 1024);
798 for (k = 0; k < min(buf_size, 80); k++) {
799 if (k % 16 == 0) {
800 if (k>0) {
801 DBG (level, "%s\n", msg);
802 memset (&msg[0], 0x00, 1024);
803 }
804 sprintf (fmt_buf, " 0x%04lx ", (unsigned long)k);
805 strcat (msg, fmt_buf);
806 }
807 if (k % 8 == 0) {
808 strcat (msg, " ");
809 }
810 sprintf (fmt_buf, " %02x" , buf[k]);
811 strcat (msg, fmt_buf);
812 }
813 if (msg[0] != 0 ) {
814 DBG (level, "%s\n", msg);
815 }
816 }
817
818 /* changing params to char seems to cause a stack problem */
kodakaio_com_str(unsigned char * buf,char * fmt_buf)819 void kodakaio_com_str(unsigned char *buf, char *fmt_buf)
820 {
821 /* returns a printable string version of the first 8 bytes assuming they are a kodakaio command*/
822 if(buf[0] == 0x1b) {
823 sprintf (fmt_buf, "esc %c %c %02x %02x %02x %02x %02x",
824 buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]);
825 }
826 else {
827 sprintf (fmt_buf, "%02x %02x %02x %02x %02x %02x %02x %02x",
828 buf[0],buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]);
829 }
830 }
831
832 static int
k_send(KodakAio_Scanner * s,void * buf,size_t buf_size,SANE_Status * status)833 k_send(KodakAio_Scanner * s, void *buf, size_t buf_size, SANE_Status * status)
834 {
835 char fmt_buf[25];
836
837 kodakaio_com_str(buf, fmt_buf);
838 DBG(15, "%s: size = %lu :%s\n", __func__, (u_long) buf_size, fmt_buf);
839
840 if (DBG_LEVEL >= 125) {
841 const unsigned char *s = buf;
842 DBG(125, "complete buffer:\n");
843 dump_hex_buffer_dense (125, s, buf_size);
844 }
845
846 if (s->hw->connection == SANE_KODAKAIO_NET) {
847 return sanei_kodakaio_net_write_raw(s, buf, buf_size, status);
848 } else if (s->hw->connection == SANE_KODAKAIO_USB) {
849 size_t n;
850 n = buf_size;
851 *status = sanei_usb_write_bulk(s->fd, buf, &n);
852 DBG(50, "USB: wrote %lu bytes, status: %s\n", (unsigned long)n, sane_strstatus(*status));
853 return n;
854 }
855
856 *status = SANE_STATUS_INVAL;
857 return 0;
858 }
859
860 static ssize_t
k_recv(KodakAio_Scanner * s,void * buf,ssize_t buf_size,SANE_Status * status)861 k_recv(KodakAio_Scanner * s, void *buf, ssize_t buf_size,
862 SANE_Status * status)
863 {
864 /* requests and receives data this function makes the split between USB and NET
865 this function called by a number of others
866
867 In USB mode, this function will wait until data is available for a maximum of SCANNER_READ_TIMEOUT seconds.
868 In NET mode the timeout is in kodakaio_net_read
869 */
870 ssize_t n = 0;
871 char fmt_buf[25];
872 time_t time_start;
873 time_t time_now;
874 struct timespec usb_delay, usb_rem;
875 usb_delay.tv_sec = 0;
876 usb_delay.tv_nsec = 300000000; /* 0.3 sec */
877
878 if (s->hw->connection == SANE_KODAKAIO_NET) {
879
880 time(&time_start);
881 DBG(min(16,DBG_READ), "[%ld] %s: net req size = %ld ", (long) time_start, __func__, (long) buf_size);
882 n = kodakaio_net_read(s, buf, buf_size, status);
883 DBG(min(16,DBG_READ), "returned %lu\n", (unsigned long)n);
884 if (*status != SANE_STATUS_GOOD) {
885 DBG(1, "%s: err returned from kodakaio_net_read, %s\n", __func__, sane_strstatus(*status));
886 }
887
888
889 } else if (s->hw->connection == SANE_KODAKAIO_USB) {
890 /* Start the clock for USB timeout */
891 time(&time_start);
892
893 /* Loop until we have data */
894 while (n == 0) {
895 n = buf_size;
896 /* but what if the data is an exact number of blocks? */
897 DBG(min(16,DBG_READ), "[%ld] %s: usb req size = %ld ", (long) time_start, __func__, (long) n);
898 *status = sanei_usb_read_bulk(s->fd, (SANE_Byte *) buf, (size_t *) & n);
899 DBG(min(16,DBG_READ), "returned %ld\n", (long) n);
900
901 if(*status != SANE_STATUS_GOOD) {
902
903 DBG(min(16,DBG_READ), "sanei_usb_read_bulk gave %s\n", sane_strstatus(*status));
904
905 if (*status == SANE_STATUS_EOF) {
906 /* If we have EOF status, wait for more data */
907 time(&time_now);
908 if (difftime(time_now, time_start) < SCANNER_READ_TIMEOUT) {
909 nanosleep(&usb_delay, &usb_rem);
910 }
911 else {
912 /* Timeout */
913 return n;
914 }
915 }
916 else {
917 /* If we've encountered another type of error, return */
918 return n;
919 }
920 }
921 }
922 }
923
924 if (n == 8) {
925 kodakaio_com_str(buf, fmt_buf);
926 DBG(min(14,DBG_READ), "%s: size = %ld, got %s\n", __func__, (long int)n, fmt_buf);
927 }
928 /* dump buffer if appropriate */
929 if (DBG_LEVEL >= 127 && n > 0) {
930 const unsigned char* b=buf;
931 dump_hex_buffer_dense (125, b, buf_size);
932 }
933 return n;
934 }
935
936
937 static SANE_Status
kodakaio_expect_ack(KodakAio_Scanner * s,unsigned char * rxbuf)938 kodakaio_expect_ack(KodakAio_Scanner *s, unsigned char *rxbuf)
939 /* gets 8 byte reply, checks reply is an Ack and returns appropriate status */
940 {
941 SANE_Status status;
942
943 k_recv(s, rxbuf, 8, &status);
944 if (status != SANE_STATUS_GOOD) {
945 DBG(1, "%s: rx err, %s\n", __func__, sane_strstatus(status));
946 return status;
947 }
948 /* strncmp ignores diffent possible responses like escSS00000 and escSS02000 */
949 if (strncmp((char *)KodakEsp_Ack,(char *)rxbuf,4)!=0) {
950 DBG (1, "No Ack received, Expected 0x%2x %2x %2x %2x... got 0x%2x %2x %2x %2x...\n",
951 KodakEsp_Ack[0], KodakEsp_Ack[1], KodakEsp_Ack[2], KodakEsp_Ack[3],rxbuf[0], rxbuf[1], rxbuf[2], rxbuf[3]);
952 return SANE_STATUS_IO_ERROR;
953 }
954
955 return status;
956 }
957
958 static SANE_Status
kodakaio_txrx(KodakAio_Scanner * s,unsigned char * txbuf,unsigned char * rxbuf)959 kodakaio_txrx(KodakAio_Scanner *s, unsigned char *txbuf, unsigned char *rxbuf)
960 /* Sends 8 byte data to scanner and returns reply and appropriate status. */
961 {
962 SANE_Status status;
963 ssize_t n = 0;
964
965 k_send(s, txbuf, 8, &status);
966 if (status != SANE_STATUS_GOOD) {
967 DBG(1, "%s: tx err, %s\n", __func__, sane_strstatus(status));
968 return status;
969 }
970 n = k_recv(s, rxbuf, 8, &status);
971 if (status != SANE_STATUS_GOOD) {
972 DBG(1, "%s: %s gave rx err, %s\n", __func__, "txvalue", sane_strstatus(status));
973 return status;
974 }
975 if (n == 0) {
976 DBG(1, "%s: try 1 k_recv returned 0 bytes with status %s\n", __func__, sane_strstatus(status));
977 n = k_recv(s, rxbuf, 8, &status);
978 if (status != SANE_STATUS_GOOD) {
979 DBG(1, "%s: %s gave rx err, %s\n", __func__, "txvalue", sane_strstatus(status));
980 return status;
981 }
982 if (n == 0) {
983 DBG(1, "%s: try 2 k_recv returned 0 bytes with status %s\n", __func__, sane_strstatus(status));
984 return status;
985 }
986 }
987 return status;
988 }
989
990 static SANE_Status
kodakaio_txrxack(KodakAio_Scanner * s,unsigned char * txbuf,unsigned char * rxbuf)991 kodakaio_txrxack(KodakAio_Scanner *s, unsigned char *txbuf, unsigned char *rxbuf)
992 /*
993 Sends 8 byte data to scanner, gets 8 byte reply, checks reply is an Ack
994 and returns appropriate status
995 */
996 {
997 SANE_Status status;
998
999 k_send(s, txbuf, 8, &status);
1000 if (status != SANE_STATUS_GOOD) {
1001 DBG(1, "%s: tx err, %s\n", __func__, sane_strstatus(status));
1002 return status;
1003 }
1004
1005 k_recv(s, rxbuf, 8, &status);
1006 if (status != SANE_STATUS_GOOD) {
1007 DBG(1, "%s: %s gave rx err, %s\n", __func__, "txvalue", sane_strstatus(status));
1008 return status;
1009 }
1010 /* strncmp ignores different possible responses like escSS00000 and escSS02000 */
1011 if (strncmp((char *)KodakEsp_Ack,(char *)rxbuf,3) == 0) { /* was 4 byte comp */
1012 if (rxbuf[4] == 0x01 && s->adf_loaded == SANE_FALSE) {
1013 s->adf_loaded = SANE_TRUE;
1014 DBG(5, "%s: News - docs in ADF\n", __func__);
1015 }
1016 else if (rxbuf[4] !=
1017 0x01 && s->adf_loaded == SANE_TRUE) {
1018 s->adf_loaded = SANE_FALSE;
1019 DBG(5, "%s: News - ADF is empty\n", __func__);
1020 }
1021 }
1022 else {
1023 DBG (1, "No Ack received, Sent 0x%2x %2x %2x %2x... got 0x%2x %2x %2x %2x...\n",
1024 txbuf[0], txbuf[1], txbuf[2], txbuf[3],rxbuf[0], rxbuf[1], rxbuf[2], rxbuf[3]);
1025 return SANE_STATUS_IO_ERROR;
1026 }
1027
1028 return status;
1029 }
1030
1031 /* unused function
1032 static ssize_t
1033 kodakaio_rxflush(KodakAio_Scanner *s)
1034
1035 Tries to get 64 byte reply
1036 and returns number of bytes read
1037
1038 {
1039 SANE_Status status;
1040 unsigned char rxbuf[64];
1041 ssize_t n = 0;
1042
1043 n = k_recv(s, rxbuf, 64, &status);
1044 if (status != SANE_STATUS_GOOD) {
1045 DBG(1, "%s: %s gave rx err, %s\n", __func__, "status", sane_strstatus(status));
1046 }
1047 DBG(5, "%s: flushed, %d bytes\n", __func__, (int)n);
1048 return n;
1049 }
1050 */
1051
1052 /*
1053 * high-level communication commands
1054 */
1055
1056 static SANE_Status
k_hello(KodakAio_Scanner * s)1057 k_hello (KodakAio_Scanner * s)
1058 {
1059 SANE_Status status;
1060 unsigned char reply[8];
1061 char fmt_buf[25];
1062
1063 DBG(5, "%s\n", __func__);
1064
1065 /* check that there is nothing already in the input buffer before starting
1066 kodakaio_rxflush(s);
1067 */
1068 /* preset the reply, so I can see if it gets changed */
1069 reply[0] = 0; reply[1] = 1; reply[2] = 2; reply[3] = 3; reply[4] = 4; reply[5] = 5; reply[6] = 6; reply[7] = 7;
1070
1071 if((status = kodakaio_txrx(s, KodakEsp_V, reply))!= SANE_STATUS_GOOD) {
1072 DBG(1, "%s: KodakEsp_V failure, %s\n", __func__, sane_strstatus(status));
1073 return SANE_STATUS_IO_ERROR;
1074 }
1075
1076 if (strncmp((char *) reply, (char *) KodakEsp_v, 3)!=0) {
1077 kodakaio_com_str(reply, fmt_buf);
1078 DBG(1, "%s: KodakEsp_v err, got %s\n", __func__, fmt_buf);
1079 return SANE_STATUS_IO_ERROR;
1080 }
1081
1082
1083 DBG(5, "%s: OK %s\n", __func__, sane_strstatus(status));
1084 return status;
1085 }
1086
1087 /* Start scan command */
1088 static SANE_Status
cmd_start_scan(SANE_Handle handle,size_t expect_total)1089 cmd_start_scan (SANE_Handle handle, size_t expect_total)
1090 /* expect_total is the expected total no of bytes just for info -ve value not a problem? or is it for size_t? */
1091 {
1092 KodakAio_Scanner *s = (KodakAio_Scanner *) handle;
1093 SANE_Status status = SANE_STATUS_GOOD;
1094 unsigned char reply[8];
1095 /*send the start command here */
1096 print_status(s, 5);
1097 /*adf added 20/2/12, apparently an extra KodakEsp_F is sent when the adf is used */
1098 if (strcmp(source_list[s->val[OPT_SOURCE].w], ADF_STR) == 0) { /* adf is in use */
1099 if (! s->adf_loaded) return SANE_STATUS_CANCELLED; /* was SANE_STATUS_NO_DOCS; */
1100 if (kodakaio_txrxack(s, KodakEsp_F, reply)!= SANE_STATUS_GOOD) {
1101 DBG(1, "%s: Did not get a good reply to KodakEsp_F\n", __func__);
1102 return SANE_STATUS_IO_ERROR;
1103 }
1104 }
1105
1106 if (kodakaio_txrxack(s, KodakEsp_E, reply)!= SANE_STATUS_GOOD) {
1107 DBG(1, "%s: Did not get a good reply to KodakEsp_E\n", __func__);
1108 return SANE_STATUS_IO_ERROR;
1109 }
1110
1111 DBG(20, "starting the scan, expected total bytes %lu\n",(unsigned long)expect_total);
1112 k_send(s, KodakEsp_Go, 8, &status);
1113
1114 if (status != SANE_STATUS_GOOD)
1115 DBG(1, "%s: KodakEsp_Go command NOT successfully sent\n", __func__);
1116 else {
1117 DBG(30, "%s: KodakEsp_Go command successfully sent\n", __func__);
1118 s->scanning = SANE_TRUE;
1119 }
1120 return status;
1121 }
1122
1123 static SANE_Status
cmd_cancel_scan(SANE_Handle handle)1124 cmd_cancel_scan (SANE_Handle handle)
1125 {
1126 KodakAio_Scanner *s = (KodakAio_Scanner *) handle;
1127 unsigned char reply[8];
1128 /* adf added 20/2/12 should it be adf? or adf with paper in? */
1129 if (strcmp(source_list[s->val[OPT_SOURCE].w], ADF_STR) == 0) { /* adf */
1130 if (kodakaio_txrxack(s, KodakEsp_F, reply)!= SANE_STATUS_GOOD)
1131 {
1132 DBG(1, "%s: KodakEsp_F command failed\n", __func__);
1133 return SANE_STATUS_IO_ERROR;
1134 }
1135 if (kodakaio_txrxack(s, KodakEsp_UnLock, reply)!= SANE_STATUS_GOOD)
1136 {
1137 DBG(1, "%s: KodakEsp_UnLock command failed\n", __func__);
1138 return SANE_STATUS_IO_ERROR;
1139 }
1140 DBG(5, "%s unlocked the scanner with adf F U\n", __func__);
1141 }
1142 else { /* no adf */
1143 if (kodakaio_txrxack(s, KodakEsp_UnLock, reply)!= SANE_STATUS_GOOD)
1144 {
1145 DBG(1, "%s: KodakEsp_UnLock command failed\n", __func__);
1146 return SANE_STATUS_IO_ERROR;
1147 }
1148 DBG(5, "%s unlocked the scanner U\n", __func__);
1149 }
1150 s->scanning = SANE_FALSE;
1151 return SANE_STATUS_GOOD;
1152 }
1153
1154 static SANE_Status
cmd_get_scanning_parameters(SANE_Handle handle,SANE_Frame * format,SANE_Int * depth,SANE_Int * data_pixels,SANE_Int * pixels_per_line,SANE_Int * lines)1155 cmd_get_scanning_parameters(SANE_Handle handle,
1156 SANE_Frame *format, SANE_Int *depth,
1157 SANE_Int *data_pixels, SANE_Int *pixels_per_line,
1158 SANE_Int *lines)
1159 {
1160 /* data_pixels is per line.
1161 Old mc cmd read this stuff from the scanner. I don't think kodak can do that easily */
1162
1163 KodakAio_Scanner *s = (KodakAio_Scanner *) handle;
1164 SANE_Status status = SANE_STATUS_GOOD;
1165 NOT_USED (format);
1166 NOT_USED (depth);
1167
1168 DBG(10, "%s\n", __func__);
1169
1170 /* Calculate returned values */
1171 *lines = s->params.lines;
1172 *pixels_per_line = s->params.pixels_per_line;
1173 *data_pixels = s->params.pixels_per_line;
1174
1175 DBG (20, "%s: data_pixels = %u, lines = %u, "
1176 "pixels_per_line = %u)\n", __func__,
1177 *data_pixels, *lines, *pixels_per_line);
1178 return status;
1179 }
1180
1181 /* Set color curve command, low level, sends commands to the scanner*/
1182 static SANE_Status
cmd_set_color_curve(SANE_Handle handle,unsigned char col)1183 cmd_set_color_curve(SANE_Handle handle, unsigned char col)
1184 {
1185 /* sends the color curve data for one color*/
1186 KodakAio_Scanner *s = (KodakAio_Scanner *) handle;
1187 SANE_Status status = SANE_STATUS_GOOD;
1188 unsigned char tx_col[8];
1189 unsigned char rx[8];
1190 unsigned char tx_curve[256];
1191 int i; /* 7/9/14 was unsigned char and that stopped the loop that made the linear curve from going to 255 */
1192 DBG(32, "%s: start\n", __func__);
1193 tx_col[0]=0x1b; tx_col[1]='S'; tx_col[2]='K'; tx_col[3]=col; tx_col[4]=0; tx_col[5]=0; tx_col[6]=0; tx_col[7]=0;
1194 /* linear curve now but could send tailor made curves in future */
1195 for(i=0;i<=255;++i) tx_curve[i]=i; /* 7/9/14 was i<255 the missing elements caused speckles */
1196
1197 k_send(s, tx_col, 8, &status);
1198 if (status != SANE_STATUS_GOOD) {
1199 DBG(1, "%s: tx err, %s\n", __func__, "curve command");
1200 return status;
1201 }
1202 k_send(s, tx_curve, 256, &status);
1203 if (status != SANE_STATUS_GOOD) {
1204 DBG(1, "%s: tx err, %s\n", __func__, "curve data");
1205 return status;
1206 }
1207 if (kodakaio_expect_ack(s, rx) != SANE_STATUS_GOOD) return SANE_STATUS_IO_ERROR;
1208 DBG(10, "%s: sent curve OK, \n", __func__);
1209 return status;
1210 }
1211
1212 /* Set scanning parameters command, low level, sends commands to the scanner*/
1213 static SANE_Status
cmd_set_scanning_parameters(SANE_Handle handle,int resolution,int tl_x,int tl_y,int width,int height,unsigned char source)1214 cmd_set_scanning_parameters(SANE_Handle handle,
1215 int resolution,
1216 int tl_x, int tl_y, int width, int height, unsigned char source)
1217
1218 /* NB. here int tl_x, int tl_y, int width, int height are in DPI units, not optres units! */
1219 /* sends params to scanner, but should we store them too? */
1220 {
1221 KodakAio_Scanner *s = (KodakAio_Scanner *) handle;
1222 SANE_Status status = SANE_STATUS_GOOD;
1223 unsigned char tx_S[8];
1224 unsigned char tx_dpi[8];
1225 unsigned char tx_topleft[8];
1226 unsigned char tx_widthheight[8];
1227 unsigned char bufread[8];
1228 int i;
1229
1230 /*don't know the purpose of F yet. windows USB repeated 4 x why? does it affect the colour balance?*/
1231 DBG(8, "%s\n", __func__);
1232 for(i=0;i<4;++i) {
1233 kodakaio_txrxack(s, KodakEsp_F, bufread);
1234 sleep(1);
1235 }
1236 /* kodakaio_txrxack(s, KodakEsp_F, bufread); for net, just once */
1237
1238 /* Source? bed /ADF */
1239 tx_S[0]=0x1b; tx_S[1]='S'; tx_S[2]='S'; tx_S[3]=source; tx_S[4]=0; tx_S[5]=0; tx_S[6]=0; tx_S[7]=0;
1240 kodakaio_txrxack(s, tx_S, bufread);
1241
1242 /* Compression */
1243 kodakaio_txrxack(s, KodakEsp_Comp, bufread);
1244
1245 /* DPI resolution */
1246 tx_dpi[0]=0x1b;
1247 tx_dpi[1]='S';
1248 tx_dpi[2]='D';
1249 tx_dpi[3]=resolution & 0xff;
1250 tx_dpi[4]=(resolution >> 8) & 0xff;
1251 tx_dpi[5]=resolution & 0xff;
1252 tx_dpi[6]=(resolution >> 8) & 0xff;
1253 tx_dpi[7]=0;
1254 kodakaio_txrxack(s, tx_dpi, bufread);
1255
1256 /* colour curves don't seem to be sent for usb preview
1257 but it seems to do no harm to send them */
1258 cmd_set_color_curve(s, 'R');
1259 cmd_set_color_curve(s, 'G');
1260 cmd_set_color_curve(s, 'B');
1261
1262
1263 /* Origin top left s->tl_x and s->tl_y are in optres units
1264 this command needs actual DPI units*/
1265 DBG(20, "%s: left (DPI)=%d, top (DPI)=%d\n", __func__, tl_x , tl_y);
1266
1267 tx_topleft[0]=0x1b;
1268 tx_topleft[1]='S';
1269 tx_topleft[2]='O';
1270 tx_topleft[3]=(tl_x) & 0xff;
1271 tx_topleft[4]=((tl_x) >> 8) & 0xff;
1272 tx_topleft[5]=(tl_y) & 0xff;
1273 tx_topleft[6]=((tl_y) >> 8) & 0xff;
1274 tx_topleft[7]=0;
1275 kodakaio_txrxack(s, tx_topleft, bufread);
1276
1277 /* Z width height note the s->width and s->height are in optres units
1278 this command needs actual DPI units*/
1279 tx_widthheight[0]=0x1b;
1280 tx_widthheight[1]='S';
1281 tx_widthheight[2]='Z';
1282 tx_widthheight[3]=(width) & 0xff;
1283 tx_widthheight[4]=((width) >> 8) & 0xff;
1284 tx_widthheight[5]=(height) & 0xff;
1285 tx_widthheight[6]=((height) >> 8) & 0xff;
1286 tx_widthheight[7]=0;
1287 kodakaio_txrxack(s, tx_widthheight, bufread);
1288
1289 if (status != SANE_STATUS_GOOD)
1290 DBG(1, "%s: Data NOT successfully sent\n", __func__);
1291 else
1292 DBG(20, "%s: Data successfully sent\n", __func__);
1293 return status;
1294 }
1295
1296 int
cmparray(unsigned char * array1,unsigned char * array2,size_t len)1297 cmparray (unsigned char *array1, unsigned char *array2, size_t len)
1298 {
1299 /* compares len bytes of the arrays returns 0 if they match
1300 returns the first mismatch position if they don't match */
1301 unsigned int i;
1302 for(i=0; i<len; ++i)
1303 {
1304 if(array1[i] != array2[i]) return -1;
1305 }
1306 return 0;
1307 }
1308
1309 static SANE_Status
cmd_read_data(SANE_Handle handle,unsigned char * buf,size_t * len)1310 cmd_read_data (SANE_Handle handle, unsigned char *buf, size_t *len)
1311 {
1312 /*
1313 cmd_read_data is only used in k_read. It reads one block of data
1314 read data using k_recv until you get the ackstring
1315 when you get the ackstring return s->ack and do padding if the padding option is selected
1316 if no padding option return EOF
1317 */
1318 KodakAio_Scanner *s = (KodakAio_Scanner *) handle;
1319 SANE_Status status;
1320 int oldtimeout = K_Request_Timeout;
1321 size_t bytecount;
1322 unsigned char *Last8; /* will point to the last 8 chars in buf */
1323 int i, line, lines;
1324
1325 if (s->ack && s->val[OPT_PADDING].w) {
1326 /* do padding of whole block*/
1327 /* memset(buf, 0x80, *len); need to work out the background colour for this */
1328 lines = *len / s->params.bytes_per_line;
1329 for (line=0; line < lines; ++line) {
1330 for (i=0; i< s->params.pixels_per_line; ++i) {
1331 buf[line * s->params.bytes_per_line + i] = s->background[0]; /*red */
1332 buf[line * s->params.bytes_per_line + s->params.pixels_per_line + i] = s->background[1]; /*green */
1333 buf[line * s->params.bytes_per_line + 2 * s->params.pixels_per_line + i] = s->background[2]; /*blue */
1334 }
1335 }
1336 s->bytes_unread -= *len;
1337 if (s->bytes_unread < 0)
1338 s->bytes_unread = 0;
1339 return SANE_STATUS_GOOD;
1340 }
1341
1342 if (s->ack && !s->val[OPT_PADDING].w) {
1343 s->bytes_unread = 0;
1344 s->eof = SANE_TRUE;
1345 return SANE_STATUS_EOF;
1346 }
1347
1348 /* Temporarily set the poll timeout long instead of short,
1349 * because a color scan needs >5 seconds to initialize. Is this needed for kodak? maybe */
1350 K_Request_Timeout = K_Scan_Data_Timeout;
1351 sanei_usb_set_timeout (K_Scan_Data_Timeout);
1352 bytecount = k_recv(s, buf, *len, &status);
1353 K_Request_Timeout = oldtimeout;
1354 sanei_usb_set_timeout (oldtimeout);
1355
1356 /* We may need to do some special thing with the block request size to cope with usb blocks of any length
1357 in order to keep the ack bytes, when using adf, at the end of one block ie not split between blocks.
1358 But it seems that the scanner takes care of that, and gives you the ack as a separate 8 byte block */
1359
1360 if (bytecount >= 8) {
1361 /* it may be the last block from the scanner so look for Ack response in last 8 bytes */
1362 Last8 = buf + bytecount - 8;
1363
1364 /* only compare 4 bytes because we sometimes get escSS02.. or escSS00..
1365 is 4 the right number ? */
1366 if (cmparray(Last8,KodakEsp_Ack,4) == 0) {
1367 DBG(min(10,DBG_READ), "%s: found KodakEsp_Ack at %lu bytes of %lu\n", __func__, (unsigned long) bytecount, (unsigned long) *len);
1368 s->ack = SANE_TRUE;
1369 *len = bytecount - 8; /* discard the Ack response */
1370 s->bytes_unread -= *len; /* return a short block */
1371 }
1372 else {
1373 /* a not full buffer is returned usb does this */
1374 DBG(min(10,DBG_READ), "%s: buffer not full, got %lu bytes of %lu\n", __func__, (unsigned long) bytecount, (unsigned long) *len);
1375 *len = bytecount;
1376 s->bytes_unread -= bytecount;
1377 }
1378 }
1379 else {
1380 DBG(min(1,DBG_READ), "%s: tiny read, got %lu bytes of %lu\n", __func__, (unsigned long) bytecount, (unsigned long) *len);
1381 return SANE_STATUS_IO_ERROR;
1382 }
1383 lines = *len / s->params.bytes_per_line;
1384 if (lines > 1) {
1385 /* store average colour as background. That's not the ideal method but it's easy to implement. What's it used for? */
1386 s->background[0] = 0;
1387 s->background[1] = 0;
1388 s->background[2] = 0;
1389
1390 for (line=0; line < lines; ++line) {
1391 for (i=0; i< s->params.pixels_per_line; ++i) {
1392 s->background[0] += buf[line * s->params.bytes_per_line + i]; /*red */
1393 s->background[1] += buf[line * s->params.bytes_per_line + s->params.pixels_per_line + i]; /*green */
1394 s->background[2] += buf[line * s->params.bytes_per_line + 2 * s->params.pixels_per_line + i]; /*blue */
1395 }
1396 }
1397 s->background[0] = s->background[0] / (lines * s->params.pixels_per_line);
1398 s->background[1] = s->background[1] / (lines * s->params.pixels_per_line);
1399 s->background[2] = s->background[2] / (lines * s->params.pixels_per_line);
1400
1401 }
1402
1403 if (status == SANE_STATUS_GOOD)
1404 if (s->bytes_unread <= 0)
1405 DBG(min(2,DBG_READ), "%s: Page fully read %d blocks, %ld bytes unread\n", __func__, s->counter, (long) s->bytes_unread);
1406 else
1407 DBG(min(20,DBG_READ), "%s: Image data successfully read %ld bytes, %ld bytes unread\n", __func__, (long) bytecount, (long) s->bytes_unread);
1408 else if (s->ack) /* was (status == SANE_STATUS_EOF) */
1409 DBG(min(2,DBG_READ), "%s: scanner data read ended %d blocks %ld bytes, %ld bytes unread\n", __func__, s->counter, (long) bytecount, (long) s->bytes_unread);
1410 else
1411 DBG(min(1,DBG_READ), "%s: Image data read stopped with %s after %d blocks %ld bytes, %ld bytes unread\n", __func__, sane_strstatus(status), s->counter, (long) bytecount, (long) s->bytes_unread);
1412
1413 return status;
1414 }
1415
1416
1417
1418 /****************************************************************************
1419 * kodakaio backend high-level operations ****************************************************************************/
1420
1421 static void
k_dev_init(Kodak_Device * dev,const char * devname,int conntype)1422 k_dev_init(Kodak_Device *dev, const char *devname, int conntype)
1423 {
1424 DBG(5, "%s for %s\n", __func__,devname);
1425
1426 dev->name = NULL;
1427 dev->model = NULL;
1428 dev->connection = conntype;
1429 dev->sane.name = devname;
1430 dev->sane.model = NULL;
1431 dev->sane.type = "flatbed scanner";
1432 dev->sane.vendor = "Kodak";
1433 dev->cap = &kodakaio_cap[CAP_DEFAULT];
1434 }
1435
1436
1437 static SANE_Status
k_set_model(KodakAio_Scanner * s,const char * model,size_t len)1438 k_set_model(KodakAio_Scanner * s, const char *model, size_t len)
1439 {
1440 unsigned char *buf;
1441 unsigned char *p;
1442 struct Kodak_Device *dev = s->hw;
1443
1444 if (len<1) return SANE_STATUS_INVAL; /* to handle missing model */
1445
1446 buf = malloc(len + 1);
1447 if (buf == NULL)
1448 return SANE_STATUS_NO_MEM;
1449
1450 memcpy(buf, model, len);
1451 buf[len] = '\0';
1452
1453 p = &buf[len - 1];
1454
1455 while (*p == ' ') {
1456 *p = '\0';
1457 p--;
1458 }
1459
1460 if (dev->model)
1461 free(dev->model);
1462
1463 dev->model = strndup((const char *) buf, len);
1464 dev->sane.model = dev->model;
1465 DBG(10, "%s: model is '%s'\n", __func__, dev->model);
1466
1467 free(buf);
1468
1469 return SANE_STATUS_GOOD;
1470 }
1471
1472 static void
k_set_device(SANE_Handle handle,SANE_Word device)1473 k_set_device (SANE_Handle handle, SANE_Word device)
1474 {
1475 KodakAio_Scanner *s = (KodakAio_Scanner *) handle;
1476 Kodak_Device *dev = s->hw;
1477 int n;
1478
1479 DBG(10, "%s: 0x%x\n", __func__, device);
1480
1481 for (n = 0; n < NELEMS (kodakaio_cap); n++) {
1482 if (kodakaio_cap[n].id == device)
1483 break;
1484 }
1485 if (n < NELEMS(kodakaio_cap)) {
1486 dev->cap = &kodakaio_cap[n];
1487 } else {
1488 dev->cap = &kodakaio_cap[CAP_DEFAULT];
1489 DBG(1, " unknown device 0x%x, using default %s\n",
1490 device, dev->cap->model);
1491 }
1492 k_set_model (s, dev->cap->model, strlen (dev->cap->model));
1493
1494 }
1495
1496 static SANE_Status
k_discover_capabilities(KodakAio_Scanner * s)1497 k_discover_capabilities(KodakAio_Scanner *s)
1498 {
1499 SANE_Status status = SANE_STATUS_GOOD;
1500 Kodak_Device *dev = s->hw;
1501 SANE_String_Const *source_list_add = source_list;
1502
1503 DBG(10, "%s\n", __func__);
1504
1505 /* always add flatbed */
1506 *source_list_add++ = FBF_STR;
1507 /* TODO: How can I check for existence of an ADF??? */
1508 if (dev->cap->ADF == SANE_TRUE) {
1509 *source_list_add++ = ADF_STR;
1510 DBG(10, "%s: added adf to list\n", __func__);
1511 }
1512
1513 /* TODO: Is there any capability that we can extract from the
1514 * device by some scanner command? So far, it looks like
1515 * the device does not support any reporting.
1516 */
1517
1518 dev->x_range = &dev->cap->fbf_x_range;
1519 dev->y_range = &dev->cap->fbf_y_range;
1520
1521 DBG(10, " x-range: %f %f\n", SANE_UNFIX(dev->x_range->min), SANE_UNFIX(dev->x_range->max));
1522 DBG(10, " y-range: %f %f\n", SANE_UNFIX(dev->y_range->min), SANE_UNFIX(dev->y_range->max));
1523
1524 DBG(5, "End of %s, status:%s\n", __func__, sane_strstatus(status));
1525 *source_list_add = NULL; /* add end marker to source list */
1526 return status;
1527 }
1528
1529 static SANE_Status
k_setup_block_mode(KodakAio_Scanner * s)1530 k_setup_block_mode (KodakAio_Scanner *s)
1531 {
1532 /* works for USB and for net changing to make block size = a number of complete lines 28/12/12 */
1533 s->block_len = MAX_BLOCK_SIZE / s->scan_bytes_per_line * s->scan_bytes_per_line;
1534 s->bytes_unread = s->data_len;
1535 s->counter = 0;
1536 s->bytes_read_in_line = 0;
1537 if (s->line_buffer)
1538 free(s->line_buffer);
1539 s->line_buffer = malloc(s->scan_bytes_per_line);
1540 if (s->line_buffer == NULL) {
1541 DBG(1, "out of memory (line %d)\n", __LINE__);
1542 return SANE_STATUS_NO_MEM;
1543 }
1544
1545 DBG (10, " %s: Setup block mode - scan_bytes_per_line=%d, pixels_per_line=%d, depth=%d, data_len=%d, block_len=%d, blocks=%d, last_len=%d\n",
1546 __func__, s->scan_bytes_per_line, s->params.pixels_per_line, s->params.depth, s->data_len, s->block_len, s->blocks, s->last_len);
1547 return SANE_STATUS_GOOD;
1548 }
1549
1550 /* Call the commands to set scanning parameters
1551 In the Kodak Aio the parameters are:
1552 (x1b,"S","F",0,0,0,0,0)
1553 (x1b,"S","S",1,0,0,0,0)
1554 #It looks like the 4th param byte of the C command is compression 1=compress, 0=no compression
1555 #1st (or 3rd) param could be channels, 2nd could be bits per channel
1556 (x1b,"S","C",3,8,3,0,0) #3,8,3,1,0) was what the kodak software used with compression
1557 (x1b,"S","D",LowByte(Res),HighByte(Res),LowByte(Res),HighByte(Res),0) #resolution in DPI
1558 SendColour(tcpCliSock,"R")
1559 SendColour(tcpCliSock,"G")
1560 SendColour(tcpCliSock,"B")
1561 (x1b,"S","O",LowByte(x0*Res),HighByte(x0*Res),LowByte(y0*Res),HighByte(y0*Res),0) #top left in pixels
1562 (x1b,"S","Z",LowByte(x1*Res),HighByte(x1*Res),LowByte(y1*Res),HighByte(y1*Res),0) #bot right in pixels
1563 (x1b,"S","E",1,0,0,0,0)
1564
1565 */
1566
1567 static SANE_Status
k_lock_scanner(KodakAio_Scanner * s)1568 k_lock_scanner (KodakAio_Scanner * s)
1569 {
1570 SANE_Status status;
1571 unsigned char reply[8];
1572
1573 status = k_hello(s);
1574 if(status != SANE_STATUS_GOOD) {
1575 DBG(1, "%s k_hello failed with %s\n", __func__, sane_strstatus(status));
1576 return status;
1577 }
1578
1579 if (kodakaio_txrxack(s, KodakEsp_Lock, reply)!= SANE_STATUS_GOOD) {
1580 DBG(1, "%s Could not lock scanner\n", __func__);
1581 return SANE_STATUS_IO_ERROR;
1582 }
1583 if (s->adf_loaded) DBG(5, "%s scanner locked, with docs in adf\n", __func__);
1584 else DBG(5, "%s scanner locked, with no docs in adf\n", __func__);
1585 return SANE_STATUS_GOOD;
1586 }
1587
1588 static SANE_Status
k_set_scanning_parameters(KodakAio_Scanner * s)1589 k_set_scanning_parameters(KodakAio_Scanner * s)
1590 {
1591 SANE_Status status;
1592 unsigned char rs, source;
1593 SANE_Int scan_pixels_per_line = 0;
1594 int dpi, optres;
1595
1596 dpi = s->val[OPT_RESOLUTION].w;
1597 optres = s->hw->cap->optical_res;
1598
1599 /* Find the resolution in the res list and assign the index (rs) */
1600 for (rs=0; rs < s->hw->cap->res_list_size; rs++ ) {
1601 if ( dpi == s->hw->cap->res_list[rs] )
1602 break;
1603 }
1604
1605 /* ADF used? */
1606 if (strcmp(source_list[s->val[OPT_SOURCE].w], ADF_STR) == 0) {
1607 source = 0x00;
1608 } else {
1609 source = 0x01;
1610 }
1611
1612
1613 /* TODO: Any way to set PREVIEW??? */
1614
1615 /* Remaining bytes unused */
1616 status = cmd_set_scanning_parameters(s, dpi,
1617 s->left * dpi / optres, s->top * dpi / optres, /* top/left start (dpi units)*/
1618 s->params.pixels_per_line, s->params.lines, /* extent was s->width, s->height*/
1619 source); /* source */
1620
1621 if (status != SANE_STATUS_GOOD)
1622 DBG (1, "%s: Command cmd_set_scanning_parameters failed, %s\n",
1623 __func__, sane_strstatus(status));
1624
1625 /* Now query the scanner for the current image parameters */
1626 status = cmd_get_scanning_parameters (s,
1627 &s->params.format, &s->params.depth,
1628 &scan_pixels_per_line,
1629 &s->params.pixels_per_line, &s->params.lines);
1630 if (status != SANE_STATUS_GOOD) {
1631 DBG (1, "%s: Command cmd_get_scanning_parameters failed, %s\n",
1632 __func__, sane_strstatus(status));
1633 return status;
1634 }
1635
1636 /* Calculate how many bytes are really used per line */
1637 s->params.bytes_per_line = ceil (s->params.pixels_per_line * s->params.depth / 8.0);
1638 if (s->val[OPT_MODE].w == MODE_COLOR)
1639 s->params.bytes_per_line *= 3;
1640
1641 /* Calculate how many bytes per line will be returned by the scanner.
1642 magicolor needed this because it uses padding so scan bytes per line != image bytes per line.
1643 * The values needed for this are returned by get_scanning_parameters */
1644 s->scan_bytes_per_line = 3 * ceil (scan_pixels_per_line); /* we always scan in colour 8 bit */
1645 s->data_len = s->scan_bytes_per_line * floor (s->height * dpi / optres + 0.5); /* NB this is the length for a full scan */
1646 DBG (5, "Check: scan_bytes_per_line = %d s->params.bytes_per_line = %d \n", s->scan_bytes_per_line, s->params.bytes_per_line);
1647
1648 /* k_setup_block_mode at the start of each page for adf to work */
1649 status = k_setup_block_mode (s);
1650 if (status != SANE_STATUS_GOOD)
1651 DBG (1, "%s: Command k_setup_block_mode failed, %s\n",
1652 __func__, sane_strstatus(status));
1653
1654 DBG (18, "%s: bytes_read_in_line: %d\n", __func__, s->bytes_read_in_line);
1655 return status;
1656 }
1657
1658 static SANE_Status
k_check_adf(KodakAio_Scanner * s)1659 k_check_adf(KodakAio_Scanner * s)
1660 {
1661 /* 20/2/12 detect paper in the adf? acknowledge esc S S 00 01.. ?*/
1662
1663 if (! s->adf_loaded) {
1664 DBG(5, "%s: NO DOCS\n", __func__);
1665 return SANE_STATUS_NO_DOCS;
1666 }
1667 else {
1668
1669 /* TODO: Check for jam in ADF */
1670 DBG(5, "%s: DOCS IN ADF\n", __func__);
1671 return SANE_STATUS_GOOD;
1672 }
1673 }
1674
1675 static SANE_Status
k_scan_finish(KodakAio_Scanner * s)1676 k_scan_finish(KodakAio_Scanner * s)
1677 {
1678 SANE_Status status = SANE_STATUS_GOOD;
1679 DBG(10, "%s called\n", __func__);
1680
1681 /* If we have not yet read all data, cancel the scan */
1682 if (s->buf && !s->eof)
1683 status = cmd_cancel_scan (s);
1684
1685 if (s->line_buffer)
1686 free (s->line_buffer);
1687 s->line_buffer = NULL;
1688 free(s->buf);
1689 s->buf = s->end = s->ptr = NULL;
1690
1691 return status;
1692 }
1693
1694 static void
k_copy_image_data(KodakAio_Scanner * s,SANE_Byte * data,SANE_Int max_length,SANE_Int * length)1695 k_copy_image_data(KodakAio_Scanner * s, SANE_Byte * data, SANE_Int max_length,
1696 SANE_Int * length)
1697 /* copies the read data from s->line_buffer to the position in data pointer to by s->ptr
1698 uncompressed data is RRRR...GGGG...BBBB per line */
1699 {
1700 SANE_Int bytes_available;
1701 SANE_Int threshold;
1702
1703 DBG (min(18,DBG_READ), "%s: bytes_read in line: %d\n", __func__, s->bytes_read_in_line);
1704 *length = 0;
1705
1706 threshold = 255 - (int) (SANE_UNFIX(s->val[OPT_THRESHOLD].w) * 255.0 / 100.0 + 0.5); /* 255 - for the grey scale version */
1707 DBG (20, "%s: threshold: %d\n", __func__, threshold);
1708
1709 while ((max_length >= s->params.bytes_per_line) && (s->ptr < s->end)) {
1710 SANE_Int bytes_to_copy = s->scan_bytes_per_line - s->bytes_read_in_line;
1711 /* First, fill the line buffer for the current line: */
1712 bytes_available = (s->end - s->ptr);
1713 /* Don't copy more than we have buffer and available */
1714 if (bytes_to_copy > bytes_available)
1715 bytes_to_copy = bytes_available;
1716
1717 if (bytes_to_copy > 0) {
1718 memcpy (s->line_buffer + s->bytes_read_in_line, s->ptr, bytes_to_copy);
1719 s->ptr += bytes_to_copy;
1720 s->bytes_read_in_line += bytes_to_copy;
1721 }
1722
1723 /* We have filled as much as possible of the current line
1724 * with data from the scanner. If we have a complete line,
1725 * copy it over.
1726 line points to the current byte in the input s->line_buffer
1727 data points to the output buffer*/
1728 if ((s->bytes_read_in_line >= s->scan_bytes_per_line) &&
1729 (s->params.bytes_per_line <= max_length))
1730 {
1731 SANE_Int i;
1732 SANE_Byte *line = s->line_buffer;
1733 *length += s->params.bytes_per_line;
1734
1735 for (i=0; i< s->params.pixels_per_line; ++i) {
1736 /* different behaviour for each mode */
1737
1738 if (s->val[OPT_MODE].w == MODE_COLOR){
1739 /*interlace was subtracting from 255 until 6/9/14 */
1740 *data++ = 255-line[0]; /*red */
1741 *data++ = 255-line[s->params.pixels_per_line]; /*green */
1742 *data++ = 255-line[2 * s->params.pixels_per_line]; /*blue */
1743 }
1744
1745 else if (s->val[OPT_MODE].w == MODE_LINEART) { /* gives 1 bit output */
1746 /*output image location*/
1747 int offset = i % 8;
1748 unsigned char mask = 0x80 >> offset;
1749 /*set if any colour is over the threshold */
1750 if (line[0] < threshold || line[s->params.pixels_per_line] < threshold || line[2 * s->params.pixels_per_line] < threshold)
1751 *data &= ~mask; /* white clear the bit in mask */
1752 else
1753 *data |= mask; /* black set the bit in mask */
1754
1755 if (offset == 7 || i == s->params.pixels_per_line-1)
1756 data++; /* move on a byte if the byte is full or the line is complete */
1757 }
1758
1759 else { /* greyscale - Average the 3 colours */
1760 *data++ = (255-line[0]
1761 +255-line[s->params.pixels_per_line]
1762 +255-line[2 * s->params.pixels_per_line])
1763 / 3;
1764 }
1765 line++;
1766 }
1767 /*debug file The same for color or grey because the scan is colour */
1768 if (RawScan != NULL) {
1769 for (i=0; i< s->scan_bytes_per_line; ++i) fputc(s->line_buffer[i],RawScan);
1770 }
1771 max_length -= s->params.bytes_per_line;
1772 s->bytes_read_in_line -= s->scan_bytes_per_line;
1773 }
1774 }
1775 }
1776
1777 static SANE_Status
k_init_parametersta(KodakAio_Scanner * s)1778 k_init_parametersta(KodakAio_Scanner * s)
1779 {
1780 int dpi, optres;
1781 /* struct mode_param *mparam; */
1782
1783 DBG(10, "%s\n", __func__);
1784
1785 memset(&s->params, 0, sizeof(SANE_Parameters));
1786
1787 dpi = s->val[OPT_RESOLUTION].w;
1788 optres = s->hw->cap->optical_res;
1789
1790 /* mparam = &mode_params[s->val[OPT_MODE].w];does this get used? */
1791
1792 if (SANE_UNFIX(s->val[OPT_BR_Y].w) == 0 ||
1793 SANE_UNFIX(s->val[OPT_BR_X].w) == 0)
1794 return SANE_STATUS_INVAL;
1795
1796 /* TODO: Use OPT_RESOLUTION or fixed 600dpi for left/top/width/height? */
1797 s->left = ((SANE_UNFIX(s->val[OPT_TL_X].w) / MM_PER_INCH) * optres) + 0.5;
1798
1799 s->top = ((SANE_UNFIX(s->val[OPT_TL_Y].w) / MM_PER_INCH) * optres) + 0.5;
1800
1801 /* width in pixels */
1802 s->width =
1803 ((SANE_UNFIX(s->val[OPT_BR_X].w -
1804 s->val[OPT_TL_X].w) / MM_PER_INCH) * optres) + 0.5;
1805
1806 s->height = ((SANE_UNFIX(s->val[OPT_BR_Y].w -
1807 s->val[OPT_TL_Y].w) / MM_PER_INCH) * optres) + 0.5;
1808
1809 DBG(20, "%s: s->width = %d, s->height = %d optres units\n",
1810 __func__, s->width, s->height);
1811
1812 s->params.pixels_per_line = s->width * dpi / optres + 0.5;
1813
1814 /* ADF used without padding? added 30/12/12 */
1815 if (strcmp(source_list[s->val[OPT_SOURCE].w], ADF_STR) == 0 && !s->val[OPT_PADDING].w)
1816 s->params.lines = -1;
1817 else
1818 s->params.lines = s->height * dpi / optres + 0.5;
1819
1820
1821 DBG(20, "%s: resolution = %d, preview = %d\n",
1822 __func__, dpi, s->val[OPT_PREVIEW].w);
1823
1824 DBG(20, "%s: %p %p tlx %f tly %f brx %f bry %f [mm]\n",
1825 __func__, (void *) s, (void *) s->val,
1826 SANE_UNFIX(s->val[OPT_TL_X].w), SANE_UNFIX(s->val[OPT_TL_Y].w),
1827 SANE_UNFIX(s->val[OPT_BR_X].w), SANE_UNFIX(s->val[OPT_BR_Y].w));
1828
1829 /*
1830 * The default color depth is stored in mode_params.depth:
1831 */
1832 if (mode_params[s->val[OPT_MODE].w].depth == 1)
1833 s->params.depth = 1;
1834 else {
1835 s->params.depth = s->val[OPT_BIT_DEPTH].w;
1836 }
1837 DBG(20, "%s: bit depth = s->params.depth = %d\n", __func__,s->params.depth);
1838 s->params.last_frame = SANE_TRUE;
1839 s->params.bytes_per_line = 3 * ceil (s->params.depth * s->params.pixels_per_line / 8.0);
1840
1841 /* kodak only scans in color and conversion to grey or lineart is done in the driver
1842 s->params.format = SANE_FRAME_RGB; */
1843 DBG(20, "%s: s->val[OPT_MODE].w = %d (color is %d)\n", __func__,s->val[OPT_MODE].w, MODE_COLOR);
1844 if (s->val[OPT_MODE].w == MODE_COLOR) s->params.format = SANE_FRAME_RGB;
1845 else if (s->val[OPT_MODE].w == MODE_LINEART) s->params.format = SANE_FRAME_GRAY;
1846 else s->params.format = SANE_FRAME_GRAY;
1847
1848 DBG(20, "%s: format=%d, bytes_per_line=%d, lines=%d\n", __func__, s->params.format, s->params.bytes_per_line, s->params.lines);
1849 return (s->params.lines >= -1) ? SANE_STATUS_GOOD : SANE_STATUS_INVAL;
1850 }
1851
1852 static SANE_Status
k_start_scan(KodakAio_Scanner * s)1853 k_start_scan(KodakAio_Scanner * s)
1854 {
1855 SANE_Status status;
1856
1857 status = cmd_start_scan (s, s->data_len);
1858 if (status != SANE_STATUS_GOOD ) {
1859 DBG (1, "%s: starting the scan failed (%s)\n", __func__, sane_strstatus(status));
1860 }
1861 return status;
1862 }
1863
1864
1865
1866 static SANE_Status
k_read(struct KodakAio_Scanner * s)1867 k_read(struct KodakAio_Scanner *s)
1868 {
1869 unsigned char rx[8];
1870
1871 /* monitors progress of blocks and calls cmd_read_data to get each block
1872 you don't know how many blocks there will be in advance because their size may be determined by the scanner*/
1873 SANE_Status status = SANE_STATUS_GOOD;
1874 size_t buf_len = 0;
1875
1876 /* have we passed everything we read to sane? */
1877 if (s->ptr == s->end) {
1878 if (s->eof)
1879 return SANE_STATUS_EOF;
1880
1881 s->counter++;
1882 if (s->bytes_unread >= s->block_len)
1883 buf_len = s->block_len;
1884 else
1885 buf_len = s->bytes_unread;
1886 DBG(min(20,DBG_READ), "%s: block %d, size %lu\n", __func__,
1887 s->counter, (unsigned long) buf_len);
1888
1889 /* receive image data + error code */
1890 status = cmd_read_data (s, s->buf, &buf_len);
1891 if (status != SANE_STATUS_GOOD && status != SANE_STATUS_EOF) { /* was just GOOD 20/2/12 */
1892 DBG (1, "%s: Receiving image data failed (%s)\n",
1893 __func__, sane_strstatus(status));
1894 cmd_cancel_scan(s);
1895 return status;
1896 }
1897
1898 DBG(min(14,DBG_READ), "%s: success %lu bytes of block %d, %d remain\n", __func__, (unsigned long) buf_len, s->counter, s->bytes_unread);
1899
1900 if (s->bytes_unread > 0) {
1901 if (s->canceling) {
1902 cmd_cancel_scan(s);
1903 return SANE_STATUS_CANCELLED;
1904 }
1905 if (status == SANE_STATUS_EOF) {
1906 /* page ended prematurely. */
1907 }
1908 }
1909 else { /* s->bytes_unread <=0 This is the end of a page */
1910 s->eof = SANE_TRUE;
1911 DBG(min(10,DBG_READ), "%s: set EOF after %d blocks\n=============\n", __func__, s->counter);
1912 /* look for the terminating ack if required */
1913 if (!s->ack) {
1914 if (kodakaio_expect_ack(s, rx) == SANE_STATUS_GOOD) {
1915 s->ack = SANE_TRUE;
1916 }
1917 else {
1918 DBG(min(1,DBG_READ), "%s: Did not get expected ack at end of page\n", __func__);
1919 return SANE_STATUS_IO_ERROR;
1920 }
1921 }
1922 }
1923 s->end = s->buf + buf_len;
1924 s->ptr = s->buf;
1925 }
1926 else {
1927 DBG(min(20,DBG_READ), "%s: data left in buffer\n", __func__);
1928 }
1929 return status;
1930 }
1931
1932 /*
1933 * SANE API implementation (high-level functions)
1934 */
1935
1936 #if WITH_AVAHI
1937 static struct KodakaioCap *
get_device_from_identification(const char * ident,const char * vid,const char * pid)1938 get_device_from_identification (const char *ident, const char *vid, const char *pid)
1939 {
1940 int n;
1941 SANE_Word pidnum, vidnum;
1942
1943 if(sscanf(vid, "%x", (unsigned int *)&vidnum) == EOF) {
1944 DBG(5, "could not convert hex vid <%s>\n", vid);
1945 return NULL;
1946 }
1947 if(sscanf(pid, "%x", (unsigned int *)&pidnum) == EOF) {
1948 DBG(5, "could not convert hex pid <%s>\n", pid);
1949 return NULL;
1950 }
1951 for (n = 0; n < NELEMS (kodakaio_cap); n++) {
1952
1953 if (strcmp (kodakaio_cap[n].model, ident)==0) {
1954 DBG(20, "matched <%s> & <%s>\n", kodakaio_cap[n].model, ident);
1955 return &kodakaio_cap[n];
1956 }
1957 else
1958 if (kodakaio_cap[n].id == pidnum && 0x040A == vidnum) {
1959 DBG(20, "matched <%s> & <%s:%s>\n", kodakaio_cap[n].model, vid, pid);
1960 return &kodakaio_cap[n];
1961 }
1962 else {
1963 DBG(20, "not found <%s> & <%s>\n", kodakaio_cap[n].model, pid);
1964 }
1965 }
1966 return NULL;
1967 }
1968 #endif /* WITH_AVAHI */
1969
1970 /*
1971 * close_scanner()
1972 *
1973 * Close the open scanner. Depending on the connection method, a different
1974 * close function is called.
1975 */
1976 static void
close_scanner(KodakAio_Scanner * s)1977 close_scanner(KodakAio_Scanner *s)
1978 {
1979 DBG(7, "%s: fd = %d\n", __func__, s->fd);
1980
1981 if (s->fd == -1)
1982 return;
1983
1984 k_scan_finish(s);
1985 if (s->hw->connection == SANE_KODAKAIO_NET) {
1986 sanei_kodakaio_net_close(s);
1987 sanei_tcp_close(s->fd);
1988 } else if (s->hw->connection == SANE_KODAKAIO_USB) {
1989 sanei_usb_close(s->fd);
1990 }
1991
1992 s->fd = -1;
1993 }
1994
1995 static SANE_Bool
split_scanner_name(const char * name,char * IP,unsigned int * model)1996 split_scanner_name (const char *name, char * IP, unsigned int *model)
1997 {
1998 const char *device = name;
1999 const char *qm;
2000 *model = 0;
2001 /* cut off leading net: */
2002 if (strncmp(device, "net:", 4) == 0)
2003 device = &device[4];
2004
2005 qm = strchr(device, '?');
2006 if (qm != NULL) {
2007 size_t len = qm-device;
2008 strncpy (IP, device, len);
2009 IP[len] = '\0';
2010 qm++;
2011 if (strncmp(qm, "model=", 6) == 0) {
2012 qm += 6;
2013 if (!sscanf(qm, "0x%x", model))
2014 sscanf(qm, "%x", model);
2015 }
2016 } else {
2017 strcpy (IP, device);
2018 }
2019 return SANE_TRUE;
2020 }
2021
2022 /*
2023 * open_scanner()
2024 *
2025 * Open the scanner device. Depending on the connection method,
2026 * different open functions are called.
2027 */
2028
2029 static SANE_Status
open_scanner(KodakAio_Scanner * s)2030 open_scanner(KodakAio_Scanner *s)
2031 {
2032 SANE_Status status = 0;
2033
2034 DBG(7, "%s: %s\n", __func__, s->hw->sane.name);
2035
2036 if (s->fd != -1) {
2037 DBG(10, "scanner is already open: fd = %d\n", s->fd);
2038 return SANE_STATUS_GOOD; /* no need to open the scanner */
2039 }
2040
2041 if (s->hw->connection == SANE_KODAKAIO_NET) {
2042 /* device name has the form net:ipaddr?model=... */
2043 char IP[1024];
2044 unsigned int model = 0;
2045 if (!split_scanner_name (s->hw->sane.name, IP, &model))
2046 return SANE_STATUS_INVAL;
2047 DBG(10, "split_scanner_name OK model=0x%x\n",model);
2048 /* normal with IP */
2049 status = sanei_tcp_open(IP, 9101, &s->fd); /* (host,port,file pointer) */
2050
2051 if (status != SANE_STATUS_GOOD ) DBG(1, "Is network scanner switched on?\n");
2052
2053 if (model>0)
2054 k_set_device (s, model);
2055 if (status == SANE_STATUS_GOOD) {
2056 status = sanei_kodakaio_net_open (s);
2057 }
2058 else DBG(1, "status was not good at net open\n");
2059
2060
2061 } else if (s->hw->connection == SANE_KODAKAIO_USB) {
2062 DBG(7, "trying to open usb\n");
2063 status = sanei_usb_open(s->hw->sane.name, &s->fd);
2064 if (s->hw->cap->out_ep>0)
2065 sanei_usb_set_endpoint (s->fd,
2066 USB_DIR_OUT | USB_ENDPOINT_TYPE_BULK, s->hw->cap->out_ep);
2067 if (s->hw->cap->in_ep>0)
2068 sanei_usb_set_endpoint (s->fd,
2069 USB_DIR_IN | USB_ENDPOINT_TYPE_BULK, s->hw->cap->in_ep);
2070 }
2071
2072 if (status == SANE_STATUS_ACCESS_DENIED) {
2073 DBG(1, "please check that you have permissions on the device.\n");
2074 DBG(1, "if this is a multi-function device with a printer,\n");
2075 DBG(1, "disable any conflicting driver (like usblp).\n");
2076 }
2077
2078 if (status != SANE_STATUS_GOOD)
2079 DBG(1, "%s open failed: %s\n", s->hw->sane.name,
2080 sane_strstatus(status));
2081 else
2082 DBG(3, "scanner opened\n");
2083 /* add check here of usb properties? */
2084 /*sanei_usb_get_descriptor( SANE_Int dn, struct sanei_usb_dev_descriptor *desc );*/
2085
2086
2087 return status;
2088 }
2089
2090 static SANE_Status
detect_usb(struct KodakAio_Scanner * s)2091 detect_usb(struct KodakAio_Scanner *s)
2092 {
2093 SANE_Status status;
2094 SANE_Word vendor, product;
2095 int i, numIds;
2096 SANE_Bool is_valid;
2097
2098 /* if the sanei_usb_get_vendor_product call is not supported,
2099 * then we just ignore this and rely on the user to config
2100 * the correct device.
2101 */
2102
2103 status = sanei_usb_get_vendor_product(s->fd, &vendor, &product);
2104 if (status != SANE_STATUS_GOOD) {
2105 DBG(1, "the device cannot be verified - will continue\n");
2106 return SANE_STATUS_GOOD;
2107 }
2108
2109 /* check the vendor ID to see if we are dealing with a kodak device */
2110 if (vendor != SANE_KODAKAIO_VENDOR_ID) {
2111 /* this is not a supported vendor ID */
2112 DBG(1, "not a Kodak Aio device at %s (vendor id=0x%x)\n",
2113 s->hw->sane.name, vendor);
2114 return SANE_STATUS_INVAL;
2115 }
2116
2117 numIds = kodakaio_getNumberOfUSBProductIds();
2118 is_valid = SANE_FALSE;
2119 i = 0;
2120
2121 /* check all known product IDs to verify that we know
2122 * about the device */
2123 while (i != numIds && !is_valid) {
2124 /* if (product == kodakaio_usb_product_ids[i]) */
2125 if (product == kodakaio_cap[i].id)
2126 is_valid = SANE_TRUE;
2127 i++;
2128 }
2129
2130 if (is_valid == SANE_FALSE) {
2131 DBG(1, "the device at %s is not a supported (product id=0x%x)\n",
2132 s->hw->sane.name, product);
2133 return SANE_STATUS_INVAL;
2134 }
2135
2136 DBG(2, "found valid usb Kodak Aio scanner: 0x%x/0x%x (vendorID/productID)\n",
2137 vendor, product);
2138 k_set_device(s, product); /* added 21/12/11 to try and get a name for the device */
2139
2140 return SANE_STATUS_GOOD;
2141 }
2142
2143 /*
2144 * used by attach* and sane_get_devices
2145 * a ptr to a single-linked list of Kodak_Device structs
2146 * a ptr to a null term array of ptrs to SANE_Device structs
2147 */
2148 static int num_devices; /* number of scanners attached to backend */
2149 static Kodak_Device *first_dev; /* first scanner in list */
2150 static const SANE_Device **devlist = NULL;
2151
2152 static struct KodakAio_Scanner *
scanner_create(struct Kodak_Device * dev,SANE_Status * status)2153 scanner_create(struct Kodak_Device *dev, SANE_Status *status)
2154 {
2155 struct KodakAio_Scanner *s;
2156
2157 s = malloc(sizeof(struct KodakAio_Scanner));
2158 if (s == NULL) {
2159 *status = SANE_STATUS_NO_MEM;
2160 return NULL;
2161 }
2162
2163 memset(s, 0x00, sizeof(struct KodakAio_Scanner));
2164
2165 s->fd = -1;
2166 s->hw = dev;
2167
2168 return s;
2169 }
2170
2171 static struct KodakAio_Scanner *
device_detect(const char * name,int type,SANE_Status * status)2172 device_detect(const char *name, int type, SANE_Status *status)
2173 {
2174 struct KodakAio_Scanner *s;
2175 struct Kodak_Device *dev;
2176
2177 /* try to find the device in our list */
2178 for (dev = first_dev; dev; dev = dev->next) {
2179 if (strcmp(dev->sane.name, name) == 0) {
2180 dev->missing = 0;
2181 DBG (10, "%s: Device %s already attached!\n", __func__,
2182 name);
2183 return scanner_create(dev, status);
2184 }
2185 }
2186
2187 if (type == SANE_KODAKAIO_NODEV) {
2188 *status = SANE_STATUS_INVAL;
2189 return NULL;
2190 }
2191
2192 /* alloc and clear our device structure */
2193 dev = malloc(sizeof(*dev));
2194 if (!dev) {
2195 *status = SANE_STATUS_NO_MEM;
2196 return NULL;
2197 }
2198 memset(dev, 0x00, sizeof(struct Kodak_Device));
2199
2200 s = scanner_create(dev, status);
2201 if (s == NULL)
2202 return NULL;
2203
2204 k_dev_init(dev, name, type);
2205
2206 *status = open_scanner(s);
2207 if (*status != SANE_STATUS_GOOD) {
2208 free(s);
2209 free(dev);
2210 return NULL;
2211 }
2212
2213 /* from now on, close_scanner() must be called */
2214
2215 /* USB requires special care */
2216 if (dev->connection == SANE_KODAKAIO_USB) {
2217 *status = detect_usb(s);
2218 }
2219
2220 if (*status != SANE_STATUS_GOOD)
2221 goto close;
2222
2223 /* set name and model (if not already set) */
2224 if (dev->model == NULL)
2225 k_set_model(s, "generic", 7);
2226
2227 dev->name = strdup(name);
2228 dev->sane.name = dev->name;
2229
2230 /* do we need to discover capabilities here? */
2231 *status = k_discover_capabilities(s);
2232 if (*status != SANE_STATUS_GOOD)
2233 goto close;
2234
2235 if (source_list[0] == NULL || dev->cap->dpi_range.min == 0) {
2236 DBG(1, "something is wrong in the discovery process, aborting.\n");
2237 *status = SANE_STATUS_IO_ERROR;
2238 goto close;
2239 }
2240
2241 /* add this scanner to the device list */
2242 num_devices++;
2243 dev->missing = 0;
2244 dev->next = first_dev;
2245 first_dev = dev;
2246
2247 return s;
2248
2249 close:
2250 close_scanner(s);
2251 free(dev);
2252 free(s);
2253 return NULL;
2254 }
2255
2256
2257
2258 #if WITH_AVAHI
2259 /* ProcessAvahiDevice is called to process each discovered device in turn */
2260 void
ProcessAvahiDevice(const char * device_id,const char * vid,const char * pid,const char * ip_addr)2261 ProcessAvahiDevice(const char *device_id, const char *vid, const char *pid, const char *ip_addr)
2262 {
2263 struct KodakaioCap *cap;
2264
2265 DBG(min(10,DBG_AUTO),"device_id = <%s> vid:pid = <%s:%s>\n", device_id,vid,pid);
2266
2267 /* check if it is a model likely to be supported: "KODAK ESP" or "KODAK HERO"
2268
2269 DBG(min(10,DBG_AUTO),"look up model <%s>\n", device_model); */
2270 cap = get_device_from_identification("", vid, pid);
2271 if (cap == NULL) {
2272 return;
2273 }
2274
2275 DBG(min(10,DBG_AUTO), "%s: Found autodiscovered device: %s (type 0x%x)\n", __func__, cap->model, cap->id);
2276 attach_one_net (ip_addr, cap->id);
2277 }
2278
2279
resolve_callback(AvahiServiceResolver * r,AVAHI_GCC_UNUSED AvahiIfIndex interface,AVAHI_GCC_UNUSED AvahiProtocol protocol,AvahiResolverEvent event,const char * name,const char * type,const char * domain,const char * host_name,const AvahiAddress * address,uint16_t port,AvahiStringList * txt,AvahiLookupResultFlags flags,AVAHI_GCC_UNUSED void * userdata)2280 static void resolve_callback(
2281 AvahiServiceResolver *r,
2282 AVAHI_GCC_UNUSED AvahiIfIndex interface,
2283 AVAHI_GCC_UNUSED AvahiProtocol protocol,
2284 AvahiResolverEvent event,
2285 const char *name,
2286 const char *type,
2287 const char *domain,
2288 const char *host_name,
2289 const AvahiAddress *address,
2290 uint16_t port,
2291 AvahiStringList *txt,
2292 AvahiLookupResultFlags flags,
2293 AVAHI_GCC_UNUSED void* userdata) {
2294
2295 AvahiStringList *vid_pair_list = NULL, *pid_pair_list = NULL;
2296 char *pidkey, *pidvalue;
2297 char *vidkey, *vidvalue;
2298 size_t valuesize;
2299 NOT_USED (flags);
2300
2301 assert(r);
2302
2303 /* Called whenever a service has been resolved successfully or timed out */
2304
2305 switch (event) {
2306 case AVAHI_RESOLVER_FAILURE:
2307 DBG(min(1,DBG_AUTO), "(Resolver) Failed to resolve service '%s' of type '%s' in domain '%s': %s\n", name, type, domain, avahi_strerror(avahi_client_errno(avahi_service_resolver_get_client(r))));
2308 break;
2309
2310 case AVAHI_RESOLVER_FOUND: {
2311 char a[AVAHI_ADDRESS_STR_MAX];
2312
2313 avahi_address_snprint(a, sizeof(a), address);
2314
2315 /* Output short for Kodak ESP */
2316 DBG(min(10,DBG_AUTO), "%s:%u %s\n", a,port,host_name);
2317
2318 vid_pair_list = avahi_string_list_find(txt, "vid");
2319 if(vid_pair_list != NULL) {
2320 avahi_string_list_get_pair(vid_pair_list, &vidkey, &vidvalue, &valuesize);
2321 DBG(min(10,DBG_AUTO), "%s=%s ", vidkey, vidvalue);
2322 }
2323 else DBG(min(10,DBG_AUTO), "failed to find key vid\n");
2324
2325 pid_pair_list = avahi_string_list_find(txt, "pid");
2326 if(pid_pair_list != NULL) {
2327 avahi_string_list_get_pair(pid_pair_list, &pidkey, &pidvalue, &valuesize);
2328 DBG(min(10,DBG_AUTO), "%s=%s\n", pidkey, pidvalue);
2329 }
2330 else DBG(min(10,DBG_AUTO), "failed to find key pid\n");
2331
2332 if(pid_pair_list != NULL && vid_pair_list != NULL) {
2333 ProcessAvahiDevice(name, vidvalue, pidvalue, a);
2334 }
2335 else DBG(min(10,DBG_AUTO), "didn't call ProcessAvahiDevice\n");
2336
2337 if(vid_pair_list != NULL) {
2338 avahi_free(vidkey);
2339 avahi_free(vidvalue);
2340 DBG(min(15,DBG_AUTO), "vidkey and vidvalue freed\n");
2341 }
2342 if(pid_pair_list != NULL) {
2343 avahi_free(pidkey);
2344 avahi_free(pidvalue);
2345 DBG(min(15,DBG_AUTO), "pidkey and pidvalue freed\n");
2346 }
2347 }
2348 }
2349 DBG(min(10,DBG_AUTO), "ending resolve_callback\n");
2350 avahi_service_resolver_free(r);
2351 }
2352
browse_callback(AvahiServiceBrowser * b,AvahiIfIndex interface,AvahiProtocol protocol,AvahiBrowserEvent event,const char * name,const char * type,const char * domain,AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,void * userdata)2353 static void browse_callback(
2354 AvahiServiceBrowser *b,
2355 AvahiIfIndex interface,
2356 AvahiProtocol protocol,
2357 AvahiBrowserEvent event,
2358 const char *name,
2359 const char *type,
2360 const char *domain,
2361 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
2362 void* userdata) {
2363 AvahiSimplePoll *simple_poll = userdata;
2364
2365 AvahiClient *c = avahi_service_browser_get_client (b);
2366
2367 /* Called whenever a new services becomes available on the LAN or is removed from the LAN */
2368 switch (event) {
2369 case AVAHI_BROWSER_FAILURE:
2370
2371 DBG(min(1,DBG_AUTO), "(Browser) %s\n", avahi_strerror(avahi_client_errno(avahi_service_browser_get_client(b))));
2372 avahi_simple_poll_quit(simple_poll);
2373 return;
2374
2375 case AVAHI_BROWSER_NEW:
2376 DBG(min(5,DBG_AUTO), "(Browser) NEW: service '%s' of type '%s' in domain '%s'\n", name, type, domain);
2377
2378 /* We ignore the returned resolver object. In the callback
2379 function we free it. If the server is terminated before
2380 the callback function is called the server will free
2381 the resolver for us. */
2382
2383 if (!(avahi_service_resolver_new(c, interface, protocol, name, type, domain, AVAHI_PROTO_UNSPEC, 0, resolve_callback, c)))
2384 DBG(min(1,DBG_AUTO), "Failed to resolve service '%s': %s\n", name, avahi_strerror(avahi_client_errno(c)));
2385
2386 break;
2387
2388 case AVAHI_BROWSER_REMOVE:
2389 DBG(min(1,DBG_AUTO), "(Browser) REMOVE: service '%s' of type '%s' in domain '%s'\n", name, type, domain);
2390 break;
2391
2392 case AVAHI_BROWSER_ALL_FOR_NOW:
2393 case AVAHI_BROWSER_CACHE_EXHAUSTED:
2394 DBG(min(5,DBG_AUTO), "(Browser) %s\n", event == AVAHI_BROWSER_CACHE_EXHAUSTED ? "CACHE_EXHAUSTED" : "ALL_FOR_NOW");
2395 break;
2396 }
2397 }
2398
client_callback(AvahiClient * c,AvahiClientState state,void * userdata)2399 static void client_callback(AvahiClient *c, AvahiClientState state, void * userdata) {
2400 AvahiSimplePoll *simple_poll = userdata;
2401 assert(c);
2402
2403 /* Called whenever the client or server state changes */
2404
2405 if (state == AVAHI_CLIENT_FAILURE) {
2406 DBG(min(1,DBG_AUTO), "Server connection failure: %s\n", avahi_strerror(avahi_client_errno(c)));
2407 avahi_simple_poll_quit(simple_poll);
2408 }
2409 }
2410
2411
2412 static int
kodak_network_discovery(const char * host)2413 kodak_network_discovery(const char*host)
2414 /* If host = NULL do autodiscovery. If host != NULL try to verify the model
2415 First version only does autodiscovery */
2416 {
2417 AvahiSimplePoll *simple_poll;
2418 AvahiClient *client = NULL;
2419 AvahiServiceBrowser *sb = NULL;
2420 int error;
2421 int i, ret = 1;
2422 NOT_USED(host);
2423
2424 DBG(2, "%s: called\n", __func__);
2425
2426 /* Allocate main loop object */
2427 if (!(simple_poll = avahi_simple_poll_new())) {
2428 DBG(min(1,DBG_AUTO), "Failed to create simple poll object.\n");
2429 goto fail;
2430 }
2431
2432 /* Allocate a new client */
2433 client = avahi_client_new(avahi_simple_poll_get(simple_poll), 0, client_callback, simple_poll, &error);
2434
2435 /* Check whether creating the client object succeeded */
2436 if (!client) {
2437 DBG(min(1,DBG_AUTO), "Failed to create client: %s\n", avahi_strerror(error));
2438 goto fail;
2439 }
2440
2441 /* Create the service browser */
2442 if (!(sb = avahi_service_browser_new(client, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "_scanner._tcp", NULL, 0, browse_callback, simple_poll))) {
2443 DBG(min(1,DBG_AUTO), "Failed to create service browser: %s\n", avahi_strerror(avahi_client_errno(client)));
2444 goto fail;
2445 }
2446
2447 /* Run the main loop */
2448 for(i=1;i<K_SNMP_Timeout/POLL_ITN_MS;++i) {
2449 if (avahi_simple_poll_iterate(simple_poll,POLL_ITN_MS) != 0)
2450 break;
2451 }
2452 ret = 0;
2453
2454 fail:
2455
2456
2457 /* Cleanup things */
2458 DBG(min(10,DBG_AUTO), "Cleaning up avahi.\n");
2459 if (sb)
2460 avahi_service_browser_free(sb);
2461
2462 if (client)
2463 avahi_client_free(client);
2464
2465 if (simple_poll)
2466 avahi_simple_poll_free(simple_poll);
2467
2468 return ret;
2469 }
2470
2471 #endif
2472
2473
2474 static SANE_Status
attach(const char * name,int type)2475 attach(const char *name, int type)
2476 {
2477 SANE_Status status;
2478 KodakAio_Scanner *s;
2479
2480 DBG(7, "%s: devname = %s, type = %d\n", __func__, name, type);
2481
2482 s = device_detect(name, type, &status);
2483 if(s == NULL)
2484 return status;
2485
2486 close_scanner(s);
2487 free(s);
2488 return status;
2489 }
2490
2491 SANE_Status
attach_one_usb(const char * dev)2492 attach_one_usb(const char *dev)
2493 {
2494 DBG(7, "%s: dev = %s\n", __func__, dev);
2495 return attach(dev, SANE_KODAKAIO_USB);
2496 }
2497
2498 static SANE_Status
attach_one_net(const char * dev,unsigned int model)2499 attach_one_net(const char *dev, unsigned int model)
2500 {
2501 char name[1024];
2502
2503 DBG(7, "%s: dev = %s\n", __func__, dev);
2504 if (model > 0) {
2505 snprintf(name, 1024, "net:%s?model=0x%x", dev, model);
2506 } else {
2507 snprintf(name, 1024, "net:%s", dev);
2508 }
2509
2510 return attach(name, SANE_KODAKAIO_NET);
2511 }
2512
2513 static SANE_Status
attach_one_config(SANEI_Config __sane_unused__ * config,const char * line,void * data)2514 attach_one_config(SANEI_Config __sane_unused__ *config, const char *line,
2515 void *data)
2516 {
2517 int vendor, product, timeout;
2518 SANE_Bool local_only = *(SANE_Bool*) data;
2519 int len = strlen(line);
2520
2521 DBG(7, "%s: len = %d, line = %s\n", __func__, len, line);
2522
2523 if (sscanf(line, "usb %i %i", &vendor, &product) == 2) {
2524 /* add the vendor and product IDs to the list of
2525 * known devices before we call the attach function */
2526
2527 int numIds = kodakaio_getNumberOfUSBProductIds();
2528
2529 if (vendor != SANE_KODAKAIO_VENDOR_ID) {
2530 DBG(7, "Wrong vendor: numIds = %d, vendor = %d\n", numIds, vendor);
2531 return SANE_STATUS_INVAL; /* this is not a Kodak device */
2532 }
2533 /* kodakaio_usb_product_ids[numIds - 1] = product; */
2534 kodakaio_cap[numIds - 1].id = product;
2535
2536 sanei_usb_attach_matching_devices(line, attach_one_usb);
2537
2538 } else if (strncmp(line, "usb", 3) == 0 && len == 3) {
2539 int i, numIds;
2540 /* auto detect ? */
2541 numIds = kodakaio_getNumberOfUSBProductIds();
2542
2543 for (i = 0; i < numIds; i++) {
2544 /* sanei_usb_find_devices(SANE_KODAKAIO_VENDOR_ID,
2545 kodakaio_usb_product_ids[i], attach_one_usb); */
2546 sanei_usb_find_devices(SANE_KODAKAIO_VENDOR_ID,
2547 kodakaio_cap[i].id, attach_one_usb);
2548 }
2549
2550 } else if (strncmp(line, "net", 3) == 0) {
2551
2552 if (!local_only) {
2553 /* remove the "net" sub string */
2554 const char *name =
2555 sanei_config_skip_whitespace(line + 3);
2556 char IP[1024];
2557 unsigned int model = 0;
2558
2559 if (strncmp(name, "autodiscovery", 13) == 0) {
2560
2561 #if WITH_AVAHI
2562 DBG (30, "%s: Initiating network autodiscovery via avahi\n", __func__);
2563 kodak_network_discovery(NULL);
2564 #else
2565 DBG (20, "%s: Network autodiscovery not done because not configured with avahi.\n", __func__);
2566 #endif
2567
2568 } else if (sscanf(name, "%s %x", IP, &model) == 2) {
2569 DBG(30, "%s: Using network device on IP %s, forcing model 0x%x\n", __func__, IP, model);
2570 attach_one_net(IP, model);
2571 } else {
2572 DBG(1, "%s: net entry %s may be a host name?\n", __func__, name);
2573 attach_one_net(name, 0);
2574 }
2575 }
2576
2577 } else if (sscanf(line, "snmp-timeout %i\n", &timeout)) {
2578 /* Timeout for auto network discovery */
2579 DBG(50, "%s: network auto-discovery timeout set to %d\n", __func__, timeout);
2580 K_SNMP_Timeout = timeout;
2581
2582 } else if (sscanf(line, "scan-data-timeout %i\n", &timeout)) {
2583 /* Timeout for scan data requests */
2584 DBG(50, "%s: Scan data timeout set to %d\n", __func__, timeout);
2585 K_Scan_Data_Timeout = timeout;
2586
2587 } else if (sscanf(line, "request-timeout %i\n", &timeout)) {
2588 /* Timeout for all other read requests */
2589 DBG(50, "%s: Request timeout set to %d\n", __func__, timeout);
2590 K_Request_Timeout = timeout;
2591
2592 } else {
2593 /* TODO: Warning about unparsable line! */
2594 }
2595
2596 return SANE_STATUS_GOOD;
2597 }
2598
2599 static void
free_devices(void)2600 free_devices(void)
2601 {
2602 Kodak_Device *dev, *next;
2603
2604 DBG(5, "%s\n", __func__);
2605
2606 for (dev = first_dev; dev; dev = next) {
2607 next = dev->next;
2608 free(dev->name);
2609 free(dev->model);
2610 free(dev);
2611 }
2612
2613 if (devlist)
2614 free(devlist);
2615 devlist = NULL;
2616 first_dev = NULL;
2617 }
2618
2619 SANE_Status
sane_init(SANE_Int * version_code,SANE_Auth_Callback __sane_unused__ authorize)2620 sane_init(SANE_Int *version_code, SANE_Auth_Callback __sane_unused__ authorize)
2621 {
2622 DBG_INIT();
2623 DBG(1, "========================================== \n");
2624 DBG(2, "%s: " PACKAGE " " VERSION "\n", __func__);
2625
2626 DBG(1, "kodakaio backend, version %i.%i.%i\n",
2627 KODAKAIO_VERSION, KODAKAIO_REVISION, KODAKAIO_BUILD);
2628 DBG(2, "%s: called\n", __func__);
2629 if (version_code != NULL)
2630 *version_code = SANE_VERSION_CODE(SANE_CURRENT_MAJOR, SANE_CURRENT_MINOR,
2631 KODAKAIO_BUILD);
2632 sanei_usb_init();
2633
2634 #if WITH_AVAHI
2635 DBG(min(3,DBG_AUTO), "avahi detected\n");
2636 #else
2637 DBG(min(3,DBG_AUTO), "avahi not detected\n");
2638 #endif
2639 return SANE_STATUS_GOOD;
2640 }
2641
2642 /* Clean up the list of attached scanners. */
2643 void
sane_exit(void)2644 sane_exit(void)
2645 {
2646 DBG(5, "%s\n", __func__);
2647 free_devices();
2648 }
2649
2650 SANE_Status
sane_get_devices(const SANE_Device *** device_list,SANE_Bool local_only)2651 sane_get_devices(const SANE_Device ***device_list, SANE_Bool local_only)
2652 {
2653 Kodak_Device *dev, *s, *prev=0;
2654 int i;
2655
2656 DBG(2, "%s: called\n", __func__);
2657
2658 sanei_usb_init();
2659
2660 /* mark all existing scanners as missing, attach_one will remove mark */
2661 for (s = first_dev; s; s = s->next) {
2662 s->missing = 1;
2663 }
2664
2665 /* Read the config, mark each device as found, possibly add new devs */
2666 sanei_configure_attach(KODAKAIO_CONFIG_FILE, NULL,
2667 attach_one_config, &local_only);
2668
2669 /*delete missing scanners from list*/
2670 for (s = first_dev; s;) {
2671 if (s->missing) {
2672 DBG (5, "%s: missing scanner %s\n", __func__, s->name);
2673
2674 /*splice s out of list by changing pointer in prev to next*/
2675 if (prev) {
2676 prev->next = s->next;
2677 free (s);
2678 s = prev->next;
2679 num_devices--;
2680 } else {
2681 /*remove s from head of list */
2682 first_dev = s->next;
2683 free(s);
2684 s = first_dev;
2685 prev=NULL;
2686 num_devices--;
2687 }
2688 } else {
2689 prev = s;
2690 s = prev->next;
2691 }
2692 }
2693
2694 DBG (15, "%s: found %d scanner(s)\n", __func__, num_devices);
2695 for (s = first_dev; s; s=s->next) {
2696 DBG (15, "%s: found scanner %s\n", __func__, s->name);
2697 }
2698
2699 if (devlist)
2700 free (devlist);
2701
2702 devlist = malloc((num_devices + 1) * sizeof(devlist[0]));
2703 if (!devlist) {
2704 DBG(1, "out of memory (line %d)\n", __LINE__);
2705 return SANE_STATUS_NO_MEM;
2706 }
2707
2708 DBG(5, "%s - results:\n", __func__);
2709
2710 for (i = 0, dev = first_dev; i < num_devices && dev; dev = dev->next, i++) {
2711 DBG(5, " %d (%d): %s\n", i, dev->connection, dev->model);
2712 devlist[i] = &dev->sane;
2713 }
2714
2715 devlist[i] = NULL;
2716
2717 if(device_list){
2718 *device_list = devlist;
2719 }
2720
2721 return SANE_STATUS_GOOD;
2722 }
2723
2724 static SANE_Status
init_options(KodakAio_Scanner * s)2725 init_options(KodakAio_Scanner *s)
2726 {
2727 int i;
2728 SANE_Word *res_list;
2729 DBG(5, "%s: called\n", __func__);
2730
2731 for (i = 0; i < NUM_OPTIONS; i++) {
2732 s->opt[i].size = sizeof(SANE_Word);
2733 s->opt[i].cap = SANE_CAP_SOFT_SELECT | SANE_CAP_SOFT_DETECT;
2734 }
2735
2736 s->opt[OPT_NUM_OPTS].title = SANE_TITLE_NUM_OPTIONS;
2737 s->opt[OPT_NUM_OPTS].desc = SANE_DESC_NUM_OPTIONS;
2738 s->opt[OPT_NUM_OPTS].type = SANE_TYPE_INT;
2739 s->opt[OPT_NUM_OPTS].cap = SANE_CAP_SOFT_DETECT;
2740 s->val[OPT_NUM_OPTS].w = NUM_OPTIONS;
2741
2742 /* "Scan Mode" group: */
2743
2744 s->opt[OPT_MODE_GROUP].name = SANE_NAME_STANDARD;
2745 s->opt[OPT_MODE_GROUP].title = SANE_TITLE_STANDARD;
2746 s->opt[OPT_MODE_GROUP].desc = SANE_DESC_STANDARD;
2747 s->opt[OPT_MODE_GROUP].type = SANE_TYPE_GROUP;
2748 s->opt[OPT_MODE_GROUP].cap = 0;
2749
2750 /* scan mode */
2751 s->opt[OPT_MODE].name = SANE_NAME_SCAN_MODE;
2752 s->opt[OPT_MODE].title = SANE_TITLE_SCAN_MODE;
2753 s->opt[OPT_MODE].desc = SANE_DESC_SCAN_MODE;
2754 s->opt[OPT_MODE].type = SANE_TYPE_STRING;
2755 s->opt[OPT_MODE].size = max_string_size(mode_list);
2756 s->opt[OPT_MODE].constraint_type = SANE_CONSTRAINT_STRING_LIST;
2757 s->opt[OPT_MODE].constraint.string_list = mode_list;
2758 s->val[OPT_MODE].w = MODE_COLOR; /* default */
2759 DBG(20, "%s: mode_list has first entry %s, default mode is %s\n", __func__, mode_list[0],mode_list[s->val[OPT_MODE].w]);
2760
2761 /* threshold the sane std says should be SANE_TYPE_FIXED 0..100 but all other backends seem to use INT 0..255 */
2762 s->opt[OPT_THRESHOLD].name = SANE_NAME_THRESHOLD;
2763 s->opt[OPT_THRESHOLD].title = SANE_TITLE_THRESHOLD;
2764 s->opt[OPT_THRESHOLD].desc = SANE_DESC_THRESHOLD;
2765 s->opt[OPT_THRESHOLD].type = SANE_TYPE_FIXED;
2766 s->opt[OPT_THRESHOLD].unit = SANE_UNIT_PERCENT;
2767 s->opt[OPT_THRESHOLD].size = sizeof(SANE_Word);
2768 s->opt[OPT_THRESHOLD].constraint_type = SANE_CONSTRAINT_RANGE;
2769 s->opt[OPT_THRESHOLD].constraint.range = &percent_range_fixed;
2770 s->val[OPT_THRESHOLD].w = SANE_FIX(50.0);
2771 DBG(20, "%s: threshold initialised to fixed %f\n", __func__, SANE_UNFIX(s->val[OPT_THRESHOLD].w));
2772
2773 /* threshold the sane std says should be SANE_TYPE_FIXED 0..100 but all other backends seem to use INT 0..255
2774 s->opt[OPT_THRESHOLD].name = SANE_NAME_THRESHOLD;
2775 s->opt[OPT_THRESHOLD].title = SANE_TITLE_THRESHOLD;
2776 s->opt[OPT_THRESHOLD].desc = SANE_DESC_THRESHOLD;
2777 s->opt[OPT_THRESHOLD].type = SANE_TYPE_INT;
2778 s->opt[OPT_THRESHOLD].unit = SANE_UNIT_PERCENT;
2779 s->opt[OPT_THRESHOLD].size = sizeof(SANE_Word);
2780 s->opt[OPT_THRESHOLD].constraint_type = SANE_CONSTRAINT_RANGE;
2781 s->opt[OPT_THRESHOLD].constraint.range = &percent_range_int;
2782 s->val[OPT_THRESHOLD].w = 51;
2783 DBG(20, "%s: threshold initialised to int %d\n", __func__, s->val[OPT_THRESHOLD].w); */
2784
2785 /* bit depth */
2786 s->opt[OPT_BIT_DEPTH].name = SANE_NAME_BIT_DEPTH;
2787 s->opt[OPT_BIT_DEPTH].title = SANE_TITLE_BIT_DEPTH;
2788 s->opt[OPT_BIT_DEPTH].desc = SANE_DESC_BIT_DEPTH;
2789 s->opt[OPT_BIT_DEPTH].type = SANE_TYPE_INT;
2790 s->opt[OPT_BIT_DEPTH].unit = SANE_UNIT_NONE;
2791 s->opt[OPT_BIT_DEPTH].constraint_type = SANE_CONSTRAINT_WORD_LIST;
2792 s->opt[OPT_BIT_DEPTH].constraint.word_list = s->hw->cap->depth_list;
2793 s->opt[OPT_BIT_DEPTH].cap |= SANE_CAP_INACTIVE;
2794 s->val[OPT_BIT_DEPTH].w = s->hw->cap->depth_list[1]; /* the first "real" element is the default */
2795
2796 DBG(20, "%s: depth list has depth_list[0] = %d entries\n", __func__, s->hw->cap->depth_list[0]);
2797 if (s->hw->cap->depth_list[0] == 1) { /* only one element in the list -> hide the option */
2798 s->opt[OPT_BIT_DEPTH].cap |= SANE_CAP_INACTIVE;
2799 DBG(20, "%s: Only one depth in list so inactive option\n", __func__);
2800 }
2801
2802 /* resolution */
2803 s->opt[OPT_RESOLUTION].name = SANE_NAME_SCAN_RESOLUTION;
2804 s->opt[OPT_RESOLUTION].title = SANE_TITLE_SCAN_RESOLUTION;
2805 s->opt[OPT_RESOLUTION].desc = SANE_DESC_SCAN_RESOLUTION;
2806 s->opt[OPT_RESOLUTION].type = SANE_TYPE_INT;
2807 s->opt[OPT_RESOLUTION].unit = SANE_UNIT_DPI;
2808 s->opt[OPT_RESOLUTION].constraint_type = SANE_CONSTRAINT_WORD_LIST;
2809 res_list = malloc((s->hw->cap->res_list_size + 1) * sizeof(SANE_Word));
2810 if (res_list == NULL) {
2811 return SANE_STATUS_NO_MEM;
2812 }
2813 *(res_list) = s->hw->cap->res_list_size;
2814 memcpy(&(res_list[1]), s->hw->cap->res_list, s->hw->cap->res_list_size * sizeof(SANE_Word));
2815 s->opt[OPT_RESOLUTION].constraint.word_list = res_list;
2816 s->val[OPT_RESOLUTION].w = s->hw->cap->dpi_range.min;
2817
2818
2819 /* trial option for debugging
2820 s->opt[OPT_TRIALOPT].name = "trialoption";
2821 s->opt[OPT_TRIALOPT].title = "trialoption";
2822 s->opt[OPT_TRIALOPT].desc = "trialoption";
2823 s->opt[OPT_TRIALOPT].type = SANE_TYPE_INT;
2824 s->opt[OPT_TRIALOPT].unit = SANE_UNIT_NONE;
2825 s->opt[OPT_TRIALOPT].size = sizeof(SANE_Word);
2826 s->opt[OPT_TRIALOPT].constraint_type = SANE_CONSTRAINT_RANGE;
2827 s->opt[OPT_TRIALOPT].constraint.range = &percent_range_int;
2828 s->val[OPT_TRIALOPT].w = 1; */
2829
2830 /* preview */
2831 s->opt[OPT_PREVIEW].name = SANE_NAME_PREVIEW;
2832 s->opt[OPT_PREVIEW].title = SANE_TITLE_PREVIEW;
2833 s->opt[OPT_PREVIEW].desc = SANE_DESC_PREVIEW;
2834 s->opt[OPT_PREVIEW].type = SANE_TYPE_BOOL;
2835 s->val[OPT_PREVIEW].w = SANE_FALSE;
2836
2837 for(i=0;source_list[i]!=NULL;++i)
2838 DBG(18, "source_list: %s\n",source_list[i]);
2839
2840 /* source */
2841 s->opt[OPT_SOURCE].name = SANE_NAME_SCAN_SOURCE;
2842 s->opt[OPT_SOURCE].title = SANE_TITLE_SCAN_SOURCE;
2843 s->opt[OPT_SOURCE].desc = SANE_DESC_SCAN_SOURCE;
2844 s->opt[OPT_SOURCE].type = SANE_TYPE_STRING;
2845 s->opt[OPT_SOURCE].size = max_string_size(source_list);
2846 s->opt[OPT_SOURCE].constraint_type = SANE_CONSTRAINT_STRING_LIST;
2847 s->opt[OPT_SOURCE].constraint.string_list = source_list;
2848 s->val[OPT_SOURCE].w = 0; /* always use Flatbed as default */
2849
2850 if ((!s->hw->cap->ADF)) {
2851 DBG(9, "device with no adf detected source option inactive\n");
2852 s->opt[OPT_SOURCE].cap |= SANE_CAP_INACTIVE;
2853 }
2854
2855 /* Are there any ESP scanners that are duplex? */
2856 s->opt[OPT_ADF_MODE].name = "adf-mode";
2857 s->opt[OPT_ADF_MODE].title = SANE_I18N("ADF Mode");
2858 s->opt[OPT_ADF_MODE].desc =
2859 SANE_I18N("Selects the ADF mode (simplex/duplex)");
2860 s->opt[OPT_ADF_MODE].type = SANE_TYPE_STRING;
2861 s->opt[OPT_ADF_MODE].size = max_string_size(adf_mode_list);
2862 s->opt[OPT_ADF_MODE].constraint_type = SANE_CONSTRAINT_STRING_LIST;
2863 s->opt[OPT_ADF_MODE].constraint.string_list = adf_mode_list;
2864 s->val[OPT_ADF_MODE].w = 0; /* simplex */
2865 if ((!s->hw->cap->ADF) || (!s->hw->cap->adf_duplex))
2866 s->opt[OPT_ADF_MODE].cap |= SANE_CAP_INACTIVE;
2867
2868
2869 /* "Geometry" group: */
2870 s->opt[OPT_GEOMETRY_GROUP].name = SANE_NAME_GEOMETRY;
2871 s->opt[OPT_GEOMETRY_GROUP].title = SANE_TITLE_GEOMETRY;
2872 s->opt[OPT_GEOMETRY_GROUP].desc = SANE_DESC_GEOMETRY;
2873 s->opt[OPT_GEOMETRY_GROUP].type = SANE_TYPE_GROUP;
2874 s->opt[OPT_GEOMETRY_GROUP].cap = SANE_CAP_ADVANCED;
2875
2876 /* top-left x */
2877 s->opt[OPT_TL_X].name = SANE_NAME_SCAN_TL_X;
2878 s->opt[OPT_TL_X].title = SANE_TITLE_SCAN_TL_X;
2879 s->opt[OPT_TL_X].desc = SANE_DESC_SCAN_TL_X;
2880 s->opt[OPT_TL_X].type = SANE_TYPE_FIXED;
2881 s->opt[OPT_TL_X].unit = SANE_UNIT_MM;
2882 s->opt[OPT_TL_X].constraint_type = SANE_CONSTRAINT_RANGE;
2883 s->opt[OPT_TL_X].constraint.range = s->hw->x_range;
2884 s->val[OPT_TL_X].w = 0;
2885
2886 /* top-left y */
2887 s->opt[OPT_TL_Y].name = SANE_NAME_SCAN_TL_Y;
2888 s->opt[OPT_TL_Y].title = SANE_TITLE_SCAN_TL_Y;
2889 s->opt[OPT_TL_Y].desc = SANE_DESC_SCAN_TL_Y;
2890 s->opt[OPT_TL_Y].type = SANE_TYPE_FIXED;
2891 s->opt[OPT_TL_Y].unit = SANE_UNIT_MM;
2892 s->opt[OPT_TL_Y].constraint_type = SANE_CONSTRAINT_RANGE;
2893 s->opt[OPT_TL_Y].constraint.range = s->hw->y_range;
2894 s->val[OPT_TL_Y].w = 0;
2895
2896 /* bottom-right x */
2897 s->opt[OPT_BR_X].name = SANE_NAME_SCAN_BR_X;
2898 s->opt[OPT_BR_X].title = SANE_TITLE_SCAN_BR_X;
2899 s->opt[OPT_BR_X].desc = SANE_DESC_SCAN_BR_X;
2900 s->opt[OPT_BR_X].type = SANE_TYPE_FIXED;
2901 s->opt[OPT_BR_X].unit = SANE_UNIT_MM;
2902 s->opt[OPT_BR_X].constraint_type = SANE_CONSTRAINT_RANGE;
2903 s->opt[OPT_BR_X].constraint.range = s->hw->x_range;
2904 s->val[OPT_BR_X].w = s->hw->x_range->max;
2905
2906 /* bottom-right y */
2907 s->opt[OPT_BR_Y].name = SANE_NAME_SCAN_BR_Y;
2908 s->opt[OPT_BR_Y].title = SANE_TITLE_SCAN_BR_Y;
2909 s->opt[OPT_BR_Y].desc = SANE_DESC_SCAN_BR_Y;
2910 s->opt[OPT_BR_Y].type = SANE_TYPE_FIXED;
2911 s->opt[OPT_BR_Y].unit = SANE_UNIT_MM;
2912 s->opt[OPT_BR_Y].constraint_type = SANE_CONSTRAINT_RANGE;
2913 s->opt[OPT_BR_Y].constraint.range = s->hw->y_range;
2914 s->val[OPT_BR_Y].w = s->hw->y_range->max;
2915
2916 /* padding short pages in adf */
2917 s->opt[OPT_PADDING].name = "adf-padding";
2918 s->opt[OPT_PADDING].title = "pad short adf pages";
2919 s->opt[OPT_PADDING].desc = "Selects whether to make short pages up to full length";
2920 s->opt[OPT_PADDING].type = SANE_TYPE_BOOL;
2921 s->val[OPT_PADDING].w = SANE_FALSE;
2922 if ((!s->hw->cap->ADF) || (strcmp(source_list[s->val[OPT_SOURCE].w], ADF_STR) != 0))
2923 {
2924 DBG(9, "adf not source so padding option off and inactive\n");
2925 s->opt[OPT_PADDING].cap |= SANE_CAP_INACTIVE;
2926 }
2927
2928 return SANE_STATUS_GOOD;
2929 }
2930
2931 SANE_Status
sane_open(SANE_String_Const name,SANE_Handle * handle)2932 sane_open(SANE_String_Const name, SANE_Handle *handle)
2933 {
2934 SANE_Status status;
2935 KodakAio_Scanner *s = NULL;
2936
2937 int l = strlen(name);
2938
2939 DBG(2, "%s: name = %s\n", __func__, name);
2940
2941 /* probe if empty device name provided */
2942 if (l == 0) {
2943
2944 status = sane_get_devices(NULL,0);
2945 if (status != SANE_STATUS_GOOD) {
2946 return status;
2947 }
2948
2949 if (first_dev == NULL) {
2950 DBG(1, "no device detected\n");
2951 return SANE_STATUS_INVAL;
2952 }
2953
2954 s = device_detect(first_dev->sane.name, first_dev->connection,
2955 &status);
2956 if (s == NULL) {
2957 DBG(1, "cannot open a perfectly valid device (%s),"
2958 " please report to the authors\n", name);
2959 return SANE_STATUS_INVAL;
2960 }
2961
2962 } else {
2963
2964 if (strncmp(name, "net:", 4) == 0) {
2965 s = device_detect(name, SANE_KODAKAIO_NET, &status);
2966 if (s == NULL)
2967 return status;
2968 } else if (strncmp(name, "libusb:", 7) == 0) {
2969 s = device_detect(name, SANE_KODAKAIO_USB, &status);
2970 if (s == NULL)
2971 return status;
2972 } else {
2973
2974 /* as a last resort, check for a match
2975 * in the device list. This should handle platforms without libusb.
2976 */
2977 if (first_dev == NULL) {
2978 status = sane_get_devices(NULL,0);
2979 if (status != SANE_STATUS_GOOD) {
2980 return status;
2981 }
2982 }
2983
2984 s = device_detect(name, SANE_KODAKAIO_NODEV, &status);
2985 if (s == NULL) {
2986 DBG(1, "invalid device name: %s\n", name);
2987 return SANE_STATUS_INVAL;
2988 }
2989 }
2990 }
2991
2992
2993 /* s is always valid here */
2994
2995 DBG(10, "handle obtained\n");
2996 status = k_discover_capabilities(s); /* added 27/12/11 to fix source list problem
2997 maybe we should only be rebuilding the source list here? */
2998 if (status != SANE_STATUS_GOOD)
2999 return status;
3000
3001 init_options(s);
3002
3003 *handle = (SANE_Handle) s;
3004
3005 /* moving the open scanner section below to sane_start 27/12/14
3006 status = open_scanner(s);
3007 if (status != SANE_STATUS_GOOD) {
3008 free(s);
3009 return status;
3010 }
3011 */
3012 return status;
3013 }
3014
3015 void
sane_close(SANE_Handle handle)3016 sane_close(SANE_Handle handle)
3017 {
3018 KodakAio_Scanner *s;
3019
3020 /*
3021 * XXX Test if there is still data pending from
3022 * the scanner. If so, then do a cancel
3023 */
3024
3025 s = (KodakAio_Scanner *) handle;
3026 DBG(2, "%s: called\n", __func__);
3027
3028 /* moving the close scanner section below to sane_cancel 27/12/14 */
3029 if (s->fd != -1)
3030 close_scanner(s);
3031 /* end of section */
3032
3033 if(RawScan != NULL)
3034 fclose(RawScan);
3035 RawScan = NULL;
3036 free(s);
3037 }
3038
3039 const SANE_Option_Descriptor *
sane_get_option_descriptor(SANE_Handle handle,SANE_Int option)3040 sane_get_option_descriptor(SANE_Handle handle, SANE_Int option)
3041 {
3042 /* this may be a sane call, but it happens way too often to have DBG level 2 */
3043 KodakAio_Scanner *s = (KodakAio_Scanner *) handle;
3044
3045 DBG(30, "%s: called for option %d\n", __func__, option);
3046
3047 if (option < 0 || option >= NUM_OPTIONS)
3048 return NULL;
3049
3050 return s->opt + option;
3051 }
3052
3053 static const SANE_String_Const *
search_string_list(const SANE_String_Const * list,SANE_String value)3054 search_string_list(const SANE_String_Const *list, SANE_String value)
3055 {
3056 while (*list != NULL && strcmp(value, *list) != 0)
3057 list++;
3058
3059 return ((*list == NULL) ? NULL : list);
3060 }
3061
3062 /*
3063 Activate, deactivate an option. Subroutines so we can add
3064 debugging info if we want. The change flag is set to TRUE
3065 if we changed an option. If we did not change an option,
3066 then the value of the changed flag is not modified.
3067 */
3068
3069 static void
activateOption(KodakAio_Scanner * s,SANE_Int option,SANE_Bool * change)3070 activateOption(KodakAio_Scanner *s, SANE_Int option, SANE_Bool *change)
3071 {
3072 if (!SANE_OPTION_IS_ACTIVE(s->opt[option].cap)) {
3073 s->opt[option].cap &= ~SANE_CAP_INACTIVE;
3074 *change = SANE_TRUE;
3075 }
3076 }
3077
3078 static void
deactivateOption(KodakAio_Scanner * s,SANE_Int option,SANE_Bool * change)3079 deactivateOption(KodakAio_Scanner *s, SANE_Int option, SANE_Bool *change)
3080 {
3081 if (SANE_OPTION_IS_ACTIVE(s->opt[option].cap)) {
3082 s->opt[option].cap |= SANE_CAP_INACTIVE;
3083 *change = SANE_TRUE;
3084 }
3085 }
3086
3087 static SANE_Status
getvalue(SANE_Handle handle,SANE_Int option,void * value)3088 getvalue(SANE_Handle handle, SANE_Int option, void *value)
3089 {
3090 KodakAio_Scanner *s = (KodakAio_Scanner *) handle;
3091 SANE_Option_Descriptor *sopt = &(s->opt[option]);
3092 Option_Value *sval = &(s->val[option]);
3093
3094 DBG(17, "%s: option = %d\n", __func__, option);
3095
3096 switch (option) {
3097
3098 case OPT_NUM_OPTS:
3099 case OPT_BIT_DEPTH:
3100 /* case OPT_TRIALOPT: */
3101 case OPT_RESOLUTION:
3102 case OPT_PREVIEW:
3103 case OPT_TL_X:
3104 case OPT_TL_Y:
3105 case OPT_BR_X:
3106 case OPT_BR_Y:
3107 *((SANE_Word *) value) = sval->w;
3108 DBG(20, "%s: got option %d as %d\n", __func__, option, *((SANE_Word *) value));
3109 break;
3110
3111 case OPT_THRESHOLD:
3112 *((SANE_Word *) value) = sval->w;
3113 DBG(20, "%s: got option %d as %f\n", __func__, option, SANE_UNFIX(*((SANE_Word *) value)));
3114 /*DBG(20, "%s: got option %d as %d\n", __func__, option, *((SANE_Word *) value));*/
3115 break;
3116
3117 case OPT_MODE:
3118 case OPT_SOURCE:
3119 case OPT_ADF_MODE:
3120 strcpy((char *) value, sopt->constraint.string_list[sval->w]);
3121 break;
3122 case OPT_PADDING:
3123 *((SANE_Bool *) value) = sval->w;
3124 break;
3125
3126 default:
3127 DBG(20, "%s: returning inval\n", __func__);
3128 return SANE_STATUS_INVAL;
3129 }
3130
3131 DBG(20, "%s: returning good\n", __func__);
3132 return SANE_STATUS_GOOD;
3133 }
3134
3135
3136 /*
3137 * Handles setting the source (flatbed, or auto document feeder (ADF)).
3138 *
3139 */
3140
3141 static void
change_source(KodakAio_Scanner * s,SANE_Int optindex,char * value)3142 change_source(KodakAio_Scanner *s, SANE_Int optindex, char *value)
3143 {
3144 int force_max = SANE_FALSE;
3145 SANE_Bool dummy;
3146
3147 DBG(5, "%s: optindex = %d, source = '%s'\n", __func__, optindex,
3148 value);
3149
3150 if (s->val[OPT_SOURCE].w == optindex)
3151 return;
3152
3153 s->val[OPT_SOURCE].w = optindex;
3154
3155 if (s->val[OPT_TL_X].w == s->hw->x_range->min
3156 && s->val[OPT_TL_Y].w == s->hw->y_range->min
3157 && s->val[OPT_BR_X].w == s->hw->x_range->max
3158 && s->val[OPT_BR_Y].w == s->hw->y_range->max) {
3159 force_max = SANE_TRUE;
3160 }
3161
3162 if (strcmp(ADF_STR, value) == 0) {
3163 s->hw->x_range = &s->hw->cap->adf_x_range;
3164 s->hw->y_range = &s->hw->cap->adf_y_range;
3165 if (s->hw->cap->adf_duplex) {
3166 activateOption(s, OPT_ADF_MODE, &dummy);
3167 } else {
3168 deactivateOption(s, OPT_ADF_MODE, &dummy);
3169 s->val[OPT_ADF_MODE].w = 0;
3170 }
3171 activateOption(s, OPT_PADDING, &dummy);
3172
3173 DBG(5, "adf activated flag = %d\n",s->hw->cap->adf_duplex);
3174
3175 } else {
3176 /* ADF not active */
3177 s->hw->x_range = &s->hw->cap->fbf_x_range;
3178 s->hw->y_range = &s->hw->cap->fbf_y_range;
3179
3180 deactivateOption(s, OPT_ADF_MODE, &dummy);
3181 deactivateOption(s, OPT_PADDING, &dummy);
3182 }
3183
3184 s->opt[OPT_BR_X].constraint.range = s->hw->x_range;
3185 s->opt[OPT_BR_Y].constraint.range = s->hw->y_range;
3186
3187 if (s->val[OPT_TL_X].w < s->hw->x_range->min || force_max)
3188 s->val[OPT_TL_X].w = s->hw->x_range->min;
3189
3190 if (s->val[OPT_TL_Y].w < s->hw->y_range->min || force_max)
3191 s->val[OPT_TL_Y].w = s->hw->y_range->min;
3192
3193 if (s->val[OPT_BR_X].w > s->hw->x_range->max || force_max)
3194 s->val[OPT_BR_X].w = s->hw->x_range->max;
3195
3196 if (s->val[OPT_BR_Y].w > s->hw->y_range->max || force_max)
3197 s->val[OPT_BR_Y].w = s->hw->y_range->max;
3198
3199 }
3200
3201 static SANE_Status
setvalue(SANE_Handle handle,SANE_Int option,void * value,SANE_Int * info)3202 setvalue(SANE_Handle handle, SANE_Int option, void *value, SANE_Int *info)
3203 {
3204 KodakAio_Scanner *s = (KodakAio_Scanner *) handle;
3205 SANE_Option_Descriptor *sopt = &(s->opt[option]);
3206 Option_Value *sval = &(s->val[option]);
3207
3208 SANE_Status status;
3209 const SANE_String_Const *optval = NULL;
3210 int optindex = 0;
3211 SANE_Bool reload = SANE_FALSE;
3212
3213 DBG(17, "%s: option = %d, value = %p, as word: %d\n", __func__, option, value, *(SANE_Word *) value);
3214
3215 status = sanei_constrain_value(sopt, value, info);
3216 if (status != SANE_STATUS_GOOD)
3217 return status;
3218
3219 if (info && value && (*info & SANE_INFO_INEXACT)
3220 && sopt->type == SANE_TYPE_INT)
3221 DBG(17, "%s: constrained val = %d\n", __func__,
3222 *(SANE_Word *) value);
3223
3224 if (sopt->constraint_type == SANE_CONSTRAINT_STRING_LIST) {
3225 optval = search_string_list(sopt->constraint.string_list,
3226 (char *) value);
3227 if (optval == NULL)
3228 return SANE_STATUS_INVAL;
3229 optindex = optval - sopt->constraint.string_list;
3230 }
3231
3232 switch (option) {
3233
3234 case OPT_MODE:
3235 {
3236 sval->w = optindex;
3237 /* if binary, then disable the bit depth selection and enable threshold */
3238 if (optindex == MODE_LINEART) {
3239 DBG(17, "%s: binary mode setting depth to 1\n", __func__);
3240 s->val[OPT_BIT_DEPTH].w = 1;
3241 s->opt[OPT_BIT_DEPTH].cap |= SANE_CAP_INACTIVE;
3242 s->opt[OPT_THRESHOLD].cap &= ~SANE_CAP_INACTIVE;
3243 } else {
3244 if (s->hw->cap->depth_list[0] == 1) { /* only one entry in the list ? */
3245 DBG(17, "%s: non-binary mode but only one depth available\n", __func__);
3246 s->val[OPT_BIT_DEPTH].w = s->hw->cap->depth_list[1];
3247 s->opt[OPT_BIT_DEPTH].cap |= SANE_CAP_INACTIVE;
3248 s->opt[OPT_THRESHOLD].cap |= SANE_CAP_INACTIVE;
3249 } else { /* there is a list to choose from ? */
3250 DBG(17, "%s: non-binary mode and depth list available\n", __func__);
3251 s->opt[OPT_BIT_DEPTH].cap &= ~SANE_CAP_INACTIVE;
3252 s->val[OPT_BIT_DEPTH].w = mode_params[optindex].depth;
3253 s->opt[OPT_THRESHOLD].cap |= SANE_CAP_INACTIVE; /* does not work in xsane ? */
3254 }
3255 }
3256 reload = SANE_TRUE;
3257 break;
3258 }
3259
3260 case OPT_BIT_DEPTH:
3261 sval->w = *((SANE_Word *) value);
3262 mode_params[s->val[OPT_MODE].w].depth = sval->w;
3263 reload = SANE_TRUE;
3264 break;
3265
3266 case OPT_THRESHOLD:
3267 sval->w = *((SANE_Word *) value);
3268 DBG(17, "setting threshold to %f\n", SANE_UNFIX(sval->w));
3269 /*DBG(17, "setting threshold to %d\n", sval->w);*/
3270 /*reload = SANE_TRUE; what does this do?*/
3271 break;
3272
3273 case OPT_RESOLUTION:
3274 sval->w = *((SANE_Word *) value);
3275 DBG(17, "setting resolution to %d\n", sval->w);
3276 reload = SANE_TRUE;
3277 break;
3278
3279 case OPT_BR_X:
3280 case OPT_BR_Y:
3281 if (SANE_UNFIX(*((SANE_Word *) value)) == 0) {
3282 DBG(17, "invalid br-x or br-y\n");
3283 return SANE_STATUS_INVAL;
3284 }
3285 // fall through
3286 case OPT_TL_X:
3287 case OPT_TL_Y:
3288 sval->w = *((SANE_Word *) value);
3289 DBG(17, "setting size to %f\n", SANE_UNFIX(sval->w));
3290 if (NULL != info)
3291 *info |= SANE_INFO_RELOAD_PARAMS;
3292 break;
3293
3294 case OPT_SOURCE:
3295 change_source(s, optindex, (char *) value);
3296 reload = SANE_TRUE;
3297 break;
3298
3299 case OPT_ADF_MODE:
3300 sval->w = optindex; /* Simple lists */
3301 break;
3302
3303 case OPT_PADDING:
3304 sval->w = *((SANE_Word *) value);
3305 break;
3306
3307 /* case OPT_TRIALOPT: */
3308 case OPT_PREVIEW: /* needed? */
3309 sval->w = *((SANE_Word *) value);
3310 break;
3311
3312 default:
3313 return SANE_STATUS_INVAL;
3314 }
3315
3316 if (reload && info != NULL)
3317 *info |= SANE_INFO_RELOAD_OPTIONS | SANE_INFO_RELOAD_PARAMS;
3318
3319 DBG(17, "%s: end\n", __func__);
3320
3321 return SANE_STATUS_GOOD;
3322 }
3323
3324 SANE_Status
sane_control_option(SANE_Handle handle,SANE_Int option,SANE_Action action,void * value,SANE_Int * info)3325 sane_control_option(SANE_Handle handle, SANE_Int option, SANE_Action action,
3326 void *value, SANE_Int *info)
3327 {
3328 KodakAio_Scanner *s = (KodakAio_Scanner *) handle;
3329 if (option < 0 || option >= NUM_OPTIONS)
3330 {
3331 DBG(1, "%s: option num = %d out of range (0..%d)\n", __func__, option, NUM_OPTIONS - 1);
3332 return SANE_STATUS_INVAL;
3333 }
3334
3335 DBG(5, "%s: action = %x, option = %d %s\n", __func__, action, option, s->opt[option].name);
3336
3337 if (info != NULL)
3338 *info = 0;
3339
3340 switch (action) {
3341 case SANE_ACTION_GET_VALUE:
3342 return getvalue(handle, option, value);
3343
3344 case SANE_ACTION_SET_VALUE:
3345 return setvalue(handle, option, value, info);
3346
3347 default:
3348 return SANE_STATUS_INVAL;
3349 }
3350
3351 return SANE_STATUS_INVAL;
3352 }
3353
3354 SANE_Status
sane_get_parameters(SANE_Handle handle,SANE_Parameters * params)3355 sane_get_parameters(SANE_Handle handle, SANE_Parameters *params)
3356 {
3357 KodakAio_Scanner *s = (KodakAio_Scanner *) handle;
3358
3359 DBG(2, "%s: called\n", __func__);
3360
3361 if (params == NULL)
3362 DBG(1, "%s: params is NULL\n", __func__);
3363
3364 /*
3365 * If sane_start was already called, then just retrieve the parameters
3366 * from the scanner data structure
3367 */
3368
3369 if (!s->eof && s->ptr != NULL) {
3370 DBG(5, "scan in progress, returning saved params structure\n");
3371 } else {
3372 /* otherwise initialize the params structure and gather the data */
3373 k_init_parametersta(s);
3374 }
3375
3376 if (params != NULL)
3377 *params = s->params;
3378
3379 print_params(s->params,20);
3380
3381 return SANE_STATUS_GOOD;
3382 }
3383
3384 /*
3385 * This function is part of the SANE API and gets called from the front end to
3386 * start the scan process.
3387 */
3388
3389 SANE_Status
sane_start(SANE_Handle handle)3390 sane_start(SANE_Handle handle)
3391 {
3392 KodakAio_Scanner *s = (KodakAio_Scanner *) handle;
3393 SANE_Status status;
3394
3395 DBG(2, "%s: called\n", __func__);
3396 if(! s->scanning) {
3397 /* calc scanning parameters */
3398 status = k_init_parametersta(s);
3399 if (status != SANE_STATUS_GOOD)
3400 return status;
3401
3402 /* set scanning parameters; also query the current image
3403 * parameters from the sanner and save
3404 * them to s->params
3405 Only set scanning params the first time, or after a cancel
3406 try change 22/2/12 take lock scanner out of k_set_scanning_parameters */
3407
3408 /* moved open_scanner here 27/12/14 from sane_open */
3409 status = open_scanner(s);
3410 if (status != SANE_STATUS_GOOD) {
3411 free(s);
3412 return status;
3413 }
3414 /* end of open scanner section */
3415
3416
3417 status = k_lock_scanner(s);
3418 if (status != SANE_STATUS_GOOD) {
3419 DBG(1, "could not lock scanner\n");
3420 return status;
3421 }
3422
3423 }
3424
3425 status = k_set_scanning_parameters(s);
3426 if (status != SANE_STATUS_GOOD)
3427 return status;
3428
3429 print_params(s->params, 5);
3430 /* if we scan from ADF, check if it is loaded */
3431 if (strcmp(source_list[s->val[OPT_SOURCE].w], ADF_STR) == 0) {
3432 status = k_check_adf(s);
3433 if (status != SANE_STATUS_GOOD) {
3434 /* returning SANE_STATUS_NO_DOCS seems not to cause simple-scan to end the adf scan, so we cancel */
3435 status = SANE_STATUS_CANCELLED;
3436 DBG(10, "%s: returning %s\n", __func__, sane_strstatus(status));
3437 return status;
3438 }
3439 }
3440
3441 /* prepare buffer here so that a memory allocation failure
3442 * will leave the scanner in a sane state.
3443 */
3444 s->buf = realloc(s->buf, s->block_len);
3445 if (s->buf == NULL)
3446 return SANE_STATUS_NO_MEM;
3447
3448 s->eof = SANE_FALSE; /* page not finished */
3449 s->ack = SANE_FALSE; /* page from scanner not finished */
3450 s->ptr = s->end = s->buf;
3451 s->canceling = SANE_FALSE;
3452
3453 if (strlen(RawScanPath) > 0 && s->params.lines > 0)
3454 RawScan = fopen(RawScanPath, "wb");/* open the debug file if it has a name */
3455 if(RawScan) fprintf(RawScan, "P5\n%d %d\n%d\n",s->scan_bytes_per_line, s->params.lines, 255);
3456
3457 /* start scanning */
3458 DBG(2, "%s: scanning...\n", __func__);
3459 status = k_start_scan(s);
3460
3461 if (status != SANE_STATUS_GOOD) {
3462 DBG(1, "%s: start failed: %s\n", __func__,
3463 sane_strstatus(status));
3464
3465 return status;
3466 }
3467
3468 return status;
3469 }
3470
3471 /* this moves data from our buffers to SANE */
3472
3473 SANE_Status
sane_read(SANE_Handle handle,SANE_Byte * data,SANE_Int max_length,SANE_Int * length)3474 sane_read(SANE_Handle handle, SANE_Byte *data, SANE_Int max_length,
3475 SANE_Int *length)
3476 {
3477 SANE_Status status;
3478 KodakAio_Scanner *s = (KodakAio_Scanner *) handle;
3479
3480 if (s->buf == NULL || s->canceling)
3481 return SANE_STATUS_CANCELLED;
3482
3483 *length = 0;
3484 DBG(18, "sane-read, bytes unread %d\n",s->bytes_unread);
3485
3486 status = k_read(s);
3487
3488 if (status == SANE_STATUS_CANCELLED) {
3489 k_scan_finish(s);
3490 return status;
3491 }
3492
3493 k_copy_image_data(s, data, max_length, length);
3494
3495 DBG(18, "%d lines read, status: %s\n",
3496 *length / s->params.bytes_per_line, sane_strstatus(status));
3497
3498 /* continue reading if appropriate */
3499 if (status == SANE_STATUS_GOOD)
3500 return status;
3501
3502 /* not sure if we want to finish (unlock scanner) here or in sane_close */
3503 /* k_scan_finish(s); */
3504 return status;
3505 }
3506
3507
3508 void
sane_cancel(SANE_Handle handle)3509 sane_cancel(SANE_Handle handle)
3510 {
3511 SANE_Status status;
3512 KodakAio_Scanner *s = (KodakAio_Scanner *) handle;
3513 DBG(2, "%s: called\n", __func__);
3514
3515 status = cmd_cancel_scan(s);
3516 if (status != SANE_STATUS_GOOD)
3517 DBG(1, "%s: cmd_cancel_scan failed: %s\n", __func__,
3518 sane_strstatus(status));
3519
3520 /* moved from close scanner section 27/12/14 */
3521 if (s->fd != -1)
3522 close_scanner(s);
3523 /* end of section */
3524
3525 }
3526
3527 /*
3528 * SANE_Status sane_set_io_mode()
3529 *
3530 * not supported - for asynchronous I/O
3531 */
3532
3533 SANE_Status
sane_set_io_mode(SANE_Handle __sane_unused__ handle,SANE_Bool __sane_unused__ non_blocking)3534 sane_set_io_mode(SANE_Handle __sane_unused__ handle,
3535 SANE_Bool __sane_unused__ non_blocking)
3536 {
3537 return SANE_STATUS_UNSUPPORTED;
3538 }
3539
3540 /*
3541 * SANE_Status sane_get_select_fd()
3542 *
3543 * not supported - for asynchronous I/O
3544 */
3545
3546 SANE_Status
sane_get_select_fd(SANE_Handle __sane_unused__ handle,SANE_Int __sane_unused__ * fd)3547 sane_get_select_fd(SANE_Handle __sane_unused__ handle,
3548 SANE_Int __sane_unused__ *fd)
3549 {
3550 return SANE_STATUS_UNSUPPORTED;
3551 }
3552