1141cc406Sopenharmony_ci/* sane - Scanner Access Now Easy.
2141cc406Sopenharmony_ci
3141cc406Sopenharmony_ci   ScanMaker 3840 Backend
4141cc406Sopenharmony_ci   Copyright (C) 2005 Earle F. Philhower, III
5141cc406Sopenharmony_ci   earle@ziplabel.com - http://www.ziplabel.com
6141cc406Sopenharmony_ci
7141cc406Sopenharmony_ci   This program is free software; you can redistribute it and/or
8141cc406Sopenharmony_ci   modify it under the terms of the GNU General Public License as
9141cc406Sopenharmony_ci   published by the Free Software Foundation; either version 2 of the
10141cc406Sopenharmony_ci   License, or (at your option) any later version.
11141cc406Sopenharmony_ci
12141cc406Sopenharmony_ci   This program is distributed in the hope that it will be useful, but
13141cc406Sopenharmony_ci   WITHOUT ANY WARRANTY; without even the implied warranty of
14141cc406Sopenharmony_ci   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15141cc406Sopenharmony_ci   General Public License for more details.
16141cc406Sopenharmony_ci
17141cc406Sopenharmony_ci   You should have received a copy of the GNU General Public License
18141cc406Sopenharmony_ci   along with this program.  If not, see <https://www.gnu.org/licenses/>.
19141cc406Sopenharmony_ci
20141cc406Sopenharmony_ci   As a special exception, the authors of SANE give permission for
21141cc406Sopenharmony_ci   additional uses of the libraries contained in this release of SANE.
22141cc406Sopenharmony_ci
23141cc406Sopenharmony_ci   The exception is that, if you link a SANE library with other files
24141cc406Sopenharmony_ci   to produce an executable, this does not by itself cause the
25141cc406Sopenharmony_ci   resulting executable to be covered by the GNU General Public
26141cc406Sopenharmony_ci   License.  Your use of that executable is in no way restricted on
27141cc406Sopenharmony_ci   account of linking the SANE library code into it.
28141cc406Sopenharmony_ci
29141cc406Sopenharmony_ci   This exception does not, however, invalidate any other reasons why
30141cc406Sopenharmony_ci   the executable file might be covered by the GNU General Public
31141cc406Sopenharmony_ci   License.
32141cc406Sopenharmony_ci
33141cc406Sopenharmony_ci   If you submit changes to SANE to the maintainers to be included in
34141cc406Sopenharmony_ci   a subsequent release, you agree by submitting the changes that
35141cc406Sopenharmony_ci   those changes may be distributed with this exception intact.
36141cc406Sopenharmony_ci
37141cc406Sopenharmony_ci   If you write modifications of your own for SANE, it is your choice
38141cc406Sopenharmony_ci   whether to permit this exception to apply to your modifications.
39141cc406Sopenharmony_ci   If you do not wish that, delete this exception notice.
40141cc406Sopenharmony_ci
41141cc406Sopenharmony_ci*/
42141cc406Sopenharmony_ci
43141cc406Sopenharmony_ci#include <stdio.h>
44141cc406Sopenharmony_ci#include <stdarg.h>
45141cc406Sopenharmony_ci#include "sm3840_lib.h"
46141cc406Sopenharmony_ci
47141cc406Sopenharmony_ci#ifndef BACKENDNAME
48141cc406Sopenharmony_cistatic void setup_scan (p_usb_dev_handle udev, SM3840_Params * p,
49141cc406Sopenharmony_ci			char *stname, int raw, int nohead);
50141cc406Sopenharmony_ci#else
51141cc406Sopenharmony_cistatic void setup_scan (p_usb_dev_handle udev, SM3840_Params * p);
52141cc406Sopenharmony_ci#endif
53141cc406Sopenharmony_ci
54141cc406Sopenharmony_ci
55141cc406Sopenharmony_ci#ifndef BACKENDNAME
56141cc406Sopenharmony_ci
57141cc406Sopenharmony_ci#include "sm3840_lib.c"
58141cc406Sopenharmony_ci
59141cc406Sopenharmony_ciint
60141cc406Sopenharmony_cimain (int argc, char *argv[])
61141cc406Sopenharmony_ci{
62141cc406Sopenharmony_ci  int i;
63141cc406Sopenharmony_ci
64141cc406Sopenharmony_ci  int gray = 0;
65141cc406Sopenharmony_ci  int dpi = 1200;
66141cc406Sopenharmony_ci  int bpp16 = 0;
67141cc406Sopenharmony_ci  int raw = 0;
68141cc406Sopenharmony_ci  int nohead = 0;
69141cc406Sopenharmony_ci  double gain = 3.5;
70141cc406Sopenharmony_ci  int offset = 1800;
71141cc406Sopenharmony_ci  usb_dev_handle *udev;
72141cc406Sopenharmony_ci
73141cc406Sopenharmony_ci  char *stname;
74141cc406Sopenharmony_ci  double topin, botin, leftin, rightin;
75141cc406Sopenharmony_ci  int topline, scanlines, leftpix, scanpix;
76141cc406Sopenharmony_ci
77141cc406Sopenharmony_ci  stname = NULL;
78141cc406Sopenharmony_ci  topin = 0.0;
79141cc406Sopenharmony_ci  botin = 11.7;
80141cc406Sopenharmony_ci  leftin = 0.0;
81141cc406Sopenharmony_ci  rightin = 8.5;
82141cc406Sopenharmony_ci  for (i = 1; i < argc; i++)
83141cc406Sopenharmony_ci    {
84141cc406Sopenharmony_ci      if (!strcmp (argv[i], "-300"))
85141cc406Sopenharmony_ci	dpi = 300;
86141cc406Sopenharmony_ci      else if (!strcmp (argv[i], "-600"))
87141cc406Sopenharmony_ci	dpi = 600;
88141cc406Sopenharmony_ci      else if (!strcmp (argv[i], "-1200"))
89141cc406Sopenharmony_ci	dpi = 1200;
90141cc406Sopenharmony_ci      else if (!strcmp (argv[i], "-150"))
91141cc406Sopenharmony_ci	dpi = 150;
92141cc406Sopenharmony_ci      else if (!strcmp (argv[i], "-top"))
93141cc406Sopenharmony_ci	topin = atof (argv[++i]);
94141cc406Sopenharmony_ci      else if (!strcmp (argv[i], "-bot"))
95141cc406Sopenharmony_ci	botin = atof (argv[++i]);
96141cc406Sopenharmony_ci      else if (!strcmp (argv[i], "-left"))
97141cc406Sopenharmony_ci	leftin = atof (argv[++i]);
98141cc406Sopenharmony_ci      else if (!strcmp (argv[i], "-right"))
99141cc406Sopenharmony_ci	rightin = atof (argv[++i]);
100141cc406Sopenharmony_ci      else if (!strcmp (argv[i], "-gain"))
101141cc406Sopenharmony_ci	gain = atof (argv[++i]);
102141cc406Sopenharmony_ci      else if (!strcmp (argv[i], "-offset"))
103141cc406Sopenharmony_ci	offset = atoi (argv[++i]);
104141cc406Sopenharmony_ci      else if (!strcmp (argv[i], "-gray"))
105141cc406Sopenharmony_ci	gray = 1;
106141cc406Sopenharmony_ci      else if (!strcmp (argv[i], "-16bpp"))
107141cc406Sopenharmony_ci	bpp16 = 1;
108141cc406Sopenharmony_ci      else if (!strcmp (argv[i], "-raw"))
109141cc406Sopenharmony_ci	raw = 1;
110141cc406Sopenharmony_ci      else if (!strcmp (argv[i], "-nohead"))
111141cc406Sopenharmony_ci	nohead = 1;
112141cc406Sopenharmony_ci      else
113141cc406Sopenharmony_ci	stname = argv[i];
114141cc406Sopenharmony_ci    }
115141cc406Sopenharmony_ci
116141cc406Sopenharmony_ci  SM3840_Params params;
117141cc406Sopenharmony_ci  params.gray = gray;
118141cc406Sopenharmony_ci  params.dpi = dpi;
119141cc406Sopenharmony_ci  params.bpp = bpp16 ? 16 : 8;
120141cc406Sopenharmony_ci  params.gain = gain;
121141cc406Sopenharmony_ci  params.offset = offset;
122141cc406Sopenharmony_ci  params.lamp = 15;
123141cc406Sopenharmony_ci  params.top = topin;
124141cc406Sopenharmony_ci  params.left = leftin;
125141cc406Sopenharmony_ci  params.height = botin - topin;
126141cc406Sopenharmony_ci  params.width = rightin - leftin;
127141cc406Sopenharmony_ci
128141cc406Sopenharmony_ci  prepare_params (&params);
129141cc406Sopenharmony_ci  udev = find_device (0x05da, 0x30d4); /* 3840 */
130141cc406Sopenharmony_ci  if (!udev)
131141cc406Sopenharmony_ci    udev = find_device (0x05da, 0x30cf); /* 4800 */
132141cc406Sopenharmony_ci  if (!udev)
133141cc406Sopenharmony_ci    fprintf (stderr, "Unable to open scanner.\n");
134141cc406Sopenharmony_ci  else
135141cc406Sopenharmony_ci    setup_scan (udev, &params, stname, raw, nohead);
136141cc406Sopenharmony_ci
137141cc406Sopenharmony_ci  return 0;
138141cc406Sopenharmony_ci}
139141cc406Sopenharmony_ci#endif
140141cc406Sopenharmony_ci
141141cc406Sopenharmony_ci#ifndef BACKENDNAME
142141cc406Sopenharmony_cistatic void
143141cc406Sopenharmony_cisetup_scan (p_usb_dev_handle udev, SM3840_Params * p,
144141cc406Sopenharmony_ci	    char *stname, int raw, int nohead)
145141cc406Sopenharmony_ci#else
146141cc406Sopenharmony_cistatic void
147141cc406Sopenharmony_cisetup_scan (p_usb_dev_handle udev, SM3840_Params * p)
148141cc406Sopenharmony_ci#endif
149141cc406Sopenharmony_ci{
150141cc406Sopenharmony_ci  int gray = p->gray ? 1 : 0;
151141cc406Sopenharmony_ci  int dpi = p->dpi;
152141cc406Sopenharmony_ci  int bpp16 = (p->bpp == 16) ? 1 : 0;
153141cc406Sopenharmony_ci  double gain = p->gain;
154141cc406Sopenharmony_ci  int offset = p->offset;
155141cc406Sopenharmony_ci  int topline = p->topline;
156141cc406Sopenharmony_ci  int scanlines = p->scanlines;
157141cc406Sopenharmony_ci  int leftpix = p->leftpix;
158141cc406Sopenharmony_ci  int scanpix = p->scanpix;
159141cc406Sopenharmony_ci  unsigned char hello[2] = { 0x55, 0xaa };
160141cc406Sopenharmony_ci  unsigned char howdy[3];
161141cc406Sopenharmony_ci  unsigned short *whitebalance;
162141cc406Sopenharmony_ci  int whitebalancesize = (dpi == 1200) ? 12672 : 6528;
163141cc406Sopenharmony_ci  unsigned short *whitemap;
164141cc406Sopenharmony_ci  int whitemapsize = (dpi == 1200) ? 29282 : 14642;
165141cc406Sopenharmony_ci  unsigned short *whitescan;
166141cc406Sopenharmony_ci  unsigned short *lightmap;
167141cc406Sopenharmony_ci  unsigned int topreg, botreg;
168141cc406Sopenharmony_ci  int redreg, greenreg, bluereg, donered, donegreen, doneblue;
169141cc406Sopenharmony_ci  int rgreg = 0x00;
170141cc406Sopenharmony_ci  int ggreg = 0x00;
171141cc406Sopenharmony_ci  int bgreg = 0x00;
172141cc406Sopenharmony_ci  int i, j;
173141cc406Sopenharmony_ci  int red, green, blue;
174141cc406Sopenharmony_ci  unsigned char rd_byte;
175141cc406Sopenharmony_ci  unsigned short GRAYMASK = 0xc000;
176141cc406Sopenharmony_ci
177141cc406Sopenharmony_ci
178141cc406Sopenharmony_ci#ifndef BACKENDNAME
179141cc406Sopenharmony_ci  char fname[64];
180141cc406Sopenharmony_ci  char head[128];
181141cc406Sopenharmony_ci
182141cc406Sopenharmony_ci  usb_set_configuration (udev, 1);
183141cc406Sopenharmony_ci  usb_claim_interface (udev, 0);
184141cc406Sopenharmony_ci  usb_clear_halt (udev, 1);
185141cc406Sopenharmony_ci  usb_clear_halt (udev, 2);
186141cc406Sopenharmony_ci  usb_clear_halt (udev, 3);
187141cc406Sopenharmony_ci#endif
188141cc406Sopenharmony_ci  DBG (2, "params.gray = %d;\n", p->gray);
189141cc406Sopenharmony_ci  DBG (2, "params.dpi = %d\n", p->dpi);
190141cc406Sopenharmony_ci  DBG (2, "params.bpp = %d\n", p->bpp);
191141cc406Sopenharmony_ci  DBG (2, "params.gain = %f\n", p->gain);
192141cc406Sopenharmony_ci  DBG (2, "params.offset = %d\n", p->offset);
193141cc406Sopenharmony_ci  DBG (2, "params.lamp = %d\n", p->lamp);
194141cc406Sopenharmony_ci  DBG (2, "params.top = %f\n", p->top);
195141cc406Sopenharmony_ci  DBG (2, "params.left = %f\n", p->left);
196141cc406Sopenharmony_ci  DBG (2, "params.height = %f\n", p->height);
197141cc406Sopenharmony_ci  DBG (2, "params.width = %f\n", p->width);
198141cc406Sopenharmony_ci
199141cc406Sopenharmony_ci  DBG (2, "params.topline = %d\n", p->topline);
200141cc406Sopenharmony_ci  DBG (2, "params.scanlines = %d\n", p->scanlines);
201141cc406Sopenharmony_ci  DBG (2, "params.leftpix = %d\n", p->leftpix);
202141cc406Sopenharmony_ci  DBG (2, "params.scanpix = %d\n", p->scanpix);
203141cc406Sopenharmony_ci
204141cc406Sopenharmony_ci  DBG (2, "params.linelen = %d\n", p->linelen);
205141cc406Sopenharmony_ci
206141cc406Sopenharmony_ci  reset_scanner (udev);
207141cc406Sopenharmony_ci
208141cc406Sopenharmony_ci  idle_ab (udev);
209141cc406Sopenharmony_ci  write_regs (udev, 4, 0x83, 0x00, 0xa3, 0x00, 0xa4, 0x00, 0x97, 0x0a);
210141cc406Sopenharmony_ci  write_vctl (udev, 0x0c, 0x0004, 0x008b, 0x00);
211141cc406Sopenharmony_ci  read_vctl (udev, 0x0c, 0x0007, 0x0000, &rd_byte);
212141cc406Sopenharmony_ci  write_regs (udev, 1, 0x97, 0x0b);
213141cc406Sopenharmony_ci  write_vctl (udev, 0x0c, 0x0004, 0x008b, 0x00);
214141cc406Sopenharmony_ci  read_vctl (udev, 0x0c, 0x0007, 0x0000, &rd_byte);
215141cc406Sopenharmony_ci  write_regs (udev, 1, 0x97, 0x0f);
216141cc406Sopenharmony_ci  write_vctl (udev, 0x0c, 0x0004, 0x008b, 0x00);
217141cc406Sopenharmony_ci  read_vctl (udev, 0x0c, 0x0007, 0x0000, &rd_byte);
218141cc406Sopenharmony_ci  write_regs (udev, 1, 0x97, 0x05);
219141cc406Sopenharmony_ci  write_vctl (udev, 0x0b, 0x0004, 0x008b, 0x00);
220141cc406Sopenharmony_ci  read_vctl (udev, 0x0c, 0x0007, 0x0000, &rd_byte);
221141cc406Sopenharmony_ci  write_regs (udev, 7, 0xa8, 0x80, 0x83, 0xa2, 0x85, 0x01, 0x83, 0x82, 0x85,
222141cc406Sopenharmony_ci	      0x00, 0x83, 0x00, 0x93, 0x00);
223141cc406Sopenharmony_ci  write_regs (udev, 1, 0xa8, 0x80);
224141cc406Sopenharmony_ci  write_regs (udev, 4, 0x83, 0x00, 0xa3, 0x00, 0xa4, 0x00, 0x97, 0x0a);
225141cc406Sopenharmony_ci  write_vctl (udev, 0x0c, 0x0004, 0x008b, 0x00);
226141cc406Sopenharmony_ci  read_vctl (udev, 0x0c, 0x0007, 0x0000, &rd_byte);
227141cc406Sopenharmony_ci  write_regs (udev, 1, 0x97, 0x0b);
228141cc406Sopenharmony_ci  write_vctl (udev, 0x0c, 0x0004, 0x008b, 0x00);
229141cc406Sopenharmony_ci  read_vctl (udev, 0x0c, 0x0007, 0x0000, &rd_byte);
230141cc406Sopenharmony_ci  write_regs (udev, 1, 0x97, 0x0f);
231141cc406Sopenharmony_ci  write_vctl (udev, 0x0c, 0x0004, 0x008b, 0x00);
232141cc406Sopenharmony_ci  read_vctl (udev, 0x0c, 0x0007, 0x0000, &rd_byte);
233141cc406Sopenharmony_ci  write_regs (udev, 1, 0x97, 0x05);
234141cc406Sopenharmony_ci  write_vctl (udev, 0x0c, 0x0004, 0x008b, 0x00);
235141cc406Sopenharmony_ci  read_vctl (udev, 0x0c, 0x0007, 0x0000, &rd_byte);
236141cc406Sopenharmony_ci  write_regs (udev, 4, 0x83, 0x20, 0x8d, 0xfe, 0x83, 0x00, 0x8d, 0xff);
237141cc406Sopenharmony_ci  write_regs (udev, 1, 0x97, 0x00);
238141cc406Sopenharmony_ci  write_vctl (udev, 0x0c, 0x0004, 0x008b, 0x00);
239141cc406Sopenharmony_ci  read_vctl (udev, 0x0c, 0x0007, 0x0000, &rd_byte);
240141cc406Sopenharmony_ci  write_regs (udev, 8, 0x80, 0x00, 0x84, 0x00, 0xbe, 0x00, 0xc0, 0x00, 0x86,
241141cc406Sopenharmony_ci	      0x00, 0x89, 0x00, 0x94, 0x00, 0x01, 0x02);
242141cc406Sopenharmony_ci  write_vctl (udev, 0x0c, 0x00c0, 0x8406, 0x00);
243141cc406Sopenharmony_ci  write_vctl (udev, 0x0c, 0x00c0, 0x0406, 0x00);
244141cc406Sopenharmony_ci  write_regs (udev, 1, 0x94, 0x51);
245141cc406Sopenharmony_ci  write_regs (udev, 1, 0xb0, 0x00);
246141cc406Sopenharmony_ci  write_regs (udev, 1, 0xb1, 0x00);
247141cc406Sopenharmony_ci  write_regs (udev, 1, 0xb2, 0x00);
248141cc406Sopenharmony_ci  write_regs (udev, 1, 0xb3, 0x00);
249141cc406Sopenharmony_ci  write_regs (udev, 1, 0xb4, 0x10);
250141cc406Sopenharmony_ci  write_regs (udev, 1, 0xb5, 0x1f);
251141cc406Sopenharmony_ci  write_regs (udev, 1, 0xb0, 0x00);
252141cc406Sopenharmony_ci  write_regs (udev, 1, 0xb1, 0x00);
253141cc406Sopenharmony_ci  write_regs (udev, 1, 0xb2, 0x00);
254141cc406Sopenharmony_ci  write_vctl (udev, 0x0c, 0x0002, 0x0002, 0x00);
255141cc406Sopenharmony_ci  usb_bulk_write (udev, 2, hello, 2, wr_timeout);
256141cc406Sopenharmony_ci  write_regs (udev, 1, 0xb0, 0x00);
257141cc406Sopenharmony_ci  write_regs (udev, 1, 0xb1, 0x00);
258141cc406Sopenharmony_ci  write_regs (udev, 1, 0xb2, 0x00);
259141cc406Sopenharmony_ci  write_vctl (udev, 0x0c, 0x0003, 0x0003, 0x00);
260141cc406Sopenharmony_ci  usb_bulk_read (udev, 1, howdy, 3, rd_timeout);
261141cc406Sopenharmony_ci  write_regs (udev, 4, 0x83, 0x00, 0xa3, 0x00, 0xa4, 0x00, 0x97, 0x0a);
262141cc406Sopenharmony_ci  write_vctl (udev, 0x0c, 0x0004, 0x008b, 0x00);
263141cc406Sopenharmony_ci  read_vctl (udev, 0x0c, 0x0007, 0x0000, &rd_byte);
264141cc406Sopenharmony_ci  write_regs (udev, 1, 0x97, 0x0b);
265141cc406Sopenharmony_ci  write_vctl (udev, 0x0c, 0x0004, 0x008b, 0x00);
266141cc406Sopenharmony_ci  read_vctl (udev, 0x0c, 0x0007, 0x0000, &rd_byte);
267141cc406Sopenharmony_ci  write_regs (udev, 1, 0x97, 0x0f);
268141cc406Sopenharmony_ci  write_vctl (udev, 0x0c, 0x0004, 0x008b, 0x00);
269141cc406Sopenharmony_ci  read_vctl (udev, 0x0c, 0x0007, 0x0000, &rd_byte);
270141cc406Sopenharmony_ci  write_regs (udev, 1, 0x97, 0x05);
271141cc406Sopenharmony_ci  write_vctl (udev, 0x0c, 0x0004, 0x008b, 0x00);
272141cc406Sopenharmony_ci  read_vctl (udev, 0x0c, 0x0007, 0x0000, &rd_byte);
273141cc406Sopenharmony_ci  write_regs (udev, 4, 0x83, 0x20, 0x8d, 0xfe, 0x83, 0x00, 0x8d, 0xff);
274141cc406Sopenharmony_ci  write_regs (udev, 1, 0x97, 0x00);
275141cc406Sopenharmony_ci  write_vctl (udev, 0x0c, 0x0004, 0x008b, 0x00);
276141cc406Sopenharmony_ci  read_vctl (udev, 0x0c, 0x0007, 0x0000, &rd_byte);
277141cc406Sopenharmony_ci  write_regs (udev, 8, 0x80, 0x00, 0x84, 0x00, 0xbe, 0x00, 0xc0, 0x00, 0x86,
278141cc406Sopenharmony_ci	      0x00, 0x89, 0x00, 0x94, 0x00, 0x01, 0x02);
279141cc406Sopenharmony_ci  write_vctl (udev, 0x0c, 0x00c0, 0x8406, 0x00);
280141cc406Sopenharmony_ci  write_vctl (udev, 0x0c, 0x00c0, 0x0406, 0x00);
281141cc406Sopenharmony_ci  write_regs (udev, 16, 0xbe, 0x18, 0x80, 0x00, 0x84, 0x00, 0x89, 0x00, 0x88,
282141cc406Sopenharmony_ci	      0x00, 0x86, 0x00, 0x90, 0x00, 0xc1, 0x00, 0xc2, 0x00, 0xc3,
283141cc406Sopenharmony_ci	      0x00, 0xc4, 0x00, 0xc5, 0x00, 0xc6, 0x00, 0xc7, 0x00, 0xc8,
284141cc406Sopenharmony_ci	      0x00, 0xc0, 0x00);
285141cc406Sopenharmony_ci  write_regs (udev, 4, 0x83, 0x20, 0x8d, 0xff, 0x83, 0x00, 0x8d, 0xff);
286141cc406Sopenharmony_ci  write_regs (udev, 5, 0x83, 0x00, 0xa3, 0xff, 0xa4, 0xff, 0xa1, 0xff, 0xa2,
287141cc406Sopenharmony_ci	      0xf7);
288141cc406Sopenharmony_ci  write_regs (udev, 4, 0x83, 0x22, 0x87, 0x01, 0x83, 0x02, 0x87, 0x16);
289141cc406Sopenharmony_ci  write_regs (udev, 11, 0xa0, 0x00, 0x9c, 0x00, 0x9f, 0x00, 0x9d, 0x00, 0x9e,
290141cc406Sopenharmony_ci	      0x00, 0xa0, 0x00, 0xce, 0x0c, 0x83, 0x20, 0xa5, 0x00, 0xa6,
291141cc406Sopenharmony_ci	      0x00, 0xa7, 0x00);
292141cc406Sopenharmony_ci
293141cc406Sopenharmony_ci  set_gain_black (udev, 0x01, 0x01, 0x01, 0xaa, 0xaa, 0xaa);
294141cc406Sopenharmony_ci
295141cc406Sopenharmony_ci  write_regs (udev, 16, 0x9b, 0x00, 0x98, 0x00, 0x99, 0x00, 0x9a, 0x00, 0x9b,
296141cc406Sopenharmony_ci	      0x01, 0x98, 0x00, 0x99, 0x00, 0x9a, 0x00, 0x9b, 0x02, 0x98,
297141cc406Sopenharmony_ci	      0x00, 0x99, 0x00, 0x9a, 0x00, 0x9b, 0x03, 0x98, 0x00, 0x99,
298141cc406Sopenharmony_ci	      0x00, 0x9a, 0x00);
299141cc406Sopenharmony_ci  write_regs (udev, 4, 0x83, 0xa2, 0x85, 0x98, 0x83, 0x82, 0x85, 0x3a);
300141cc406Sopenharmony_ci  write_regs (udev, 1, 0x9d, 0x80);
301141cc406Sopenharmony_ci  write_regs (udev, 1, 0x9d, 0x00);
302141cc406Sopenharmony_ci
303141cc406Sopenharmony_ci  if (dpi == 1200)
304141cc406Sopenharmony_ci    write_regs (udev, 1, 0x94, 0x51);
305141cc406Sopenharmony_ci  else
306141cc406Sopenharmony_ci    write_regs (udev, 1, 0x94, 0x61);
307141cc406Sopenharmony_ci
308141cc406Sopenharmony_ci  whitemap = (unsigned short *) malloc (whitemapsize);
309141cc406Sopenharmony_ci
310141cc406Sopenharmony_ci  set_lightmap_white (whitemap, dpi, 0);
311141cc406Sopenharmony_ci  if (dpi == 1200)
312141cc406Sopenharmony_ci    write_regs (udev, 6, 0xb0, 0x00, 0xb1, 0x80, 0xb2, 0x06, 0xb3, 0xff, 0xb4,
313141cc406Sopenharmony_ci		0xff, 0xb5, 0x06);
314141cc406Sopenharmony_ci  else
315141cc406Sopenharmony_ci    write_regs (udev, 6, 0xb0, 0x00, 0xb1, 0x40, 0xb2, 0x07, 0xb3, 0xff, 0xb4,
316141cc406Sopenharmony_ci		0x7f, 0xb5, 0x07);
317141cc406Sopenharmony_ci  write_vctl (udev, 0x0c, 0x0002, whitemapsize, 0x00);
318141cc406Sopenharmony_ci  usb_bulk_write (udev, 2, (unsigned char *) whitemap, whitemapsize,
319141cc406Sopenharmony_ci                  wr_timeout);
320141cc406Sopenharmony_ci
321141cc406Sopenharmony_ci  set_lightmap_white (whitemap, dpi, 1);
322141cc406Sopenharmony_ci  if (dpi == 1200)
323141cc406Sopenharmony_ci    write_regs (udev, 6, 0xb0, 0x00, 0xb1, 0x00, 0xb2, 0x07, 0xb3, 0xff, 0xb4,
324141cc406Sopenharmony_ci		0x7f, 0xb5, 0x07);
325141cc406Sopenharmony_ci  else
326141cc406Sopenharmony_ci    write_regs (udev, 6, 0xb0, 0x00, 0xb1, 0x80, 0xb2, 0x07, 0xb3, 0xff, 0xb4,
327141cc406Sopenharmony_ci		0xbf, 0xb5, 0x07);
328141cc406Sopenharmony_ci  write_vctl (udev, 0x0c, 0x0002, whitemapsize, 0x00);
329141cc406Sopenharmony_ci  usb_bulk_write (udev, 2, (unsigned char *) whitemap, whitemapsize,
330141cc406Sopenharmony_ci                  wr_timeout);
331141cc406Sopenharmony_ci
332141cc406Sopenharmony_ci  set_lightmap_white (whitemap, dpi, 2);
333141cc406Sopenharmony_ci  if (dpi == 1200)
334141cc406Sopenharmony_ci    write_regs (udev, 6, 0xb0, 0x00, 0xb1, 0x80, 0xb2, 0x07, 0xb3, 0xff, 0xb4,
335141cc406Sopenharmony_ci		0xff, 0xb5, 0x07);
336141cc406Sopenharmony_ci  else
337141cc406Sopenharmony_ci    write_regs (udev, 6, 0xb0, 0x00, 0xb1, 0xc0, 0xb2, 0x07, 0xb3, 0xff, 0xb4,
338141cc406Sopenharmony_ci		0xff, 0xb5, 0x07);
339141cc406Sopenharmony_ci  write_vctl (udev, 0x0c, 0x0002, whitemapsize, 0x00);
340141cc406Sopenharmony_ci  usb_bulk_write (udev, 2, (unsigned char *) whitemap, whitemapsize,
341141cc406Sopenharmony_ci                  wr_timeout);
342141cc406Sopenharmony_ci
343141cc406Sopenharmony_ci  free (whitemap);
344141cc406Sopenharmony_ci
345141cc406Sopenharmony_ci  /* Move to head... */
346141cc406Sopenharmony_ci  idle_ab (udev);
347141cc406Sopenharmony_ci  write_regs (udev, 1, 0x97, 0x00);
348141cc406Sopenharmony_ci  idle_ab (udev);
349141cc406Sopenharmony_ci  write_regs (udev, 16, 0xbe, 0x18, 0x80, 0x00, 0x84, 0x00, 0x89, 0x00, 0x88,
350141cc406Sopenharmony_ci	      0x00, 0x86, 0x00, 0x90, 0x00, 0xc1, 0x00, 0xc2, 0x00, 0xc3,
351141cc406Sopenharmony_ci	      0x00, 0xc4, 0x00, 0xc5, 0x00, 0xc6, 0x00, 0xc7, 0x00, 0xc8,
352141cc406Sopenharmony_ci	      0x00, 0xc0, 0x00);
353141cc406Sopenharmony_ci  idle_ab (udev);
354141cc406Sopenharmony_ci  write_regs (udev, 16, 0x84, 0x94, 0x80, 0xd1, 0x80, 0xc1, 0x82, 0x7f, 0xcf,
355141cc406Sopenharmony_ci	      0x04, 0xc1, 0x02, 0xc2, 0x00, 0xc3, 0x06, 0xc4, 0xff, 0xc5,
356141cc406Sopenharmony_ci	      0x40, 0xc6, 0x8c, 0xc7, 0xdc, 0xc8, 0x20, 0xc0, 0x72, 0x89,
357141cc406Sopenharmony_ci	      0xff, 0x86, 0xff);
358141cc406Sopenharmony_ci  poll1 (udev);
359141cc406Sopenharmony_ci
360141cc406Sopenharmony_ci  write_regs (udev, 4, 0x83, 0xa2, 0x85, 0x01, 0x83, 0x82, 0x85, 0x00);
361141cc406Sopenharmony_ci  write_regs (udev, 1, 0x9d, 0x80);
362141cc406Sopenharmony_ci  write_regs (udev, 1, 0x9d, 0x00);
363141cc406Sopenharmony_ci  if (dpi == 1200)
364141cc406Sopenharmony_ci    write_regs (udev, 10, 0xb0, 0x00, 0xb1, 0x00, 0xb2, 0x00, 0xb3, 0xff,
365141cc406Sopenharmony_ci		0xb4, 0xff, 0xb5, 0x03, 0xb6, 0x01, 0xb7, 0x00, 0xb8, 0x77,
366141cc406Sopenharmony_ci		0xb9, 0x1e);
367141cc406Sopenharmony_ci  else
368141cc406Sopenharmony_ci    write_regs (udev, 10, 0xb0, 0x00, 0xb1, 0x00, 0xb2, 0x00, 0xb3, 0xff,
369141cc406Sopenharmony_ci		0xb4, 0xff, 0xb5, 0x03, 0xb6, 0x01, 0xb7, 0x00, 0xb8, 0x3b,
370141cc406Sopenharmony_ci		0xb9, 0x1f);
371141cc406Sopenharmony_ci  write_regs (udev, 5, 0xc0, 0x00, 0x84, 0x00, 0x80, 0xa1, 0xcf, 0x04, 0x82,
372141cc406Sopenharmony_ci	      0x00);
373141cc406Sopenharmony_ci  if (dpi == 1200)
374141cc406Sopenharmony_ci    write_regs (udev, 18, 0x83, 0xa2, 0x85, 0x02, 0x83, 0x82, 0x85, 0x00,
375141cc406Sopenharmony_ci		0xbc, 0x01, 0xbd, 0x01, 0x88, 0xa4, 0xc1, 0x02, 0xc2, 0x00,
376141cc406Sopenharmony_ci		0xc3, 0x02, 0xc4, 0x01, 0xc5, 0x01, 0xc6, 0xa3, 0xc7, 0xa4,
377141cc406Sopenharmony_ci		0xc8, 0x04, 0xc0, 0xd2, 0x89, 0x05, 0x86, 0x00);
378141cc406Sopenharmony_ci  else
379141cc406Sopenharmony_ci    write_regs (udev, 18, 0x83, 0xa2, 0x85, 0x04, 0x83, 0x82, 0x85, 0x00,
380141cc406Sopenharmony_ci		0xbc, 0x01, 0xbd, 0x01, 0x88, 0xd0, 0xc1, 0x01, 0xc2, 0x00,
381141cc406Sopenharmony_ci		0xc3, 0x04, 0xc4, 0x01, 0xc5, 0x01, 0xc6, 0xcf, 0xc7, 0xd0,
382141cc406Sopenharmony_ci		0xc8, 0x14, 0xc0, 0xd1, 0x89, 0x0a, 0x86, 0x00);
383141cc406Sopenharmony_ci  write_regs (udev, 8, 0xbb, 0x01, 0x9b, 0x24, 0x8b, 0x00, 0x8e, 0x80, 0xbf,
384141cc406Sopenharmony_ci	      0x00, 0x90, 0x40, 0x91, 0x00, 0x83, 0x82);
385141cc406Sopenharmony_ci  write_regs (udev, 1, 0xbe, 0x0d);
386141cc406Sopenharmony_ci  write_vctl (udev, 0x0c, 0x0003, 0x0001, 0x00);
387141cc406Sopenharmony_ci  whitebalance = (unsigned short *) malloc (whitebalancesize);
388141cc406Sopenharmony_ci  usb_bulk_read (udev, 1, &rd_byte, 1, rd_timeout);
389141cc406Sopenharmony_ci  write_vctl (udev, 0x0c, 0x0001, 0x0000, 0x00);
390141cc406Sopenharmony_ci  usb_bulk_read (udev, 1, (unsigned char *) whitebalance, whitebalancesize,
391141cc406Sopenharmony_ci                 rd_timeout);
392141cc406Sopenharmony_ci  write_regs (udev, 2, 0xbe, 0x00, 0x84, 0x00);
393141cc406Sopenharmony_ci  write_vctl (udev, 0x0c, 0x00c0, 0x8406, 0x00);
394141cc406Sopenharmony_ci  write_vctl (udev, 0x0c, 0x00c0, 0x0406, 0x00);
395141cc406Sopenharmony_ci  redreg = greenreg = bluereg = 0x80;
396141cc406Sopenharmony_ci  red = green = blue = 0;
397141cc406Sopenharmony_ci  donered = donegreen = doneblue = 0;
398141cc406Sopenharmony_ci  DBG (2, "setting blackpoint\n");
399141cc406Sopenharmony_ci  for (j = 0; (j < 16) && !(donered && donegreen && doneblue); j++)
400141cc406Sopenharmony_ci    {
401141cc406Sopenharmony_ci      set_gain_black (udev, 0x01, 0x01, 0x01, redreg, greenreg, bluereg);
402141cc406Sopenharmony_ci
403141cc406Sopenharmony_ci      if (dpi == 1200)
404141cc406Sopenharmony_ci	write_regs (udev, 10, 0xb0, 0x00, 0xb1, 0x00, 0xb2, 0x00, 0xb3, 0xff,
405141cc406Sopenharmony_ci		    0xb4, 0xff, 0xb5, 0x03, 0xb6, 0x01, 0xb7, 0x00, 0xb8,
406141cc406Sopenharmony_ci		    0x77, 0xb9, 0x1e);
407141cc406Sopenharmony_ci      else
408141cc406Sopenharmony_ci	write_regs (udev, 10, 0xb0, 0x00, 0xb1, 0x00, 0xb2, 0x00, 0xb3, 0xff,
409141cc406Sopenharmony_ci		    0xb4, 0xff, 0xb5, 0x03, 0xb6, 0x01, 0xb7, 0x00, 0xb8,
410141cc406Sopenharmony_ci		    0x3b, 0xb9, 0x1f);
411141cc406Sopenharmony_ci      write_regs (udev, 8, 0xbb, 0x01, 0x9b, 0x24, 0x8b, 0x00, 0x8e, 0x80,
412141cc406Sopenharmony_ci		  0xbf, 0x00, 0x90, 0x40, 0x91, 0x00, 0x83, 0x82);
413141cc406Sopenharmony_ci      write_regs (udev, 1, 0xbe, 0x0d);
414141cc406Sopenharmony_ci      write_vctl (udev, 0x0c, 0x0003, 0x0001, 0x00);
415141cc406Sopenharmony_ci      usb_bulk_read (udev, 1, &rd_byte, 1, rd_timeout);
416141cc406Sopenharmony_ci      write_vctl (udev, 0x0c, 0x0001, 0x0000, 0x00);
417141cc406Sopenharmony_ci      usb_bulk_read (udev, 1, (unsigned char *) whitebalance,
418141cc406Sopenharmony_ci                     whitebalancesize, rd_timeout);
419141cc406Sopenharmony_ci      fix_endian_short (whitebalance, whitebalancesize/2);
420141cc406Sopenharmony_ci      if (!donered)
421141cc406Sopenharmony_ci	{
422141cc406Sopenharmony_ci	  red = (whitebalance[0] + whitebalance[3] + whitebalance[6]) / 3;
423141cc406Sopenharmony_ci	  if (red > 0x1000)
424141cc406Sopenharmony_ci	    redreg += 0x10;
425141cc406Sopenharmony_ci	  else if (red > 0x500)
426141cc406Sopenharmony_ci	    redreg += 0x08;
427141cc406Sopenharmony_ci	  else if (red > 0x0010)
428141cc406Sopenharmony_ci	    redreg++;
429141cc406Sopenharmony_ci	  else
430141cc406Sopenharmony_ci	    donered = 1;
431141cc406Sopenharmony_ci	}
432141cc406Sopenharmony_ci      if (!donegreen)
433141cc406Sopenharmony_ci	{
434141cc406Sopenharmony_ci	  green = (whitebalance[1] + whitebalance[4] + whitebalance[7]) / 3;
435141cc406Sopenharmony_ci	  if (green > 0x1000)
436141cc406Sopenharmony_ci	    greenreg += 0x10;
437141cc406Sopenharmony_ci	  else if (green > 0x0500)
438141cc406Sopenharmony_ci	    greenreg += 0x08;
439141cc406Sopenharmony_ci	  else if (green > 0x0010)
440141cc406Sopenharmony_ci	    greenreg++;
441141cc406Sopenharmony_ci	  else
442141cc406Sopenharmony_ci	    donegreen = 1;
443141cc406Sopenharmony_ci	}
444141cc406Sopenharmony_ci      if (!doneblue)
445141cc406Sopenharmony_ci	{
446141cc406Sopenharmony_ci	  blue = (whitebalance[2] + whitebalance[5] + whitebalance[8]) / 3;
447141cc406Sopenharmony_ci	  if (blue > 0x1000)
448141cc406Sopenharmony_ci	    bluereg += 0x10;
449141cc406Sopenharmony_ci	  else if (blue > 0x0500)
450141cc406Sopenharmony_ci	    bluereg += 0x08;
451141cc406Sopenharmony_ci	  else if (blue > 0x0010)
452141cc406Sopenharmony_ci	    bluereg++;
453141cc406Sopenharmony_ci	  else
454141cc406Sopenharmony_ci	    doneblue = 1;
455141cc406Sopenharmony_ci	}
456141cc406Sopenharmony_ci      DBG (2, "red=%d(%d)%02x, green=%d(%d)%02x, blue=%d(%d)%02x\n",
457141cc406Sopenharmony_ci	   red, donered, redreg, green, donegreen, greenreg,
458141cc406Sopenharmony_ci	   blue, doneblue, bluereg);
459141cc406Sopenharmony_ci      write_regs (udev, 2, 0xbe, 0x00, 0x84, 0x00);
460141cc406Sopenharmony_ci      write_vctl (udev, 0x0c, 0x00c0, 0x8406, 0x00);
461141cc406Sopenharmony_ci      write_vctl (udev, 0x0c, 0x00c0, 0x0406, 0x00);
462141cc406Sopenharmony_ci    }
463141cc406Sopenharmony_ci  DBG (2, "setting whitepoint\n");
464141cc406Sopenharmony_ci  donegreen = donered = doneblue = 0;
465141cc406Sopenharmony_ci  for (j = 0; (j < 16) && !(donered && donegreen && doneblue); j++)
466141cc406Sopenharmony_ci    {
467141cc406Sopenharmony_ci      set_gain_black (udev, rgreg, ggreg, bgreg, redreg, greenreg, bluereg);
468141cc406Sopenharmony_ci
469141cc406Sopenharmony_ci      if (dpi == 1200)
470141cc406Sopenharmony_ci	idle_ab (udev);
471141cc406Sopenharmony_ci      write_regs (udev, 10, 0xb0, 0x00, 0xb1, 0x00, 0xb2, 0x00, 0xb3, 0xff,
472141cc406Sopenharmony_ci		  0xb4, 0xff, 0xb5, 0x03, 0xb6, 0x01, 0xb7, 0x00, 0xb8, 0x3b,
473141cc406Sopenharmony_ci		  0xb9, 0x1f);
474141cc406Sopenharmony_ci      if (dpi == 1200)
475141cc406Sopenharmony_ci	idle_ab (udev);
476141cc406Sopenharmony_ci      write_regs (udev, 8, 0xbb, 0x01, 0x9b, 0x24, 0x8b, 0x00, 0x8e, 0x80,
477141cc406Sopenharmony_ci		  0xbf, 0x00, 0x90, 0x40, 0x91, 0x00, 0x83, 0x82);
478141cc406Sopenharmony_ci      write_regs (udev, 1, 0xbe, 0x0d);
479141cc406Sopenharmony_ci      write_vctl (udev, 0x0c, 0x0003, 0x0001, 0x00);
480141cc406Sopenharmony_ci      usb_bulk_read (udev, 1, &rd_byte, 1, rd_timeout);
481141cc406Sopenharmony_ci      write_vctl (udev, 0x0c, 0x0001, 0x0000, 0x00);
482141cc406Sopenharmony_ci      usb_bulk_read (udev, 1, (unsigned char *) whitebalance,
483141cc406Sopenharmony_ci                     whitebalancesize, rd_timeout);
484141cc406Sopenharmony_ci      fix_endian_short (whitebalance, whitebalancesize/2);
485141cc406Sopenharmony_ci      if (!donered)
486141cc406Sopenharmony_ci	{
487141cc406Sopenharmony_ci	  red =
488141cc406Sopenharmony_ci	    (whitebalance[180 * 3 + 0] + whitebalance[180 * 3 + 3] +
489141cc406Sopenharmony_ci	     whitebalance[180 * 3 + 6]) / 3;
490141cc406Sopenharmony_ci	  if (red < 0x5000)
491141cc406Sopenharmony_ci	    rgreg += 0x02;
492141cc406Sopenharmony_ci	  else if (red < 0x8000)
493141cc406Sopenharmony_ci	    rgreg += 0x01;
494141cc406Sopenharmony_ci	  else
495141cc406Sopenharmony_ci	    donered = 1;
496141cc406Sopenharmony_ci	}
497141cc406Sopenharmony_ci      if (!donegreen)
498141cc406Sopenharmony_ci	{
499141cc406Sopenharmony_ci	  green =
500141cc406Sopenharmony_ci	    (whitebalance[180 * 3 + 1] + whitebalance[180 * 3 + 4] +
501141cc406Sopenharmony_ci	     whitebalance[180 * 3 + 7]) / 3;
502141cc406Sopenharmony_ci	  if (green < 0x5000)
503141cc406Sopenharmony_ci	    ggreg += 0x02;
504141cc406Sopenharmony_ci	  else if (green < 0x8000)
505141cc406Sopenharmony_ci	    ggreg += 0x01;
506141cc406Sopenharmony_ci	  else
507141cc406Sopenharmony_ci	    donegreen = 1;
508141cc406Sopenharmony_ci	}
509141cc406Sopenharmony_ci      if (!doneblue)
510141cc406Sopenharmony_ci	{
511141cc406Sopenharmony_ci	  blue =
512141cc406Sopenharmony_ci	    (whitebalance[180 * 3 + 2] + whitebalance[180 * 3 + 5] +
513141cc406Sopenharmony_ci	     whitebalance[180 * 3 + 8]) / 3;
514141cc406Sopenharmony_ci	  if (blue < 0x5000)
515141cc406Sopenharmony_ci	    bgreg += 0x02;
516141cc406Sopenharmony_ci	  else if (blue < 0x8000)
517141cc406Sopenharmony_ci	    bgreg += 0x01;
518141cc406Sopenharmony_ci	  else
519141cc406Sopenharmony_ci	    doneblue = 1;
520141cc406Sopenharmony_ci	}
521141cc406Sopenharmony_ci      DBG (2, "red=%d(%d)%02x, green=%d(%d)%02x, blue=%d(%d)%02x\n",
522141cc406Sopenharmony_ci	   red, donered, rgreg, green, donegreen, ggreg, blue, doneblue,
523141cc406Sopenharmony_ci	   bgreg);
524141cc406Sopenharmony_ci      write_regs (udev, 2, 0xbe, 0x00, 0x84, 0x00);
525141cc406Sopenharmony_ci      write_vctl (udev, 0x0c, 0x00c0, 0x8406, 0x00);
526141cc406Sopenharmony_ci      write_vctl (udev, 0x0c, 0x00c0, 0x0406, 0x00);
527141cc406Sopenharmony_ci    }
528141cc406Sopenharmony_ci  free (whitebalance);
529141cc406Sopenharmony_ci
530141cc406Sopenharmony_ci  /* One step down for optimal contrast... */
531141cc406Sopenharmony_ci  if (rgreg)
532141cc406Sopenharmony_ci    rgreg--;
533141cc406Sopenharmony_ci  if (bgreg)
534141cc406Sopenharmony_ci    bgreg--;
535141cc406Sopenharmony_ci  if (ggreg)
536141cc406Sopenharmony_ci    ggreg--;
537141cc406Sopenharmony_ci
538141cc406Sopenharmony_ci
539141cc406Sopenharmony_ci  write_regs (udev, 8, 0x80, 0x00, 0x84, 0x00, 0xbe, 0x00, 0xc0, 0x00, 0x86,
540141cc406Sopenharmony_ci	      0x00, 0x89, 0x00, 0x94, 0x00, 0x01, 0x02);
541141cc406Sopenharmony_ci  write_vctl (udev, 0x0c, 0x00c0, 0x8406, 0x00);
542141cc406Sopenharmony_ci  write_vctl (udev, 0x0c, 0x00c0, 0x0406, 0x00);
543141cc406Sopenharmony_ci  write_regs (udev, 16, 0xbe, 0x18, 0x80, 0x00, 0x84, 0x00, 0x89, 0x00, 0x88,
544141cc406Sopenharmony_ci	      0x00, 0x86, 0x00, 0x90, 0x00, 0xc1, 0x00, 0xc2, 0x00, 0xc3,
545141cc406Sopenharmony_ci	      0x00, 0xc4, 0x00, 0xc5, 0x00, 0xc6, 0x00, 0xc7, 0x00, 0xc8,
546141cc406Sopenharmony_ci	      0x00, 0xc0, 0x00);
547141cc406Sopenharmony_ci  write_regs (udev, 4, 0x83, 0x20, 0x8d, 0xff, 0x83, 0x00, 0x8d, 0xff);
548141cc406Sopenharmony_ci  write_regs (udev, 5, 0x83, 0x00, 0xa3, 0xff, 0xa4, 0xff, 0xa1, 0xff, 0xa2,
549141cc406Sopenharmony_ci	      0xf7);
550141cc406Sopenharmony_ci  write_regs (udev, 4, 0x83, 0x22, 0x87, 0x01, 0x83, 0x02, 0x87, 0x16);
551141cc406Sopenharmony_ci  write_regs (udev, 11, 0xa0, 0x00, 0x9c, 0x00, 0x9f, 0x00, 0x9d, 0x00, 0x9e,
552141cc406Sopenharmony_ci	      0x00, 0xa0, 0x00, 0xce, 0x0c, 0x83, 0x20, 0xa5, 0x00, 0xa6,
553141cc406Sopenharmony_ci	      0x00, 0xa7, 0x00);
554141cc406Sopenharmony_ci  set_gain_black (udev, rgreg, ggreg, bgreg, redreg, greenreg, bluereg);
555141cc406Sopenharmony_ci
556141cc406Sopenharmony_ci  write_regs (udev, 16, 0x9b, 0x00, 0x98, 0x00, 0x99, 0x00, 0x9a, 0x00, 0x9b,
557141cc406Sopenharmony_ci	      0x01, 0x98, 0x00, 0x99, 0x00, 0x9a, 0x00, 0x9b, 0x02, 0x98,
558141cc406Sopenharmony_ci	      0x00, 0x99, 0x00, 0x9a, 0x00, 0x9b, 0x03, 0x98, 0x00, 0x99,
559141cc406Sopenharmony_ci	      0x00, 0x9a, 0x00);
560141cc406Sopenharmony_ci  write_regs (udev, 4, 0x83, 0xa2, 0x85, 0x98, 0x83, 0x82, 0x85, 0x3a);
561141cc406Sopenharmony_ci  write_regs (udev, 1, 0x9d, 0x80);
562141cc406Sopenharmony_ci  write_regs (udev, 1, 0x9d, 0x00);
563141cc406Sopenharmony_ci  if (dpi == 1200)
564141cc406Sopenharmony_ci    write_regs (udev, 1, 0x94, 0x71);
565141cc406Sopenharmony_ci  else
566141cc406Sopenharmony_ci    write_regs (udev, 1, 0x94, 0x61);
567141cc406Sopenharmony_ci
568141cc406Sopenharmony_ci  write_regs (udev, 4, 0x83, 0xa2, 0x85, 0x01, 0x83, 0x82, 0x85, 0x00);
569141cc406Sopenharmony_ci  write_regs (udev, 1, 0x9d, 0x80);
570141cc406Sopenharmony_ci  write_regs (udev, 1, 0x9d, 0x00);
571141cc406Sopenharmony_ci  if (dpi == 1200)
572141cc406Sopenharmony_ci    write_regs (udev, 10, 0xb0, 0x00, 0xb1, 0x00, 0xb2, 0x00, 0xb3, 0xff,
573141cc406Sopenharmony_ci		0xb4, 0xff, 0xb5, 0x03, 0xb6, 0x01, 0xb7, 0x00, 0xb8, 0xbf,
574141cc406Sopenharmony_ci		0xb9, 0x17);
575141cc406Sopenharmony_ci  else
576141cc406Sopenharmony_ci    write_regs (udev, 10, 0xb0, 0x00, 0xb1, 0x00, 0xb2, 0x00, 0xb3, 0xff,
577141cc406Sopenharmony_ci		0xb4, 0xff, 0xb5, 0x03, 0xb6, 0x01, 0xb7, 0x00, 0xb8, 0xdf,
578141cc406Sopenharmony_ci		0xb9, 0x1b);
579141cc406Sopenharmony_ci  write_regs (udev, 6, 0xc0, 0x00, 0x84, 0x00, 0x84, 0xb4, 0x80, 0xe1, 0xcf,
580141cc406Sopenharmony_ci	      0x04, 0x82, 0x00);
581141cc406Sopenharmony_ci  if (dpi == 1200)
582141cc406Sopenharmony_ci    write_regs (udev, 18, 0x83, 0xa2, 0x85, 0x02, 0x83, 0x82, 0x85, 0x00,
583141cc406Sopenharmony_ci		0xbc, 0x20, 0xbd, 0x08, 0x88, 0xa4, 0xc1, 0x02, 0xc2, 0x00,
584141cc406Sopenharmony_ci		0xc3, 0x02, 0xc4, 0x20, 0xc5, 0x08, 0xc6, 0x96, 0xc7, 0xa4,
585141cc406Sopenharmony_ci		0xc8, 0x06, 0xc0, 0xd2, 0x89, 0x24, 0x86, 0x01);
586141cc406Sopenharmony_ci  else
587141cc406Sopenharmony_ci    write_regs (udev, 18, 0x83, 0xa2, 0x85, 0x04, 0x83, 0x82, 0x85, 0x00,
588141cc406Sopenharmony_ci		0xbc, 0x20, 0xbd, 0x10, 0x88, 0xd0, 0xc1, 0x01, 0xc2, 0x00,
589141cc406Sopenharmony_ci		0xc3, 0x04, 0xc4, 0x20, 0xc5, 0x10, 0xc6, 0xc3, 0xc7, 0xd0,
590141cc406Sopenharmony_ci		0xc8, 0x1c, 0xc0, 0xd1, 0x89, 0x24, 0x86, 0x01);
591141cc406Sopenharmony_ci  write_regs (udev, 8, 0xbb, 0x05, 0x9b, 0x24, 0x8b, 0x00, 0x8e, 0x80, 0xbf,
592141cc406Sopenharmony_ci	      0x00, 0x90, 0x40, 0x91, 0x00, 0x83, 0x82);
593141cc406Sopenharmony_ci  write_regs (udev, 1, 0xbe, 0x1d);
594141cc406Sopenharmony_ci  write_vctl (udev, 0x0c, 0x0003, 0x0001, 0x00);
595141cc406Sopenharmony_ci  usb_bulk_read (udev, 1, &rd_byte, 1, rd_timeout);
596141cc406Sopenharmony_ci  write_vctl (udev, 0x0c, 0x0001, 0x0000, 0x00);
597141cc406Sopenharmony_ci  record_mem (udev, (unsigned char **) (void *)&whitescan,
598141cc406Sopenharmony_ci	      (5632 * 2 * 3 * (dpi == 1200 ? 2 : 1)) * 4);
599141cc406Sopenharmony_ci  fix_endian_short (whitescan, (5632 * 2 * 3 * (dpi == 1200 ? 2 : 1)) * 2);
600141cc406Sopenharmony_ci  write_regs (udev, 5, 0x83, 0x00, 0xa3, 0xff, 0xa4, 0xff, 0xa1, 0xff, 0xa2,
601141cc406Sopenharmony_ci	      0xff);
602141cc406Sopenharmony_ci  write_vctl (udev, 0x0c, 0x0001, 0x0000, 0x00);
603141cc406Sopenharmony_ci  write_regs (udev, 2, 0xbe, 0x00, 0x84, 0x00);
604141cc406Sopenharmony_ci  write_vctl (udev, 0x0c, 0x00c0, 0x8406, 0x00);
605141cc406Sopenharmony_ci  write_vctl (udev, 0x0c, 0x00c0, 0x0406, 0x00);
606141cc406Sopenharmony_ci  write_regs (udev, 4, 0x83, 0x00, 0xa3, 0x00, 0xa4, 0x00, 0x97, 0x0a);
607141cc406Sopenharmony_ci  write_vctl (udev, 0x0c, 0x0004, 0x008b, 0x00);
608141cc406Sopenharmony_ci  read_vctl (udev, 0x0c, 0x0007, 0x0000, &rd_byte);
609141cc406Sopenharmony_ci  write_regs (udev, 1, 0x97, 0x0b);
610141cc406Sopenharmony_ci  write_vctl (udev, 0x0c, 0x0004, 0x008b, 0x00);
611141cc406Sopenharmony_ci  read_vctl (udev, 0x0c, 0x0007, 0x0000, &rd_byte);
612141cc406Sopenharmony_ci  write_regs (udev, 1, 0x97, 0x0f);
613141cc406Sopenharmony_ci  write_vctl (udev, 0x0c, 0x0004, 0x008b, 0x00);
614141cc406Sopenharmony_ci  read_vctl (udev, 0x0c, 0x0007, 0x0000, &rd_byte);
615141cc406Sopenharmony_ci  write_regs (udev, 1, 0x97, 0x05);
616141cc406Sopenharmony_ci  write_vctl (udev, 0x0c, 0x0004, 0x008b, 0x00);
617141cc406Sopenharmony_ci  read_vctl (udev, 0x0c, 0x0007, 0x0000, &rd_byte);
618141cc406Sopenharmony_ci  write_regs (udev, 4, 0x83, 0x20, 0x8d, 0xff, 0x83, 0x00, 0x8d, 0xff);
619141cc406Sopenharmony_ci  write_regs (udev, 1, 0x97, 0x00);
620141cc406Sopenharmony_ci  write_vctl (udev, 0x0c, 0x0004, 0x008b, 0x00);
621141cc406Sopenharmony_ci  read_vctl (udev, 0x0c, 0x0007, 0x0000, &rd_byte);
622141cc406Sopenharmony_ci  write_regs (udev, 1, 0x97, 0x00);
623141cc406Sopenharmony_ci  write_vctl (udev, 0x0c, 0x0004, 0x008b, 0x00);
624141cc406Sopenharmony_ci  read_vctl (udev, 0x0c, 0x0007, 0x0000, &rd_byte);
625141cc406Sopenharmony_ci  write_regs (udev, 16, 0xbe, 0x18, 0x80, 0x00, 0x84, 0x00, 0x89, 0x00, 0x88,
626141cc406Sopenharmony_ci	      0x00, 0x86, 0x00, 0x90, 0x00, 0xc1, 0x00, 0xc2, 0x00, 0xc3,
627141cc406Sopenharmony_ci	      0x00, 0xc4, 0x00, 0xc5, 0x00, 0xc6, 0x00, 0xc7, 0x00, 0xc8,
628141cc406Sopenharmony_ci	      0x00, 0xc0, 0x00);
629141cc406Sopenharmony_ci  write_regs (udev, 16, 0x84, 0x94, 0x80, 0xd1, 0x80, 0xc1, 0x82, 0x7f, 0xcf,
630141cc406Sopenharmony_ci	      0x04, 0xc1, 0x02, 0xc2, 0x00, 0xc3, 0x06, 0xc4, 0xff, 0xc5,
631141cc406Sopenharmony_ci	      0x40, 0xc6, 0x8c, 0xc7, 0xdc, 0xc8, 0x20, 0xc0, 0x72, 0x89,
632141cc406Sopenharmony_ci	      0xff, 0x86, 0xff);
633141cc406Sopenharmony_ci  poll1 (udev);
634141cc406Sopenharmony_ci
635141cc406Sopenharmony_ci  /* ready scan position */
636141cc406Sopenharmony_ci  /* 1/3" of unscannable area at top... */
637141cc406Sopenharmony_ci  if (dpi == 300)
638141cc406Sopenharmony_ci    topreg = 120 * 4;
639141cc406Sopenharmony_ci  else if (dpi == 600)
640141cc406Sopenharmony_ci    topreg = 139 * 4;
641141cc406Sopenharmony_ci  else if (dpi == 1200)
642141cc406Sopenharmony_ci    topreg = 152 * 4;
643141cc406Sopenharmony_ci  else				/*if (dpi == 150) */
644141cc406Sopenharmony_ci    topreg = 120 * 4;
645141cc406Sopenharmony_ci  topreg += topline * (1200 / dpi);
646141cc406Sopenharmony_ci
647141cc406Sopenharmony_ci  write_regs (udev, 16, 0xbe, 0x18, 0x80, 0x00, 0x84, 0x00, 0x89, 0x00, 0x88,
648141cc406Sopenharmony_ci	      0x00, 0x86, 0x00, 0x90, 0x00, 0xc1, 0x00, 0xc2, 0x00, 0xc3,
649141cc406Sopenharmony_ci	      0x00, 0xc4, 0x00, 0xc5, 0x00, 0xc6, 0x00, 0xc7, 0x00, 0xc8,
650141cc406Sopenharmony_ci	      0x00, 0xc0, 0x00);
651141cc406Sopenharmony_ci  write_regs (udev, 14, 0x84, 0xb4, 0x80, 0xe1, 0xcf, 0x04, 0xc1, 0x02, 0xc2,
652141cc406Sopenharmony_ci	      0x00, 0xc3, 0x07, 0xc4, 0xff, 0xc5, 0x40, 0xc6, 0x8c, 0xc7,
653141cc406Sopenharmony_ci	      0xdc, 0xc8, 0x20, 0xc0, 0x72, 0x89, topreg & 255, 0x86,
654141cc406Sopenharmony_ci	      255 & (topreg >> 8));
655141cc406Sopenharmony_ci  write_regs (udev, 1, 0x97, 0x00);
656141cc406Sopenharmony_ci  poll2 (udev);
657141cc406Sopenharmony_ci
658141cc406Sopenharmony_ci  write_regs (udev, 8, 0x80, 0x00, 0x84, 0x00, 0xbe, 0x00, 0xc0, 0x00, 0x86,
659141cc406Sopenharmony_ci	      0x00, 0x89, 0x00, 0x94, 0x00, 0x01, 0x02);
660141cc406Sopenharmony_ci  write_vctl (udev, 0x0c, 0x00c0, 0x8406, 0x00);
661141cc406Sopenharmony_ci  write_vctl (udev, 0x0c, 0x00c0, 0x0406, 0x00);
662141cc406Sopenharmony_ci  write_regs (udev, 16, 0xbe, 0x18, 0x80, 0x00, 0x84, 0x00, 0x89, 0x00, 0x88,
663141cc406Sopenharmony_ci	      0x00, 0x86, 0x00, 0x90, 0x00, 0xc1, 0x00, 0xc2, 0x00, 0xc3,
664141cc406Sopenharmony_ci	      0x00, 0xc4, 0x00, 0xc5, 0x00, 0xc6, 0x00, 0xc7, 0x00, 0xc8,
665141cc406Sopenharmony_ci	      0x00, 0xc0, 0x00);
666141cc406Sopenharmony_ci  if (dpi == 1200)
667141cc406Sopenharmony_ci    write_regs (udev, 4, 0x83, 0x20, 0x8d, 0x24, 0x83, 0x00, 0x8d, 0xff);
668141cc406Sopenharmony_ci  else
669141cc406Sopenharmony_ci    write_regs (udev, 4, 0x83, 0x20, 0x8d, 0xff, 0x83, 0x00, 0x8d, 0xff);
670141cc406Sopenharmony_ci  if (dpi != 1200)
671141cc406Sopenharmony_ci    write_regs (udev, 5, 0x83, 0x00, 0xa3, 0xff, 0xa4, 0xff, 0xa1, 0xff, 0xa2,
672141cc406Sopenharmony_ci		0xf7);
673141cc406Sopenharmony_ci  if (dpi == 1200)
674141cc406Sopenharmony_ci    write_regs (udev, 4, 0x83, 0x22, 0x87, 0x01, 0x83, 0x02, 0x87, 0x2c);
675141cc406Sopenharmony_ci  else
676141cc406Sopenharmony_ci    write_regs (udev, 4, 0x83, 0x22, 0x87, 0x01, 0x83, 0x02, 0x87, 0x16);
677141cc406Sopenharmony_ci  if (dpi == 1200)
678141cc406Sopenharmony_ci    write_regs (udev, 11, 0xa0, 0x00, 0x9c, 0x00, 0x9f, 0x40, 0x9d, 0x00,
679141cc406Sopenharmony_ci		0x9e, 0x00, 0xa0, 0x00, 0xce, 0x0c, 0x83, 0x20, 0xa5, 0x00,
680141cc406Sopenharmony_ci		0xa6, 0x00, 0xa7, 0x00);
681141cc406Sopenharmony_ci  else
682141cc406Sopenharmony_ci    write_regs (udev, 11, 0xa0, 0x00, 0x9c, 0x00, 0x9f, 0x00, 0x9d, 0x00,
683141cc406Sopenharmony_ci		0x9e, 0x00, 0xa0, 0x00, 0xce, 0x0c, 0x83, 0x20, 0xa5, 0x00,
684141cc406Sopenharmony_ci		0xa6, 0x00, 0xa7, 0x00);
685141cc406Sopenharmony_ci
686141cc406Sopenharmony_ci  set_gain_black (udev, rgreg, ggreg, bgreg, redreg, greenreg, bluereg);
687141cc406Sopenharmony_ci  if (!bpp16)
688141cc406Sopenharmony_ci    {
689141cc406Sopenharmony_ci      if (dpi == 1200)
690141cc406Sopenharmony_ci	write_regs (udev, 16, 0x9b, 0x00, 0x98, 0xc7, 0x99, 0x99, 0x9a, 0xd5,
691141cc406Sopenharmony_ci		    0x9b, 0x01, 0x98, 0x00, 0x99, 0x00, 0x9a, 0x00, 0x9b,
692141cc406Sopenharmony_ci		    0x02, 0x98, 0xc8, 0x99, 0x99, 0x9a, 0xd3, 0x9b, 0x03,
693141cc406Sopenharmony_ci		    0x98, 0x00, 0x99, 0x00, 0x9a, 0x00);
694141cc406Sopenharmony_ci      else if (dpi == 150)
695141cc406Sopenharmony_ci	write_regs (udev, 16, 0x9b, 0x00, 0x98, 0x94, 0x99, 0x67, 0x9a, 0x83,
696141cc406Sopenharmony_ci		    0x9b, 0x01, 0x98, 0x00, 0x99, 0x00, 0x9a, 0x00, 0x9b,
697141cc406Sopenharmony_ci		    0x02, 0x98, 0x7e, 0x99, 0x5d, 0x9a, 0x7d, 0x9b, 0x03,
698141cc406Sopenharmony_ci		    0x98, 0x00, 0x99, 0x00, 0x9a, 0x00);
699141cc406Sopenharmony_ci      else
700141cc406Sopenharmony_ci	write_regs (udev, 16, 0x9b, 0x00, 0x98, 0xb3, 0x99, 0x72, 0x9a, 0x9d,
701141cc406Sopenharmony_ci		    0x9b, 0x01, 0x98, 0x00, 0x99, 0x00, 0x9a, 0x00, 0x9b,
702141cc406Sopenharmony_ci		    0x02, 0x98, 0xa3, 0x99, 0x6f, 0x9a, 0x94, 0x9b, 0x03,
703141cc406Sopenharmony_ci		    0x98, 0x00, 0x99, 0x00, 0x9a, 0x00);
704141cc406Sopenharmony_ci    }
705141cc406Sopenharmony_ci  else
706141cc406Sopenharmony_ci    {
707141cc406Sopenharmony_ci      if (dpi == 1200)
708141cc406Sopenharmony_ci	write_regs (udev, 16, 0x9b, 0x00, 0x98, 0xb9, 0x99, 0x7a, 0x9a, 0xd6,
709141cc406Sopenharmony_ci		    0x9b, 0x01, 0x98, 0x00, 0x99, 0x00, 0x9a, 0x00, 0x9b,
710141cc406Sopenharmony_ci		    0x02, 0x98, 0xbc, 0x99, 0x7c, 0x9a, 0xd3, 0x9b, 0x03,
711141cc406Sopenharmony_ci		    0x98, 0x00, 0x99, 0x00, 0x9a, 0x00);
712141cc406Sopenharmony_ci      else if (dpi == 150)
713141cc406Sopenharmony_ci	write_regs (udev, 16, 0x9b, 0x00, 0x98, 0x9c, 0x99, 0x5f, 0x9a, 0x87,
714141cc406Sopenharmony_ci		    0x9b, 0x01, 0x98, 0x00, 0x99, 0x00, 0x9a, 0x00, 0x9b,
715141cc406Sopenharmony_ci		    0x02, 0x98, 0x97, 0x99, 0x58, 0x9a, 0x81, 0x9b, 0x03,
716141cc406Sopenharmony_ci		    0x98, 0x00, 0x99, 0x00, 0x9a, 0x00);
717141cc406Sopenharmony_ci      else
718141cc406Sopenharmony_ci	write_regs (udev, 16, 0x9b, 0x00, 0x98, 0x9d, 0x99, 0x79, 0x9a, 0x8e,
719141cc406Sopenharmony_ci		    0x9b, 0x01, 0x98, 0x00, 0x99, 0x00, 0x9a, 0x00, 0x9b,
720141cc406Sopenharmony_ci		    0x02, 0x98, 0x89, 0x99, 0x71, 0x9a, 0x80, 0x9b, 0x03,
721141cc406Sopenharmony_ci		    0x98, 0x00, 0x99, 0x00, 0x9a, 0x00);
722141cc406Sopenharmony_ci    }
723141cc406Sopenharmony_ci  write_regs (udev, 4, 0x83, 0xa2, 0x85, 0x98, 0x83, 0x82, 0x85, 0x3a);
724141cc406Sopenharmony_ci  write_regs (udev, 1, 0x9d, 0x80);
725141cc406Sopenharmony_ci  write_regs (udev, 1, 0x9d, 0x00);
726141cc406Sopenharmony_ci  if (!bpp16)
727141cc406Sopenharmony_ci    {
728141cc406Sopenharmony_ci      if (dpi == 1200)
729141cc406Sopenharmony_ci	write_regs (udev, 1, 0x94, 0x51);
730141cc406Sopenharmony_ci      else
731141cc406Sopenharmony_ci	write_regs (udev, 1, 0x94, 0x41);
732141cc406Sopenharmony_ci    }
733141cc406Sopenharmony_ci  else
734141cc406Sopenharmony_ci    {
735141cc406Sopenharmony_ci      if (dpi == 1200)
736141cc406Sopenharmony_ci	write_regs (udev, 1, 0x94, 0x71);
737141cc406Sopenharmony_ci      else
738141cc406Sopenharmony_ci	write_regs (udev, 1, 0x94, 0x61);
739141cc406Sopenharmony_ci    }
740141cc406Sopenharmony_ci  lightmap = (unsigned short *) malloc (whitemapsize);
741141cc406Sopenharmony_ci  calc_lightmap (whitescan, lightmap, 0, dpi, gain, offset);
742141cc406Sopenharmony_ci  select_pixels (lightmap, dpi, leftpix, scanpix);
743141cc406Sopenharmony_ci  if (dpi == 1200)
744141cc406Sopenharmony_ci    write_regs (udev, 6, 0xb0, 0x00, 0xb1, 0x80, 0xb2, 0x06, 0xb3, 0xff, 0xb4,
745141cc406Sopenharmony_ci		0xff, 0xb5, 0x06);
746141cc406Sopenharmony_ci  else
747141cc406Sopenharmony_ci    write_regs (udev, 6, 0xb0, 0x00, 0xb1, 0x40, 0xb2, 0x07, 0xb3, 0xff, 0xb4,
748141cc406Sopenharmony_ci		0x7f, 0xb5, 0x07);
749141cc406Sopenharmony_ci  write_vctl (udev, 0x0c, 0x0002, whitemapsize, 0x00);
750141cc406Sopenharmony_ci  usb_bulk_write (udev, 2, (unsigned char *) lightmap, whitemapsize,
751141cc406Sopenharmony_ci                  wr_timeout);
752141cc406Sopenharmony_ci
753141cc406Sopenharmony_ci  calc_lightmap (whitescan, lightmap, 1, dpi, gain, offset);
754141cc406Sopenharmony_ci  if (dpi == 1200)
755141cc406Sopenharmony_ci    write_regs (udev, 6, 0xb0, 0x00, 0xb1, 0x00, 0xb2, 0x07, 0xb3, 0xff, 0xb4,
756141cc406Sopenharmony_ci		0x7f, 0xb5, 0x07);
757141cc406Sopenharmony_ci  else
758141cc406Sopenharmony_ci    write_regs (udev, 6, 0xb0, 0x00, 0xb1, 0x80, 0xb2, 0x07, 0xb3, 0xff, 0xb4,
759141cc406Sopenharmony_ci		0xbf, 0xb5, 0x07);
760141cc406Sopenharmony_ci  write_vctl (udev, 0x0c, 0x0002, whitemapsize, 0x00);
761141cc406Sopenharmony_ci  fix_endian_short (&GRAYMASK, 1);
762141cc406Sopenharmony_ci  if (gray)
763141cc406Sopenharmony_ci    for (i = 0; i < whitemapsize / 2; i++)
764141cc406Sopenharmony_ci      lightmap[i] |= GRAYMASK;
765141cc406Sopenharmony_ci  usb_bulk_write (udev, 2, (unsigned char *) lightmap, whitemapsize,
766141cc406Sopenharmony_ci                  wr_timeout);
767141cc406Sopenharmony_ci
768141cc406Sopenharmony_ci  calc_lightmap (whitescan, lightmap, 2, dpi, gain, offset);
769141cc406Sopenharmony_ci  if (dpi == 1200)
770141cc406Sopenharmony_ci    write_regs (udev, 6, 0xb0, 0x00, 0xb1, 0x80, 0xb2, 0x07, 0xb3, 0xff, 0xb4,
771141cc406Sopenharmony_ci		0xff, 0xb5, 0x07);
772141cc406Sopenharmony_ci  else
773141cc406Sopenharmony_ci    write_regs (udev, 6, 0xb0, 0x00, 0xb1, 0xc0, 0xb2, 0x07, 0xb3, 0xff, 0xb4,
774141cc406Sopenharmony_ci		0xff, 0xb5, 0x07);
775141cc406Sopenharmony_ci  write_vctl (udev, 0x0c, 0x0002, whitemapsize, 0x00);
776141cc406Sopenharmony_ci  usb_bulk_write (udev, 2, (unsigned char *) lightmap, whitemapsize,
777141cc406Sopenharmony_ci                  wr_timeout);
778141cc406Sopenharmony_ci
779141cc406Sopenharmony_ci  free (whitescan);
780141cc406Sopenharmony_ci  free (lightmap);
781141cc406Sopenharmony_ci
782141cc406Sopenharmony_ci  if (!bpp16)
783141cc406Sopenharmony_ci    download_lut8 (udev, dpi, gray ? 0 : 1);
784141cc406Sopenharmony_ci
785141cc406Sopenharmony_ci  write_regs (udev, 4, 0x83, 0xa2, 0x85, 0x01, 0x83, 0x82, 0x85, 0x00);
786141cc406Sopenharmony_ci  write_regs (udev, 1, 0x9d, 0x80);
787141cc406Sopenharmony_ci  write_regs (udev, 1, 0x9d, 0x00);
788141cc406Sopenharmony_ci
789141cc406Sopenharmony_ci  if (!bpp16)
790141cc406Sopenharmony_ci    {
791141cc406Sopenharmony_ci      if (dpi == 1200)
792141cc406Sopenharmony_ci	write_regs (udev, 10, 0xb0, 0x00, 0xb1, 0x00, 0xb2, 0x00, 0xb3, 0xff,
793141cc406Sopenharmony_ci		    0xb4, 0x1f, 0xb5, 0x06, 0xb6, 0x01, 0xb7, 0x00, 0xb8,
794141cc406Sopenharmony_ci		    0x43, 0xb9, 0x2d);
795141cc406Sopenharmony_ci      else if (dpi == 150)
796141cc406Sopenharmony_ci	write_regs (udev, 10, 0xb0, 0x00, 0xb1, 0x00, 0xb2, 0x00, 0xb3, 0xff,
797141cc406Sopenharmony_ci		    0xb4, 0x0f, 0xb5, 0x07, 0xb6, 0x01, 0xb7, 0x00, 0xb8,
798141cc406Sopenharmony_ci		    0xe0, 0xb9, 0x37);
799141cc406Sopenharmony_ci      else
800141cc406Sopenharmony_ci	write_regs (udev, 10, 0xb0, 0x00, 0xb1, 0x00, 0xb2, 0x00, 0xb3, 0xff,
801141cc406Sopenharmony_ci		    0xb4, 0x0f, 0xb5, 0x07, 0xb6, 0x01, 0xb7, 0x00, 0xb8,
802141cc406Sopenharmony_ci		    0x90, 0xb9, 0x37);
803141cc406Sopenharmony_ci    }
804141cc406Sopenharmony_ci  else
805141cc406Sopenharmony_ci    {
806141cc406Sopenharmony_ci      if (dpi == 1200)
807141cc406Sopenharmony_ci	write_regs (udev, 10, 0xb0, 0x00, 0xb1, 0x00, 0xb2, 0x00, 0xb3, 0xff,
808141cc406Sopenharmony_ci		    0xb4, 0xff, 0xb5, 0x03, 0xb6, 0x01, 0xb7, 0x00, 0xb8,
809141cc406Sopenharmony_ci		    0x87, 0xb9, 0x18);
810141cc406Sopenharmony_ci      else if (dpi == 150)
811141cc406Sopenharmony_ci	write_regs (udev, 10, 0xb0, 0x00, 0xb1, 0x00, 0xb2, 0x00, 0xb3, 0xff,
812141cc406Sopenharmony_ci		    0xb4, 0xff, 0xb5, 0x03, 0xb6, 0x01, 0xb7, 0x00, 0xb8,
813141cc406Sopenharmony_ci		    0xc1, 0xb9, 0x1e);
814141cc406Sopenharmony_ci      else
815141cc406Sopenharmony_ci	write_regs (udev, 10, 0xb0, 0x00, 0xb1, 0x00, 0xb2, 0x00, 0xb3, 0xff,
816141cc406Sopenharmony_ci		    0xb4, 0xff, 0xb5, 0x03, 0xb6, 0x01, 0xb7, 0x00, 0xb8,
817141cc406Sopenharmony_ci		    0x21, 0xb9, 0x1e);
818141cc406Sopenharmony_ci    }
819141cc406Sopenharmony_ci
820141cc406Sopenharmony_ci  /* [86,89] controls number of 300dpi steps to scan */
821141cc406Sopenharmony_ci  botreg = scanlines * (1200 / dpi) + 400;
822141cc406Sopenharmony_ci  write_regs (udev, 6, 0xc0, 0x00, 0x84, 0x00, 0x84, 0xb4, 0x80, 0xe1, 0xcf,
823141cc406Sopenharmony_ci	      0x04, 0x82, 0x00);
824141cc406Sopenharmony_ci
825141cc406Sopenharmony_ci  if (!bpp16)
826141cc406Sopenharmony_ci    {
827141cc406Sopenharmony_ci      if (dpi == 300)
828141cc406Sopenharmony_ci	write_regs (udev, 18, 0x83, 0xa2, 0x85, 0x04, 0x83, 0x82, 0x85, 0x00,
829141cc406Sopenharmony_ci		    0xbc, 0x20, 0xbd, 0x10, 0x88, 0xd0, 0xc1, 0x01, 0xc2,
830141cc406Sopenharmony_ci		    0x00, 0xc3, 0x04, 0xc4, 0x20, 0xc5, 0x10, 0xc6, 0xc3,
831141cc406Sopenharmony_ci		    0xc7, 0xd0, 0xc8, 0x12, 0xc0, 0xd1, 0x89, botreg & 255,
832141cc406Sopenharmony_ci		    0x86, 255 & (botreg >> 8));
833141cc406Sopenharmony_ci      else if (dpi == 600)
834141cc406Sopenharmony_ci	write_regs (udev, 18, 0x83, 0xa2, 0x85, 0x04, 0x83, 0x82, 0x85, 0x00,
835141cc406Sopenharmony_ci		    0xbc, 0x20, 0xbd, 0x10, 0x88, 0xd0, 0xc1, 0x01, 0xc2,
836141cc406Sopenharmony_ci		    0x00, 0xc3, 0x04, 0xc4, 0x20, 0xc5, 0x10, 0xc6, 0xc3,
837141cc406Sopenharmony_ci		    0xc7, 0xd0, 0xc8, 0x16, 0xc0, 0xd1, 0x89, botreg & 255,
838141cc406Sopenharmony_ci		    0x86, 255 & (botreg >> 8));
839141cc406Sopenharmony_ci      else if (dpi == 1200)
840141cc406Sopenharmony_ci	write_regs (udev, 18, 0x83, 0xa2, 0x85, 0x02, 0x83, 0x82, 0x85, 0x00,
841141cc406Sopenharmony_ci		    0xbc, 0x20, 0xbd, 0x08, 0x88, 0xa4, 0xc1, 0x02, 0xc2,
842141cc406Sopenharmony_ci		    0x00, 0xc3, 0x02, 0xc4, 0x20, 0xc5, 0x08, 0xc6, 0x96,
843141cc406Sopenharmony_ci		    0xc7, 0xa4, 0xc8, 0x06, 0xc0, 0xd2, 0x89, botreg & 255,
844141cc406Sopenharmony_ci		    0x86, 255 & (botreg >> 8));
845141cc406Sopenharmony_ci      else if (dpi == 150)
846141cc406Sopenharmony_ci	write_regs (udev, 18, 0x83, 0xa2, 0x85, 0x06, 0x83, 0x82, 0x85, 0x00,
847141cc406Sopenharmony_ci		    0xbc, 0x1c, 0xbd, 0x08, 0x88, 0xe0, 0xc1, 0x01, 0xc2,
848141cc406Sopenharmony_ci		    0x00, 0xc3, 0x03, 0xc4, 0x1c, 0xc5, 0x08, 0xc6, 0xd7,
849141cc406Sopenharmony_ci		    0xc7, 0xe0, 0xc8, 0x11, 0xc0, 0xd1, 0x89, botreg & 255,
850141cc406Sopenharmony_ci		    0x86, 255 & (botreg >> 8));
851141cc406Sopenharmony_ci
852141cc406Sopenharmony_ci      if (dpi == 300)
853141cc406Sopenharmony_ci	write_regs (udev, 8, 0xbb, 0x01, 0x9b, 0x24, 0x8b, 0x00, 0x8e, 0x80,
854141cc406Sopenharmony_ci		    0xbf, 0x00, 0x90, 0x20, 0x91, 0x00, 0x83, 0x82);
855141cc406Sopenharmony_ci      else if (dpi == 600)
856141cc406Sopenharmony_ci	write_regs (udev, 8, 0xbb, 0x02, 0x9b, 0x24, 0x8b, 0x00, 0x8e, 0x80,
857141cc406Sopenharmony_ci		    0xbf, 0x00, 0x90, 0x20, 0x91, 0x00, 0x83, 0x82);
858141cc406Sopenharmony_ci      else if (dpi == 1200)
859141cc406Sopenharmony_ci	write_regs (udev, 8, 0xbb, 0x02, 0x9b, 0x24, 0x8b, 0x00, 0x8e, 0x80,
860141cc406Sopenharmony_ci		    0xbf, 0x00, 0x90, 0x20, 0x91, 0x00, 0x83, 0x82);
861141cc406Sopenharmony_ci      else if (dpi == 150)
862141cc406Sopenharmony_ci	write_regs (udev, 8, 0xbb, 0x00, 0x9b, 0x24, 0x8b, 0x00, 0x8e, 0x80,
863141cc406Sopenharmony_ci		    0xbf, 0x00, 0x90, 0x20, 0x91, 0x00, 0x83, 0x82);
864141cc406Sopenharmony_ci    }
865141cc406Sopenharmony_ci  else
866141cc406Sopenharmony_ci    {
867141cc406Sopenharmony_ci      if (dpi == 300)
868141cc406Sopenharmony_ci	write_regs (udev, 18, 0x83, 0xa2, 0x85, 0x04, 0x83, 0x82, 0x85, 0x00,
869141cc406Sopenharmony_ci		    0xbc, 0x20, 0xbd, 0x10, 0x88, 0xd0, 0xc1, 0x01, 0xc2,
870141cc406Sopenharmony_ci		    0x00, 0xc3, 0x04, 0xc4, 0x20, 0xc5, 0x10, 0xc6, 0xc3,
871141cc406Sopenharmony_ci		    0xc7, 0xd0, 0xc8, 0x13, 0xc0, 0xd1, 0x89, botreg & 255,
872141cc406Sopenharmony_ci		    0x86, 255 & (botreg >> 8));
873141cc406Sopenharmony_ci      else if (dpi == 150)
874141cc406Sopenharmony_ci	write_regs (udev, 18, 0x83, 0xa2, 0x85, 0x06, 0x83, 0x82, 0x85, 0x00,
875141cc406Sopenharmony_ci		    0xbc, 0x1c, 0xbd, 0x08, 0x88, 0xe0, 0xc1, 0x01, 0xc2,
876141cc406Sopenharmony_ci		    0x00, 0xc3, 0x03, 0xc4, 0x1c, 0xc5, 0x08, 0xc6, 0xd7,
877141cc406Sopenharmony_ci		    0xc7, 0xe0, 0xc8, 0x12, 0xc0, 0xd1, 0x89, botreg & 255,
878141cc406Sopenharmony_ci		    0x86, 255 & (botreg >> 8));
879141cc406Sopenharmony_ci      else if (dpi == 1200)
880141cc406Sopenharmony_ci	write_regs (udev, 18, 0x83, 0xa2, 0x85, 0x02, 0x83, 0x82, 0x85, 0x00,
881141cc406Sopenharmony_ci		    0xbc, 0x20, 0xbd, 0x08, 0x88, 0xa4, 0xc1, 0x02, 0xc2,
882141cc406Sopenharmony_ci		    0x00, 0xc3, 0x02, 0xc4, 0x20, 0xc5, 0x08, 0xc6, 0x96,
883141cc406Sopenharmony_ci		    0xc7, 0xa4, 0xc8, 0x0c, 0xc0, 0xd2, 0x89, botreg & 255,
884141cc406Sopenharmony_ci		    0x86, 255 & (botreg >> 8));
885141cc406Sopenharmony_ci      else if (dpi == 600)
886141cc406Sopenharmony_ci	write_regs (udev, 18, 0x83, 0xa2, 0x85, 0x04, 0x83, 0x82, 0x85, 0x00,
887141cc406Sopenharmony_ci		    0xbc, 0x20, 0xbd, 0x10, 0x88, 0xd0, 0xc1, 0x01, 0xc2,
888141cc406Sopenharmony_ci		    0x00, 0xc3, 0x04, 0xc4, 0x20, 0xc5, 0x10, 0xc6, 0xc3,
889141cc406Sopenharmony_ci		    0xc7, 0xd0, 0xc8, 0x1a, 0xc0, 0xd1, 0x89, botreg & 255,
890141cc406Sopenharmony_ci		    0x86, 255 & (botreg >> 8));
891141cc406Sopenharmony_ci
892141cc406Sopenharmony_ci      if (dpi == 300)
893141cc406Sopenharmony_ci	write_regs (udev, 8, 0xbb, 0x02, 0x9b, 0x24, 0x8b, 0x00, 0x8e, 0x80,
894141cc406Sopenharmony_ci		    0xbf, 0x00, 0x90, 0x70, 0x91, 0x00, 0x83, 0x82);
895141cc406Sopenharmony_ci      else if (dpi == 150)
896141cc406Sopenharmony_ci	write_regs (udev, 8, 0xbb, 0x01, 0x9b, 0x24, 0x8b, 0x00, 0x8e, 0x80,
897141cc406Sopenharmony_ci		    0xbf, 0x00, 0x90, 0x70, 0x91, 0x00, 0x83, 0x82);
898141cc406Sopenharmony_ci      else if (dpi == 1200)
899141cc406Sopenharmony_ci	write_regs (udev, 8, 0xbb, 0x05, 0x9b, 0x24, 0x8b, 0x00, 0x8e, 0x80,
900141cc406Sopenharmony_ci		    0xbf, 0x00, 0x90, 0x70, 0x91, 0x00, 0x83, 0x82);
901141cc406Sopenharmony_ci      else if (dpi == 600)
902141cc406Sopenharmony_ci	write_regs (udev, 8, 0xbb, 0x04, 0x9b, 0x24, 0x8b, 0x00, 0x8e, 0x80,
903141cc406Sopenharmony_ci		    0xbf, 0x00, 0x90, 0x70, 0x91, 0x00, 0x83, 0x82);
904141cc406Sopenharmony_ci
905141cc406Sopenharmony_ci
906141cc406Sopenharmony_ci    }
907141cc406Sopenharmony_ci
908141cc406Sopenharmony_ci  if (gray)
909141cc406Sopenharmony_ci    write_regs (udev, 1, 0xbe, 0x05);
910141cc406Sopenharmony_ci  else
911141cc406Sopenharmony_ci    write_regs (udev, 1, 0xbe, 0x0d);
912141cc406Sopenharmony_ci  write_vctl (udev, 0x0c, 0x0003, 0x0001, 0x00);
913141cc406Sopenharmony_ci  usb_bulk_read (udev, 1, &rd_byte, 1, rd_timeout);
914141cc406Sopenharmony_ci  write_vctl (udev, 0x0c, 0x0001, 0x0000, 0x00);
915141cc406Sopenharmony_ci
916141cc406Sopenharmony_ci#ifndef BACKENDNAME
917141cc406Sopenharmony_ci  sprintf (fname, "%d.%s", dpi, gray ? "pgm" : "ppm");
918141cc406Sopenharmony_ci  if (stname)
919141cc406Sopenharmony_ci    strcpy (fname, stname);
920141cc406Sopenharmony_ci  sprintf (head, "P%d\n%d %d\n%d\n", gray ? 5 : 6, scanpix, scanlines,
921141cc406Sopenharmony_ci	   bpp16 ? 65535 : 255);
922141cc406Sopenharmony_ci  if (nohead)
923141cc406Sopenharmony_ci    head[0] = 0;
924141cc406Sopenharmony_ci  if (!raw)
925141cc406Sopenharmony_ci    record_image (udev, fname, dpi, scanpix, scanlines, gray, head, bpp16);
926141cc406Sopenharmony_ci  else
927141cc406Sopenharmony_ci    record_head (udev, fname,
928141cc406Sopenharmony_ci		 scanpix * (gray ? 1 : 3) * (bpp16 ? 2 : 1) * scanlines, "");
929141cc406Sopenharmony_ci
930141cc406Sopenharmony_ci  reset_scanner (udev);
931141cc406Sopenharmony_ci  idle_ab (udev);
932141cc406Sopenharmony_ci  set_lamp_timer (udev, 5);
933141cc406Sopenharmony_ci#endif
934141cc406Sopenharmony_ci}
935