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
|
/* sane - Scanner Access Now Easy.
Copyright (C) Marian Eichholz 2001
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, see <https://www.gnu.org/licenses/>.
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.
*/
/* ======================================================================
Userspace scan tool for the Microtek 3600 scanner
(C) Marian Eichholz 2001
26.4.2001: Added an abstraction layer for TransferControlMsg.
====================================================================== */
#include "sm3600-scantool.h"
/* **********************************************************************
TransferControlMsg()
********************************************************************** */
static int TransferControlMsg(TInstance *this,
int nReqType,
int nRequest,
int nValue,
int nIndex,
void *pBuffer,
int cchBuffer,
int cJiffiesTimeout)
{
SANE_Status err;
cJiffiesTimeout = cJiffiesTimeout;
err = sanei_usb_control_msg (this->hScanner,
nReqType,
nRequest,
nValue,
nIndex,
cchBuffer,
pBuffer);
if (err)
return err;
return cchBuffer;
}
/* **********************************************************************
cch=BulkRead()
********************************************************************** */
static int TransferBulkRead(TInstance *this,
int nEndPoint,
void *pBuffer,
int cchMax,
int cJiffiesTimeout)
{
int err;
size_t sz = cchMax;
nEndPoint = nEndPoint;
cJiffiesTimeout = cJiffiesTimeout;
err = sanei_usb_read_bulk(this->hScanner,
pBuffer,
&sz);
if (err)
return err;
return sz;
}
/* **********************************************************************
RegWrite(iRegister, cb, ulValue)
RegWriteArray(iRegister, cb, unsigned char uchValues)
********************************************************************** */
__SM3600EXPORT__
TState RegWrite(TInstance *this, int iRegister, int cb, unsigned long ulValue)
{
char *pchBuffer;
int i;
TBool bOk=true;
INST_ASSERT();
/* some rough assertions */
if (cb<1 || cb>4)
return SetError(this,SANE_STATUS_INVAL,"unsupported control transfer size %d",cb);
pchBuffer=malloc(cb);
CHECK_POINTER(pchBuffer);
for (i=0; i<cb; i++)
{
pchBuffer[i]=(char)(ulValue&0xFF);
ulValue=ulValue>>8;
}
if (!bOk)
{
free(pchBuffer);
return SetError(this,SANE_STATUS_IO_ERROR,
"error in reg out: %d,%d,%08X",iRegister,cb,ulValue);
}
i=TransferControlMsg(this, /* handle */
0x40, /* request type */
0x08, /* request */
iRegister, /* value */
0, /* index */
pchBuffer, cb, /* bytes, size */
USB_TIMEOUT_JIFFIES); /* TO, jiffies... */
free(pchBuffer);
if (i<0)
return SetError(this,SANE_STATUS_IO_ERROR,"error during register write");
return SANE_STATUS_GOOD;
}
__SM3600EXPORT__
TState RegWriteArray(TInstance *this, int iRegister, int cb, unsigned char *pchBuffer)
{
int i;
INST_ASSERT();
/* some rough assertions */
i=TransferControlMsg(this, /* handle */
0x40, /* request type */
0x08, /* request */
iRegister, /* value */
0, /* index */
pchBuffer, cb, /* bytes, size */
USB_TIMEOUT_JIFFIES); /* TO, jiffies... */
if (i<0)
return SetError(this,SANE_STATUS_IO_ERROR,"error during register write");
return SANE_STATUS_GOOD;
}
/* **********************************************************************
MemWriteArray(iAddress, cb, ulValue)
********************************************************************** */
__SM3600EXPORT__
TState MemWriteArray(TInstance *this, int iAddress,
int cb, unsigned char *pchBuffer)
{
int i;
INST_ASSERT();
/* some rough assertions */
i=TransferControlMsg(this,
0x40, /* request type */
0x09, /* request */
iAddress, /* value */
0, /* index */
pchBuffer, cb, /* bytes, size */
10000); /* TO, jiffies... */
if (i<0)
return SetError(this,SANE_STATUS_IO_ERROR,"error during memory write");
return SANE_STATUS_GOOD;
}
/* **********************************************************************
MemReadArray(iRegister, cb, ulValue)
********************************************************************** */
#ifdef INSANE_VERSION
__SM3600EXPORT__
TState MemReadArray(TInstance *this, int iAddress, int cb, unsigned char *pchBuffer)
{
int i;
INST_ASSERT();
/* some rough assertions */
i=TransferControlMsg(this,
0xC0, /* request type */
0x01, /* request */
iAddress, /* value */
0, /* index */
pchBuffer, cb, /* bytes, size */
USB_TIMEOUT_JIFFIES); /* TO, jiffies... */
if (i<0)
return SetError(this,SANE_STATUS_IO_ERROR,"error during memory read");
return SANE_STATUS_GOOD;
}
/* **********************************************************************
RegCheck(iRegister, cb, ulValue)
********************************************************************** */
__SM3600EXPORT__
TState RegCheck(TInstance *this, int iRegister, int cch, unsigned long ulValue)
{
char *pchBuffer,*pchTransfer;
int i,rcCode;
TBool bOk;
INST_ASSERT();
if (cch<1 || cch>3)
return SetError(this,SANE_STATUS_INVAL,"unsupported control transfer size %d",cch);
pchBuffer=malloc(cch);
pchTransfer=calloc(1,cch);
rcCode=SANE_STATUS_GOOD;
if (!pchBuffer || !pchTransfer)
{
if (pchBuffer) free(pchBuffer);
if (pchTransfer) free(pchTransfer);
rcCode=SetError(this, SANE_STATUS_NO_MEM, "no memory in RegCheck()");
}
bOk=true;
for (i=0; !rcCode && i<cch; i++)
{
pchBuffer[i]=(char)(ulValue&0x00FF);
ulValue=(ulValue>>8);
}
if (!rcCode)
{
if (!bOk)
rcCode=SetError(this,SANE_STATUS_IO_ERROR,
"error in reg out: %d,%d,%08X",iRegister,cch,ulValue);
else
{
i=TransferControlMsg(this, /* handle */
0xC0, /* request type */
0x00, /* request */
iRegister, /* value */
0, /* index */
pchTransfer, cch, /* bytes, size */
USB_TIMEOUT_JIFFIES); /* TO, jiffies... */
if (i<0)
rcCode=SetError(this,SANE_STATUS_IO_ERROR,
"error during register check");
}
}
if (!rcCode && memcmp(pchTransfer,pchBuffer,cch))
{
DumpBuffer(stdout,pchTransfer,cch);
rcCode=SetError(this,SANE_STATUS_IO_ERROR,
"check register failed for %d,%d,%08X",
iRegister,cch,ulValue);
}
free(pchTransfer); free(pchBuffer);
return rcCode;
}
/* **********************************************************************
cchRead=BulkRead(fh,cchBulk)
********************************************************************** */
__SM3600EXPORT__
int BulkRead(TInstance *this, FILE *fhOut, unsigned int cchBulk)
{
int cchRead,rc;
char *pchBuffer;
INST_ASSERT();
pchBuffer=(char*)malloc(cchBulk);
CHECK_POINTER(pchBuffer);
cchRead=0;
rc=0;
while (!rc && cchBulk)
{
int cchChunk;
int cchReal;
cchChunk=cchBulk;
if (cchChunk>0x1000)
cchChunk=0x1000;
cchReal=TransferBulkRead(this,
0x82,
pchBuffer+cchRead,
cchChunk,
USB_TIMEOUT_JIFFIES);
dprintf(DEBUG_COMM,"bulk read: %d -> %d\n",cchChunk,cchReal);
if (cchReal>=0)
{
cchBulk-=cchReal;
cchRead+=cchReal;
if (cchReal<cchChunk) /* last Chunk of a series */
break;
}
else
{
rc=SetError(this,SANE_STATUS_IO_ERROR,
"bulk read of %d bytes failed: %s",
cchChunk,
"I/O error"
);
continue;
}
}
dprintf(DEBUG_COMM,"writing %d bytes\n",cchRead);
if (fhOut && !rc)
{
fwrite(pchBuffer,1,cchRead,fhOut);
if (ferror(fhOut))
rc=SetError(this,SANE_STATUS_IO_ERROR,
"scan file write failed: %s",
strerror(errno));
}
free(pchBuffer);
return rc ? -1 : cchRead;
}
#endif
/* **********************************************************************
cchRead=BulkReadBuffer(puchBuffer, cchBulk)
********************************************************************** */
__SM3600EXPORT__
int BulkReadBuffer(TInstance *this,
unsigned char *puchBufferOut,
unsigned int cchBulk)
{
int cchRead,rc;
char *pchBuffer;
INST_ASSERT();
pchBuffer=(char*)malloc(cchBulk);
CHECK_POINTER(pchBuffer);
cchRead=0;
rc=0;
while (!rc && cchBulk)
{
int cchChunk;
int cchReal;
cchChunk=cchBulk;
if (cchChunk>0x1000)
cchChunk=0x1000;
cchReal=TransferBulkRead(this,
0x82,
pchBuffer+cchRead,
cchChunk,
USB_TIMEOUT_JIFFIES);
dprintf(DEBUG_COMM,"bulk read: %d -> %d\n",cchChunk,cchReal);
if (cchReal>=0)
{
cchBulk-=cchReal;
cchRead+=cchReal;
if (cchReal<cchChunk) /* last Chunk of a series */
break;
}
else
rc=SetError(this,SANE_STATUS_IO_ERROR,
"bulk read of %d bytes failed: %s",
cchChunk,
"I/O error"
);
}
dprintf(DEBUG_COMM,"writing %d bytes\n",cchRead);
if (!rc && puchBufferOut)
memcpy(puchBufferOut,pchBuffer,cchRead);
free(pchBuffer);
return rc ? -1 : cchRead;
}
/* **********************************************************************
RegRead(iRegister, int cch)
Read register in big endian (INTEL-) format.
********************************************************************** */
__SM3600EXPORT__
unsigned int RegRead(TInstance *this, int iRegister, int cch)
{
char *pchTransfer;
int i;
unsigned int n;
INST_ASSERT();
if (cch<1 || cch>4)
{
SetError(this,SANE_STATUS_INVAL,
"unsupported control read size %d",cch);
return 0;
}
pchTransfer=calloc(1,cch);
CHECK_POINTER(pchTransfer);
i=TransferControlMsg(this, /* handle */
0xC0, /* request type */
0x00, /* request */
iRegister, /* value */
0, /* index */
pchTransfer, cch, /* bytes, size */
USB_TIMEOUT_JIFFIES); /* TO, jiffies... */
if (i>=0)
{
n=0;
for (i=cch-1; i>=0; i--)
n=(n<<8)|(unsigned char)pchTransfer[i];
free(pchTransfer);
return n;
}
free(pchTransfer);
SetError(this,SANE_STATUS_IO_ERROR,"error during register read");
return 0;
}
|