1141cc406Sopenharmony_ci/* sane - Scanner Access Now Easy.
2141cc406Sopenharmony_ci
3141cc406Sopenharmony_ci   Copyright (C) 2003 Oliver Rauch
4141cc406Sopenharmony_ci   Copyright (C) 2003, 2004 Henning Meier-Geinitz <henning@meier-geinitz.de>
5141cc406Sopenharmony_ci   Copyright (C) 2004, 2005 Gerhard Jaeger <gerhard@gjaeger.de>
6141cc406Sopenharmony_ci   Copyright (C) 2004-2013 Stéphane Voltz <stef.dev@free.fr>
7141cc406Sopenharmony_ci   Copyright (C) 2005-2009 Pierre Willenbrock <pierre@pirsoft.dnsalias.org>
8141cc406Sopenharmony_ci   Copyright (C) 2006 Laurent Charpentier <laurent_pubs@yahoo.com>
9141cc406Sopenharmony_ci   Parts of the structs have been taken from the gt68xx backend by
10141cc406Sopenharmony_ci   Sergey Vlasov <vsu@altlinux.ru> et al.
11141cc406Sopenharmony_ci
12141cc406Sopenharmony_ci   This file is part of the SANE package.
13141cc406Sopenharmony_ci
14141cc406Sopenharmony_ci   This program is free software; you can redistribute it and/or
15141cc406Sopenharmony_ci   modify it under the terms of the GNU General Public License as
16141cc406Sopenharmony_ci   published by the Free Software Foundation; either version 2 of the
17141cc406Sopenharmony_ci   License, or (at your option) any later version.
18141cc406Sopenharmony_ci
19141cc406Sopenharmony_ci   This program is distributed in the hope that it will be useful, but
20141cc406Sopenharmony_ci   WITHOUT ANY WARRANTY; without even the implied warranty of
21141cc406Sopenharmony_ci   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22141cc406Sopenharmony_ci   General Public License for more details.
23141cc406Sopenharmony_ci
24141cc406Sopenharmony_ci   You should have received a copy of the GNU General Public License
25141cc406Sopenharmony_ci   along with this program.  If not, see <https://www.gnu.org/licenses/>.
26141cc406Sopenharmony_ci*/
27141cc406Sopenharmony_ci
28141cc406Sopenharmony_ci#ifndef GENESYS_LOW_H
29141cc406Sopenharmony_ci#define GENESYS_LOW_H
30141cc406Sopenharmony_ci
31141cc406Sopenharmony_ci
32141cc406Sopenharmony_ci#include "../include/sane/config.h"
33141cc406Sopenharmony_ci
34141cc406Sopenharmony_ci#include <errno.h>
35141cc406Sopenharmony_ci#include <string.h>
36141cc406Sopenharmony_ci#include <stdlib.h>
37141cc406Sopenharmony_ci#include <unistd.h>
38141cc406Sopenharmony_ci#include <math.h>
39141cc406Sopenharmony_ci#include <stddef.h>
40141cc406Sopenharmony_ci#ifdef HAVE_SYS_TIME_H
41141cc406Sopenharmony_ci#include <sys/time.h>
42141cc406Sopenharmony_ci#endif
43141cc406Sopenharmony_ci#ifdef HAVE_SYS_TYPES_H
44141cc406Sopenharmony_ci#include <sys/types.h>
45141cc406Sopenharmony_ci#endif
46141cc406Sopenharmony_ci#ifdef HAVE_MKDIR
47141cc406Sopenharmony_ci#include <sys/stat.h>
48141cc406Sopenharmony_ci#include <sys/types.h>
49141cc406Sopenharmony_ci#endif
50141cc406Sopenharmony_ci
51141cc406Sopenharmony_ci#include "../include/sane/sane.h"
52141cc406Sopenharmony_ci#include "../include/sane/sanei.h"
53141cc406Sopenharmony_ci#include "../include/sane/saneopts.h"
54141cc406Sopenharmony_ci
55141cc406Sopenharmony_ci#include "../include/sane/sanei_backend.h"
56141cc406Sopenharmony_ci#include "../include/sane/sanei_usb.h"
57141cc406Sopenharmony_ci
58141cc406Sopenharmony_ci#include "../include/_stdint.h"
59141cc406Sopenharmony_ci
60141cc406Sopenharmony_ci#include "device.h"
61141cc406Sopenharmony_ci#include "enums.h"
62141cc406Sopenharmony_ci#include "error.h"
63141cc406Sopenharmony_ci#include "fwd.h"
64141cc406Sopenharmony_ci#include "usb_device.h"
65141cc406Sopenharmony_ci#include "sensor.h"
66141cc406Sopenharmony_ci#include "serialize.h"
67141cc406Sopenharmony_ci#include "settings.h"
68141cc406Sopenharmony_ci#include "static_init.h"
69141cc406Sopenharmony_ci#include "status.h"
70141cc406Sopenharmony_ci#include "register.h"
71141cc406Sopenharmony_ci
72141cc406Sopenharmony_ci#include <algorithm>
73141cc406Sopenharmony_ci#include <array>
74141cc406Sopenharmony_ci#include <cstring>
75141cc406Sopenharmony_ci#include <functional>
76141cc406Sopenharmony_ci#include <iostream>
77141cc406Sopenharmony_ci#include <sstream>
78141cc406Sopenharmony_ci#include <limits>
79141cc406Sopenharmony_ci#include <memory>
80141cc406Sopenharmony_ci#include <stdexcept>
81141cc406Sopenharmony_ci#include <string>
82141cc406Sopenharmony_ci#include <vector>
83141cc406Sopenharmony_ci
84141cc406Sopenharmony_ci#define GENESYS_RED   0
85141cc406Sopenharmony_ci#define GENESYS_GREEN 1
86141cc406Sopenharmony_ci#define GENESYS_BLUE  2
87141cc406Sopenharmony_ci
88141cc406Sopenharmony_ci#define GENESYS_HAS_NO_BUTTONS       0              /**< scanner has no supported button */
89141cc406Sopenharmony_ci#define GENESYS_HAS_SCAN_SW          (1 << 0)       /**< scanner has SCAN button */
90141cc406Sopenharmony_ci#define GENESYS_HAS_FILE_SW          (1 << 1)       /**< scanner has FILE button */
91141cc406Sopenharmony_ci#define GENESYS_HAS_COPY_SW          (1 << 2)       /**< scanner has COPY button */
92141cc406Sopenharmony_ci#define GENESYS_HAS_EMAIL_SW         (1 << 3)       /**< scanner has EMAIL button */
93141cc406Sopenharmony_ci#define GENESYS_HAS_PAGE_LOADED_SW   (1 << 4)       /**< scanner has paper in detection */
94141cc406Sopenharmony_ci#define GENESYS_HAS_OCR_SW           (1 << 5)       /**< scanner has OCR button */
95141cc406Sopenharmony_ci#define GENESYS_HAS_POWER_SW         (1 << 6)       /**< scanner has power button */
96141cc406Sopenharmony_ci#define GENESYS_HAS_CALIBRATE        (1 << 7)       /**< scanner has 'calibrate' software button to start calibration */
97141cc406Sopenharmony_ci#define GENESYS_HAS_EXTRA_SW         (1 << 8)       /**< scanner has extra function button */
98141cc406Sopenharmony_ci#define GENESYS_HAS_TRANSP_SW        (1 << 9)       /**< scanner has TRANSPARENCY/SCAN_FILM button */
99141cc406Sopenharmony_ci#define GENESYS_HAS_PDF1_SW          (1 << 10)      /**< scanner has special PDF button 1 */
100141cc406Sopenharmony_ci#define GENESYS_HAS_PDF2_SW          (1 << 11)      /**< scanner has special PDF button 2 */
101141cc406Sopenharmony_ci#define GENESYS_HAS_PDF3_SW          (1 << 12)      /**< scanner has special PDF button 3 */
102141cc406Sopenharmony_ci#define GENESYS_HAS_PDF4_SW          (1 << 13)      /**< scanner has special PDF button 4 */
103141cc406Sopenharmony_ci
104141cc406Sopenharmony_ci/* USB control message values */
105141cc406Sopenharmony_ci#define REQUEST_TYPE_IN		(USB_TYPE_VENDOR | USB_DIR_IN)
106141cc406Sopenharmony_ci#define REQUEST_TYPE_OUT	(USB_TYPE_VENDOR | USB_DIR_OUT)
107141cc406Sopenharmony_ci#define REQUEST_REGISTER	0x0c
108141cc406Sopenharmony_ci#define REQUEST_BUFFER		0x04
109141cc406Sopenharmony_ci#define VALUE_BUFFER		0x82
110141cc406Sopenharmony_ci#define VALUE_SET_REGISTER	0x83
111141cc406Sopenharmony_ci#define VALUE_READ_REGISTER	0x84
112141cc406Sopenharmony_ci#define VALUE_WRITE_REGISTER	0x85
113141cc406Sopenharmony_ci#define VALUE_INIT		0x87
114141cc406Sopenharmony_ci#define GPIO_OUTPUT_ENABLE	0x89
115141cc406Sopenharmony_ci#define GPIO_READ		0x8a
116141cc406Sopenharmony_ci#define GPIO_WRITE		0x8b
117141cc406Sopenharmony_ci#define VALUE_BUF_ENDACCESS	0x8c
118141cc406Sopenharmony_ci#define VALUE_GET_REGISTER	0x8e
119141cc406Sopenharmony_ci#define INDEX			0x00
120141cc406Sopenharmony_ci
121141cc406Sopenharmony_ci/* todo: used?
122141cc406Sopenharmony_ci#define VALUE_READ_STATUS	0x86
123141cc406Sopenharmony_ci*/
124141cc406Sopenharmony_ci
125141cc406Sopenharmony_ci/* Read/write bulk data/registers */
126141cc406Sopenharmony_ci#define BULK_OUT		0x01
127141cc406Sopenharmony_ci#define BULK_IN			0x00
128141cc406Sopenharmony_ci#define BULK_RAM		0x00
129141cc406Sopenharmony_ci#define BULK_REGISTER		0x11
130141cc406Sopenharmony_ci
131141cc406Sopenharmony_ci#define BULKOUT_MAXSIZE         0xF000
132141cc406Sopenharmony_ci
133141cc406Sopenharmony_ci/* AFE values */
134141cc406Sopenharmony_ci#define AFE_INIT       1
135141cc406Sopenharmony_ci#define AFE_SET        2
136141cc406Sopenharmony_ci#define AFE_POWER_SAVE 4
137141cc406Sopenharmony_ci
138141cc406Sopenharmony_cinamespace genesys {
139141cc406Sopenharmony_ci
140141cc406Sopenharmony_ciclass UsbDeviceEntry {
141141cc406Sopenharmony_cipublic:
142141cc406Sopenharmony_ci    static constexpr std::uint16_t BCD_DEVICE_NOT_SET = 0xffff;
143141cc406Sopenharmony_ci
144141cc406Sopenharmony_ci    UsbDeviceEntry(std::uint16_t vendor_id, std::uint16_t product_id,
145141cc406Sopenharmony_ci                   const Genesys_Model& model) :
146141cc406Sopenharmony_ci        vendor_{vendor_id}, product_{product_id},
147141cc406Sopenharmony_ci        bcd_device_{BCD_DEVICE_NOT_SET}, model_{model}
148141cc406Sopenharmony_ci    {}
149141cc406Sopenharmony_ci
150141cc406Sopenharmony_ci    UsbDeviceEntry(std::uint16_t vendor_id, std::uint16_t product_id, std::uint16_t bcd_device,
151141cc406Sopenharmony_ci                   const Genesys_Model& model) :
152141cc406Sopenharmony_ci        vendor_{vendor_id}, product_{product_id},
153141cc406Sopenharmony_ci        bcd_device_{bcd_device}, model_{model}
154141cc406Sopenharmony_ci    {}
155141cc406Sopenharmony_ci
156141cc406Sopenharmony_ci    std::uint16_t vendor_id() const { return vendor_; }
157141cc406Sopenharmony_ci    std::uint16_t product_id() const { return product_; }
158141cc406Sopenharmony_ci    std::uint16_t bcd_device() const { return bcd_device_; }
159141cc406Sopenharmony_ci
160141cc406Sopenharmony_ci    const Genesys_Model& model() const { return model_; }
161141cc406Sopenharmony_ci
162141cc406Sopenharmony_ci    bool matches(std::uint16_t vendor_id, std::uint16_t product_id, std::uint16_t bcd_device)
163141cc406Sopenharmony_ci    {
164141cc406Sopenharmony_ci        if (vendor_ != vendor_id)
165141cc406Sopenharmony_ci            return false;
166141cc406Sopenharmony_ci        if (product_ != product_id)
167141cc406Sopenharmony_ci            return false;
168141cc406Sopenharmony_ci        if (bcd_device_ != BCD_DEVICE_NOT_SET && bcd_device != BCD_DEVICE_NOT_SET &&
169141cc406Sopenharmony_ci            bcd_device_ != bcd_device)
170141cc406Sopenharmony_ci        {
171141cc406Sopenharmony_ci            return false;
172141cc406Sopenharmony_ci        }
173141cc406Sopenharmony_ci        return true;
174141cc406Sopenharmony_ci    }
175141cc406Sopenharmony_ci
176141cc406Sopenharmony_ciprivate:
177141cc406Sopenharmony_ci    // USB vendor identifier
178141cc406Sopenharmony_ci    std::uint16_t vendor_;
179141cc406Sopenharmony_ci    // USB product identifier
180141cc406Sopenharmony_ci    std::uint16_t product_;
181141cc406Sopenharmony_ci    // USB bcdProduct identifier
182141cc406Sopenharmony_ci    std::uint16_t bcd_device_;
183141cc406Sopenharmony_ci    // Scanner model information
184141cc406Sopenharmony_ci    Genesys_Model model_;
185141cc406Sopenharmony_ci};
186141cc406Sopenharmony_ci
187141cc406Sopenharmony_ci/*--------------------------------------------------------------------------*/
188141cc406Sopenharmony_ci/*       common functions needed by low level specific functions            */
189141cc406Sopenharmony_ci/*--------------------------------------------------------------------------*/
190141cc406Sopenharmony_ci
191141cc406Sopenharmony_cistd::unique_ptr<CommandSet> create_cmd_set(AsicType asic_type);
192141cc406Sopenharmony_ci
193141cc406Sopenharmony_ci// reads the status of the scanner
194141cc406Sopenharmony_ciStatus scanner_read_status(Genesys_Device& dev);
195141cc406Sopenharmony_ci
196141cc406Sopenharmony_ci// reads the status of the scanner reliably. This is done by reading the status twice. The first
197141cc406Sopenharmony_ci// read sometimes returns the home sensor as engaged when this is not true.
198141cc406Sopenharmony_ciStatus scanner_read_reliable_status(Genesys_Device& dev);
199141cc406Sopenharmony_ci
200141cc406Sopenharmony_ci// reads and prints the scanner status
201141cc406Sopenharmony_civoid scanner_read_print_status(Genesys_Device& dev);
202141cc406Sopenharmony_ci
203141cc406Sopenharmony_civoid debug_print_status(DebugMessageHelper& dbg, Status status);
204141cc406Sopenharmony_ci
205141cc406Sopenharmony_civoid scanner_register_rw_clear_bits(Genesys_Device& dev, std::uint16_t address, std::uint8_t mask);
206141cc406Sopenharmony_civoid scanner_register_rw_set_bits(Genesys_Device& dev, std::uint16_t address, std::uint8_t mask);
207141cc406Sopenharmony_civoid scanner_register_rw_bits(Genesys_Device& dev, std::uint16_t address,
208141cc406Sopenharmony_ci                              std::uint8_t value, std::uint8_t mask);
209141cc406Sopenharmony_ci
210141cc406Sopenharmony_civoid sanei_genesys_write_ahb(Genesys_Device* dev, std::uint32_t addr, std::uint32_t size,
211141cc406Sopenharmony_ci                             std::uint8_t* data);
212141cc406Sopenharmony_ci
213141cc406Sopenharmony_ciextern void sanei_genesys_init_structs (Genesys_Device * dev);
214141cc406Sopenharmony_ci
215141cc406Sopenharmony_ciconst Genesys_Sensor& sanei_genesys_find_sensor_any(const Genesys_Device* dev);
216141cc406Sopenharmony_ciconst Genesys_Sensor& sanei_genesys_find_sensor(const Genesys_Device* dev, unsigned dpi,
217141cc406Sopenharmony_ci                                                unsigned channels, ScanMethod scan_method);
218141cc406Sopenharmony_cibool sanei_genesys_has_sensor(const Genesys_Device* dev, unsigned dpi, unsigned channels,
219141cc406Sopenharmony_ci                              ScanMethod scan_method);
220141cc406Sopenharmony_ciGenesys_Sensor& sanei_genesys_find_sensor_for_write(Genesys_Device* dev, unsigned dpi,
221141cc406Sopenharmony_ci                                                    unsigned channels, ScanMethod scan_method);
222141cc406Sopenharmony_ci
223141cc406Sopenharmony_cistd::vector<std::reference_wrapper<const Genesys_Sensor>>
224141cc406Sopenharmony_ci    sanei_genesys_find_sensors_all(const Genesys_Device* dev, ScanMethod scan_method);
225141cc406Sopenharmony_cistd::vector<std::reference_wrapper<Genesys_Sensor>>
226141cc406Sopenharmony_ci    sanei_genesys_find_sensors_all_for_write(Genesys_Device* dev, ScanMethod scan_method);
227141cc406Sopenharmony_ci
228141cc406Sopenharmony_ciextern void sanei_genesys_init_shading_data(Genesys_Device* dev, const Genesys_Sensor& sensor,
229141cc406Sopenharmony_ci                                            int pixels_per_line);
230141cc406Sopenharmony_ci
231141cc406Sopenharmony_ciextern void sanei_genesys_read_valid_words(Genesys_Device* dev, unsigned int* steps);
232141cc406Sopenharmony_ci
233141cc406Sopenharmony_ciextern void sanei_genesys_read_scancnt(Genesys_Device* dev, unsigned int* steps);
234141cc406Sopenharmony_ci
235141cc406Sopenharmony_ciextern void sanei_genesys_read_feed_steps(Genesys_Device* dev, unsigned int* steps);
236141cc406Sopenharmony_ci
237141cc406Sopenharmony_civoid sanei_genesys_set_lamp_power(Genesys_Device* dev, const Genesys_Sensor& sensor,
238141cc406Sopenharmony_ci                                  Genesys_Register_Set& regs, bool set);
239141cc406Sopenharmony_ci
240141cc406Sopenharmony_civoid sanei_genesys_set_motor_power(Genesys_Register_Set& regs, bool set);
241141cc406Sopenharmony_ci
242141cc406Sopenharmony_cibool should_enable_gamma(const ScanSession& session, const Genesys_Sensor& sensor);
243141cc406Sopenharmony_ci
244141cc406Sopenharmony_ci/** Calculates the values of the Z{1,2}MOD registers. They are a phase correction to synchronize
245141cc406Sopenharmony_ci    with the line clock during acceleration and deceleration.
246141cc406Sopenharmony_ci
247141cc406Sopenharmony_ci    two_table is true if moving is done by two tables, false otherwise.
248141cc406Sopenharmony_ci
249141cc406Sopenharmony_ci    acceleration_steps is the number of steps for acceleration, i.e. the number written to
250141cc406Sopenharmony_ci    REG_STEPNO.
251141cc406Sopenharmony_ci
252141cc406Sopenharmony_ci    move_steps number of steps to move, i.e. the number written to REG_FEEDL.
253141cc406Sopenharmony_ci
254141cc406Sopenharmony_ci    buffer_acceleration_steps, the number of steps for acceleration when buffer condition is met,
255141cc406Sopenharmony_ci    i.e. the number written to REG_FWDSTEP.
256141cc406Sopenharmony_ci*/
257141cc406Sopenharmony_civoid sanei_genesys_calculate_zmod(bool two_table,
258141cc406Sopenharmony_ci                                  std::uint32_t exposure_time,
259141cc406Sopenharmony_ci                                  const std::vector<std::uint16_t>& slope_table,
260141cc406Sopenharmony_ci                                  unsigned acceleration_steps,
261141cc406Sopenharmony_ci                                  unsigned move_steps,
262141cc406Sopenharmony_ci                                  unsigned buffer_acceleration_steps,
263141cc406Sopenharmony_ci                                  std::uint32_t* out_z1, std::uint32_t* out_z2);
264141cc406Sopenharmony_ci
265141cc406Sopenharmony_ciextern void sanei_genesys_set_buffer_address(Genesys_Device* dev, std::uint32_t addr);
266141cc406Sopenharmony_ci
267141cc406Sopenharmony_ciunsigned sanei_genesys_get_bulk_max_size(AsicType asic_type);
268141cc406Sopenharmony_ci
269141cc406Sopenharmony_ciSANE_Int sanei_genesys_exposure_time2(Genesys_Device* dev, const MotorProfile& profile, float ydpi,
270141cc406Sopenharmony_ci                                      int endpixel, int led_exposure);
271141cc406Sopenharmony_ci
272141cc406Sopenharmony_civoid sanei_genesys_create_default_gamma_table(Genesys_Device* dev,
273141cc406Sopenharmony_ci                                              std::vector<std::uint16_t>& gamma_table, float gamma);
274141cc406Sopenharmony_ci
275141cc406Sopenharmony_cistd::vector<std::uint16_t> get_gamma_table(Genesys_Device* dev, const Genesys_Sensor& sensor,
276141cc406Sopenharmony_ci                                           int color);
277141cc406Sopenharmony_ci
278141cc406Sopenharmony_civoid sanei_genesys_send_gamma_table(Genesys_Device* dev, const Genesys_Sensor& sensor);
279141cc406Sopenharmony_ci
280141cc406Sopenharmony_ciextern void sanei_genesys_stop_motor(Genesys_Device* dev);
281141cc406Sopenharmony_ci
282141cc406Sopenharmony_ci// moves the scan head by the specified steps at the motor base dpi
283141cc406Sopenharmony_civoid scanner_move(Genesys_Device& dev, ScanMethod scan_method, unsigned steps, Direction direction);
284141cc406Sopenharmony_ci
285141cc406Sopenharmony_civoid scanner_move_back_home(Genesys_Device& dev, bool wait_until_home);
286141cc406Sopenharmony_civoid scanner_move_back_home_ta(Genesys_Device& dev);
287141cc406Sopenharmony_ci
288141cc406Sopenharmony_ci/** Search for a full width black or white strip.
289141cc406Sopenharmony_ci    This function searches for a black or white stripe across the scanning area.
290141cc406Sopenharmony_ci    When searching backward, the searched area must completely be of the desired
291141cc406Sopenharmony_ci    color since this area will be used for calibration which scans forward.
292141cc406Sopenharmony_ci
293141cc406Sopenharmony_ci    @param dev scanner device
294141cc406Sopenharmony_ci    @param forward true if searching forward, false if searching backward
295141cc406Sopenharmony_ci    @param black true if searching for a black strip, false for a white strip
296141cc406Sopenharmony_ci */
297141cc406Sopenharmony_civoid scanner_search_strip(Genesys_Device& dev, bool forward, bool black);
298141cc406Sopenharmony_ci
299141cc406Sopenharmony_cibool should_calibrate_only_active_area(const Genesys_Device& dev,
300141cc406Sopenharmony_ci                                       const Genesys_Settings& settings);
301141cc406Sopenharmony_ci
302141cc406Sopenharmony_civoid scanner_offset_calibration(Genesys_Device& dev, const Genesys_Sensor& sensor,
303141cc406Sopenharmony_ci                                Genesys_Register_Set& regs);
304141cc406Sopenharmony_ci
305141cc406Sopenharmony_civoid scanner_coarse_gain_calibration(Genesys_Device& dev, const Genesys_Sensor& sensor,
306141cc406Sopenharmony_ci                                     Genesys_Register_Set& regs, unsigned dpi);
307141cc406Sopenharmony_ci
308141cc406Sopenharmony_ciSensorExposure scanner_led_calibration(Genesys_Device& dev, const Genesys_Sensor& sensor,
309141cc406Sopenharmony_ci                                       Genesys_Register_Set& regs);
310141cc406Sopenharmony_ci
311141cc406Sopenharmony_civoid scanner_clear_scan_and_feed_counts(Genesys_Device& dev);
312141cc406Sopenharmony_ci
313141cc406Sopenharmony_civoid scanner_send_slope_table(Genesys_Device* dev, const Genesys_Sensor& sensor, unsigned table_nr,
314141cc406Sopenharmony_ci                              const std::vector<std::uint16_t>& slope_table);
315141cc406Sopenharmony_ci
316141cc406Sopenharmony_ciextern void sanei_genesys_write_file(const char* filename, const std::uint8_t* data,
317141cc406Sopenharmony_ci                                     std::size_t length);
318141cc406Sopenharmony_ci
319141cc406Sopenharmony_civoid wait_until_buffer_non_empty(Genesys_Device* dev, bool check_status_twice = false);
320141cc406Sopenharmony_ci
321141cc406Sopenharmony_civoid sanei_genesys_read_data_from_scanner(Genesys_Device* dev, std::uint8_t* data, size_t size);
322141cc406Sopenharmony_ci
323141cc406Sopenharmony_ciImage read_unshuffled_image_from_scanner(Genesys_Device* dev, const ScanSession& session,
324141cc406Sopenharmony_ci                                         std::size_t total_bytes);
325141cc406Sopenharmony_ci
326141cc406Sopenharmony_civoid regs_set_exposure(AsicType asic_type, Genesys_Register_Set& regs,
327141cc406Sopenharmony_ci                       const SensorExposure& exposure);
328141cc406Sopenharmony_ci
329141cc406Sopenharmony_civoid regs_set_optical_off(AsicType asic_type, Genesys_Register_Set& regs);
330141cc406Sopenharmony_ci
331141cc406Sopenharmony_civoid sanei_genesys_set_dpihw(Genesys_Register_Set& regs, unsigned dpihw);
332141cc406Sopenharmony_ci
333141cc406Sopenharmony_ciinline SensorExposure sanei_genesys_fixup_exposure(SensorExposure exposure)
334141cc406Sopenharmony_ci{
335141cc406Sopenharmony_ci    exposure.red = std::max<std::uint16_t>(1, exposure.red);
336141cc406Sopenharmony_ci    exposure.green = std::max<std::uint16_t>(1, exposure.green);
337141cc406Sopenharmony_ci    exposure.blue = std::max<std::uint16_t>(1, exposure.blue);
338141cc406Sopenharmony_ci    return exposure;
339141cc406Sopenharmony_ci}
340141cc406Sopenharmony_ci
341141cc406Sopenharmony_cibool get_registers_gain4_bit(AsicType asic_type, const Genesys_Register_Set& regs);
342141cc406Sopenharmony_ci
343141cc406Sopenharmony_ciextern void sanei_genesys_wait_for_home(Genesys_Device* dev);
344141cc406Sopenharmony_ci
345141cc406Sopenharmony_ciextern void sanei_genesys_asic_init(Genesys_Device* dev);
346141cc406Sopenharmony_ci
347141cc406Sopenharmony_civoid scanner_start_action(Genesys_Device& dev, bool start_motor);
348141cc406Sopenharmony_civoid scanner_stop_action(Genesys_Device& dev);
349141cc406Sopenharmony_civoid scanner_stop_action_no_move(Genesys_Device& dev, Genesys_Register_Set& regs);
350141cc406Sopenharmony_ci
351141cc406Sopenharmony_cibool scanner_is_motor_stopped(Genesys_Device& dev);
352141cc406Sopenharmony_ci
353141cc406Sopenharmony_civoid scanner_setup_sensor(Genesys_Device& dev, const Genesys_Sensor& sensor,
354141cc406Sopenharmony_ci                          Genesys_Register_Set& regs);
355141cc406Sopenharmony_ci
356141cc406Sopenharmony_ciconst MotorProfile* get_motor_profile_ptr(const std::vector<MotorProfile>& profiles,
357141cc406Sopenharmony_ci                                          unsigned exposure,
358141cc406Sopenharmony_ci                                          const ScanSession& session);
359141cc406Sopenharmony_ci
360141cc406Sopenharmony_ciconst MotorProfile& get_motor_profile(const std::vector<MotorProfile>& profiles,
361141cc406Sopenharmony_ci                                      unsigned exposure,
362141cc406Sopenharmony_ci                                      const ScanSession& session);
363141cc406Sopenharmony_ci
364141cc406Sopenharmony_ciMotorSlopeTable create_slope_table(AsicType asic_type, const Genesys_Motor& motor, unsigned ydpi,
365141cc406Sopenharmony_ci                                   unsigned exposure, unsigned step_multiplier,
366141cc406Sopenharmony_ci                                   const MotorProfile& motor_profile);
367141cc406Sopenharmony_ci
368141cc406Sopenharmony_ciMotorSlopeTable create_slope_table_fastest(AsicType asic_type, unsigned step_multiplier,
369141cc406Sopenharmony_ci                                           const MotorProfile& motor_profile);
370141cc406Sopenharmony_ci
371141cc406Sopenharmony_ci/** @brief find lowest motor resolution for the device.
372141cc406Sopenharmony_ci * Parses the resolution list for motor and
373141cc406Sopenharmony_ci * returns the lowest value.
374141cc406Sopenharmony_ci * @param dev for which to find the lowest motor resolution
375141cc406Sopenharmony_ci * @return the lowest available motor resolution for the device
376141cc406Sopenharmony_ci */
377141cc406Sopenharmony_ciextern
378141cc406Sopenharmony_ciint sanei_genesys_get_lowest_ydpi(Genesys_Device *dev);
379141cc406Sopenharmony_ci
380141cc406Sopenharmony_ci/** @brief find lowest resolution for the device.
381141cc406Sopenharmony_ci * Parses the resolution list for motor and sensor and
382141cc406Sopenharmony_ci * returns the lowest value.
383141cc406Sopenharmony_ci * @param dev for which to find the lowest resolution
384141cc406Sopenharmony_ci * @return the lowest available resolution for the device
385141cc406Sopenharmony_ci */
386141cc406Sopenharmony_ciextern
387141cc406Sopenharmony_ciint sanei_genesys_get_lowest_dpi(Genesys_Device *dev);
388141cc406Sopenharmony_ci
389141cc406Sopenharmony_cibool sanei_genesys_is_compatible_calibration(Genesys_Device* dev,
390141cc406Sopenharmony_ci                                             const ScanSession& session,
391141cc406Sopenharmony_ci                                             const Genesys_Calibration_Cache* cache,
392141cc406Sopenharmony_ci                                             bool for_overwrite);
393141cc406Sopenharmony_ci
394141cc406Sopenharmony_ciextern void sanei_genesys_load_lut(unsigned char* lut,
395141cc406Sopenharmony_ci                                   int in_bits, int out_bits,
396141cc406Sopenharmony_ci                                   int out_min, int out_max,
397141cc406Sopenharmony_ci                                   int slope, int offset);
398141cc406Sopenharmony_ci
399141cc406Sopenharmony_cistd::vector<std::uint8_t> generate_gamma_buffer(Genesys_Device* dev,
400141cc406Sopenharmony_ci                                                const Genesys_Sensor& sensor,
401141cc406Sopenharmony_ci                                                int bits, int max, int size);
402141cc406Sopenharmony_ci
403141cc406Sopenharmony_ciunsigned session_adjust_output_pixels(unsigned output_pixels,
404141cc406Sopenharmony_ci                                      const Genesys_Device& dev, const Genesys_Sensor& sensor,
405141cc406Sopenharmony_ci                                      unsigned output_xresolution, unsigned output_yresolution,
406141cc406Sopenharmony_ci                                      bool adjust_output_pixels);
407141cc406Sopenharmony_ci
408141cc406Sopenharmony_civoid compute_session(const Genesys_Device* dev, ScanSession& s, const Genesys_Sensor& sensor);
409141cc406Sopenharmony_ci
410141cc406Sopenharmony_ciImagePipelineStack build_image_pipeline(const Genesys_Device& dev, const ScanSession& session,
411141cc406Sopenharmony_ci                                        unsigned pipeline_index, bool log_image_data);
412141cc406Sopenharmony_ci
413141cc406Sopenharmony_ci// sets up a image pipeline for device `dev`
414141cc406Sopenharmony_civoid setup_image_pipeline(Genesys_Device& dev, const ScanSession& session);
415141cc406Sopenharmony_ci
416141cc406Sopenharmony_cistd::uint8_t compute_frontend_gain(float value, float target_value,
417141cc406Sopenharmony_ci                                   FrontendType frontend_type);
418141cc406Sopenharmony_ci
419141cc406Sopenharmony_ci/*---------------------------------------------------------------------------*/
420141cc406Sopenharmony_ci/*                ASIC specific functions declarations                       */
421141cc406Sopenharmony_ci/*---------------------------------------------------------------------------*/
422141cc406Sopenharmony_ci
423141cc406Sopenharmony_ciextern StaticInit<std::vector<Genesys_Sensor>> s_sensors;
424141cc406Sopenharmony_ciextern StaticInit<std::vector<Genesys_Frontend>> s_frontends;
425141cc406Sopenharmony_ciextern StaticInit<std::vector<Genesys_Gpo>> s_gpo;
426141cc406Sopenharmony_ciextern StaticInit<std::vector<MemoryLayout>> s_memory_layout;
427141cc406Sopenharmony_ciextern StaticInit<std::vector<Genesys_Motor>> s_motors;
428141cc406Sopenharmony_ciextern StaticInit<std::vector<UsbDeviceEntry>> s_usb_devices;
429141cc406Sopenharmony_ci
430141cc406Sopenharmony_civoid genesys_init_sensor_tables();
431141cc406Sopenharmony_civoid genesys_init_frontend_tables();
432141cc406Sopenharmony_civoid genesys_init_gpo_tables();
433141cc406Sopenharmony_civoid genesys_init_memory_layout_tables();
434141cc406Sopenharmony_civoid genesys_init_motor_tables();
435141cc406Sopenharmony_civoid genesys_init_usb_device_tables();
436141cc406Sopenharmony_civoid verify_sensor_tables();
437141cc406Sopenharmony_civoid verify_usb_device_tables();
438141cc406Sopenharmony_ci
439141cc406Sopenharmony_citemplate<class T>
440141cc406Sopenharmony_civoid debug_dump(unsigned level, const T& value)
441141cc406Sopenharmony_ci{
442141cc406Sopenharmony_ci    std::stringstream out;
443141cc406Sopenharmony_ci    out << value;
444141cc406Sopenharmony_ci    DBG(level, "%s\n", out.str().c_str());
445141cc406Sopenharmony_ci}
446141cc406Sopenharmony_ci
447141cc406Sopenharmony_ci} // namespace genesys
448141cc406Sopenharmony_ci
449141cc406Sopenharmony_ci#endif /* not GENESYS_LOW_H */
450