1 /* sane - Scanner Access Now Easy. 2 3 ScanMaker 3840 Backend 4 Copyright (C) 2005-7 Earle F. Philhower, III 5 earle@ziplabel.com - http://www.ziplabel.com 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 #include <stdio.h> 44 #include <stdarg.h> 45 46 #ifndef BACKENDNAME 47 #include <usb.h> 48 #include <string.h> 49 50 #ifndef DEFINED_USB_HANDLE 51 #define DEFINED_USB_HANDLE 52 typedef usb_dev_handle *p_usb_dev_handle; 53 #endif 54 55 static p_usb_dev_handle find_device (unsigned int idVendor, 56 unsigned int idProduct); 57 #else 58 #include "../include/sane/sanei_usb.h" 59 60 #ifndef USBWRAPPER 61 #define USBWRAPPER 62 63 typedef SANE_Int p_usb_dev_handle; 64 #define usb_control_msg my_usb_control_msg 65 #define usb_bulk_read my_usb_bulk_read 66 #define usb_bulk_write my_usb_bulk_write 67 static int my_usb_bulk_write (p_usb_dev_handle dev, int ep, 68 unsigned char *bytes, 69 int size, int timeout); 70 static int my_usb_bulk_read (p_usb_dev_handle dev, int ep, 71 unsigned char *bytes, 72 int size, int timeout); 73 static int my_usb_control_msg (p_usb_dev_handle dev, int requesttype, 74 int request, int value, int index, 75 unsigned char *bytes, 76 int size, int timeout); 77 #endif /* USBWRAPPER */ 78 79 #endif 80 81 #include "sm3840_params.h" 82 83 static void idle_ab (p_usb_dev_handle udev); 84 static void write_regs (p_usb_dev_handle udev, int regs, int reg1, int val1, 85 ... /* int reg, int val, ... */ ); 86 static int write_vctl (p_usb_dev_handle udev, int request, int value, 87 int index, unsigned char byte); 88 static int read_vctl (p_usb_dev_handle udev, int request, int value, 89 int index, unsigned char *byte); 90 91 #ifndef BACKENDNAME 92 static void record (p_usb_dev_handle udev, char *fname, int bytes); 93 static void record_image (p_usb_dev_handle udev, char *fname, int dpi, 94 int scanpix, int scanlines, int gray, char *head, 95 int bpp16); 96 static void check_buttons (p_usb_dev_handle udev, int *scan, int *print, 97 int *mail); 98 static void record_head (p_usb_dev_handle udev, char *fname, int bytes, 99 char *header); 100 #endif 101 102 static void poll1 (p_usb_dev_handle udev); 103 static void poll2 (p_usb_dev_handle udev); 104 105 static void reset_scanner (p_usb_dev_handle udev); 106 107 108 static void set_lightmap_white (unsigned short *map, int dpi, int color); 109 110 static void calc_lightmap (unsigned short *buff, 111 unsigned short *storage, int index, int dpi, 112 double gain, int offset); 113 static void select_pixels (unsigned short *map, int dpi, int start, int end); 114 115 static void record_mem (p_usb_dev_handle udev, unsigned char **dest, 116 int bytes); 117 static void set_lamp_timer (p_usb_dev_handle udev, int timeout_in_mins); 118 119 static void set_gain_black (p_usb_dev_handle udev, 120 int r_gain, int g_gain, int b_gain, 121 int r_black, int g_black, int b_black); 122 123 static void idle_ab (p_usb_dev_handle udev); 124 static void write_regs (p_usb_dev_handle udev, int regs, int reg1, int val1, 125 ... /* int reg, int val, ... */ ); 126 static int write_vctl (p_usb_dev_handle udev, int request, int value, 127 int index, unsigned char byte); 128 static int read_vctl (p_usb_dev_handle udev, int request, int value, 129 int index, unsigned char *byte); 130 131 static void download_lut8 (p_usb_dev_handle udev, int dpi, int incolor); 132 133 static void record_line (int reset, 134 p_usb_dev_handle udev, 135 unsigned char *storeline, 136 int dpi, int scanpix, int gray, int bpp16, 137 int *save_i, 138 unsigned char **save_scan_line, 139 unsigned char **save_dpi1200_remap, 140 unsigned char **save_color_remap); 141 142 143 static void prepare_params (SM3840_Params * params); 144 145 static void fix_endian_short (unsigned short *data, int count); 146 147 #define rd_timeout 10000 148 #define wr_timeout 10000 149