1 /* lexmark_x2600.c: SANE backend for Lexmark x2600 scanners.
2
3 (C) 2023 "Benoit Juin" <benoit.juin@gmail.com>
4
5 This file is part of the SANE package.
6
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <https://www.gnu.org/licenses/>.
19
20 As a special exception, the authors of SANE give permission for
21 additional uses of the libraries contained in this release of SANE.
22
23 The exception is that, if you link a SANE library with other files
24 to produce an executable, this does not by itself cause the
25 resulting executable to be covered by the GNU General Public
26 License. Your use of that executable is in no way restricted on
27 account of linking the SANE library code into it.
28
29 This exception does not, however, invalidate any other reasons why
30 the executable file might be covered by the GNU General Public
31 License.
32
33 If you submit changes to SANE to the maintainers to be included in
34 a subsequent release, you agree by submitting the changes that
35 those changes may be distributed with this exception intact.
36
37 If you write modifications of your own for SANE, it is your choice
38 whether to permit this exception to apply to your modifications.
39 If you do not wish that, delete this exception notice.
40
41 **************************************************************************/
42
43
44 #include "lexmark_x2600.h"
45
46 #define BUILD 1
47 #define LEXMARK_X2600_CONFIG_FILE "lexmark_x2600.conf"
48 #define MAX_OPTION_STRING_SIZE 255
49 static SANE_Int transfer_buffer_size = 32768;
50 static Lexmark_Device *first_device = 0;
51 static SANE_Int num_devices = 0;
52 static const SANE_Device **devlist = 0;
53
54 static SANE_Bool initialized = SANE_FALSE;
55
56 // first value is the size of the wordlist!
57 static SANE_Int dpi_list[] = {
58 4, 100, 200, 300, 600
59 };
60 static SANE_Int dpi_list_size = sizeof(dpi_list) / sizeof(dpi_list[0]);
61
62 static SANE_String_Const mode_list[] = {
63 SANE_VALUE_SCAN_MODE_COLOR,
64 SANE_VALUE_SCAN_MODE_GRAY,
65 NULL
66 };
67
68 static SANE_Range x_range = {
69 0, /* minimum */
70 5078, /* maximum */
71 1 /* quantization */
72 };
73
74 static SANE_Range y_range = {
75 0, /* minimum */
76 7015, /* maximum */
77 1 /* quantization */
78 };
79
80 static SANE_Byte command1_block[] = {
81 0xA5, 0x00, 0x19, 0x10, 0x01, 0x83, 0xAA, 0xBB,
82 0xCC, 0xDD, 0x02, 0x00, 0x1B, 0x53, 0x03, 0x00,
83 0x00, 0x00, 0x80, 0x00, 0xAA, 0xBB, 0xCC, 0xDD,
84 0xAA, 0xBB, 0xCC, 0xDD};
85 static SANE_Int command1_block_size = sizeof(command1_block);
86
87 static SANE_Byte command2_block[] = {
88 0xA5, 0x00, 0x19, 0x10, 0x01, 0x83, 0xAA, 0xBB,
89 0xCC, 0xDD, 0x02, 0x00, 0x1B, 0x53, 0x04, 0x00,
90 0x00, 0x00, 0x80, 0x00, 0xAA, 0xBB, 0xCC, 0xDD,
91 0xAA, 0xBB, 0xCC, 0xDD};
92 static SANE_Int command2_block_size = sizeof(command2_block);
93
94 static SANE_Byte command_with_params_block[] = {
95 0xA5, 0x00, 0x31, 0x10, 0x01, 0x83, 0xAA, 0xBB,
96 0xCC, 0xDD, 0x02, 0x00, 0x1B, 0x53, 0x05, 0x00,
97 0x18, 0x00, 0x80, 0x00, 0xFF, 0x00, 0x00, 0x02,
98 0xFF, 0xFF, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00,
99 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
100 0xFF, 0xFF, 0xFF, 0xFF, 0xAA, 0xBB, 0xCC, 0xDD,
101 0xAA, 0xBB, 0xCC, 0xDD};
102 static SANE_Int command_with_params_block_size = sizeof(command_with_params_block);
103
104 static SANE_Byte command_cancel1_block[] = {
105 0xa5, 0x00, 0x19, 0x10, 0x01, 0x83, 0xaa, 0xbb,
106 0xcc, 0xdd, 0x02, 0x00, 0x1b, 0x53, 0x0f, 0x00,
107 0x00, 0x00, 0x00, 0x00, 0xaa, 0xbb, 0xcc, 0xdd,
108 0xaa, 0xbb, 0xcc, 0xdd};
109 static SANE_Byte command_cancel2_block[] = {
110 0xa5, 0x00, 0x19, 0x10, 0x01, 0x83, 0xaa, 0xbb,
111 0xcc, 0xdd, 0x02, 0x00, 0x1b, 0x53, 0x06, 0x00,
112 0x00, 0x00, 0x80, 0x00, 0xaa, 0xbb, 0xcc, 0xdd,
113 0xaa, 0xbb, 0xcc, 0xdd};
114 static SANE_Int command_cancel_size = sizeof(command_cancel1_block);
115
116 static SANE_Byte empty_line_data_packet[] = {
117 0x1b, 0x53, 0x02, 0x00, 0x01, 0x00, 0x00, 0x00,
118 0x00};
119 static SANE_Int empty_line_data_packet_size = sizeof(empty_line_data_packet);
120
121 static SANE_Byte last_data_packet[] = {
122 0x1b, 0x53, 0x02, 0x00, 0x01, 0x00, 0x00, 0x00,
123 0x01};
124 static SANE_Int last_data_packet_size = sizeof(last_data_packet);
125
126 static SANE_Byte cancel_packet[] = {
127 0x1b, 0x53, 0x02, 0x00, 0x01, 0x00, 0x00, 0x00,
128 0x03};
129 static SANE_Int cancel_packet_size = sizeof(cancel_packet);
130
131 static SANE_Byte linebegin_data_packet[] = {
132 0x1b, 0x53, 0x02, 0x00};
133 static SANE_Int linebegin_data_packet_size = sizeof(linebegin_data_packet);
134
135 static SANE_Byte unknown_a_data_packet[] = {
136 0x1b, 0x53, 0x01, 0x00, 0x01, 0x00, 0x80, 0x00};
137 static SANE_Int unknown_a_data_packet_size = sizeof(unknown_a_data_packet);
138
139 static SANE_Byte unknown_b_data_packet[] = {
140 0x1b, 0x53, 0x04, 0x00, 0x00, 0x00, 0x80, 0x00};
141 static SANE_Int unknown_b_data_packet_size = sizeof(unknown_b_data_packet);
142
143 static SANE_Byte unknown_c_data_packet[] = {
144 0x1b, 0x53, 0x04, 0x00, 0x00, 0x00, 0x84, 0x00};
145 static SANE_Int unknown_c_data_packet_size = sizeof(unknown_c_data_packet);
146
147 static SANE_Byte unknown_d_data_packet[] = {
148 0x1b, 0x53, 0x05, 0x00, 0x00, 0x00};
149 static SANE_Int unknown_d_data_packet_size = sizeof(unknown_d_data_packet);
150
151 static SANE_Byte unknown_e_data_packet[] = {
152 0xa5, 0x00, 0x06, 0x10, 0x01, 0xaa, 0xbb, 0xcc,
153 0xdd};
154 static SANE_Int unknown_e_data_packet_size = sizeof(unknown_e_data_packet);
155
156 /* static SANE_Byte not_ready_data_packet[] = { */
157 /* 0x1b, 0x53, 0x01, 0x00, 0x01, 0x00, 0x84, 0x00}; */
158 /* static SANE_Int not_ready_data_packet_size = sizeof(not_ready_data_packet); */
159
160
161 static SANE_Int line_header_length = 9;
162
163
164 //static SANE_Byte empty_data_packet[8] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
165
166 SANE_Status
clean_and_copy_data(const SANE_Byte * source,SANE_Int source_size,SANE_Byte * destination,SANE_Int * destination_length,SANE_Int mode,SANE_Int max_length,SANE_Handle dev)167 clean_and_copy_data(const SANE_Byte * source, SANE_Int source_size,
168 SANE_Byte * destination, SANE_Int * destination_length,
169 SANE_Int mode, SANE_Int max_length, SANE_Handle dev)
170 {
171 DBG (10, "clean_and_copy_data\n");
172 // if source doesnt start with 1b 53 02, then it is a continuation packet
173 // SANE_Int k = 0;
174 // SANE_Int bytes_written = 0;
175 // BW 1b 53 02 00 21 00 00 00 00 | 32 | 21 -> 33 (segmentlng= 32)
176 // BW 1b 53 02 00 41 00 00 00 00 | 64 | 41 -> 65 (segmentlng= 64)
177 // COLOR 1b 53 02 00 c1 00 00 00 00 | 64 | c1 -> 193 (segmentlng= 192)
178 // COLOR 1b 53 02 00 01 06 00 00 00 | 512 | 601 -> 1537 (segmentlng= 1536)
179 // COLOR 1b 53 02 00 99 3a 00 00 00 | 5000 | 3a99 -> 15001 (segmentlng=15000)
180 // COLOR 1b 53 02 00 f7 0f 00 | 1362 | 0ff7 -> 4087 <- limit where sane_read can a read a line at e time, more that 1362 and then the rest
181 // of the line will be available in the next sane_read call
182 // COLOR 1b 53 02 00 fa 0f 00 | | 0ffa -> 4090 <- in that case the line doesnt fit, clean_and_copy_data will be called again with the rest of the data
183
184
185 // edge case segment doesn(t feet in the packet size
186 /* if(segment_length > source_size - 9) */
187 /* segment_length = source_size - 9; */
188
189 // the scanner sends series of 8 lines function param source
190 // every lines has prefix see linebegin_data_packet
191 // the source parameter as a limited length :function param source_size
192 // so the serie og 8 lines can be splited
193 // in such case, in the next call of this function, source contain the end of the
194 // broken segment.
195 // Here is the way data is read:
196 // 1 - check that source begin with a linebegin_data_packet signature
197 // if this is the case the source[4] & source[5] contains how much data
198 // can be read before onother header is reach (linebegin_data_packet)
199
200 Lexmark_Device * ldev = (Lexmark_Device * ) dev;
201 SANE_Int i = 0;
202 SANE_Int bytes_read = 0;
203 SANE_Byte tmp = 0;
204 SANE_Int source_read_cursor = 0;
205 SANE_Int block_pixel_data_length = 0;
206 SANE_Int size_to_realloc = 0;
207
208
209 if(!ldev->eof){
210
211 // does source start with linebegin_data_packet?
212 if (memcmp(linebegin_data_packet, source, linebegin_data_packet_size) == 0){
213 // extract the number of bytes we can read befor new header is reached
214 // store it in the device in case of continuation packet
215 ldev->read_buffer->linesize = (source[4] + ((source[5] << 8) & 0xFF00)) - 1;
216 ldev->read_buffer->last_line_bytes_read = ldev->read_buffer->linesize;
217 DBG (10, " this is the begining of a line linesize=%ld\n",
218 ldev->read_buffer->linesize);
219 } else {
220 DBG (10, " this is not a new line packet, continue to fill the read buffer\n");
221 //return;
222 }
223
224 if(ldev->read_buffer->linesize == 0){
225 DBG (10, " linesize=0 something went wrong, lets ignore that USB packet\n");
226 return SANE_STATUS_CANCELLED;
227 }
228
229
230 // loop over source buffer
231 while(i < source_size){
232 // last line was full
233 if(ldev->read_buffer->last_line_bytes_read == ldev->read_buffer->linesize){
234 // if next block fit in the source
235 if(i + line_header_length + (SANE_Int) ldev->read_buffer->linesize <= source_size){
236 ldev->read_buffer->image_line_no += 1;
237 source_read_cursor = i + line_header_length;
238 block_pixel_data_length = ldev->read_buffer->linesize;
239 ldev->read_buffer->last_line_bytes_read = block_pixel_data_length;
240 size_to_realloc = ldev->read_buffer->image_line_no *
241 ldev->read_buffer->linesize * sizeof(SANE_Byte);
242 bytes_read = block_pixel_data_length + line_header_length;
243 }
244 // next block cannot be read fully because source_size is too small
245 // (USB packet fragmentation)
246 else{
247 ldev->read_buffer->image_line_no += 1;
248 source_read_cursor = i + line_header_length;
249 block_pixel_data_length = source_size - i - line_header_length;
250 ldev->read_buffer->last_line_bytes_read = block_pixel_data_length;
251 size_to_realloc = ((ldev->read_buffer->image_line_no-1) *
252 ldev->read_buffer->linesize + block_pixel_data_length) * sizeof(SANE_Byte);
253 bytes_read = block_pixel_data_length + line_header_length;
254 }
255 }
256 // last line was not full lets extract what is left
257 // this is du to USB packet fragmentation
258 else{
259 // the last line was not full so no increment
260 ldev->read_buffer->image_line_no += 0;
261 source_read_cursor = i;
262 block_pixel_data_length = ldev->read_buffer->linesize -
263 ldev->read_buffer->last_line_bytes_read;
264 // we completed the last line with missing bytes so new the line is full
265 ldev->read_buffer->last_line_bytes_read = ldev->read_buffer->linesize;
266 size_to_realloc = ldev->read_buffer->image_line_no *
267 ldev->read_buffer->linesize * sizeof(SANE_Byte);
268 bytes_read = block_pixel_data_length;
269 }
270
271 DBG (20, " size_to_realloc=%d i=%d image_line_no=%d\n",
272 size_to_realloc, i, ldev->read_buffer->image_line_no);
273 // do realoc memory space for our buffer
274 SANE_Byte* alloc_result = realloc(ldev->read_buffer->data, size_to_realloc);
275 if(alloc_result == NULL){
276 // TODO allocation was not possible
277 DBG (20, " REALLOC failed\n");
278 return SANE_STATUS_NO_MEM;
279 }
280 // point data to our new memary space
281 ldev->read_buffer->data = alloc_result;
282 // reposition writeptr and readptr to the correct memory adress
283 // to do that use write_byte_counter and read_byte_counter
284 ldev->read_buffer->writeptr =
285 ldev->read_buffer->data + ldev->read_buffer->write_byte_counter;
286 // copy new data
287 memcpy(
288 ldev->read_buffer->writeptr,
289 source + source_read_cursor,
290 block_pixel_data_length
291 );
292
293 // store how long is the buffer
294 ldev->read_buffer->write_byte_counter += block_pixel_data_length;
295
296 i += bytes_read;
297 }
298 }
299
300 // reposition our readptr
301 ldev->read_buffer->readptr =
302 ldev->read_buffer->data + ldev->read_buffer->read_byte_counter;
303
304
305 // read our buffer to fill the destination buffer
306 // mulitple call so read may has been already started
307 // length already read is stored in ldev->read_buffer->read_byte_counter
308
309 SANE_Int available_bytes_to_read =
310 ldev->read_buffer->write_byte_counter - ldev->read_buffer->read_byte_counter;
311
312 DBG (20, " source read done now sending to destination \n");
313
314 // we will copy image data 3 bytes by 3 bytes if color mod to allow color swap
315 // this avoid error on color channels swapping
316 if (mode == SANE_FRAME_RGB){
317
318 // get max chunk
319 SANE_Int data_chunk_size = max_length;
320 if(data_chunk_size > available_bytes_to_read){
321 data_chunk_size = available_bytes_to_read;
322 }
323 data_chunk_size = data_chunk_size / 3;
324 data_chunk_size = data_chunk_size * 3;
325
326 // we have to invert color channels
327 SANE_Byte * color_swarp_ptr = ldev->read_buffer->readptr;
328 for(SANE_Int j=0; j < data_chunk_size;j += 3){
329 // DBG (20, " swapping RGB <- BGR j=%d\n", j);
330 tmp = *(color_swarp_ptr + j);
331 *(color_swarp_ptr + j) = *(color_swarp_ptr + j + 2);
332 *(color_swarp_ptr + j + 2) = tmp;
333 }
334
335 memcpy (destination,
336 ldev->read_buffer->readptr,
337 data_chunk_size);
338
339 ldev->read_buffer->read_byte_counter += data_chunk_size;
340 *destination_length = data_chunk_size;
341
342 }
343 // gray mode copy until max_length
344 else{
345
346 SANE_Int data_chunk_size = max_length;
347 if(data_chunk_size > available_bytes_to_read){
348 data_chunk_size = available_bytes_to_read;
349 }
350 memcpy (
351 destination,
352 ldev->read_buffer->readptr,
353 data_chunk_size
354 );
355 ldev->read_buffer->read_byte_counter += data_chunk_size;;
356 *destination_length = data_chunk_size;
357
358 }
359
360 DBG (20, " done destination_length=%d available_bytes_to_read=%d\n",
361 *destination_length, available_bytes_to_read);
362
363 if(available_bytes_to_read > 0){
364 return SANE_STATUS_GOOD;
365 }else{
366 ldev->eof = 0;
367 return SANE_STATUS_EOF;
368 }
369
370 }
371
372 SANE_Status
usb_write_then_read(Lexmark_Device * dev,SANE_Byte * cmd,size_t cmd_size)373 usb_write_then_read (Lexmark_Device * dev, SANE_Byte * cmd, size_t cmd_size)
374 {
375 size_t buf_size = 256;
376 SANE_Byte buf[buf_size];
377 SANE_Status status;
378
379 DBG (10, "usb_write_then_read: %d\n", dev->devnum);
380 sanei_usb_set_endpoint(dev->devnum, USB_DIR_OUT|USB_ENDPOINT_TYPE_BULK, 0x02);
381 DBG (10, " endpoint set: %d\n", dev->devnum);
382
383 /* status = sanei_usb_read_bulk (dev->devnum, buf, &buf_size); */
384 /* DBG (10, " readdone: %d\n", dev->devnum); */
385 /* if (status != SANE_STATUS_GOOD && status != SANE_STATUS_EOF) */
386 /* { */
387 /* DBG (1, "USB READ IO Error in usb_write_then_read, fail devnum=%d\n", */
388 /* dev->devnum); */
389 /* return status; */
390 /* } */
391
392 DBG (10, " attempting to write...: %d\n", dev->devnum);
393 status = sanei_usb_write_bulk (dev->devnum, cmd, &cmd_size);
394 DBG (10, " writedone: %d\n", dev->devnum);
395 if (status != SANE_STATUS_GOOD)
396 {
397 DBG (1, "USB WRITE IO Error in usb_write_then_read, launch fail: %d\n",
398 status);
399 return status;
400 }
401
402 debug_packet(cmd, cmd_size, WRITE);
403
404 DBG (10, " attempting to read...: %d\n", dev->devnum);
405 status = sanei_usb_read_bulk (dev->devnum, buf, &buf_size);
406 DBG (10, " readdone: %d\n", dev->devnum);
407 if (status != SANE_STATUS_GOOD && status != SANE_STATUS_EOF)
408 {
409 DBG (1, "USB READ IO Error in usb_write_then_read, fail devnum=%d\n",
410 dev->devnum);
411 return status;
412 }
413 debug_packet(buf, buf_size, READ);
414 return SANE_STATUS_GOOD;
415 }
416
417 void
build_packet(Lexmark_Device * dev,SANE_Byte packet_id,SANE_Byte * buffer)418 build_packet(Lexmark_Device * dev, SANE_Byte packet_id, SANE_Byte * buffer){
419 memcpy(buffer, command_with_params_block, command_with_params_block_size);
420 // protocole related... "ID?"
421 buffer[14] = packet_id;
422
423 // mode
424 if (memcmp(dev->val[OPT_MODE].s, "Color", 5) == 0 )
425 buffer[20] = 0x03;
426 else
427 buffer[20] = 0x02;
428
429 // pixel width (swap lower byte -> higher byte)
430 buffer[24] = dev->val[OPT_BR_X].w & 0xFF;
431 buffer[25] = (dev->val[OPT_BR_X].w >> 8) & 0xFF;
432
433 // pixel height (swap lower byte -> higher byte)
434 buffer[28] = dev->val[OPT_BR_Y].w & 0xFF;
435 buffer[29] = (dev->val[OPT_BR_Y].w >> 8) & 0xFF;
436
437 // dpi x (swap lower byte -> higher byte)
438 buffer[40] = dev->val[OPT_RESOLUTION].w & 0xFF;
439 buffer[41] = (dev->val[OPT_RESOLUTION].w >> 8) & 0xFF;
440
441 // dpi y (swap lower byte -> higher byte)
442 buffer[42] = dev->val[OPT_RESOLUTION].w & 0xFF;
443 buffer[43] = (dev->val[OPT_RESOLUTION].w >> 8) & 0xFF;
444 }
445
446 SANE_Status
init_options(Lexmark_Device * dev)447 init_options (Lexmark_Device * dev)
448 {
449
450 SANE_Option_Descriptor *od;
451
452 DBG (2, "init_options: dev = %p\n", (void *) dev);
453
454 /* number of options */
455 od = &(dev->opt[OPT_NUM_OPTS]);
456 od->name = SANE_NAME_NUM_OPTIONS;
457 od->title = SANE_TITLE_NUM_OPTIONS;
458 od->desc = SANE_DESC_NUM_OPTIONS;
459 od->type = SANE_TYPE_INT;
460 od->unit = SANE_UNIT_NONE;
461 od->size = sizeof (SANE_Word);
462 od->cap = SANE_CAP_SOFT_DETECT;
463 od->constraint_type = SANE_CONSTRAINT_NONE;
464 od->constraint.range = 0;
465 dev->val[OPT_NUM_OPTS].w = NUM_OPTIONS;
466
467 /* mode - sets the scan mode: Color / Gray */
468 od = &(dev->opt[OPT_MODE]);
469 od->name = SANE_NAME_SCAN_MODE;
470 od->title = SANE_TITLE_SCAN_MODE;
471 od->desc = SANE_DESC_SCAN_MODE;;
472 od->type = SANE_TYPE_STRING;
473 od->unit = SANE_UNIT_NONE;
474 od->size = MAX_OPTION_STRING_SIZE;
475 od->cap = SANE_CAP_SOFT_DETECT | SANE_CAP_SOFT_SELECT;
476 od->constraint_type = SANE_CONSTRAINT_STRING_LIST;
477 od->constraint.string_list = mode_list;
478 dev->val[OPT_MODE].s = malloc (od->size);
479 if (!dev->val[OPT_MODE].s)
480 return SANE_STATUS_NO_MEM;
481 strcpy (dev->val[OPT_MODE].s, SANE_VALUE_SCAN_MODE_COLOR);
482
483 /* resolution */
484 od = &(dev->opt[OPT_RESOLUTION]);
485 od->name = SANE_NAME_SCAN_RESOLUTION;
486 od->title = SANE_TITLE_SCAN_RESOLUTION;
487 od->desc = SANE_DESC_SCAN_RESOLUTION;
488 od->type = SANE_TYPE_INT;
489 od->unit = SANE_UNIT_DPI;
490 od->size = sizeof (SANE_Int);
491 od->cap = SANE_CAP_SOFT_DETECT | SANE_CAP_SOFT_SELECT;
492 od->constraint_type = SANE_CONSTRAINT_WORD_LIST;
493 od->constraint.word_list = dpi_list;
494 dev->val[OPT_RESOLUTION].w = 200;
495
496 /* preview mode */
497 od = &(dev->opt[OPT_PREVIEW]);
498 od->name = SANE_NAME_PREVIEW;
499 od->title = SANE_TITLE_PREVIEW;
500 od->desc = SANE_DESC_PREVIEW;
501 od->size = sizeof (SANE_Word);
502 od->cap = SANE_CAP_INACTIVE;
503 od->type = SANE_TYPE_BOOL;
504 od->constraint_type = SANE_CONSTRAINT_NONE;
505 dev->val[OPT_PREVIEW].w = SANE_FALSE;
506
507 /* "Geometry" group: */
508 od = &(dev->opt[OPT_GEOMETRY_GROUP]);
509 od->name = "";
510 od->title = SANE_I18N ("Geometry");
511 od->desc = "";
512 od->type = SANE_TYPE_GROUP;
513 od->cap = SANE_CAP_SOFT_DETECT | SANE_CAP_SOFT_SELECT;
514 od->size = 0;
515 od->constraint_type = SANE_CONSTRAINT_NONE;
516 //
517
518 /* top-left x */
519 od = &(dev->opt[OPT_TL_X]);
520 od->name = SANE_NAME_SCAN_TL_X;
521 od->title = SANE_TITLE_SCAN_TL_X;
522 od->desc = SANE_DESC_SCAN_TL_X;
523 od->type = SANE_TYPE_INT;
524 od->cap = SANE_CAP_INACTIVE;
525 od->size = sizeof (SANE_Word);
526 od->unit = SANE_UNIT_PIXEL;
527 od->constraint_type = SANE_CONSTRAINT_RANGE;
528 od->constraint.range = &x_range;
529 dev->val[OPT_TL_X].w = 0;
530
531 /* top-left y */
532 od = &(dev->opt[OPT_TL_Y]);
533 od->name = SANE_NAME_SCAN_TL_Y;
534 od->title = SANE_TITLE_SCAN_TL_Y;
535 od->desc = SANE_DESC_SCAN_TL_Y;
536 od->type = SANE_TYPE_INT;
537 od->cap = SANE_CAP_INACTIVE;
538 od->size = sizeof (SANE_Word);
539 od->unit = SANE_UNIT_PIXEL;
540 od->constraint_type = SANE_CONSTRAINT_RANGE;
541 od->constraint.range = &y_range;
542 dev->val[OPT_TL_Y].w = 0;
543
544 /* bottom-right x */
545 od = &(dev->opt[OPT_BR_X]);
546 od->name = SANE_NAME_SCAN_BR_X;
547 od->title = SANE_TITLE_SCAN_BR_X;
548 od->desc = SANE_DESC_SCAN_BR_X;
549 od->type = SANE_TYPE_INT;
550 od->size = sizeof (SANE_Word);
551 od->cap = SANE_CAP_SOFT_DETECT | SANE_CAP_SOFT_SELECT;
552 od->unit = SANE_UNIT_PIXEL;
553 od->constraint_type = SANE_CONSTRAINT_RANGE;
554 od->constraint.range = &x_range;
555 dev->val[OPT_BR_X].w = 1654;
556
557 /* bottom-right y */
558 od = &(dev->opt[OPT_BR_Y]);
559 od->name = SANE_NAME_SCAN_BR_Y;
560 od->title = SANE_TITLE_SCAN_BR_Y;
561 od->desc = SANE_DESC_SCAN_BR_Y;
562 od->type = SANE_TYPE_INT;
563 od->size = sizeof (SANE_Word);
564 od->cap = SANE_CAP_SOFT_DETECT | SANE_CAP_SOFT_SELECT;
565 od->unit = SANE_UNIT_PIXEL;
566 od->constraint_type = SANE_CONSTRAINT_RANGE;
567 od->constraint.range = &y_range;
568 dev->val[OPT_BR_Y].w = 2339;
569
570 return SANE_STATUS_GOOD;
571 }
572
573 /* callback function for sanei_usb_attach_matching_devices
574 */
575 static SANE_Status
attach_one(SANE_String_Const devname)576 attach_one (SANE_String_Const devname)
577 {
578 Lexmark_Device *lexmark_device;
579
580 DBG (2, "attach_one: attachLexmark: devname=%s first_device=%p\n",
581 devname, (void *)first_device);
582
583 for (lexmark_device = first_device; lexmark_device;
584 lexmark_device = lexmark_device->next){
585 /* already attached devices */
586
587 if (strcmp (lexmark_device->sane.name, devname) == 0){
588 lexmark_device->missing = SANE_FALSE;
589 return SANE_STATUS_GOOD;
590 }
591 }
592
593 lexmark_device = (Lexmark_Device *) malloc (sizeof (Lexmark_Device));
594 if (lexmark_device == NULL)
595 return SANE_STATUS_NO_MEM;
596
597 lexmark_device->sane.name = strdup (devname);
598 if (lexmark_device->sane.name == NULL)
599 return SANE_STATUS_NO_MEM;
600 lexmark_device->sane.vendor = "Lexmark";
601 lexmark_device->sane.model = "X2600 series";
602 lexmark_device->sane.type = "flat bed";
603
604 /* init transfer_buffer */
605 lexmark_device->transfer_buffer = malloc (transfer_buffer_size);
606 if (lexmark_device->transfer_buffer == NULL)
607 return SANE_STATUS_NO_MEM;
608
609 /* Make the pointer to the read buffer null here */
610 lexmark_device->read_buffer = malloc (sizeof (Read_Buffer));
611 if (lexmark_device->read_buffer == NULL)
612 return SANE_STATUS_NO_MEM;
613
614 /* mark device as present */
615 lexmark_device->missing = SANE_FALSE;
616 lexmark_device->device_cancelled = SANE_FALSE;
617 /* insert it a the start of the chained list */
618 lexmark_device->next = first_device;
619 first_device = lexmark_device;
620 num_devices++;
621 DBG (2, " first_device=%p\n", (void *)first_device);
622
623 return SANE_STATUS_GOOD;
624 }
625
626 SANE_Status
scan_devices()627 scan_devices(){
628 DBG (2, "scan_devices\n");
629 SANE_Char config_line[PATH_MAX];
630 FILE *fp;
631 const char *lp;
632 num_devices = 0;
633
634 // -- free existing device we are doning a full re-scan
635 while (first_device){
636 Lexmark_Device *this_device = first_device;
637 first_device = first_device->next;
638 DBG (2, " free first_device\n");
639 free(this_device);
640 }
641
642 fp = sanei_config_open (LEXMARK_X2600_CONFIG_FILE);
643 if (!fp)
644 {
645 DBG (2, " No config no prob...(%s)\n", LEXMARK_X2600_CONFIG_FILE);
646 return SANE_STATUS_GOOD;
647 }
648 while (sanei_config_read (config_line, sizeof (config_line), fp))
649 {
650 if (config_line[0] == '#')
651 continue; /* ignore line comments */
652
653 lp = sanei_config_skip_whitespace (config_line);
654 /* skip empty lines */
655 if (*lp == 0)
656 continue;
657
658 DBG (4, " attach_matching_devices(%s)\n", config_line);
659 sanei_usb_init();
660 sanei_usb_attach_matching_devices (config_line, attach_one);
661 }
662
663 fclose (fp);
664 return SANE_STATUS_GOOD;
665 }
666
667 SANE_Status
sane_init(SANE_Int * version_code,SANE_Auth_Callback authorize)668 sane_init (SANE_Int *version_code, SANE_Auth_Callback authorize)
669 {
670 DBG_INIT ();
671 DBG (2, "sane_init: version_code %s 0, authorize %s 0\n",
672 version_code == 0 ? "=" : "!=", authorize == 0 ? "=" : "!=");
673 DBG (1, " SANE lexmark_x2600 backend version %d.%d.%d from %s\n",
674 SANE_CURRENT_MAJOR, SANE_CURRENT_MINOR, BUILD, PACKAGE_STRING);
675
676 if (version_code)
677 *version_code = SANE_VERSION_CODE (SANE_CURRENT_MAJOR, SANE_CURRENT_MINOR, BUILD);
678
679
680 SANE_Status status = scan_devices();
681 initialized = SANE_TRUE;
682 return status;
683 }
684
685 SANE_Status
sane_get_devices(const SANE_Device *** device_list,SANE_Bool local_only)686 sane_get_devices (const SANE_Device ***device_list, SANE_Bool local_only)
687 {
688 SANE_Int index;
689 Lexmark_Device *lexmark_device;
690
691 DBG (2, "sane_get_devices: device_list=%p, local_only=%d num_devices=%d\n",
692 (void *) device_list, local_only, num_devices);
693
694 //sanei_usb_scan_devices ();
695 SANE_Status status = scan_devices();
696
697 if (devlist)
698 free (devlist);
699
700 devlist = malloc ((num_devices + 1) * sizeof (devlist[0]));
701 if (!devlist)
702 return (SANE_STATUS_NO_MEM);
703
704 index = 0;
705 lexmark_device = first_device;
706 while (lexmark_device != NULL)
707 {
708 DBG (2, " lexmark_device->missing:%d\n",
709 lexmark_device->missing);
710 if (lexmark_device->missing == SANE_FALSE)
711 {
712
713 devlist[index] = &(lexmark_device->sane);
714 index++;
715 }
716 lexmark_device = lexmark_device->next;
717 }
718 devlist[index] = 0;
719
720 *device_list = devlist;
721
722 return status;
723 }
724
725 SANE_Status
sane_open(SANE_String_Const devicename,SANE_Handle * handle)726 sane_open (SANE_String_Const devicename, SANE_Handle * handle)
727 {
728 Lexmark_Device *lexmark_device;
729 SANE_Status status;
730
731 DBG (2, "sane_open: devicename=\"%s\", handle=%p\n", devicename,
732 (void *) handle);
733
734 /* walk the linked list of scanner device until there is a match
735 * with the device name */
736 for (lexmark_device = first_device; lexmark_device;
737 lexmark_device = lexmark_device->next)
738 {
739 DBG (10, " devname from list: %s\n",
740 lexmark_device->sane.name);
741 if (strcmp (devicename, "") == 0
742 || strcmp (devicename, "lexmark") == 0
743 || strcmp (devicename, lexmark_device->sane.name) == 0)
744 break;
745 }
746
747 *handle = lexmark_device;
748
749 status = init_options (lexmark_device);
750 if (status != SANE_STATUS_GOOD)
751 return status;
752
753 DBG(2, " device `%s' opening devnum: '%d'\n",
754 lexmark_device->sane.name, lexmark_device->devnum);
755 status = sanei_usb_open (lexmark_device->sane.name, &(lexmark_device->devnum));
756 if (status != SANE_STATUS_GOOD)
757 {
758 DBG (1, " couldn't open device `%s': %s\n",
759 lexmark_device->sane.name,
760 sane_strstatus (status));
761 return status;
762 }
763 else
764 {
765 DBG (2, " device `%s' successfully opened devnum: '%d'\n",
766 lexmark_device->sane.name, lexmark_device->devnum);
767 }
768
769 return status;
770 }
771
772 const SANE_Option_Descriptor *
sane_get_option_descriptor(SANE_Handle handle,SANE_Int option)773 sane_get_option_descriptor (SANE_Handle handle, SANE_Int option)
774 {
775 Lexmark_Device *lexmark_device;
776
777 //DBG (2, "sane_get_option_descriptor: handle=%p, option = %d\n",
778 // (void *) handle, option);
779
780 /* Check for valid option number */
781 if ((option < 0) || (option >= NUM_OPTIONS))
782 return NULL;
783
784 for (lexmark_device = first_device; lexmark_device;
785 lexmark_device = lexmark_device->next)
786 {
787 if (lexmark_device == handle)
788 break;
789 }
790
791 if (!lexmark_device)
792 return NULL;
793
794 if (lexmark_device->opt[option].name)
795 {
796 //DBG (2, " name=%s\n",
797 // lexmark_device->opt[option].name);
798 }
799
800 return &(lexmark_device->opt[option]);
801 }
802
803 SANE_Status
sane_control_option(SANE_Handle handle,SANE_Int option,SANE_Action action,void * value,SANE_Word * info)804 sane_control_option (SANE_Handle handle, SANE_Int option, SANE_Action action,
805 void * value, SANE_Word * info)
806 {
807 Lexmark_Device *lexmark_device;
808 SANE_Status status;
809 SANE_Word w;
810 SANE_Int res_selected;
811
812 DBG (2, "sane_control_option: handle=%p, opt=%d, act=%d, val=%p, info=%p\n",
813 (void *) handle, option, action, (void *) value, (void *) info);
814
815 for (lexmark_device = first_device; lexmark_device;
816 lexmark_device = lexmark_device->next){
817 if (lexmark_device == handle)
818 break;
819 }
820
821
822 if (value == NULL)
823 return SANE_STATUS_INVAL;
824
825 switch (action){
826 case SANE_ACTION_SET_VALUE:
827 if (!SANE_OPTION_IS_SETTABLE (lexmark_device->opt[option].cap)){
828 return SANE_STATUS_INVAL;
829 }
830 /* Make sure boolean values are only TRUE or FALSE */
831 if (lexmark_device->opt[option].type == SANE_TYPE_BOOL){
832 if (!
833 ((*(SANE_Bool *) value == SANE_FALSE)
834 || (*(SANE_Bool *) value == SANE_TRUE)))
835 return SANE_STATUS_INVAL;
836 }
837
838 /* Check range constraints */
839 if (lexmark_device->opt[option].constraint_type ==
840 SANE_CONSTRAINT_RANGE){
841 status =
842 sanei_constrain_value (&(lexmark_device->opt[option]), value,
843 info);
844 if (status != SANE_STATUS_GOOD){
845 DBG (2, " SANE_CONTROL_OPTION: Bad value for range\n");
846 return SANE_STATUS_INVAL;
847 }
848 }
849 switch (option){
850 case OPT_NUM_OPTS:
851 case OPT_RESOLUTION:
852 res_selected = *(SANE_Int *) value;
853 // first value is the size of the wordlist!
854 for(int i=1; i<dpi_list_size; i++){
855 DBG (10, " posible res=%d selected=%d\n", dpi_list[i], res_selected);
856 if(res_selected == dpi_list[i]){
857 lexmark_device->val[option].w = *(SANE_Word *) value;
858 }
859 }
860 break;
861 case OPT_TL_X:
862 case OPT_TL_Y:
863 case OPT_BR_X:
864 case OPT_BR_Y:
865 DBG (2, " Option value set to %d (%s)\n", *(SANE_Word *) value,
866 lexmark_device->opt[option].name);
867 lexmark_device->val[option].w = *(SANE_Word *) value;
868 if (lexmark_device->val[OPT_TL_X].w >
869 lexmark_device->val[OPT_BR_X].w){
870 w = lexmark_device->val[OPT_TL_X].w;
871 lexmark_device->val[OPT_TL_X].w =
872 lexmark_device->val[OPT_BR_X].w;
873 lexmark_device->val[OPT_BR_X].w = w;
874 if (info)
875 *info |= SANE_INFO_RELOAD_PARAMS;
876 }
877 if (lexmark_device->val[OPT_TL_Y].w >
878 lexmark_device->val[OPT_BR_Y].w){
879 w = lexmark_device->val[OPT_TL_Y].w;
880 lexmark_device->val[OPT_TL_Y].w =
881 lexmark_device->val[OPT_BR_Y].w;
882 lexmark_device->val[OPT_BR_Y].w = w;
883 if (info)
884 *info |= SANE_INFO_RELOAD_PARAMS;
885 }
886 break;
887 case OPT_MODE:
888 strcpy (lexmark_device->val[option].s, value);
889 if (info)
890 *info |= SANE_INFO_RELOAD_PARAMS | SANE_INFO_RELOAD_OPTIONS;
891 return SANE_STATUS_GOOD;
892 }
893
894
895 if (info != NULL)
896 *info |= SANE_INFO_RELOAD_PARAMS;
897
898 break;
899 case SANE_ACTION_GET_VALUE:
900 switch (option){
901 case OPT_NUM_OPTS:
902 case OPT_RESOLUTION:
903 case OPT_PREVIEW:
904 case OPT_TL_X:
905 case OPT_TL_Y:
906 case OPT_BR_X:
907 case OPT_BR_Y:
908 *(SANE_Word *) value = lexmark_device->val[option].w;
909 //DBG (2, " Option value = %d (%s)\n", *(SANE_Word *) value,
910 // lexmark_device->opt[option].name);
911 break;
912 case OPT_MODE:
913 strcpy (value, lexmark_device->val[option].s);
914 break;
915 }
916 break;
917
918 default:
919 return SANE_STATUS_INVAL;
920 }
921
922 return SANE_STATUS_GOOD;
923 }
924
925 SANE_Status
sane_get_parameters(SANE_Handle handle,SANE_Parameters * params)926 sane_get_parameters (SANE_Handle handle, SANE_Parameters * params)
927 {
928 Lexmark_Device *lexmark_device;
929 SANE_Parameters *device_params;
930 SANE_Int width_px;
931
932 DBG (2, "sane_get_parameters: handle=%p, params=%p\n", (void *) handle,
933 (void *) params);
934
935 for (lexmark_device = first_device; lexmark_device;
936 lexmark_device = lexmark_device->next)
937 {
938 if (lexmark_device == handle)
939 break;
940 }
941
942 if (!lexmark_device)
943 return SANE_STATUS_INVAL;
944
945 // res = lexmark_device->val[OPT_RESOLUTION].w;
946 device_params = &(lexmark_device->params);
947
948 width_px =
949 lexmark_device->val[OPT_BR_X].w - lexmark_device->val[OPT_TL_X].w;
950
951 /* 24 bit colour = 8 bits/channel for each of the RGB channels */
952 device_params->pixels_per_line = width_px;
953 device_params->format = SANE_FRAME_RGB; // SANE_FRAME_GRAY
954 device_params->depth = 8;
955 device_params->bytes_per_line =
956 (SANE_Int) (3 * device_params->pixels_per_line);
957
958 if (strcmp (lexmark_device->val[OPT_MODE].s, SANE_VALUE_SCAN_MODE_COLOR)
959 != 0)
960 {
961 device_params->format = SANE_FRAME_GRAY;
962 device_params->bytes_per_line =
963 (SANE_Int) (device_params->pixels_per_line);
964 }
965
966 /* geometry in pixels */
967 device_params->last_frame = SANE_TRUE;
968 device_params->lines = -1;//lexmark_device->val[OPT_BR_Y].w;
969
970 DBG (2, " device_params->pixels_per_line=%d\n",
971 device_params->pixels_per_line);
972 DBG (2, " device_params->bytes_per_line=%d\n",
973 device_params->bytes_per_line);
974 DBG (2, " device_params->depth=%d\n",
975 device_params->depth);
976 DBG (2, " device_params->format=%d\n",
977 device_params->format);
978 DBG (2, " SANE_FRAME_GRAY: %d\n",
979 SANE_FRAME_GRAY);
980 DBG (2, " SANE_FRAME_RGB: %d\n",
981 SANE_FRAME_RGB);
982
983 if (params != 0)
984 {
985 params->format = device_params->format;
986 params->last_frame = device_params->last_frame;
987 params->lines = device_params->lines;
988 params->depth = device_params->depth;
989 params->pixels_per_line = device_params->pixels_per_line;
990 params->bytes_per_line = device_params->bytes_per_line;
991 }
992 return SANE_STATUS_GOOD;
993 }
994
995 SANE_Status
sane_start(SANE_Handle handle)996 sane_start (SANE_Handle handle)
997 {
998 Lexmark_Device * lexmark_device;
999 SANE_Status status;
1000 SANE_Byte * cmd = (SANE_Byte *) malloc
1001 (command_with_params_block_size * sizeof (SANE_Byte));
1002 if (cmd == NULL)
1003 return SANE_STATUS_NO_MEM;
1004
1005 DBG (2, "sane_start: handle=%p initialized=%d\n", (void *) handle, initialized);
1006
1007 if (!initialized)
1008 return SANE_STATUS_INVAL;
1009
1010 for (lexmark_device = first_device; lexmark_device;
1011 lexmark_device = lexmark_device->next)
1012 {
1013 if (lexmark_device == handle)
1014 break;
1015 }
1016
1017 if(lexmark_device == NULL){
1018 DBG (2, " Cannot find device\n");
1019 free(cmd);
1020 return SANE_STATUS_IO_ERROR;
1021 }
1022
1023 lexmark_device->read_buffer->data = NULL;
1024 lexmark_device->read_buffer->size = 0;
1025 lexmark_device->read_buffer->last_line_bytes_read = 0;
1026 lexmark_device->read_buffer->image_line_no = 0;
1027 lexmark_device->read_buffer->write_byte_counter = 0;
1028 lexmark_device->read_buffer->read_byte_counter = 0;
1029 lexmark_device->eof = SANE_FALSE;
1030 lexmark_device->device_cancelled = SANE_FALSE;
1031
1032 //launch scan commands
1033 status = usb_write_then_read(lexmark_device, command1_block,
1034 command1_block_size);
1035 if (status != SANE_STATUS_GOOD){
1036 free(cmd);
1037 return status;
1038 }
1039 status = usb_write_then_read(lexmark_device, command2_block,
1040 command2_block_size);
1041 if (status != SANE_STATUS_GOOD){
1042 free(cmd);
1043 return status;
1044 }
1045 build_packet(lexmark_device, 0x05, cmd);
1046 status = usb_write_then_read(lexmark_device, cmd,
1047 command_with_params_block_size);
1048 if (status != SANE_STATUS_GOOD){
1049 free(cmd);
1050 return status;
1051 }
1052 build_packet(lexmark_device, 0x01, cmd);;
1053 status = usb_write_then_read(lexmark_device, cmd,
1054 command_with_params_block_size);
1055 if (status != SANE_STATUS_GOOD){
1056 free(cmd);
1057 return status;
1058 }
1059
1060 free(cmd);
1061 return SANE_STATUS_GOOD;
1062 }
1063
1064
debug_packet(const SANE_Byte * source,SANE_Int source_size,Debug_Packet dp)1065 void debug_packet(const SANE_Byte * source, SANE_Int source_size, Debug_Packet dp){
1066 if(dp == READ){
1067 DBG (10, "source READ <<< size=%d\n", source_size);
1068 }else{
1069 DBG (10, "source WRITE >>> size=%d\n", source_size);
1070 }
1071
1072 DBG (10, " %02hhx %02hhx %02hhx %02hhx | %02hhx %02hhx %02hhx %02hhx \n",
1073 source[0], source[1], source[2], source[3], source[4], source[5], source[6], source[7]);
1074 DBG (10, " %02hhx %02hhx %02hhx %02hhx | %02hhx %02hhx %02hhx %02hhx \n",
1075 source[8], source[9], source[10], source[11], source[12], source[13], source[14], source[15]);
1076 int debug_offset = 4092;
1077 if(source_size > debug_offset){
1078 DBG (10, " %02hhx %02hhx %02hhx %02hhx | %02hhx %02hhx %02hhx %02hhx \n",
1079 source[source_size-16-debug_offset],
1080 source[source_size-15-debug_offset],
1081 source[source_size-14-debug_offset],
1082 source[source_size-13-debug_offset],
1083 source[source_size-12-debug_offset],
1084 source[source_size-11-debug_offset],
1085 source[source_size-10-debug_offset],
1086 source[source_size-9-debug_offset]);
1087 DBG (10, " %02hhx %02hhx %02hhx %02hhx | %02hhx %02hhx %02hhx %02hhx \n",
1088 source[source_size-8-debug_offset],
1089 source[source_size-7-debug_offset],
1090 source[source_size-6-debug_offset],
1091 source[source_size-5-debug_offset],
1092 source[source_size-4-debug_offset],
1093 source[source_size-3-debug_offset],
1094 source[source_size-2-debug_offset],
1095 source[source_size-1-debug_offset]);
1096 }
1097 return;
1098 }
1099
1100 SANE_Status
sane_read(SANE_Handle handle,SANE_Byte * data,SANE_Int max_length,SANE_Int * length)1101 sane_read (SANE_Handle handle, SANE_Byte * data,
1102 SANE_Int max_length, SANE_Int * length)
1103 {
1104 Lexmark_Device * lexmark_device;
1105 SANE_Status status;
1106 size_t size = transfer_buffer_size;
1107 //SANE_Byte buf[size];
1108 DBG (1, "\n");
1109 DBG (1, "sane_read max_length=%d:\n", max_length);
1110
1111 for (lexmark_device = first_device; lexmark_device;
1112 lexmark_device = lexmark_device->next)
1113 {
1114 if (lexmark_device == handle)
1115 break;
1116 }
1117
1118 if (lexmark_device->device_cancelled == SANE_TRUE) {
1119 DBG (10, "device_cancelled=True \n");
1120 usb_write_then_read(lexmark_device, command_cancel1_block,
1121 command_cancel_size);
1122 usb_write_then_read(lexmark_device, command_cancel2_block,
1123 command_cancel_size);
1124 usb_write_then_read(lexmark_device, command_cancel1_block,
1125 command_cancel_size);
1126 usb_write_then_read(lexmark_device, command_cancel2_block,
1127 command_cancel_size);
1128 // to empty buffers
1129 status = sanei_usb_read_bulk (
1130 lexmark_device->devnum, lexmark_device->transfer_buffer, &size);
1131 if(status == SANE_STATUS_GOOD){
1132 status = sanei_usb_read_bulk (
1133 lexmark_device->devnum, lexmark_device->transfer_buffer, &size);
1134 }
1135 if(status == SANE_STATUS_GOOD){
1136 status = sanei_usb_read_bulk (
1137 lexmark_device->devnum, lexmark_device->transfer_buffer, &size);
1138 }
1139
1140 return status;
1141 }
1142
1143 //status = sanei_usb_read_bulk (lexmark_device->devnum, buf, &size);
1144 if(!lexmark_device->eof){
1145 DBG (1, " usb_read\n");
1146 status = sanei_usb_read_bulk (
1147 lexmark_device->devnum, lexmark_device->transfer_buffer, &size);
1148 if (status != SANE_STATUS_GOOD && status != SANE_STATUS_EOF)
1149 {
1150 DBG (1, " USB READ Error in sanei_usb_read_bulk, cannot read devnum=%d status=%d size=%ld\n",
1151 lexmark_device->devnum, status, size);
1152 return status;
1153 }
1154 DBG (1, " usb_read done size=%ld\n", size);
1155 debug_packet(lexmark_device->transfer_buffer, size, READ);
1156 }else{
1157 DBG (1, " no usb_read eof reached\n");
1158 }
1159
1160 // is last data packet ?
1161 if (!lexmark_device->eof && memcmp(last_data_packet, lexmark_device->transfer_buffer, last_data_packet_size) == 0){
1162
1163 // we may still have data left to send in our buffer device->read_buffer->data
1164 //length = 0;
1165 //return SANE_STATUS_EOF;
1166 lexmark_device->eof = SANE_TRUE;
1167 DBG (1, " EOF PACKET no more data from scanner\n");
1168
1169 return SANE_STATUS_GOOD;
1170 }
1171 // cancel packet received?
1172 if (memcmp(cancel_packet, lexmark_device->transfer_buffer, cancel_packet_size) == 0){
1173 length = 0;
1174 return SANE_STATUS_CANCELLED;
1175 }
1176 if (memcmp(empty_line_data_packet, lexmark_device->transfer_buffer, empty_line_data_packet_size) == 0){
1177 return SANE_STATUS_GOOD;
1178 }
1179 if (memcmp(unknown_a_data_packet, lexmark_device->transfer_buffer, unknown_a_data_packet_size) == 0){
1180 return SANE_STATUS_GOOD;
1181 }
1182 if (memcmp(unknown_b_data_packet, lexmark_device->transfer_buffer, unknown_b_data_packet_size) == 0){
1183 return SANE_STATUS_GOOD;
1184 }
1185 if (memcmp(unknown_c_data_packet, lexmark_device->transfer_buffer, unknown_c_data_packet_size) == 0){
1186 return SANE_STATUS_GOOD;
1187 }
1188 if (memcmp(unknown_d_data_packet, lexmark_device->transfer_buffer, unknown_d_data_packet_size) == 0){
1189 return SANE_STATUS_GOOD;
1190 }
1191 if (memcmp(unknown_e_data_packet, lexmark_device->transfer_buffer, unknown_e_data_packet_size) == 0){
1192 return SANE_STATUS_GOOD;
1193 }
1194
1195 status = clean_and_copy_data(
1196 lexmark_device->transfer_buffer,
1197 size,
1198 data,
1199 length,
1200 lexmark_device->params.format,
1201 max_length,
1202 handle);
1203
1204 return status;
1205 }
1206
1207 SANE_Status
sane_set_io_mode(SANE_Handle handle,SANE_Bool non_blocking)1208 sane_set_io_mode (SANE_Handle handle, SANE_Bool non_blocking)
1209 {
1210 DBG (2, "sane_set_io_mode: handle = %p, non_blocking = %d\n",
1211 (void *) handle, non_blocking);
1212
1213 if (non_blocking)
1214 return SANE_STATUS_UNSUPPORTED;
1215
1216 return SANE_STATUS_GOOD;
1217 }
1218
1219 SANE_Status
sane_get_select_fd(SANE_Handle handle,SANE_Int * fd)1220 sane_get_select_fd (SANE_Handle handle, SANE_Int * fd)
1221 {
1222 DBG (2, "sane_get_select_fd: handle = %p, fd %s 0\n", (void *) handle,
1223 fd ? "!=" : "=");
1224
1225 return SANE_STATUS_UNSUPPORTED;
1226 }
1227
1228 void
sane_cancel(SANE_Handle handle)1229 sane_cancel (SANE_Handle handle)
1230 {
1231 Lexmark_Device * lexmark_device;
1232
1233 DBG (2, "sane_cancel: handle = %p\n", (void *) handle);
1234
1235 for (lexmark_device = first_device; lexmark_device;
1236 lexmark_device = lexmark_device->next)
1237 {
1238 if (lexmark_device == handle)
1239 break;
1240 }
1241 sanei_usb_reset (lexmark_device->devnum);
1242 lexmark_device->device_cancelled = SANE_TRUE;
1243 }
1244
1245 void
sane_close(SANE_Handle handle)1246 sane_close (SANE_Handle handle)
1247 {
1248 Lexmark_Device * lexmark_device;
1249
1250 DBG (2, "sane_close: handle=%p\n", (void *) handle);
1251
1252 for (lexmark_device = first_device; lexmark_device;
1253 lexmark_device = lexmark_device->next)
1254 {
1255 if (lexmark_device == handle)
1256 break;
1257 }
1258
1259 sanei_usb_close (lexmark_device->devnum);
1260 }
1261
1262 void
sane_exit(void)1263 sane_exit (void)
1264 {
1265 Lexmark_Device *lexmark_device, *next_lexmark_device;
1266
1267 DBG (2, "sane_exit\n");
1268
1269 if (!initialized)
1270 return;
1271
1272 for (lexmark_device = first_device; lexmark_device;
1273 lexmark_device = next_lexmark_device)
1274 {
1275 next_lexmark_device = lexmark_device->next;
1276 free (lexmark_device->transfer_buffer);
1277 free (lexmark_device->read_buffer);
1278 free (lexmark_device);
1279 }
1280
1281 if (devlist)
1282 free (devlist);
1283
1284 sanei_usb_exit();
1285 initialized = SANE_FALSE;
1286
1287 }
1288