1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
|
/* sane - Scanner Access Now Easy.
Copyright (C) 2019 Povilas Kanapickas <povilas@radix.lt>
This file is part of the SANE package.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA.
As a special exception, the authors of SANE give permission for
additional uses of the libraries contained in this release of SANE.
The exception is that, if you link a SANE library with other files
to produce an executable, this does not by itself cause the
resulting executable to be covered by the GNU General Public
License. Your use of that executable is in no way restricted on
account of linking the SANE library code into it.
This exception does not, however, invalidate any other reasons why
the executable file might be covered by the GNU General Public
License.
If you submit changes to SANE to the maintainers to be included in
a subsequent release, you agree by submitting the changes that
those changes may be distributed with this exception intact.
If you write modifications of your own for SANE, it is your choice
whether to permit this exception to apply to your modifications.
If you do not wish that, delete this exception notice.
*/
#define DEBUG_DECLARE_ONLY
#include "scanner_interface_usb.h"
#include "low.h"
#include <thread>
namespace genesys {
ScannerInterfaceUsb::~ScannerInterfaceUsb() = default;
ScannerInterfaceUsb::ScannerInterfaceUsb(Genesys_Device* dev) : dev_{dev} {}
bool ScannerInterfaceUsb::is_mock() const
{
return false;
}
std::uint8_t ScannerInterfaceUsb::read_register(std::uint16_t address)
{
DBG_HELPER(dbg);
std::uint8_t value = 0;
if (dev_->model->asic_type == AsicType::GL847 ||
dev_->model->asic_type == AsicType::GL845 ||
dev_->model->asic_type == AsicType::GL846 ||
dev_->model->asic_type == AsicType::GL124)
{
std::uint8_t value2x8[2];
std::uint16_t address16 = 0x22 + (address << 8);
std::uint16_t usb_value = VALUE_GET_REGISTER;
if (address > 0xff) {
usb_value |= 0x100;
}
usb_dev_.control_msg(REQUEST_TYPE_IN, REQUEST_BUFFER, usb_value, address16, 2, value2x8);
// check usb link status
if (value2x8[1] != 0x55) {
throw SaneException(SANE_STATUS_IO_ERROR, "invalid read, scanner unplugged?");
}
DBG(DBG_io, "%s (0x%02x, 0x%02x) completed\n", __func__, address, value2x8[0]);
value = value2x8[0];
} else {
if (address > 0xff) {
throw SaneException("Invalid register address 0x%04x", address);
}
std::uint8_t address8 = address & 0xff;
usb_dev_.control_msg(REQUEST_TYPE_OUT, REQUEST_REGISTER, VALUE_SET_REGISTER, INDEX,
1, &address8);
usb_dev_.control_msg(REQUEST_TYPE_IN, REQUEST_REGISTER, VALUE_READ_REGISTER, INDEX,
1, &value);
}
return value;
}
void ScannerInterfaceUsb::write_register(std::uint16_t address, std::uint8_t value)
{
DBG_HELPER_ARGS(dbg, "address: 0x%04x, value: 0x%02x", static_cast<unsigned>(address),
static_cast<unsigned>(value));
if (dev_->model->asic_type == AsicType::GL847 ||
dev_->model->asic_type == AsicType::GL845 ||
dev_->model->asic_type == AsicType::GL846 ||
dev_->model->asic_type == AsicType::GL124)
{
std::uint8_t buffer[2];
buffer[0] = address & 0xff;
buffer[1] = value;
std::uint16_t usb_value = VALUE_SET_REGISTER;
if (address > 0xff) {
usb_value |= 0x100;
}
usb_dev_.control_msg(REQUEST_TYPE_OUT, REQUEST_BUFFER, usb_value, INDEX,
2, buffer);
} else {
if (address > 0xff) {
throw SaneException("Invalid register address 0x%04x", address);
}
std::uint8_t address8 = address & 0xff;
usb_dev_.control_msg(REQUEST_TYPE_OUT, REQUEST_REGISTER, VALUE_SET_REGISTER, INDEX,
1, &address8);
usb_dev_.control_msg(REQUEST_TYPE_OUT, REQUEST_REGISTER, VALUE_WRITE_REGISTER, INDEX,
1, &value);
}
DBG(DBG_io, "%s (0x%02x, 0x%02x) completed\n", __func__, address, value);
}
void ScannerInterfaceUsb::write_registers(const Genesys_Register_Set& regs)
{
DBG_HELPER(dbg);
if (dev_->model->asic_type == AsicType::GL646 ||
dev_->model->asic_type == AsicType::GL841)
{
uint8_t outdata[8];
std::vector<uint8_t> buffer;
buffer.reserve(regs.size() * 2);
/* copy registers and values in data buffer */
for (const auto& r : regs) {
buffer.push_back(r.address);
buffer.push_back(r.value);
}
DBG(DBG_io, "%s (elems= %zu, size = %zu)\n", __func__, regs.size(), buffer.size());
if (dev_->model->asic_type == AsicType::GL646) {
outdata[0] = BULK_OUT;
outdata[1] = BULK_REGISTER;
outdata[2] = 0x00;
outdata[3] = 0x00;
outdata[4] = (buffer.size() & 0xff);
outdata[5] = ((buffer.size() >> 8) & 0xff);
outdata[6] = ((buffer.size() >> 16) & 0xff);
outdata[7] = ((buffer.size() >> 24) & 0xff);
usb_dev_.control_msg(REQUEST_TYPE_OUT, REQUEST_BUFFER, VALUE_BUFFER, INDEX,
sizeof(outdata), outdata);
size_t write_size = buffer.size();
usb_dev_.bulk_write(buffer.data(), &write_size);
} else {
for (std::size_t i = 0; i < regs.size();) {
std::size_t c = regs.size() - i;
if (c > 32) /*32 is max on GL841. checked that.*/
c = 32;
usb_dev_.control_msg(REQUEST_TYPE_OUT, REQUEST_BUFFER, VALUE_SET_REGISTER,
INDEX, c * 2, buffer.data() + i * 2);
i += c;
}
}
} else {
for (const auto& r : regs) {
write_register(r.address, r.value);
}
}
DBG(DBG_io, "%s: wrote %zu registers\n", __func__, regs.size());
}
void ScannerInterfaceUsb::write_0x8c(std::uint8_t index, std::uint8_t value)
{
DBG_HELPER_ARGS(dbg, "0x%02x,0x%02x", index, value);
usb_dev_.control_msg(REQUEST_TYPE_OUT, REQUEST_REGISTER, VALUE_BUF_ENDACCESS, index, 1, &value);
}
static void bulk_read_data_send_header(UsbDevice& usb_dev, AsicType asic_type, size_t size)
{
DBG_HELPER(dbg);
uint8_t outdata[8];
if (asic_type == AsicType::GL124 ||
asic_type == AsicType::GL845 ||
asic_type == AsicType::GL846 ||
asic_type == AsicType::GL847)
{
// hard coded 0x10000000 address
outdata[0] = 0;
outdata[1] = 0;
outdata[2] = 0;
outdata[3] = 0x10;
} else if (asic_type == AsicType::GL841 ||
asic_type == AsicType::GL842 ||
asic_type == AsicType::GL843)
{
outdata[0] = BULK_IN;
outdata[1] = BULK_RAM;
outdata[2] = 0x82; //
outdata[3] = 0x00;
} else {
outdata[0] = BULK_IN;
outdata[1] = BULK_RAM;
outdata[2] = 0x00;
outdata[3] = 0x00;
}
/* data size to transfer */
outdata[4] = (size & 0xff);
outdata[5] = ((size >> 8) & 0xff);
outdata[6] = ((size >> 16) & 0xff);
outdata[7] = ((size >> 24) & 0xff);
usb_dev.control_msg(REQUEST_TYPE_OUT, REQUEST_BUFFER, VALUE_BUFFER, 0x00,
sizeof(outdata), outdata);
}
void ScannerInterfaceUsb::bulk_read_data(std::uint8_t addr, std::uint8_t* data, std::size_t size)
{
// currently supported: GL646, GL841, GL843, GL845, GL846, GL847, GL124
DBG_HELPER(dbg);
unsigned is_addr_used = 1;
unsigned has_header_before_each_chunk = 0;
if (dev_->model->asic_type == AsicType::GL124 ||
dev_->model->asic_type == AsicType::GL845 ||
dev_->model->asic_type == AsicType::GL846 ||
dev_->model->asic_type == AsicType::GL847)
{
is_addr_used = 0;
has_header_before_each_chunk = 1;
}
if (is_addr_used) {
DBG(DBG_io, "%s: requesting %zu bytes from 0x%02x addr\n", __func__, size, addr);
} else {
DBG(DBG_io, "%s: requesting %zu bytes\n", __func__, size);
}
if (size == 0)
return;
if (is_addr_used) {
usb_dev_.control_msg(REQUEST_TYPE_OUT, REQUEST_REGISTER, VALUE_SET_REGISTER, 0x00,
1, &addr);
}
std::size_t target_size = size;
std::size_t max_in_size = sanei_genesys_get_bulk_max_size(dev_->model->asic_type);
if (!has_header_before_each_chunk) {
bulk_read_data_send_header(usb_dev_, dev_->model->asic_type, size);
}
// loop until computed data size is read
while (target_size > 0) {
std::size_t block_size = std::min(target_size, max_in_size);
if (has_header_before_each_chunk) {
bulk_read_data_send_header(usb_dev_, dev_->model->asic_type, block_size);
}
DBG(DBG_io2, "%s: trying to read %zu bytes of data\n", __func__, block_size);
usb_dev_.bulk_read(data, &block_size);
DBG(DBG_io2, "%s: read %zu bytes, %zu remaining\n", __func__, block_size, target_size - block_size);
target_size -= block_size;
data += block_size;
}
}
void ScannerInterfaceUsb::bulk_write_data(std::uint8_t addr, std::uint8_t* data, std::size_t len)
{
DBG_HELPER_ARGS(dbg, "writing %zu bytes", len);
// supported: GL646, GL841, GL843
std::size_t size;
std::uint8_t outdata[8];
usb_dev_.control_msg(REQUEST_TYPE_OUT, REQUEST_REGISTER, VALUE_SET_REGISTER, INDEX,
1, &addr);
std::size_t max_out_size = sanei_genesys_get_bulk_max_size(dev_->model->asic_type);
while (len) {
if (len > max_out_size)
size = max_out_size;
else
size = len;
if (dev_->model->asic_type == AsicType::GL841) {
outdata[0] = BULK_OUT;
outdata[1] = BULK_RAM;
// both 0x82 and 0x00 works on GL841.
outdata[2] = 0x82;
outdata[3] = 0x00;
} else {
outdata[0] = BULK_OUT;
outdata[1] = BULK_RAM;
// 8600F uses 0x82, but 0x00 works too. 8400F uses 0x02 for certain transactions.
outdata[2] = 0x00;
outdata[3] = 0x00;
}
outdata[4] = (size & 0xff);
outdata[5] = ((size >> 8) & 0xff);
outdata[6] = ((size >> 16) & 0xff);
outdata[7] = ((size >> 24) & 0xff);
usb_dev_.control_msg(REQUEST_TYPE_OUT, REQUEST_BUFFER, VALUE_BUFFER, 0x00,
sizeof(outdata), outdata);
usb_dev_.bulk_write(data, &size);
DBG(DBG_io2, "%s: wrote %zu bytes, %zu remaining\n", __func__, size, len - size);
len -= size;
data += size;
}
}
void ScannerInterfaceUsb::write_buffer(std::uint8_t type, std::uint32_t addr, std::uint8_t* data,
std::size_t size)
{
DBG_HELPER_ARGS(dbg, "type: 0x%02x, addr: 0x%08x, size: 0x%08zx", type, addr, size);
if (dev_->model->asic_type != AsicType::GL646 &&
dev_->model->asic_type != AsicType::GL841 &&
dev_->model->asic_type != AsicType::GL842 &&
dev_->model->asic_type != AsicType::GL843)
{
throw SaneException("Unsupported transfer mode");
}
if (dev_->model->asic_type == AsicType::GL843) {
write_register(0x2b, ((addr >> 4) & 0xff));
write_register(0x2a, ((addr >> 12) & 0xff));
write_register(0x29, ((addr >> 20) & 0xff));
} else {
write_register(0x2b, ((addr >> 4) & 0xff));
write_register(0x2a, ((addr >> 12) & 0xff));
}
bulk_write_data(type, data, size);
}
void ScannerInterfaceUsb::write_gamma(std::uint8_t type, std::uint32_t addr, std::uint8_t* data,
std::size_t size)
{
DBG_HELPER_ARGS(dbg, "type: 0x%02x, addr: 0x%08x, size: 0x%08zx", type, addr, size);
if (dev_->model->asic_type != AsicType::GL841 &&
dev_->model->asic_type != AsicType::GL842 &&
dev_->model->asic_type != AsicType::GL843)
{
throw SaneException("Unsupported transfer mode");
}
write_register(0x5b, ((addr >> 12) & 0xff));
write_register(0x5c, ((addr >> 4) & 0xff));
bulk_write_data(type, data, size);
if (dev_->model->asic_type == AsicType::GL842 ||
dev_->model->asic_type == AsicType::GL843)
{
// it looks like we need to reset the address so that subsequent buffer operations work.
// Most likely the MTRTBL register is to blame.
write_register(0x5b, 0);
write_register(0x5c, 0);
}
}
void ScannerInterfaceUsb::write_ahb(std::uint32_t addr, std::uint32_t size, std::uint8_t* data)
{
DBG_HELPER_ARGS(dbg, "address: 0x%08x, size: %d", static_cast<unsigned>(addr),
static_cast<unsigned>(size));
if (dev_->model->asic_type != AsicType::GL845 &&
dev_->model->asic_type != AsicType::GL846 &&
dev_->model->asic_type != AsicType::GL847 &&
dev_->model->asic_type != AsicType::GL124)
{
throw SaneException("Unsupported transfer type");
}
std::uint8_t outdata[8];
outdata[0] = addr & 0xff;
outdata[1] = ((addr >> 8) & 0xff);
outdata[2] = ((addr >> 16) & 0xff);
outdata[3] = ((addr >> 24) & 0xff);
outdata[4] = (size & 0xff);
outdata[5] = ((size >> 8) & 0xff);
outdata[6] = ((size >> 16) & 0xff);
outdata[7] = ((size >> 24) & 0xff);
// write addr and size for AHB
usb_dev_.control_msg(REQUEST_TYPE_OUT, REQUEST_BUFFER, VALUE_BUFFER, 0x01, 8, outdata);
std::size_t max_out_size = sanei_genesys_get_bulk_max_size(dev_->model->asic_type);
// write actual data
std::size_t written = 0;
do {
std::size_t block_size = std::min(size - written, max_out_size);
usb_dev_.bulk_write(data + written, &block_size);
written += block_size;
} while (written < size);
}
std::uint16_t ScannerInterfaceUsb::read_fe_register(std::uint8_t address)
{
DBG_HELPER(dbg);
Genesys_Register_Set reg;
reg.init_reg(0x50, address);
// set up read address
write_registers(reg);
// read data
std::uint16_t value = read_register(0x46) << 8;
value |= read_register(0x47);
DBG(DBG_io, "%s (0x%02x, 0x%04x)\n", __func__, address, value);
return value;
}
void ScannerInterfaceUsb::write_fe_register(std::uint8_t address, std::uint16_t value)
{
DBG_HELPER_ARGS(dbg, "0x%02x, 0x%04x", address, value);
Genesys_Register_Set reg(Genesys_Register_Set::SEQUENTIAL);
reg.init_reg(0x51, address);
if (dev_->model->asic_type == AsicType::GL124) {
reg.init_reg(0x5d, (value / 256) & 0xff);
reg.init_reg(0x5e, value & 0xff);
} else {
reg.init_reg(0x3a, (value / 256) & 0xff);
reg.init_reg(0x3b, value & 0xff);
}
write_registers(reg);
}
IUsbDevice& ScannerInterfaceUsb::get_usb_device()
{
return usb_dev_;
}
void ScannerInterfaceUsb::sleep_us(unsigned microseconds)
{
if (sanei_usb_is_replay_mode_enabled()) {
return;
}
std::this_thread::sleep_for(std::chrono::microseconds{microseconds});
}
void ScannerInterfaceUsb::record_progress_message(const char* msg)
{
sanei_usb_testing_record_message(msg);
}
void ScannerInterfaceUsb::record_slope_table(unsigned table_nr,
const std::vector<std::uint16_t>& steps)
{
(void) table_nr;
(void) steps;
}
void ScannerInterfaceUsb::record_key_value(const std::string& key, const std::string& value)
{
(void) key;
(void) value;
}
void ScannerInterfaceUsb::test_checkpoint(const std::string& name)
{
(void) name;
}
} // namespace genesys
|