1 /* sane - Scanner Access Now Easy.
2    Copyright (C) 2007 Jeremy Johnson
3    This file is part of a SANE backend for Ricoh IS450
4    and IS420 family of HS2P Scanners using the SCSI controller.
5 
6    This file is part of the SANE package.
7 
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2 of the
11    License, or (at your option) any later version.
12 
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17 
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <https://www.gnu.org/licenses/>.
20 
21    As a special exception, the authors of SANE give permission for
22    additional uses of the libraries contained in this release of SANE.
23 
24    The exception is that, if you link a SANE library with other files
25    to produce an executable, this does not by itself cause the
26    resulting executable to be covered by the GNU General Public
27    License.  Your use of that executable is in no way restricted on
28    account of linking the SANE library code into it.
29 
30    This exception does not, however, invalidate any other reasons why
31    the executable file might be covered by the GNU General Public
32    License.
33 
34    If you submit changes to SANE to the maintainers to be included in
35    a subsequent release, you agree by submitting the changes that
36    those changes may be distributed with this exception intact.
37 
38    If you write modifications of your own for SANE, it is your choice
39    whether to permit this exception to apply to your modifications.
40    If you do not wish that, delete this exception notice. */
41 
42 #include <time.h>
43 #include "hs2p.h"
44 
45 
46 static SANE_String_Const
print_devtype(SANE_Byte devtype)47 print_devtype (SANE_Byte devtype)
48 {
49   int i = devtype;
50   static SANE_String devtypes[] = {
51     "disk",
52     "tape",
53     "printer",
54     "processor",
55     "CD-writer",
56     "CD-drive",
57     "scanner",
58     "optical-drive",
59     "jukebox",
60     "communicator"
61   };
62 
63   return (i >= 0 && i < NELEMS (devtypes)) ? devtypes[i] : "unknown-device";
64 }
65 static void
print_bytes(const void *buf, size_t bufsize)66 print_bytes (const void *buf, size_t bufsize)
67 {
68   const SANE_Byte *bp;
69   unsigned i;
70 
71   for (i = 0, bp = buf; i < bufsize; i++, bp++)
72     DBG (DBG_error, "%3d: 0x%02x %d\n", i, *bp, *bp);
73 }
74 
75 static void
ScannerDump(HS2P_Scanner * s)76 ScannerDump (HS2P_Scanner * s)
77 {
78   int i;
79   HS2P_Info *info;
80   SANE_Device *sdev;
81 
82   info = &s->hw->info;
83   sdev = &s->hw->sane;
84 
85   DBG (DBG_info, "\n\n");
86   DBG (DBG_info, ">> ScannerDump:\n");
87   DBG (DBG_info, "SANE Device: '%s' Vendor: '%s' Model: '%s' Type: '%s'\n",
88        sdev->name, sdev->vendor, sdev->model, sdev->type);
89 
90   DBG (DBG_info, "Type: '%s' Vendor: '%s' Product: '%s' Revision: '%s'\n",
91        print_devtype (info->devtype), info->vendor, info->product,
92        info->revision);
93 
94   DBG (DBG_info, "Automatic Document Feeder: %s%s%s%s\n",
95        info->hasADF ? "Installed " : "Not Installed ",
96        info->hasSimplex ? "simplex" : "",
97        info->hasDuplex ? "duplex" : "",
98        info->hasARDF ? "reverse double-sided" : "");
99   DBG (DBG_info, "Endorser             :%s\n",
100        info->hasEndorser ? " <Installed>" : " <Not Installed>");
101   DBG (DBG_info, "Image Processing Unit:%s\n",
102        info->hasIPU ? " <Installed>" : " <Not Installed>");
103   DBG (DBG_info, "Extended Board       :%s\n",
104        info->hasXBD ? " <Installed>" : " <Not Installed>");
105 
106   DBG (DBG_info, "\n");
107   DBG (DBG_info, "Image Composition Support\n");
108   DBG (DBG_info, "Line Art (B/W) Support      : %s\n",
109        info->supports_lineart ? "Yes" : "No");
110   DBG (DBG_info, "Dithering (Halftone) Support: %s\n",
111        info->supports_dithering ? "Yes" : "No");
112   DBG (DBG_info, "Error Diffusion Support     : %s\n",
113        info->supports_errordiffusion ? "Yes" : "No");
114   DBG (DBG_info, "Color Support               : %s\n",
115        info->supports_color ? "Yes" : "No");
116   DBG (DBG_info, "4 Bit Gray Support          : %s\n",
117        info->supports_4bitgray ? "Yes" : "No");
118   DBG (DBG_info, "5-8 Bit Gray Support        : %s\n",
119        info->supports_8bitgray ? "Yes" : "No");
120 
121   DBG (DBG_info, "Image Data processing:%s%s%s%s%s%s\n",
122        info->supports_whiteframing ? " <White Frame>" : "",
123        info->supports_blackframing ? " <Black Frame>" : "",
124        info->supports_edgeextraction ? " <Edge Extraction>" : "",
125        info->supports_noiseremoval ? " <Noise Filter>" : "",
126        info->supports_smoothing ? " <Smooth>" : "",
127        info->supports_linebolding ? " <Line Bolding>" : "");
128 
129   DBG (DBG_info, "Image Compression:%s%s%s%s\n",
130        info->supports_MH ? " <MH support>" : "",
131        info->supports_MR ? " <MR support>" : "",
132        info->supports_MMR ? " <MMR support>" : "",
133        info->supports_MHB ? " <MH byte boundary support>" : "");
134   DBG (DBG_info, "Marker Recognition: %s\n",
135        info->supports_markerrecognition ? "<supported>" : "<not supported>");
136   DBG (DBG_info, "Size Recognition  : %s\n",
137        info->supports_sizerecognition ? "<supported>" : "<not supported>");
138   DBG (DBG_info, "X Maximum Output Pixels = %d\n", info->xmaxoutputpixels);
139 
140   /*
141      DBG (DBG_info, "Optional Features:%s%s%s%s\n",
142      info->canBorderRecog ? " <Border Recognition>" : "",
143      info->canBarCode ? " <BarCode Decoding>" : "",
144      info->canIcon ? " <Icon Generation>" : "",
145      info->canSection ? " <Section Support>" : "");
146    */
147 
148   DBG (DBG_info, "Max bytes per scan-line: %d (%d pixels)\n",
149        info->xmaxoutputpixels / 8, info->xmaxoutputpixels);
150 
151   DBG (DBG_info, "Basic resolution   (X/Y) : %d/%d\n", info->resBasicX,
152        info->resBasicY);
153   DBG (DBG_info, "Maximum resolution (X/Y) : %d/%d\n", info->resMaxX,
154        info->resMaxY);
155   DBG (DBG_info, "Minimum resolution (X/Y) : %d/%d\n", info->resMinX,
156        info->resMinY);
157   DBG (DBG_info, "Standard Resolutions:\n");
158   for (i = 1; i <= info->resStdList[0]; i++)
159     DBG (DBG_info, " %d\n", info->resStdList[i]);
160 
161   DBG (DBG_info,
162        "Window Width/Height (in basic res) %d/%d (%.2f/%.2f inches)\n",
163        info->winWidth, info->winHeight,
164        (info->resBasicX !=
165 	0) ? ((float) info->winWidth) / info->resBasicX : 0.0,
166        (info->resBasicY) ? ((float) info->winHeight) / info->resBasicY : 0.0);
167 
168   /*
169      DBG (DBG_info, "Summary:%s%s%s\n",
170      info->canDuplex ? "Duplex Scanner" : "Simplex Scanner",
171      info->canACE ? " (ACE capable)" : "",
172      info->canCheckADF ? " (ADF Paper Sensor capable)" : "");
173    */
174 
175   DBG (DBG_info, "Buffer Full Ratio     = %#02x\n",
176        info->cxn.buffer_full_ratio);
177   DBG (DBG_info, "Buffer Empty Ratio    = %#02x\n",
178        info->cxn.buffer_empty_ratio);
179   DBG (DBG_info, "Bus Inactive Limit    = %#02x\n",
180        info->cxn.bus_inactive_limit[0] << 8 | info->cxn.
181        bus_inactive_limit[1]);
182   DBG (DBG_info, "Disconnect Time Limit = %#04x\n",
183        info->cxn.disconnect_time_limit[0] << 8 | info->cxn.
184        disconnect_time_limit[1]);
185   DBG (DBG_info, "Connect Time Limit    = %#02x\n",
186        info->cxn.connect_time_limit[0] << 8 | info->cxn.
187        connect_time_limit[1]);
188   DBG (DBG_info, "Maximum Burst Size    = %#04x\n",
189        info->cxn.maximum_burst_size[0] << 8 | info->cxn.
190        maximum_burst_size[1]);
191   DBG (DBG_info, "DTDC                  = %#02x\n", info->cxn.dtdc & 0x03);
192 
193   DBG (DBG_info, "White Balance is %s\n",
194        info->white_balance == 1 ? "Absolute" : "Relative");
195   DBG (DBG_info, "Medium Wait Timer is <not supported>\n");	/* get_medium_wait_timer(fd) */
196   DBG (DBG_info, "Scan Wait Mode is %s\n",
197        info->scan_wait_mode == 0 ? "OFF" : "ON");
198   DBG (DBG_info, "Service Mode is in Select %s Mode\n",
199        info->service_mode == 0 ? "Self-Diagnostics" : "Optical Adjustment");
200 
201   sprintf (info->inquiry_data, "Vendor: %s Product: %s Rev: %s %s%s\n",
202 	   info->vendor, info->product, info->revision,
203 	   info->hasADF && info->hasDuplex ? "Duplex Scanner" : "",
204 	   info->hasADF && info->hasSimplex ? "Simplex Scanner" : "");
205 
206   DBG (DBG_info, "duplex_default=%d\n", info->default_duplex);
207   /*
208      DBG (DBG_info, "autoborder_default=%d\n", info->autoborder_default);
209      DBG (DBG_info, "batch_default=%d\n", info->batch_default);
210      DBG (DBG_info, "deskew_default=%d\n", info->deskew_default);
211      DBG (DBG_info, "check_adf_default=%d\n", info->check_adf_default);
212      DBG (DBG_info, "timeout_adf_default=%d\n", info->timeout_adf_default);
213      DBG (DBG_info, "timeout_manual_default=%d\n", info->timeout_manual_default);
214      DBG (DBG_info, "control_panel_default=%d\n", info->control_panel_default);
215    */
216 
217   DBG (DBG_info, "bmu = %d\n", info->bmu);
218   DBG (DBG_info, "mud = %d\n", info->mud);
219   DBG (DBG_info, "white balance = %#0x\n", info->white_balance);
220   DBG (DBG_info, "adf control = %#0x\n", info->adf_control);
221   DBG (DBG_info, "adf mode control = %#0x\n", info->adf_mode_control);
222   DBG (DBG_info, "endorser control = %#0x\n", info->endorser_control);
223   DBG (DBG_info, "endorser string = %s\n", info->endorser_string);
224   DBG (DBG_info, "scan wait mode = %#0x\n", info->scan_wait_mode);
225   DBG (DBG_info, "service mode = %#0x\n", info->service_mode);
226 
227   DBG (DBG_info, "BasicXRes = %d\n", info->resBasicX);
228   DBG (DBG_info, "BasicYRes = %d\n", info->resBasicY);
229 
230   DBG (DBG_info, "XResStep  = %d\n", info->resXstep);
231   DBG (DBG_info, "YResStep  = %d\n", info->resYstep);
232 
233   DBG (DBG_info, "MaxXres   = %d\n", info->resMaxX);
234   DBG (DBG_info, "MaxYres   = %d\n", info->resMaxY);
235 
236   DBG (DBG_info, "MinXres   = %d\n", info->resMinX);
237   DBG (DBG_info, "MinYres   = %d\n", info->resMinY);
238 
239   DBG (DBG_info, "Width     = %d\n", info->winWidth);
240   DBG (DBG_info, "Height    = %d\n", info->winHeight);
241 
242   DBG (DBG_info, "<< ScannerDump\n");
243 }
244 static void
print_vpd_info(struct inquiry_vpd_data *vbuf)245 print_vpd_info (struct inquiry_vpd_data *vbuf)
246 {
247   DBG (DBG_info, "VPD IDENTIFIER C0H\n");
248   DBG (DBG_info, "[00] Peripheral             %#02x\n", vbuf->devtype);
249   DBG (DBG_info, "[01] Page Code              %#02x\n", vbuf->pagecode);
250   DBG (DBG_info, "[02] reserved               %#02x\n", vbuf->byte2);
251   DBG (DBG_info, "[03] Page Length            %#02x\n", vbuf->pagelength);
252   DBG (DBG_info, "[04] ADF ID                 %#02x\n", vbuf->adf_id);
253   DBG (DBG_info, "[05] Endorser ID            %#02x\n", vbuf->end_id);
254   DBG (DBG_info, "[06] Image Processing Unit  %#02x\n", vbuf->ipu_id);
255   DBG (DBG_info, "[07] Image Composition      %#02x\n",
256        vbuf->imagecomposition);
257   DBG (DBG_info, "[08] Image Data Processing  %lu\n",
258        _2btol (&vbuf->imagedataprocessing[0]));
259   DBG (DBG_info, "[10] Compression            %#02x\n", vbuf->compression);
260   DBG (DBG_info, "[11] Marker Recognition     %#02x\n",
261        vbuf->markerrecognition);
262   DBG (DBG_info, "[12] Size Recognition       %#02x\n",
263        vbuf->sizerecognition);
264   DBG (DBG_info, "[13] reserved               %#02x\n", vbuf->byte13);
265   DBG (DBG_info, "[14] X Maximum Output Pixel %lu\n",
266        _2btol (&vbuf->xmaxoutputpixels[0]));
267 }
268 static void
print_jis_info(struct inquiry_jis_data *jbuf)269 print_jis_info (struct inquiry_jis_data *jbuf)
270 {
271   DBG (DBG_info, "JIS IDENTIFIER F0H\n");
272   DBG (DBG_info, "[00] devtype   %#02x\n", jbuf->devtype);
273   DBG (DBG_info, "[01] Page Code %#02x\n", jbuf->pagecode);
274   DBG (DBG_info, "[02] JIS Ver   %#02x\n", jbuf->jisversion);
275   DBG (DBG_info, "[03] reserved1 %#02x\n", jbuf->reserved1);
276   DBG (DBG_info, "[04] Page Len  %#02x\n", jbuf->alloclen);
277   DBG (DBG_info, "[05] BasicXRes %lu\n", _2btol (&jbuf->BasicRes.x[0]));
278   DBG (DBG_info, "[07] BasicYRes %lu\n", _2btol (&jbuf->BasicRes.y[0]));
279   DBG (DBG_info, "[09] Resolution step %#02x\n", jbuf->resolutionstep);
280   DBG (DBG_info, "[10] MaxXRes   %lu\n", _2btol (&jbuf->MaxRes.x[0]));
281   DBG (DBG_info, "[12] MaxYRes   %lu\n", _2btol (&jbuf->MaxRes.y[0]));
282   DBG (DBG_info, "[14] MinXRes   %lu\n", _2btol (&jbuf->MinRes.x[0]));
283   DBG (DBG_info, "[16] MinYRes   %lu\n", _2btol (&jbuf->MinRes.y[0]));
284   DBG (DBG_info, "[18] Std Res   %#0x\n",
285        (jbuf->standardres[0] << 8) | jbuf->standardres[1]);
286   DBG (DBG_info, "[20] Win Width %lu\n", _4btol (&jbuf->Window.width[0]));	/* Manual says 4787/12B3H pixels @400dpi = 12in */
287   DBG (DBG_info, "[24] Win Len   %lu\n", _4btol (&jbuf->Window.length[0]));	/* Manual says 6803/1A93H pixels @400dpi = 17in) */
288   DBG (DBG_info, "[28] function  %#02x\n", jbuf->functions);
289   DBG (DBG_info, "[29] reserved  %#02x\n", jbuf->reserved2);
290 }
291 
292 /* 1-3-1                  TEST UNIT READY
293   Byte0: |                           0x00                       |
294   Byte1: | 7-5 Logical Unit Number | Reserved                   |
295   Byte2: |                           Reserved                   |
296   Byte3: |                           Reserved                   |
297   Byte4: |                           Reserved                   |
298   Byte5: | 7-6 Vendor Unique | 5-2   Reserved | 1 Flag | 0 Link |
299 */
300 static SANE_Status
test_unit_ready(int fd)301 test_unit_ready (int fd)
302 {
303   static SANE_Byte cmd[6];
304   SANE_Status status;
305   DBG (DBG_proc, ">> test_unit_ready\n");
306 
307   memset (cmd, 0, sizeof (cmd));
308   cmd[0] = HS2P_SCSI_TEST_UNIT_READY;
309   status = sanei_scsi_cmd (fd, cmd, sizeof (cmd), 0, 0);
310 
311   DBG (DBG_proc, "<< test_unit_ready\n");
312   return (status);
313 }
314 
315 /* 1-3-2                  REQUEST SENSE
316   Byte0: |                           0x00                       |
317   Byte1: | 7-5 Logical Unit Number | Reserved                   |
318   Byte2: |                           Reserved                   |
319   Byte3: |                           Reserved                   |
320   Byte4: |                     Allocation Length                |
321   Byte5: | 7-6 Vendor Unique | 5-2   Reserved | 1 Flag | 0 Link |
322 */
323 
324 #if 0
325 static SANE_Status
326 get_sense_data (int fd, SENSE_DATA * sense_data)
327 {
328   SANE_Status status;
329   DBG (DBG_sane_proc, ">> get_sense_data\n");
330 
331   static SANE_Byte cmd[6];
332   size_t len;
333 
334   len = sizeof (*sense_data);
335   memset (sense_data, 0, len);
336   memset (cmd, 0, sizeof (cmd));
337 
338   cmd[0] = HS2P_SCSI_REQUEST_SENSE;
339   cmd[4] = len;
340 
341   status = sanei_scsi_cmd (fd, cmd, sizeof (cmd), sense_data, &len);
342 
343   DBG (DBG_proc, "<< get_sense_data\n");
344   return (status);
345 }
346 #endif
347 
348 static SANE_Status
print_sense_data(int dbg_level, SENSE_DATA * data)349 print_sense_data (int dbg_level, SENSE_DATA * data)
350 {
351   SANE_Status status = SANE_STATUS_GOOD;
352   SANE_Byte *bp, *end;
353   SANE_Int i;
354 
355   DBG (DBG_sane_proc, ">> print_sense_data\n");
356 
357   bp = (SANE_Byte *) data;
358   end = bp + (SANE_Byte) sizeof (SENSE_DATA);
359   for (i = 0; bp < end; bp++, i++)
360     {
361       DBG (dbg_level, "Byte #%2d is %3d, 0x%02x\n", i, *bp, *bp);
362     }
363 
364   DBG (dbg_level, "Valid=%1d, ErrorCode=%#x\n",
365        (data->error_code & 0x80) >> 7, data->error_code & 0x7F);
366   DBG (dbg_level, "Segment number = %d\n", data->segment_number);
367   DBG (dbg_level,
368        "F-mark=%1d, EOM=%1d, ILI=%1d, Reserved=%1d, SenseKey=%#x\n",
369        (data->sense_key & 0x80) >> 7, (data->sense_key & 0x40) >> 6,
370        (data->sense_key & 0x20) >> 5, (data->sense_key & 0x10) >> 4,
371        (data->sense_key & 0x0F));
372   DBG (dbg_level, "Information Byte = %lu\n", _4btol (data->information));
373   DBG (dbg_level, "Additional Sense Length = %d\n", data->sense_length);
374   DBG (dbg_level, "Command Specific Information = %lu\n",
375        _4btol (data->command_specific_information));
376   DBG (dbg_level, "Additional Sense Code = %#x\n", data->sense_code);
377   DBG (dbg_level, "Additional Sense Code Qualifier = %#x\n",
378        data->sense_code_qualifier);
379 
380   DBG (DBG_proc, "<< print_sense_data\n");
381   return (status);
382 }
383 
384 static struct sense_key *
lookup_sensekey_errmsg(int code)385 lookup_sensekey_errmsg (int code)
386 {
387   int i;
388   struct sense_key *k = &sensekey_errmsg[0];
389 
390   for (i = 0; i < 16; i++, k++)
391     if (k->key == code)
392       return k;
393   return NULL;
394 }
395 static struct ASCQ *
lookup_ascq_errmsg(unsigned int code)396 lookup_ascq_errmsg (unsigned int code)
397 {
398   unsigned int i;
399   struct ASCQ *k = &ascq_errmsg[0];
400 
401   for (i = 0; i < 74; i++, k++)
402     if (k->codequalifier == code)
403       return k;
404   return NULL;
405 }
406 
407 /* a sensible sense handler
408    arg is a pointer to the associated HS2P_Scanner structure
409 
410    SENSE DATA FORMAT:  14 bytes bits[7-0]
411    Byte  0: [7]:valid [6-0]:Error Code
412    Byte  1: Segment Number
413    Byte  2: [7]: F-mark; [6]:EOM; [5]:ILI; [4]:reserved; [3-0]:Sense Key
414    Byte  3: Information Byte
415    Byte  4: Information Byte
416    Byte  5: Information Byte
417    Byte  6: Information Byte
418    Byte  7: Additional Sense Length (n-7)
419    Byte  8: Command Specific Information
420    Byte  9: Command Specific Information
421    Byte 10: Command Specific Information
422    Byte 11: Command Specific Information
423    Byte 12: Additional Sense Code
424    Byte 13: Additional Sense Code Qualifier
425 */
426 static SANE_Status
sense_handler(int __sane_unused__ scsi_fd, u_char * sense_buffer, void *sd)427 sense_handler (int __sane_unused__ scsi_fd, u_char * sense_buffer, void *sd)
428 {
429   u_char sense, asc, ascq, EOM, ILI, ErrorCode, ValidData;
430   u_long MissingBytes;
431   char *sense_str = "";
432 
433   struct sense_key *skey;
434   struct ASCQ *ascq_key;
435   SENSE_DATA *sdp = (SENSE_DATA *) sd;
436   SANE_Int i;
437   SANE_Status status = SANE_STATUS_INVAL;
438   SANE_Char print_sense[(16 * 3) + 1];
439 
440   DBG (DBG_proc, ">> sense_handler\n");
441   if (DBG_LEVEL >= DBG_info)
442     print_sense_data (DBG_LEVEL, (SENSE_DATA *) sense_buffer);
443 
444   /* store sense_buffer */
445   DBG (DBG_info, ">> copying %lu bytes from sense_buffer[] to sense_data\n",
446        (u_long) sizeof (SENSE_DATA));
447   memcpy (sdp, sense_buffer, sizeof (SENSE_DATA));
448   if (DBG_LEVEL >= DBG_info)
449     print_sense_data (DBG_LEVEL, sdp);
450 
451   ErrorCode = sense_buffer[0] & 0x7F;
452   ValidData = (sense_buffer[0] & 0x80) != 0;
453   sense = sense_buffer[2] & 0x0f;	/* Sense Key */
454   asc = sense_buffer[12];	/* Additional Sense Code */
455   ascq = sense_buffer[13];	/* Additional Sense Code Qualifier */
456   EOM = (sense_buffer[2] & 0x40) != 0;	/* End Of Media */
457   ILI = (sense_buffer[2] & 0x20) != 0;	/* Invalid Length Indicator */
458   MissingBytes = ValidData ? _4btol (&sense_buffer[3]) : 0;
459 
460   DBG (DBG_sense,
461        "sense_handler: sense_buffer=%#x, sense=%#x, asc=%#x, ascq=%#x\n",
462        sense_buffer[0], sense, asc, ascq);
463   DBG (DBG_sense,
464        "sense_handler: ErrorCode %02x ValidData: %d "
465        "EOM: %d ILI: %d MissingBytes: %lu\n", ErrorCode, ValidData, EOM,
466        ILI, MissingBytes);
467 
468   memset (print_sense, '\0', sizeof (print_sense));
469   for (i = 0; i < 16; i++)
470     sprintf (print_sense + strlen (print_sense), "%02x ", sense_buffer[i]);
471   DBG (DBG_sense, "sense_handler: sense=%s\n", print_sense);
472 
473   if (ErrorCode != 0x70 && ErrorCode != 0x71)
474     {
475       DBG (DBG_error, "sense_handler: error code is invalid.\n");
476       return SANE_STATUS_IO_ERROR;	/* error code is invalid */
477     }
478 
479   skey = lookup_sensekey_errmsg (sense);	/* simple sequential search */
480   DBG (DBG_sense, "sense_handler: sense_key=%#x '%s - %s'\n", skey->key,
481        skey->meaning, skey->description);
482 
483   DBG (DBG_sense, "Looking up ascq=(%#x,%#x)=%#x\n", asc, ascq,
484        (asc << 8) | ascq);
485   ascq_key = lookup_ascq_errmsg ((asc << 8) | ascq);	/* simple sequential search */
486   DBG (DBG_sense, "sense_handler: ascq=(%#x,%#x): %#x '%s'\n", asc, ascq,
487        ascq_key->codequalifier, ascq_key->description);
488 
489   /* handle each sense key: Translate from HS2P message to SANE_STATUS_ message
490    * SANE_STATUS_GOOD, _ACCESS_DEINIED, _NO_MEM, _INVAL, _IO_ERROR, _DEVICE_BUSY,
491    * _EOF, _UNSUPPORTED, _CANCELLED, _JAMMED, _NO_DOCS, _COVER_OPEN
492    */
493   switch (sense)
494     {
495     case 0x00:			/* no sense */
496       status = SANE_STATUS_GOOD;
497       break;
498     case 0x01:			/* recovered error */
499       status = SANE_STATUS_INVAL;
500       break;
501     case 0x02:			/* not ready */
502       status = SANE_STATUS_DEVICE_BUSY;
503       break;
504     case 0x03:			/* medium error */
505       status = SANE_STATUS_JAMMED;
506       break;
507     case 0x04:			/* hardware error */
508       status = SANE_STATUS_IO_ERROR;
509       break;
510     case 0x05:			/* illegal request */
511       status = SANE_STATUS_INVAL;
512       break;
513     case 0x06:			/* unit attention */
514       status = SANE_STATUS_GOOD;
515       break;
516     case 0x07:			/* data protect */
517       status = SANE_STATUS_INVAL;
518       break;
519     case 0x08:			/* blank check */
520       status = SANE_STATUS_INVAL;
521       break;
522     case 0x09:			/* vendor specific */
523       status = SANE_STATUS_INVAL;
524       break;
525     case 0x0A:			/* copy aborted */
526       status = SANE_STATUS_CANCELLED;
527       break;
528     case 0x0B:			/* aborted command */
529       status = SANE_STATUS_CANCELLED;
530       break;
531     case 0x0C:			/* equal */
532       status = SANE_STATUS_INVAL;
533       break;
534     case 0x0D:			/* volume overflow */
535       status = SANE_STATUS_INVAL;
536       break;
537     case 0x0E:			/* miscompare */
538       status = SANE_STATUS_INVAL;
539       break;
540     case 0x0F:			/* reserved */
541       status = SANE_STATUS_INVAL;
542       break;
543     }
544   if (ErrorCode == 0x70)	/* Additional Sense Codes available */
545     switch ((asc << 8) | ascq)
546       {
547       case 0x0000:		/* No additional Information */
548 	status = SANE_STATUS_GOOD;
549 	break;
550       case 0x0002:		/* End of Medium */
551 	status = SANE_STATUS_NO_DOCS;
552 	break;
553       case 0x0005:		/* End of Data */
554 	status = SANE_STATUS_EOF;
555 	break;
556       case 0x0400:		/* LUN not ready */
557 	status = SANE_STATUS_DEVICE_BUSY;
558 	break;
559       case 0x0401:		/* LUN becoming ready */
560 	status = SANE_STATUS_DEVICE_BUSY;
561 	break;
562       case 0x0403:		/* LUN not ready. Manual intervention needed */
563 	status = SANE_STATUS_IO_ERROR;
564 	break;
565       case 0x0500:		/* LUN doesn't respond to selection */
566 	status = SANE_STATUS_INVAL;
567 	break;
568       case 0x0700:		/* Multiple peripheral devices selected */
569 	status = SANE_STATUS_INVAL;
570 	break;
571       case 0x1100:		/* Unrecovered read error */
572 	status = SANE_STATUS_IO_ERROR;
573 	break;
574       case 0x1101:		/* Read retries exhausted */
575 	status = SANE_STATUS_IO_ERROR;
576 	break;
577       case 0x1501:		/* Mechanical positioning error */
578 	status = SANE_STATUS_IO_ERROR;
579 	break;
580       case 0x1A00:		/* Parameter list length error */
581 	status = SANE_STATUS_INVAL;
582 	break;
583       case 0x2000:		/* Invalid command operation code */
584 	status = SANE_STATUS_INVAL;
585 	break;
586       case 0x2400:		/* Invalid field in CDB (check field pointer) */
587 	status = SANE_STATUS_INVAL;
588 	break;
589       case 0x2500:		/* LUN not supported */
590 	status = SANE_STATUS_UNSUPPORTED;
591 	break;
592       case 0x2600:		/* Invalid field in parameter list (check field pointer) */
593 	status = SANE_STATUS_INVAL;
594 	break;
595       case 0x2900:		/* Power on, reset, or BUS DEVICE RESET occurred */
596 	status = SANE_STATUS_GOOD;
597 	break;
598       case 0x2A01:		/* (MODE parameter changed) */
599 	status = SANE_STATUS_INVAL;
600 	break;
601       case 0x2C00:		/* Command sequence error */
602 	status = SANE_STATUS_INVAL;
603 	break;
604       case 0x2C01:		/* Too many windows specified */
605 	status = SANE_STATUS_INVAL;
606 	break;
607       case 0x2C02:		/* Invalid combination of windows specified */
608 	status = SANE_STATUS_INVAL;
609 	break;
610       case 0x3700:		/* (Rounded parameter) */
611 	status = SANE_STATUS_INVAL;
612 	break;
613       case 0x3900:		/* (Saving parameters not supported) */
614 	status = SANE_STATUS_INVAL;
615 	break;
616       case 0x3A00:		/* Medium not present */
617 	status = SANE_STATUS_NO_DOCS;
618 	break;
619       case 0x3B09:		/* Read past end of medium */
620 	status = SANE_STATUS_EOF;
621 	break;
622       case 0x3B0B:		/* Position past end of medium */
623 	status = SANE_STATUS_EOF;
624 	break;
625       case 0x3D00:		/* Invalid bits in IDENTIFY message */
626 	status = SANE_STATUS_INVAL;
627 	break;
628       case 0x4300:		/* Message error */
629 	status = SANE_STATUS_INVAL;
630 	break;
631       case 0x4500:		/* Select/Reselect failure */
632 	status = SANE_STATUS_IO_ERROR;
633 	break;
634       case 0x4700:		/* (SCSI parity error) */
635 	status = SANE_STATUS_IO_ERROR;
636 	break;
637       case 0x4800:		/* Initiator detected error message received */
638 	status = SANE_STATUS_IO_ERROR;
639 	break;
640       case 0x4900:		/* Invalid message error */
641 	status = SANE_STATUS_INVAL;
642 	break;
643       case 0x4B00:		/* Data phase error */
644 	status = SANE_STATUS_IO_ERROR;
645 	break;
646       case 0x5300:		/* (Media Load/Eject failed) */
647 	status = SANE_STATUS_IO_ERROR;
648 	break;
649       case 0x6000:		/* Lamp failure */
650 	status = SANE_STATUS_IO_ERROR;
651 	break;
652       case 0x6001:		/* Shading error */
653 	status = SANE_STATUS_IO_ERROR;
654 	break;
655       case 0x6002:		/* White adjustment error */
656 	status = SANE_STATUS_IO_ERROR;
657 	break;
658       case 0x6010:		/* Reverse Side Lamp Failure */
659 	status = SANE_STATUS_IO_ERROR;
660 	break;
661       case 0x6200:		/* Scan head positioning error */
662 	status = SANE_STATUS_IO_ERROR;
663 	break;
664       case 0x6300:		/* Document Waiting Cancel */
665 	status = SANE_STATUS_CANCELLED;
666 	break;
667       case 0x8000:		/* (PSU over heate) */
668 	status = SANE_STATUS_IO_ERROR;
669 	break;
670       case 0x8001:		/* (PSU 24V fuse down) */
671 	status = SANE_STATUS_IO_ERROR;
672 	break;
673       case 0x8002:		/* (ADF 24V fuse down) */
674 	status = SANE_STATUS_IO_ERROR;
675 	break;
676       case 0x8003:		/* (5V fuse down) */
677 	status = SANE_STATUS_IO_ERROR;
678 	break;
679       case 0x8004:		/* (-12V fuse down) */
680 	status = SANE_STATUS_IO_ERROR;
681 	break;
682       case 0x8100:		/* (ADF 24V power off) */
683 	status = SANE_STATUS_IO_ERROR;
684 	break;
685       case 0x8102:		/* (Base 12V power off) */
686 	status = SANE_STATUS_IO_ERROR;
687 	break;
688       case 0x8103:		/* Lamp cover open (Lamp 24V power off) */
689 	status = SANE_STATUS_IO_ERROR;
690 	break;
691       case 0x8104:		/* (-12V power off) */
692 	status = SANE_STATUS_IO_ERROR;
693 	break;
694       case 0x8105:		/* (Endorser 6V power off) */
695 	status = SANE_STATUS_IO_ERROR;
696 	break;
697       case 0x8106:		/* SCU 3.3V power down error */
698 	status = SANE_STATUS_IO_ERROR;
699 	break;
700       case 0x8107:		/* RCU 3.3V power down error */
701 	status = SANE_STATUS_IO_ERROR;
702 	break;
703       case 0x8108:		/* OIPU 3.3V power down error */
704 	status = SANE_STATUS_IO_ERROR;
705 	break;
706       case 0x8200:		/* Memory Error (Bus error) */
707 	status = SANE_STATUS_IO_ERROR;
708 	break;
709       case 0x8210:		/* Reverse-side memory error (Bus error) */
710 	status = SANE_STATUS_IO_ERROR;
711 	break;
712       case 0x8300:		/* (Image data processing LSI error) */
713 	status = SANE_STATUS_IO_ERROR;
714 	break;
715       case 0x8301:		/* (Interface LSI error) */
716 	status = SANE_STATUS_IO_ERROR;
717 	break;
718       case 0x8302:		/* (SCSI controller error) */
719 	status = SANE_STATUS_IO_ERROR;
720 	break;
721       case 0x8303:		/* (Compression unit error) */
722 	status = SANE_STATUS_IO_ERROR;
723 	break;
724       case 0x8304:		/* (Marker detect unit error) */
725 	status = SANE_STATUS_IO_ERROR;
726 	break;
727       case 0x8400:		/* Endorser error */
728 	status = SANE_STATUS_IO_ERROR;
729 	break;
730       case 0x8500:		/* (Origin Positioning error) */
731 	status = SANE_STATUS_IO_ERROR;
732 	break;
733       case 0x8600:		/* Mechanical Time Out error (Pick Up Roller error) */
734 	status = SANE_STATUS_IO_ERROR;
735 	break;
736       case 0x8700:		/* (Heater error) */
737 	status = SANE_STATUS_IO_ERROR;
738 	break;
739       case 0x8800:		/* (Thermistor error) */
740 	status = SANE_STATUS_IO_ERROR;
741 	break;
742       case 0x8900:		/* ADF cover open */
743 	status = SANE_STATUS_COVER_OPEN;
744 	break;
745       case 0x8901:		/* (ADF lift up) */
746 	status = SANE_STATUS_COVER_OPEN;
747 	break;
748       case 0x8902:		/* Document jam error for ADF */
749 	status = SANE_STATUS_JAMMED;
750 	break;
751       case 0x8903:		/* Document misfeed for ADF */
752 	status = SANE_STATUS_JAMMED;
753 	break;
754       case 0x8A00:		/* (Interlock open) */
755 	status = SANE_STATUS_COVER_OPEN;
756 	break;
757       case 0x8B00:		/* (Not enough memory) */
758 	status = SANE_STATUS_NO_MEM;
759 	break;
760       case 0x8C00:		/* Size Detection failed */
761 	status = SANE_STATUS_IO_ERROR;
762 	break;
763       default:			/* Should never get here */
764 	status = SANE_STATUS_INVAL;
765 	DBG (DBG_sense,
766 	     "sense_handler: 'Undocumented code': ascq=(%#x,%#x)\n",
767 	     asc & 0xFF00, ascq & 0x00FF);
768 	break;
769       }
770 
771 
772   DBG (DBG_proc, "sense_handler %s: '%s'-'%s' '%s' return:%d\n", sense_str,
773        skey->meaning, skey->description, ascq_key->description, status);
774   return status;
775 }
776 
777 /* VPD IDENTIFIER Page Code 0x00
778  * A list of all Page Codes supported by scanner is returned as data
779  * Byte0 => bit7-5: Peripheral Qualifier, bits4-0: Peripheral Device Type
780  * Byte1 => Page Code of CDB is set as Page Code 0
781  * Byte2 => Reserved
782  * Byte3 => Page Length is 2 because scanner supports just two page codes: C0H and F0H
783  * Byte4 => First Support Page Code
784  * Byte5 => Second Support Page Code
785 */
786 #if 0
787 static SANE_Status
788 vpd_indentifier_00H (int fd)
789 {
790   static SANE_Byte cmd[6];
791   SANE_Status status;
792   DBG (DBG_proc, ">> vpd_identifier_00H\n");
793 
794   cmd[0] = HS2P_SCSI_REQUEST_SENSE;
795   memset (cmd, 0, sizeof (cmd));
796   status = sanei_scsi_cmd (fd, cmd, sizeof (cmd), 0, 0);
797 
798   DBG (DBG_proc, "<< vpd_identifier_00H\n");
799   return (status);
800 }
801 #endif
802 
803 #if 0
804 static SANE_Status
805 vpd_identifier_C0H (int fd)
806 {
807   static SANE_Byte cmd[6];
808   SANE_Status status;
809   DBG (DBG_proc, ">> vpd_identifier_C0H\n");
810 
811   cmd[0] = HS2P_SCSI_REQUEST_SENSE;
812   memset (cmd, 0, sizeof (cmd));
813   status = sanei_scsi_cmd (fd, cmd, sizeof (cmd), 0, 0);
814 
815   DBG (DBG_proc, "<< vpd_identifier_C0H\n");
816   return (status);
817 }
818 #endif
819 
820 /* 1-3-3 INQUIRY : 6 bytes:
821  * Byte0 => 0x12
822  * Byte1 => bits7-5: Logical Unit number
823  *          bits4-1: Reserved
824  *          bit0:    EVPD
825  * Byte2 => Page Code
826  * Byte3 => Reserved
827  * Byte4 => Allocation Length
828  * Byte5 => bits7-6: Vendor Unique
829  *          bits5-2: Reserved
830  *          bit1:    Flag
831  *          bit0:    Link
832 */
833 static SANE_Status
inquiry(int fd, void *buf, size_t * buf_size, SANE_Byte evpd, SANE_Byte page_code)834 inquiry (int fd, void *buf, size_t * buf_size, SANE_Byte evpd,
835 	 SANE_Byte page_code)
836 {
837   static SANE_Byte cmd[6];
838   SANE_Status status;
839   DBG (DBG_proc, ">> inquiry\n");
840 
841   memset (cmd, 0, sizeof (cmd));
842   cmd[0] = HS2P_SCSI_INQUIRY;
843   cmd[1] = evpd;
844   cmd[2] = page_code;
845 /*cmd[3] Reserved */
846   cmd[4] = *buf_size;
847 /*cmd[5] vendorunique+reserved+flag+link */
848   status = sanei_scsi_cmd (fd, cmd, sizeof (cmd), buf, buf_size);
849 
850   DBG (DBG_proc, "<< inquiry\n");
851   return (status);
852 }
853 
854 /* 1-3-6 MODE SELECT -- sets various operation mode parameters for scanner */
855 static SANE_Status
mode_select(int fd, MP * settings)856 mode_select (int fd, MP * settings)
857 {
858   static struct
859   {
860     SELECT cmd;			/* Mode page Select command */
861     MP mp;			/* Hdr + Parameters         */
862   } msc;			/* Mode Select Command      */
863   SANE_Status status;
864   size_t npages;
865 
866   DBG (DBG_proc, ">> mode_select\n");
867 
868   memset (&msc, 0, sizeof (msc));	/* Fill struct with zeros     */
869   msc.cmd.opcode = HS2P_SCSI_MODE_SELECT;	/* choose Mode Select Command */
870   msc.cmd.byte1 &= ~SMS_SP;	/* unset bit0 SavePage to 0   */
871   msc.cmd.byte1 |= SMS_PF;	/* set bit4 PageFormat to 1   */
872   npages = (settings->page.code == 2) ? 16 : 8;
873   msc.cmd.len = sizeof (msc.mp.hdr) + npages;	/* either 4+8 or 4+20      */
874 
875   memcpy (&msc.mp, settings, msc.cmd.len);	/* Copy hdr+pages from Settings to msc.mp  */
876   memset (&msc.mp.hdr, 0, sizeof (msc.mp.hdr));	/* make sure the hdr is all zeros          */
877   /*
878      msc.hdr.data_len     = 0x00;
879      msc.hdr.medium_type  = 0x00;
880      msc.hdr.dev_spec     = 0x00;
881      msc.hdr.blk_desc_len = 0x00;
882    */
883 
884   /* Now execute the whole command */
885   if ((status =
886        sanei_scsi_cmd (fd, &msc, sizeof (msc.cmd) + msc.cmd.len, 0,
887 		       0)) != SANE_STATUS_GOOD)
888     {
889       DBG (DBG_error, "ERROR: mode_select: %s\n", sane_strstatus (status));
890       DBG (DBG_error, "PRINTING CMD BLOCK:\n");
891       print_bytes (&msc.cmd, sizeof (msc.cmd));
892       DBG (DBG_error, "PRINTING MP HEADER:\n");
893       print_bytes (&msc.mp.hdr, sizeof (msc.mp.hdr));
894       DBG (DBG_error, "PRINTING MP PAGES:\n");
895       print_bytes (&msc.mp.page, msc.cmd.len);
896     }
897 
898   DBG (DBG_proc, "<< mode_select\n");
899   return (status);
900 }
901 
902 /* 1-3-7 MODE SENSE -- gets various operation mode parameters from scanner */
903 static SANE_Status
mode_sense(int fd, MP * buf, SANE_Byte page_code)904 mode_sense (int fd, MP * buf, SANE_Byte page_code)
905 {
906   SANE_Status status;
907   SENSE cmd;			/* 6byte cmd */
908   MP msp;			/* Mode Sense Page
909 				 * 4byte hdr + {2bytes +14 bytes}
910 				 * buffer to hold mode sense data gotten from scanner */
911 
912   size_t nbytes;
913 
914   DBG (DBG_proc, ">>>>> mode_sense: fd=%d, page_code=%#02x\n", fd, page_code);
915   nbytes = sizeof (msp);
916 
917   DBG (DBG_info,
918        ">>>>> mode_sense: Zero'ing ModeSenseCommand msc and msp structures\n");
919 
920   memset (&cmd, 0, sizeof (cmd));	/* Fill cmd struct with zeros */
921   memset (&msp, 0, sizeof (msp));	/* Fill msp struct with zeros */
922 
923   /* set up Mode Sense Command */
924   DBG (DBG_info, ">>>>> mode_sense: Initializing Mode Sense cmd\n");
925   cmd.opcode = HS2P_SCSI_MODE_SENSE;
926   cmd.dbd &= ~(1 << 3);		/* Disable Block Description (bit3) is set to 0 */
927   cmd.pc = (page_code & 0x3F);	/* bits 5-0 */
928   cmd.pc &= ~(0x03 << 6);	/* unset PC Field (bits7-6)
929 				 * 00 Current Value is the only effective value
930 				 * 01 Changeable Value
931 				 * 10 Default Value
932 				 * 11 Saved Value */
933   /* cmd.len = ??? Allocation Length */
934 
935   /* Now execute the whole command  and store results in msc */
936   DBG (DBG_info, ">>>>> mode_sense: sanei_scsi_cmd\n");
937   DBG (DBG_info, ">>>>> cmd.opcode=%#0x cmd.dbd=%#02x, cmd.pc=%#02x\n",
938        cmd.opcode, cmd.dbd, cmd.pc);
939 
940   nbytes = (page_code == 2) ? 20 : 12;
941   DBG (DBG_info,
942        ">>>>> sizeof(cmd)=%lu sizeof(msp)=%lu sizeof(hdr)=%lu sizeof(page)=%lu requesting %lu bytes\n",
943        (u_long) sizeof (cmd), (u_long) sizeof (msp),
944        (u_long) sizeof (msp.hdr), (u_long) sizeof (msp.page),
945        (u_long) nbytes);
946 
947   status = sanei_scsi_cmd (fd, &cmd, sizeof (cmd), &msp, &nbytes);
948 
949   if (status != SANE_STATUS_GOOD)
950     {
951       DBG (DBG_error, "ERROR mode_sense: sanei_scsi_cmd error \"%s\"\n",
952 	   sane_strstatus (status));
953       DBG (DBG_error,
954 	   ">>>>> mode sense: number of bytes received from scanner: %lu\n",
955 	   (u_long) nbytes);
956       DBG (DBG_error, "PRINTING CMD BLOCK:\n");
957       print_bytes (&cmd, sizeof (cmd));
958       DBG (DBG_error, "PRINTING MP HEADER:\n");
959       print_bytes (&msp.hdr, sizeof (msp.hdr));
960       DBG (DBG_error, "PRINTING MP PAGES:\n");
961       print_bytes (&msp.page, sizeof (msp.page));
962     }
963   else
964     {
965       /* nbytes = (page_code==2)? 14 : 6; */
966       DBG (DBG_info, ">> >> got %lu bytes from scanner\n", (u_long) nbytes);
967       nbytes -= 4;		/* we won't copy 4 byte hdr */
968       DBG (DBG_info, ">>>>> copying from msp to calling function's buf\n"
969 	   ">>>>> msp.page_size=%lu bytes=%lu buf_size=%lu\n",
970 	   (u_long) sizeof (msp.page), (u_long) nbytes,
971 	   (u_long) sizeof (*buf));
972       memcpy (buf, &(msp.page), nbytes);
973     }
974 
975   DBG (DBG_proc, "<<<<< mode_sense\n");
976   return (status);
977 }
978 
979 static SANE_Status
set_window(int fd, SWD * swd)980 set_window (int fd, SWD * swd)
981 {
982   static struct
983   {
984     struct set_window_cmd cmd;
985     struct set_window_data swd;
986   } win;
987   SANE_Status status;
988   static size_t wdl, tl;	/*window descriptor length, transfer length */
989   DBG (DBG_proc, ">> set_window\n");
990 
991   /* initialize our struct with zeros */
992   memset (&win, 0, sizeof (win));
993 
994   /* fill in struct with opcode */
995   win.cmd.opcode = HS2P_SCSI_SET_WINDOW;
996 
997   /* bytes 1-5 are reserved */
998 
999   /* Transfer length is header + window data */
1000   tl = sizeof (*swd);
1001   _lto3b (tl, &win.cmd.len[0]);	/* 8 + (2*320) = 648 */
1002   DBG (DBG_info,
1003        "set_window: SET WINDOW COMMAND Transfer Length = %lu (should be 648)\n",
1004        (unsigned long) tl);
1005 
1006   /* Copy data from swd (including 8-byte header) to win.swd */
1007   DBG (DBG_info,
1008        "set_window: COPYING %lu bytes from settings to Set Window Command (%lu)\n",
1009        (u_long) sizeof (*swd), (u_long) sizeof (win.swd));
1010   if (!memcpy (&(win.swd), swd, sizeof (*swd)))
1011     {
1012       DBG (DBG_error, "set_window: error with memcpy\n");
1013     }
1014 
1015   /* Set Window Data Header: 0-5:reserved; 6-7:Window Descriptor Length=640 */
1016   wdl = sizeof (win.swd) - sizeof (win.swd.hdr);
1017   _lto2b (wdl, &win.swd.hdr.len[0]);
1018   DBG (DBG_info,
1019        "set_window: SET WINDOW COMMAND Window Descriptor Length = %lu (should be 640)\n",
1020        (unsigned long) wdl);
1021 
1022   /* Now execute command */
1023   DBG (DBG_info,
1024        "set_window: calling sanei_scsi_cmd(%d,&win,%lu, NULL, NULL)\n", fd,
1025        (u_long) sizeof (win));
1026   status = sanei_scsi_cmd (fd, &win, sizeof (win), NULL, NULL);
1027   /*
1028      status = sanei_scsi_cmd2 (fd, &win.cmd, sizeof(win.cmd),  &win.swd, sizeof(win.swd), NULL, NULL);
1029    */
1030   if (status != SANE_STATUS_GOOD)
1031     {
1032       DBG (DBG_error, "*********************\n");
1033       DBG (DBG_error, "ERROR: set_window: %s\n", sane_strstatus (status));
1034       DBG (DBG_error, "PRINTING SWD CMD BLK:\n");
1035       print_bytes (&win.cmd, sizeof (win.cmd));
1036       DBG (DBG_error, "PRINTING SWD HEADER:\n");
1037       print_bytes (&win.swd.hdr, sizeof (win.swd.hdr));
1038       DBG (DBG_error, "PRINTING SWD DATA[0]:\n");
1039       print_bytes (&win.swd.data[0], sizeof (win.swd.data[0]));
1040       DBG (DBG_error, "PRINTING SWD DATA[1]:\n");
1041       print_bytes (&win.swd.data[1], sizeof (win.swd.data[1]));
1042       DBG (DBG_error, "*********************\n");
1043     }
1044 
1045   DBG (DBG_proc, "<< set_window\n");
1046   return (status);
1047 }
1048 
1049 static SANE_Status
get_window(int fd, GWD * gwd)1050 get_window (int fd, GWD * gwd)
1051 {
1052   struct get_window_cmd cmd;
1053   SANE_Status status;
1054   static size_t gwd_size;
1055 
1056   DBG (DBG_proc, ">> get_window\n");
1057 
1058   gwd_size = sizeof (*gwd);
1059   DBG (DBG_info, ">> get_window datalen = %lu\n", (unsigned long) gwd_size);
1060 
1061   /* fill in get_window_cmd */
1062   memset (&cmd, 0, sizeof (cmd));	/* CLEAR cmd */
1063   cmd.opcode = HS2P_SCSI_GET_WINDOW;
1064   cmd.byte1 &= ~0x01;		/* unset single bit 0 */
1065   cmd.win_id = 0x00;		/* either 0 or 1 */
1066   _lto3b (gwd_size, cmd.len);	/* Transfer Length is byte length of DATA to be returned */
1067 
1068   status = sanei_scsi_cmd (fd, &cmd, sizeof (cmd), gwd, &gwd_size);
1069 
1070   DBG (DBG_proc, "<< get_window, datalen = %lu\n", (unsigned long) gwd_size);
1071   return (status);
1072 }
1073 static void
print_window_data(SWD * buf)1074 print_window_data (SWD * buf)
1075 {
1076   int i, j, k;
1077   struct hs2p_window_data *data;
1078   struct window_section *ws;
1079 
1080   DBG (DBG_proc, ">> print_window_data\n");
1081   DBG (DBG_info, "HEADER\n");
1082   for (i = 0; i < 6; i++)
1083     DBG (DBG_info, "%#02x\n", buf->hdr.reserved[i]);
1084   DBG (DBG_info, "Window Descriptor Length=%lu\n\n", _2btol (buf->hdr.len));
1085 
1086   for (i = 0; i < 2; i++)
1087     {
1088       data = &buf->data[i];
1089       DBG (DBG_info, "Window Identifier = %d\n", data->window_id);
1090       DBG (DBG_info, "AutoBit = %#x\n", data->auto_bit);
1091       DBG (DBG_info, "X-Axis Resolution = %lu\n", _2btol (data->xres));
1092       DBG (DBG_info, "Y-Axis Resolution = %lu\n", _2btol (data->yres));
1093       DBG (DBG_info, "X-Axis Upper Left = %lu\n", _4btol (data->ulx));
1094       DBG (DBG_info, "Y-Axis Upper Left = %lu\n", _4btol (data->uly));
1095       DBG (DBG_info, "Window Width  = %lu\n", _4btol (data->width));
1096       DBG (DBG_info, "Window Length = %lu\n", _4btol (data->length));
1097       DBG (DBG_info, "Brightness = %d\n", data->brightness);
1098       DBG (DBG_info, "Threshold  = %d\n", data->threshold);
1099       DBG (DBG_info, "Contrast   = %d\n", data->contrast);
1100       DBG (DBG_info, "Image Composition   = %#0x\n", data->image_composition);
1101       DBG (DBG_info, "Bits per Pixel = %d\n", data->bpp);
1102       DBG (DBG_info, "Halftone Code = %#0x\n", data->halftone_code);
1103       DBG (DBG_info, "Halftone Id   = %#0x\n", data->halftone_id);
1104       DBG (DBG_info, "Byte29   = %#0x RIF=%d PaddingType=%d\n", data->byte29,
1105 	   data->byte29 & 0x80, data->byte29 & 0x7);
1106       DBG (DBG_info, "Bit Ordering = %lu\n", _2btol (data->bit_ordering));
1107       DBG (DBG_info, "Compression Type = %#x\n", data->compression_type);
1108       DBG (DBG_info, "Compression Arg  = %#x\n", data->compression_arg);
1109       for (j = 0; j < 6; j++)
1110 	DBG (DBG_info, "Reserved=%#x\n", data->reserved2[j]);
1111       DBG (DBG_info, "Ignored = %#x\n", data->ignored1);
1112       DBG (DBG_info, "Ignored = %#x\n", data->ignored2);
1113       DBG (DBG_info, "Byte42 = %#x MRIF=%d Filtering=%d GammaID=%d\n",
1114 	   data->byte42, data->byte42 & 0x80, data->byte42 & 0x70,
1115 	   data->byte42 & 0x0F);
1116       DBG (DBG_info, "Ignored = %#x\n", data->ignored3);
1117       DBG (DBG_info, "Ignored = %#x\n", data->ignored4);
1118       DBG (DBG_info, "Binary Filtering = %#x\n", data->binary_filtering);
1119       DBG (DBG_info, "Ignored = %#x\n", data->ignored5);
1120       DBG (DBG_info, "Ignored = %#x\n", data->ignored6);
1121       DBG (DBG_info, "Automatic Separation = %#x\n",
1122 	   data->automatic_separation);
1123       DBG (DBG_info, "Ignored = %#x\n", data->ignored7);
1124       DBG (DBG_info, "Automatic Binarization = %#x\n",
1125 	   data->automatic_binarization);
1126       for (j = 0; j < 13; j++)
1127 	DBG (DBG_info, "Ignored = %#x\n", data->ignored8[j]);
1128 
1129       for (k = 0; k < 8; k++)
1130 	{
1131 	  ws = &data->sec[k];
1132 	  DBG (DBG_info, "\n\n");
1133 	  DBG (DBG_info, "SECTION %d\n", k);
1134 	  DBG (DBG_info, "Section Enable Flat (sef bit) = %#x\n", ws->sef);
1135 	  DBG (DBG_info, "ignored = %d\n", ws->ignored0);
1136 	  DBG (DBG_info, "Upper Left X = %lu\n", _4btol (ws->ulx));
1137 	  DBG (DBG_info, "Upper Left Y = %lu\n", _4btol (ws->uly));
1138 	  DBG (DBG_info, "Width = %lu\n", _4btol (ws->width));
1139 	  DBG (DBG_info, "Length = %lu\n", _4btol (ws->length));
1140 	  DBG (DBG_info, "Binary Filtering = %#x\n", ws->binary_filtering);
1141 	  DBG (DBG_info, "ignored = %d\n", ws->ignored1);
1142 	  DBG (DBG_info, "Threshold = %#x\n", ws->threshold);
1143 	  DBG (DBG_info, "ignored = %d\n", ws->ignored2);
1144 	  DBG (DBG_info, "Image Composition = %#x\n", ws->image_composition);
1145 	  DBG (DBG_info, "Halftone Id = %#x\n", ws->halftone_id);
1146 	  DBG (DBG_info, "Halftone Code = %#x\n", ws->halftone_code);
1147 	  for (j = 0; j < 7; j++)
1148 	    DBG (DBG_info, "ignored = %d\n", ws->ignored3[j]);
1149 	}
1150     }
1151   DBG (DBG_proc, "<< print_window_data\n");
1152 }
1153 
1154 static SANE_Status
read_data(int fd, void *buf, size_t * buf_size, SANE_Byte dtc, u_long dtq)1155 read_data (int fd, void *buf, size_t * buf_size, SANE_Byte dtc, u_long dtq)
1156 {
1157   static struct scsi_rs_scanner_cmd cmd;
1158   SANE_Status status;
1159   DBG (DBG_proc, ">> read_data buf_size=%lu dtc=0x%2.2x dtq=%lu\n",
1160        (unsigned long) *buf_size, (int) dtc, dtq);
1161   if (fd < 0)
1162     {
1163       DBG (DBG_error, "read_data: scanner is closed!\n");
1164       return SANE_STATUS_INVAL;
1165     }
1166 
1167   memset (&cmd, 0, sizeof (cmd));	/* CLEAR */
1168   cmd.opcode = HS2P_SCSI_READ_DATA;
1169   cmd.dtc = dtc;
1170   _lto2b (dtq, cmd.dtq);
1171   _lto3b (*buf_size, cmd.len);
1172 
1173   DBG (DBG_info, "read_data ready to send scsi cmd\n");
1174   DBG (DBG_info, "opcode=0x%2.2x, dtc=0x%2.2x, dtq=%lu, transfer len =%d\n",
1175        cmd.opcode, cmd.dtc, _2btol (cmd.dtq), _3btol (cmd.len));
1176 
1177   status = sanei_scsi_cmd (fd, &cmd, sizeof (cmd), buf, buf_size);
1178 
1179   if (status != SANE_STATUS_GOOD)
1180     DBG (DBG_error, "read_data: %s\n", sane_strstatus (status));
1181   DBG (DBG_proc, "<< read_data %lu\n", (unsigned long) *buf_size);
1182   return (status);
1183 }
1184 
1185 #if 0
1186 static SANE_Status
1187 send_data (int fd, void *buf, size_t * buf_size)
1188 {
1189   static struct scsi_rs_scanner_cmd cmd;
1190   SANE_Status status;
1191   DBG (DBG_proc, ">> send_data %lu\n", (unsigned long) *buf_size);
1192 
1193   memset (&cmd, 0, sizeof (cmd));	/* CLEAR */
1194   memcpy (&cmd, buf, sizeof (*buf));	/* Fill in our struct with set values */
1195   cmd.opcode = HS2P_SCSI_SEND_DATA;
1196   _lto3b (*buf_size, cmd.len);
1197   status = sanei_scsi_cmd (fd, &cmd, sizeof (cmd), buf, buf_size);
1198 
1199   DBG (DBG_proc, "<< send_data %lu\n", (unsigned long) *buf_size);
1200   return (status);
1201 }
1202 #endif
1203 
1204 static SANE_Bool
is_valid_endorser_character(char c)1205 is_valid_endorser_character (char c)
1206 {
1207   int i = (int) c;
1208   /* 44 characters can be printed by endorser */
1209 
1210   if (i >= 0x30 && i <= 0x3A)
1211     return SANE_TRUE;		/* 0123456789: */
1212   if (i == 0x23)
1213     return SANE_TRUE;		/* # */
1214   if (i == 0x27)
1215     return SANE_TRUE;		/* ` */
1216   if (i >= 0x2C && i <= 0x2F)
1217     return SANE_TRUE;		/* '-./ */
1218   if (i == 0x20)
1219     return SANE_TRUE;		/* space */
1220   if (i >= 0x41 && i <= 0x5A)
1221     return SANE_TRUE;		/* ABCDEFGHIJKLMNOPQRSTUVWXYZ <spaces> */
1222   if (i >= 0x61 && i <= 0x7A)
1223     return SANE_TRUE;		/* abcdefghijklmnopqrstuvwxyz <spaces> */
1224 
1225   return SANE_FALSE;
1226 }
1227 
1228 static SANE_Status
set_endorser_string(int fd, SANE_String s)1229 set_endorser_string (int fd, SANE_String s)
1230 {
1231   struct
1232   {
1233     struct scsi_rs_scanner_cmd cmd;
1234     SANE_Byte endorser[19];
1235   } out;
1236   char *t;
1237   int i, len;
1238 
1239   SANE_Status status;
1240   DBG (DBG_proc, ">> set_endorser_string %s\n", s);
1241 
1242   for (i = 0, t = s; *t != '\0' && i < 19; i++)
1243     {
1244       DBG (DBG_info, "CHAR=%c\n", *t);
1245       if (!is_valid_endorser_character (*t++))
1246 	return SANE_STATUS_INVAL;
1247     }
1248   len = strlen (s);
1249 
1250   memset (&out, 0, sizeof (out));	/* CLEAR            */
1251   out.cmd.opcode = HS2P_SCSI_SEND_DATA;	/* 2AH              */
1252   out.cmd.dtc = 0x80;		/* Endorser Data    */
1253   _lto3b (len, &out.cmd.len[0]);	/* 19 bytes max     */
1254   memset (&out.endorser[0], ' ', 19);	/* fill with spaces */
1255   memcpy (&out.endorser[0], s, len);
1256 
1257   status = sanei_scsi_cmd (fd, &out, sizeof (out), NULL, NULL);
1258 
1259 
1260   DBG (DBG_proc, "<< set_endorser_string s=\"%s\" len=%d\n", s, len);
1261   return (status);
1262 }
1263 
1264 static SANE_Status
hs2p_send_gamma(HS2P_Scanner * s)1265 hs2p_send_gamma (HS2P_Scanner * s)
1266 {
1267   SANE_Status status;
1268   struct
1269   {
1270     struct scsi_rs_scanner_cmd cmd;
1271     SANE_Byte gamma[2 + GAMMA_LENGTH];
1272   } out;
1273   int i;
1274   size_t len = sizeof (out.gamma);
1275 
1276   DBG (DBG_proc, ">> teco_send_gamma\n");
1277 
1278   memset (&out, 0, sizeof (out));	/* CLEAR               */
1279   out.cmd.opcode = HS2P_SCSI_SEND_DATA;	/* 2AH                 */
1280   out.cmd.dtc = 0x03;		/* Gamma Function Data */
1281   _lto3b (len, &out.cmd.len[0]);	/* 19 bytes max        */
1282   out.gamma[0] = 0x08;		/* Gamma ID for Download table      */
1283   out.gamma[1] = 0x08;		/* The Number of gray scale (M) = 8 */
1284   for (i = 0; i < GAMMA_LENGTH; i++)
1285     {
1286       out.gamma[i + 2] = s->gamma_table[i];
1287     }
1288   status = sanei_scsi_cmd (s->fd, &out, sizeof (out), NULL, NULL);
1289 
1290   DBG (DBG_proc, "<< teco_send_gamma\n");
1291   return (status);
1292 }
1293 
1294 #if 0
1295 static SANE_Status
1296 clear_maintenance_data (int fd, int code, char XorY, int number)
1297 {
1298   struct
1299   {
1300     struct scsi_rs_scanner_cmd cmd;
1301     char string[20];
1302   } out;
1303 
1304   SANE_Status status;
1305   DBG (DBG_proc, ">> set_maintenance data\n");
1306 
1307   memset (&out, 0, sizeof (out));	/* CLEAR */
1308   out.cmd.opcode = HS2P_SCSI_SEND_DATA;	/* 2AH   */
1309   out.cmd.dtc = 0x85;		/* Maintenance Data */
1310   _lto3b (20, out.cmd.len);	/* 20 bytes */
1311   switch (code)
1312     {
1313     case 1:
1314       strcpy (out.string, "EEPROM ALL ALL RESET");
1315       break;
1316     case 2:
1317       strcpy (out.string, "EEPROM ALL RESET");
1318       break;
1319     case 3:
1320       strcpy (out.string, "ADF RESET");
1321       break;
1322     case 4:
1323       strcpy (out.string, "FLATBED RESET");
1324       break;
1325     case 5:
1326       strcpy (out.string, "LAMP RESET");
1327       break;
1328     case 6:
1329       sprintf (out.string, "EEPROM ADF %c %+4.1d", XorY, number);
1330       break;
1331     case 7:
1332       sprintf (out.string, "EEPROM BOOK %c %4.1d", XorY, number);
1333       break;
1334     case 8:
1335       sprintf (out.string, "WHITE ADJUST DATA %3d", number);
1336       break;
1337     case 9:
1338       strcpy (out.string, "EEPROM FIRST WHITE ODD");
1339       break;
1340     case 10:
1341       strcpy (out.string, "EEPROM FIRST WHITE EVEN");
1342       break;
1343     case 11:
1344       strcpy (out.string, "R ADF RESET");
1345       break;
1346     case 12:
1347       strcpy (out.string, "R LAMP RESET");
1348       break;
1349     case 13:
1350       sprintf (out.string, "EEPROM R ADF %c %4.1d", XorY, number);
1351       break;
1352     case 14:
1353       strcpy (out.string, "ENDORSER RESET");
1354       break;
1355     }
1356   status = sanei_scsi_cmd (fd, &out, sizeof (out), NULL, NULL);
1357 
1358   DBG (DBG_proc, "<< set_maintenance data\n");
1359   return (status);
1360 }
1361 #endif
1362 
1363 #if 0
1364 static SANE_Status
1365 read_halftone_mask (int fd, SANE_Byte halftone_id, void *buf,
1366 		    size_t * buf_size)
1367 {
1368   static struct scsi_rs_scanner_cmd cmd;
1369   SANE_Status status;
1370   SANE_Int len;
1371   DBG (DBG_proc, ">> read_halftone_mask\n");
1372 
1373   memset (&cmd, 0, sizeof (cmd));	/* CLEAR */
1374   cmd.opcode = HS2P_SCSI_READ_DATA;
1375   cmd.dtc = DATA_TYPE_HALFTONE;
1376   _lto2b (halftone_id, cmd.dtq);
1377 
1378   /* Each cell of an NxM dither pattern is 1 byte from the set {2,3,4,6,8,16} */
1379   switch (halftone_id)
1380     {
1381     case 0x01:
1382       len = 32;
1383       break;			/* 8x4, 45 degree */
1384     case 0x02:
1385       len = 36;
1386       break;			/* 6x6, spiral */
1387     case 0x03:
1388       len = 16;
1389       break;			/* 4x4, spiral */
1390     case 0x04:
1391       len = 64;
1392       break;			/* 8x8, 90 degree */
1393     case 0x05:
1394       len = 70;
1395       break;			/* 70 lines */
1396     case 0x06:
1397       len = 95;
1398       break;			/* 95 lines */
1399     case 0x07:
1400       len = 180;
1401       break;			/* 180 lines */
1402     case 0x08:
1403       len = 128;
1404       break;			/* 16x8, 45 degree */
1405     case 0x09:
1406       len = 256;
1407       break;			/* 16x16, 90 degree */
1408     case 0x0A:
1409       len = 64;
1410       break;			/* 8x8, Bayer */
1411     default:
1412       return SANE_STATUS_INVAL;	/* Reserved */
1413     }
1414 
1415   _lto3b (len, cmd.len);
1416   status = sanei_scsi_cmd (fd, &cmd, sizeof (cmd), buf, buf_size);
1417 
1418   DBG (DBG_proc, "<< read_halftone_mask\n");
1419   return (status);
1420 }
1421 #endif
1422 
1423 #if 0
1424 static SANE_Status
1425 set_halftone_mask (int fd, SANE_Byte halftone_id, void *buf,
1426 		   size_t * buf_size)
1427 {
1428   static struct scsi_rs_scanner_cmd cmd;
1429   SANE_Status status;
1430   DBG (DBG_proc, ">> set_halftone_mask\n");
1431 
1432   memset (&cmd, 0, sizeof (cmd));	/* CLEAR */
1433   cmd.opcode = HS2P_SCSI_READ_DATA;
1434   cmd.dtc = DATA_TYPE_HALFTONE;
1435   _lto2b (halftone_id, cmd.dtq);
1436 
1437   /* Each cell of an NxM dither pattern is 1 byte from the set {2,3,4,6,8,16}
1438    *  0x80, 0x81 are User definable custom dither patterns
1439    */
1440   if (halftone_id != 0x80 && halftone_id != 0x81)
1441     return SANE_STATUS_INVAL;
1442 
1443   _lto3b (*buf_size, cmd.len);
1444   status = sanei_scsi_cmd (fd, &cmd, sizeof (cmd), buf, buf_size);
1445 
1446   DBG (DBG_proc, "<< set_halftone_mask\n");
1447   return (status);
1448 }
1449 #endif
1450 
1451 #if 0
1452 static SANE_Status
1453 read_gamma_function (int fd)
1454 {
1455   SANE_Status status = SANE_STATUS_GOOD;
1456   return (status);
1457 }
1458 
1459 static SANE_Status
1460 read_endorser_data (int fd)
1461 {
1462   SANE_Status status = SANE_STATUS_GOOD;
1463   return (status);
1464 }
1465 
1466 static SANE_Status
1467 read_size_data (int fd)
1468 {
1469   SANE_Status status = SANE_STATUS_GOOD;
1470   return (status);
1471 }
1472 #endif
1473 
1474 #if 0
1475 static SANE_Status
1476 read_maintenance_data (int fd)
1477 {
1478   SANE_Status status = SANE_STATUS_GOOD;
1479   return (status);
1480 }
1481 #endif
1482 
1483 /* Bit0: is 0 if document on ADF; else 1
1484  * Bit1: is 0 if ADF cover is closed; else 1
1485  * Bit2: reserved
1486  * Bits7-3: reserved
1487 */
1488 
1489 
1490 #if 0
1491 static SANE_Status
1492 read_adf_status (int fd, SANE_Byte * adf_status_byte)
1493 {
1494   SANE_Status status = SANE_STATUS_GOOD;
1495   struct scsi_rs_scanner_cmd cmd;
1496   static size_t len = 1;
1497 
1498   DBG (DBG_proc, ">> read_adf_status\n");
1499 
1500   memset (&cmd, 0, sizeof (cmd));
1501   cmd.opcode = HS2P_SCSI_READ_DATA;
1502   cmd.dtc = DATA_TYPE_ADF_STATUS;
1503   _lto3b (0x01, cmd.len);	/* convert 0x01 into 3-byte Transfer Length */
1504   if ((status =
1505        sanei_scsi_cmd (fd, &cmd, sizeof (cmd), adf_status_byte,
1506 		       &len)) != SANE_STATUS_GOOD)
1507     {
1508       DBG (DBG_error, "read_adf_status ERROR: %s\n", sane_strstatus (status));
1509     }
1510   DBG (DBG_proc, "<< read_adf_status\n");
1511   return (status);
1512 }
1513 #endif
1514 
1515 /*
1516  * read_ipu_photoletter_parameters
1517  * read_ipu_threshold_parameters
1518  * read_sensor_data (WHAT DATA TYPE CODE?)
1519 */
1520 /* SEND CMD */
1521 /*
1522  * send_halftone_mask
1523  * send_gamma_function
1524  * send_endorser_data
1525  * send_maintenance_data
1526  *     EPROM All Clear
1527  *     EPROM Counter Clear
1528  *     ADF Counter Clear
1529  *     Flatbed Counter Clear
1530  *     Lamp Counter Clear
1531  *     ADF Register Data
1532  *     Flatbed Register Data
1533  *     White Adjustment Data
1534  *     White level first Data (ODD)
1535  *     White level first Data (EVEN)
1536  *     Reverse side ADF Counter Clear
1537  *     Reverse side Lamp Counter Clear
1538  *     Reverse side ADF Register Data
1539  *     Endorser Character Counter Clear
1540  * send_ipu_parameters
1541 */
1542 
1543 /* OBJECT POSITION        */
1544 /* GET DATA BUFFER STATUS */
1545 
1546 /* 1-3-4 MODE SELECT */
1547 
1548 /* 1-3-5 Reserve Unit: 0x16
1549  * 1-3-6 Release Unit: 0x17
1550 */
1551 static SANE_Status
unit_cmd(int fd, SANE_Byte opcode)1552 unit_cmd (int fd, SANE_Byte opcode)
1553 {
1554   static struct
1555   {
1556     SANE_Byte opcode;		/* 16H: Reserve Unit    17H: Release Unit                     */
1557     SANE_Byte byte1;		/* 7-5: LUN; 4: 3rd Party; 3-1: 3rd Party Device; 0: Reserved */
1558     SANE_Byte reserved[3];
1559     SANE_Byte control;		/* 7-6: Vendor Unique; 5-2: Reserved; 1: Flag; 0: Link        */
1560   } cmd;
1561 
1562   SANE_Byte LUN = (0x00 & 0x07) << 5;
1563   SANE_Status status;
1564   DBG (DBG_proc, ">> unit_cmd\n");
1565 
1566   cmd.opcode = opcode;
1567   cmd.byte1 = LUN & 0xE1;	/* Mask=11100001 3rd Party and 3rd Party Device must be 0 */
1568   memset (&cmd, 0, sizeof (cmd));
1569   status = sanei_scsi_cmd (fd, &cmd, sizeof (cmd), 0, 0);
1570 
1571   DBG (DBG_proc, "<< unit_cmd\n");
1572   return (status);
1573 
1574 }
1575 
1576 /* The OBJECT POSITION command is used for carriage control or
1577   * document feed and eject with ADF
1578   *
1579   * Position Function: Byte1 bits2-0
1580   *  000 Unload instructs document eject
1581   *  001 Load   instructs document feed to scan start position
1582   *  010 Absolute Positioning - instructs carriage to move to carriage lock position
1583   *      The carriage moves in the Y-axis direction as the amount set in Count when
1584   *      count>0
1585   *      (Not supported in IS420)
1586   *
1587 */
1588 static SANE_Status
object_position(int fd, int load)1589 object_position (int fd, int load)
1590 {
1591   static struct scsi_object_position_cmd cmd;
1592   SANE_Status status;
1593   DBG (DBG_proc, ">> object_position\n");
1594 
1595   /* byte    0 opcode
1596    * byte    1 position function
1597    * bytes 2-4 reserved
1598    * bytes 5-8 reserved
1599    * byte    9 control
1600    */
1601 
1602 
1603   memset (&cmd, 0, sizeof (cmd));
1604   cmd.opcode = HS2P_SCSI_OBJECT_POSITION;
1605   if (load)
1606     cmd.position_func = OBJECT_POSITION_LOAD;
1607   else
1608     cmd.position_func = OBJECT_POSITION_UNLOAD;
1609 
1610 
1611   status = sanei_scsi_cmd (fd, &cmd, sizeof (cmd), 0, 0);
1612 
1613   DBG (DBG_proc, "<< object_position\n");
1614   return (status);
1615 }
1616 
1617 static SANE_Status
get_data_status(int fd, STATUS_DATA * dbs)1618 get_data_status (int fd, STATUS_DATA * dbs)
1619 {
1620   static GET_DBS_CMD cmd;
1621   static STATUS_BUFFER buf;	/* hdr + data */
1622   size_t bufsize = sizeof (buf);
1623   SANE_Status status;
1624   DBG (DBG_proc, ">> get_data_status %lu\n", (unsigned long) bufsize);
1625 
1626   /* Set up GET DATA BUFFER STATUS cmd */
1627   memset (&cmd, 0, sizeof (cmd));	/* CLEAR cmd */
1628   cmd.opcode = HS2P_SCSI_GET_BUFFER_STATUS;
1629   cmd.wait &= ~0x01;		/* unset Wait bit0 */
1630   _lto2b (bufsize, cmd.len);
1631 
1632   /* Now execute cmd, and put returned results in buf */
1633   status = sanei_scsi_cmd (fd, &cmd, sizeof (cmd), &buf, &bufsize);
1634 
1635   /* Now copy from buf.data to dbs */
1636   memcpy (dbs, &buf.data, sizeof (*dbs));
1637 
1638   if (status == SANE_STATUS_GOOD &&
1639       ((unsigned int) _3btol (buf.hdr.len) <= sizeof (*dbs)
1640        || _3btol (buf.data.filled) == 0))
1641     {
1642       DBG (DBG_info, "get_data_status: busy\n");
1643       status = SANE_STATUS_DEVICE_BUSY;
1644     }
1645   DBG (DBG_proc, "<< get_data_status %lu\n", (unsigned long) bufsize);
1646   return (status);
1647 }
1648 
1649 /* 1-3-7 MODE SENSE */
1650 /* 1-3-8 SCAN       */
1651 
1652 /* 1-3-9 Receive Diagnostic
1653  * Byte0: 1CH
1654  * Byte1: 7-5 LUN; 4-0: reserved
1655  * Byte2: Reserved
1656  * Byte3-4: Allocation Length
1657  * Byte5: 7-6: Vendor Unique; 5-2: Reserved; 1: Flag; 0: Link
1658  *
1659  * This command is treated as a dummy command
1660  * Return GOOD unless there is an error in command in which case it returns CHECK
1661 */
1662 
1663 /*
1664 *  The IS450 performs 7 self-diagnostics tests
1665 *  1) Home position error check
1666 *  2) Exposure lamp error check
1667 *  3) White level error check
1668 *  4) Document table error check
1669 *  5) SCU error check
1670 *  6) RCU error check
1671 *  7) Memory error check
1672 *
1673 *  and uses the lights on the scanner to indicate the result
1674 *
1675 *                               PowerOn    MachineBusy  DocumentInPlace   Error
1676 *                               (green)    (green)      (green)           (red)
1677 *
1678 *  SCU error check              Blinking                                  Blinking
1679 *  RCU error check              Blinking                On                Blinking
1680 *  Home position error check    Blinking   Blinking     Blinking          On
1681 *  Exposure lamp error check    Blinking   Blinking     On                On
1682 *  White level error check      Blinking   Blinking
1683 *  Memory Error (Simplex)       Blinking
1684 *  Memory Error (Duplex)                   Blinking
1685 *
1686 */
1687 #if 0
1688 static SANE_Status
1689 receive_diagnostic (int fd)
1690 {
1691   static SANE_Byte cmd[6];
1692   SANE_Status status;
1693   DBG (DBG_proc, ">> receive_diagnostic\n");
1694 
1695   cmd[0] = HS2P_SCSI_RECEIVE_DIAGNOSTICS;
1696   memset (cmd, 0, sizeof (cmd));
1697   status = sanei_scsi_cmd (fd, cmd, sizeof (cmd), 0, 0);
1698 
1699   DBG (DBG_proc, "<< receive_diagnostic\n");
1700   return (status);
1701 }
1702 #endif
1703 
1704 /* 1-3-10 Send Diagnostic
1705  * Byte0: 1DH
1706  * Byte1: 7-5 LUN; 4: PF; 3: Reserved; 2: S-Test; 1: DevOfl; 0: U-Ofl
1707  * Byte2: Reserved
1708  * Byte3-4: Parameter List Length
1709  * Byte5: 7-6: Vendor Unique; 5-2: Reserved; 1: Flag; 0: Link
1710  * This command executes self-diagnostic and optical-adjustment
1711  * PF, DevOfl, and Parameter List Length must be 0 or CHECK condition is returned.
1712 */
1713 #if 0
1714 static SANE_Status
1715 send_diagnostic (int fd)
1716 {
1717   static SANE_Byte cmd[6];
1718   SANE_Status status;
1719   DBG (DBG_proc, ">> send_diagnostic\n");
1720 
1721   cmd[0] = HS2P_SCSI_SEND_DIAGNOSTICS;
1722   cmd[1] = 0x00 & (1 << 2) & 0xED;	/* Set Self-Test bit  and clear PF, DevOfl bits */
1723   cmd[3] = 0x00;
1724   cmd[4] = 0x00;		/* Parameter list (bytes3-4) must  be 0x00 */
1725   memset (cmd, 0, sizeof (cmd));
1726   status = sanei_scsi_cmd (fd, cmd, sizeof (cmd), 0, 0);
1727 
1728   DBG (DBG_proc, "<< send_diagnostic\n");
1729   return (status);
1730 }
1731 #endif
1732 
1733 
1734 /* 1-3-8 SCAN command is used to instruct scanner to start scanning */
1735 static SANE_Status
trigger_scan(HS2P_Scanner * s)1736 trigger_scan (HS2P_Scanner * s)
1737 {
1738   static struct
1739   {
1740     START_SCAN cmd;
1741     SANE_Byte wid[2];		/* scanner supports up to 2 windows */
1742   } scan;
1743   SANE_Status status;
1744   DBG (DBG_proc, ">> trigger scan\n");
1745 
1746   memset (&scan, 0, sizeof (scan));	/* CLEAR scan */
1747   scan.cmd.opcode = HS2P_SCSI_START_SCAN;
1748   /* Transfer length is the byte length of Window List transferred
1749    * Window List is a list of Window Identifier created by SET WINDOW command
1750    * Since only 1 Window is supported by SCAN command, 0 or 1 is used for Window Identifier
1751    * and 1 or 2 for length
1752    status = sanei_scsi_cmd (s->fd, &trigger, sizeof (trigger), &window_id_list[0], &wl_size);
1753    */
1754   scan.cmd.len = (s->val[OPT_DUPLEX].w == SANE_TRUE) ? 2 : 1;
1755 
1756   DBG (DBG_info, "trigger_scan: sending %d Window Id to scanner\n",
1757        scan.cmd.len);
1758   status =
1759     sanei_scsi_cmd (s->fd, &scan, sizeof (scan.cmd) + scan.cmd.len, NULL,
1760 		    NULL);
1761 
1762   DBG (DBG_proc, "<< trigger scan\n");
1763   return (status);
1764 }
1765 
1766 #define MAX_WAITING_TIME       15
1767 static SANE_Status
hs2p_wait_ready(HS2P_Scanner * s)1768 hs2p_wait_ready (HS2P_Scanner * s)
1769 {
1770   STATUS_DATA dbs;		/* Status Buffer Status DATA */
1771   time_t now, start;
1772   SANE_Status status;
1773 
1774   start = time (NULL);
1775 
1776   while (1)
1777     {
1778       status = get_data_status (s->fd, &dbs);
1779 
1780       switch (status)
1781 	{
1782 	default:
1783 	  /* Ignore errors while waiting for scanner to become ready.
1784 	     Some SCSI drivers return EIO while the scanner is
1785 	     returning to the home position.  */
1786 	  DBG (DBG_error, "scsi_wait_ready: get datat status failed (%s)\n",
1787 	       sane_strstatus (status));
1788 	  /* fall through */
1789 	case SANE_STATUS_DEVICE_BUSY:
1790 	  now = time (NULL);
1791 	  if (now - start >= MAX_WAITING_TIME)
1792 	    {
1793 	      DBG (DBG_error,
1794 		   "hs2p_wait_ready: timed out after %lu seconds\n",
1795 		   (u_long) (now - start));
1796 	      return SANE_STATUS_INVAL;
1797 	    }
1798 	  break;
1799 
1800 	case SANE_STATUS_GOOD:
1801 	  DBG (DBG_proc, "hs2p_wait_ready: %d bytes ready\n",
1802 	       _3btol (dbs.filled));
1803 	  return status;
1804 	  break;
1805 	}
1806       usleep (1000000);		/* retry after 100ms */
1807     }
1808   return SANE_STATUS_INVAL;
1809 }
1810 
1811 
1812 /* MODE PAGES GET/SET */
1813 static SANE_Status
connection_parameters(int fd, MP_CXN * settings, SANE_Bool flag)1814 connection_parameters (int fd, MP_CXN * settings, SANE_Bool flag)
1815 {
1816   SANE_Status status;
1817   MP_CXN buf;
1818   size_t nbytes;
1819   DBG (DBG_proc, ">> connection_parameters\n");
1820   nbytes = sizeof (buf);
1821 
1822   if (flag)
1823     {				/* GET */
1824       DBG (DBG_info, ">> GET connection_parameters >> calling mode_sense\n");
1825       status =
1826 	mode_sense (fd, (MP *) & buf, (SANE_Byte) PAGE_CODE_CONNECTION);
1827       if (status != SANE_STATUS_GOOD)
1828 	{
1829 	  DBG (DBG_error,
1830 	       "get_connection_parameters: MODE_SELECT failed with status=%d\n",
1831 	       status);
1832 	  return (status);
1833 	}
1834       memcpy (settings, &buf, nbytes);
1835     }
1836   else
1837     {				/* SET */
1838       DBG (DBG_info, ">> SET connection_parameters >> calling mode_select\n");
1839       /* Fill in struct then hand off to mode_select */
1840       memset (&buf, 0, sizeof (buf));	/* Fill struct with zeros */
1841       memcpy (&buf, settings, nbytes);
1842       /* Make sure calling function didn't change these bytes           */
1843       memset (&buf.hdr, 0, sizeof (buf.hdr));	/* Make sure 4bytes are 0 */
1844       buf.code = PAGE_CODE_CONNECTION;	/* bits5-0: Page Code 02H */
1845       buf.code &= ~(1 << 7);	/* Bit7 PS is set to 0    */
1846       buf.len = 0x0E;		/* This is the only page with 14 bytes */
1847 
1848       status = mode_select (fd, (MP *) & buf);
1849       if (status != SANE_STATUS_GOOD)
1850 	{
1851 	  DBG (DBG_error,
1852 	       "set_connection_parameters: MODE_SELECT failed with status=%d\n",
1853 	       status);
1854 	  return (-1);
1855 	}
1856     }
1857   DBG (DBG_proc, "<< connection_parameters\n");
1858   return (status);
1859 }
1860 
1861 static SANE_Status
get_basic_measurement_unit(int fd, SANE_Int * bmu, SANE_Int * mud)1862 get_basic_measurement_unit (int fd, SANE_Int * bmu, SANE_Int * mud)
1863 {
1864   SANE_Status status;
1865   MP_SMU buf;
1866 
1867   DBG (DBG_proc, ">> get_basic_measurement_unit: fd=\"%d\"\n", fd);
1868 
1869   status =
1870     mode_sense (fd, (MP *) & buf,
1871 		(SANE_Byte) PAGE_CODE_SCANNING_MEASUREMENTS);
1872   if (status != SANE_STATUS_GOOD)
1873     {
1874       DBG (DBG_error,
1875 	   "set_basic_measurement_unit: MODE_SELECT failed with status=%d\n",
1876 	   status);
1877       return (SANE_STATUS_INVAL);
1878     }
1879   *bmu = buf.bmu;
1880   *mud = ((buf.mud[0] << 8) | buf.mud[1]);
1881 
1882   DBG (DBG_proc, "<< get_basic_measurement_unit: bmu=%d mud=%d\n", *bmu,
1883        *mud);
1884   return (status);
1885 }
1886 
1887 static SANE_Status
set_basic_measurement_unit(int fd, SANE_Byte bmu)1888 set_basic_measurement_unit (int fd, SANE_Byte bmu)
1889 {
1890   MP_SMU buf;			/* Mode Page Scanning Measurements Page Code */
1891   SANE_Status status;
1892   SANE_Int mud;
1893   size_t bufsize = sizeof (buf);
1894 
1895   DBG (DBG_proc, ">> set_basic_measurement_unit: %d\n", bmu);
1896 
1897   /* Set up buf */
1898   memset (&buf, 0, bufsize);	/* CLEAR buf            */
1899   buf.code = PAGE_CODE_SCANNING_MEASUREMENTS;	/* bits5-0: Page Code   */
1900   buf.code &= ~(1 << 7);	/* Bit7 PS is set to 0  */
1901   buf.len = 0x06;
1902 
1903   buf.bmu = bmu;		/* Power on default is POINTS */
1904   mud = (bmu == INCHES) ? DEFAULT_MUD : 1;
1905   DBG (DBG_info, "SET_BASIC_MEASUREMENT_UNIT: bmu=%d mud=%d\n", bmu, mud);
1906   _lto2b (mud, &buf.mud[0]);	/* buf.mud[0]  = (mud >> 8) & 0xff; buf.mud[1]  = (mud & 0xff); */
1907 
1908   status = mode_select (fd, (MP *) & buf);
1909   if (status != SANE_STATUS_GOOD)
1910     {
1911       DBG (DBG_error,
1912 	   "set_basic_measurement_unit: MODE_SELECT failed with status=%d\n",
1913 	   status);
1914       status = SANE_STATUS_INVAL;
1915     }
1916 
1917   DBG (DBG_proc,
1918        "<< set_basic_measurement_unit: opcode=%d len=%d bmu=%d mud=%ld\n",
1919        buf.code, buf.len, buf.bmu, _2btol (&buf.mud[0]));
1920   return (status);
1921 }
1922 
1923 static SANE_Status
adf_control(int fd, SANE_Bool flag, SANE_Byte * adf_control, SANE_Byte * adf_mode, SANE_Byte * mwt)1924 adf_control (int fd, SANE_Bool flag, SANE_Byte * adf_control,
1925 	     SANE_Byte * adf_mode, SANE_Byte * mwt)
1926 {
1927   SANE_Status status;
1928   MP_ADF buf;
1929   size_t bufsize = sizeof (buf);
1930 
1931   DBG (DBG_proc, ">> adf_control\n");
1932 
1933   memset (&buf, 0, bufsize);	/* Fill struct with zeros  */
1934 
1935   if (flag)
1936     {				/* GET */
1937       DBG (DBG_info, ">> GET ADF_control>> calling mode_sense\n");
1938       status =
1939 	mode_sense (fd, (MP *) & buf, (SANE_Byte) PAGE_CODE_ADF_CONTROL);
1940       if (status != SANE_STATUS_GOOD)
1941 	{
1942 	  DBG (DBG_error, "get_adf_control: MODE_SELECT failed\n");
1943 	  return (status);
1944 	}
1945       *adf_control = buf.adf_control;
1946       *adf_mode = buf.adf_mode_control;
1947       *mwt = buf.medium_wait_timer;
1948     }
1949   else
1950     {				/* SET */
1951       /* Fill in struct then hand off to mode_select                  */
1952       buf.code = PAGE_CODE_ADF_CONTROL;	/* bits5-0: Page Code      */
1953       buf.code &= ~(1 << 7);	/* Bit7 PS is set to 0     */
1954       buf.len = 0x06;
1955       /* Byte2: adf_control:      7-2:reserved; 1-0:adf_control: Default 00H Flatbed, 01H Simplex, 02H Duplex */
1956       /* Byte3: adf_mode_control: 7-3:reserved; 2: Prefeed Mode: 0 invalid, 1 valid; 1-0: ignored */
1957       /* Byte4: medium_wait_timer: timeout period. Not supported */
1958       buf.adf_control = (*adf_control & 0x03);
1959       buf.adf_mode_control = (*adf_mode & 0x04);
1960       buf.medium_wait_timer = *mwt;
1961       status = mode_select (fd, (MP *) & buf);
1962       if (status != SANE_STATUS_GOOD)
1963 	{
1964 	  DBG (DBG_error,
1965 	       "set_adf_control: MODE_SELECT failed with status=%d\n",
1966 	       status);
1967 	  return (status);
1968 	}
1969     }
1970   DBG (DBG_proc, ">> adf_control\n");
1971   return (status);
1972 }
1973 
1974 static SANE_Status
white_balance(int fd, int *val, SANE_Bool flag)1975 white_balance (int fd, int *val, SANE_Bool flag)
1976 {
1977   SANE_Status status;
1978   MP_WhiteBal buf;		/* White Balance Page Code */
1979   size_t bufsize = sizeof (buf);
1980 
1981   memset (&buf, 0, bufsize);
1982 
1983   if (flag)
1984     {				/* GET */
1985       DBG (DBG_proc, ">> GET white_balance>> calling mode_sense\n");
1986       status =
1987 	mode_sense (fd, (MP *) & buf, (SANE_Byte) PAGE_CODE_WHITE_BALANCE);
1988       if (status != SANE_STATUS_GOOD)
1989 	{
1990 	  DBG (DBG_error,
1991 	       "get_white_balance: MODE_SELECT failed with status=%d\n",
1992 	       status);
1993 	  return (status);
1994 	}
1995       *val = buf.white_balance;
1996     }
1997   else
1998     {				/* SET */
1999       /* Fill in struct then hand off to mode_select */
2000       memset (&buf, 0, sizeof (buf));	/* Fill struct with zeros */
2001       buf.code = PAGE_CODE_WHITE_BALANCE;	/* bits5-0: Page Code     */
2002       buf.code &= ~(1 << 7);	/* Bit7 PS is set to 0    */
2003       buf.len = 0x06;
2004       buf.white_balance = *val;	/* Power on default is RELATIVE_WHITE */
2005       status = mode_select (fd, (MP *) & buf);
2006       if (status != SANE_STATUS_GOOD)
2007 	{
2008 	  DBG (DBG_error,
2009 	       "set_white_balance: MODE_SELECT failed with status=%d\n",
2010 	       status);
2011 	  return (status);
2012 	}
2013     }
2014   DBG (DBG_proc, "<< white balance: buf.white_balance=%#02x\n",
2015        buf.white_balance);
2016   return (status);
2017 }
2018 
2019 #if 0
2020 static SANE_Int
2021 lamp_timer (int fd, int val, SANE_Bool flag)
2022 {
2023   SANE_Status status;
2024   MP_LampTimer buf;		/* Lamp Timer Page Code */
2025 
2026   DBG (DBG_proc, ">> lamp timer\n");
2027   if (flag)
2028     {				/* GET */
2029       DBG (DBG_info, ">> GET lamp_timer>> calling mode_sense\n");
2030       if ((status =
2031 	   mode_sense (fd, (MP *) & buf,
2032 		       (SANE_Byte) PAGE_CODE_LAMP_TIMER_SET)) !=
2033 	  SANE_STATUS_GOOD)
2034 	{
2035 	  DBG (DBG_error, "get_lamp_timer: MODE_SELECT failed\n");
2036 	  return (-1);
2037 	}
2038     }
2039   else
2040     {				/* SET */
2041       /* Fill in struct then hand off to mode_select */
2042       memset (&buf, 0, sizeof (buf));	/* Fill struct with zeros */
2043       buf.code = PAGE_CODE_LAMP_TIMER_SET;	/* bits5-0: Page Code     */
2044       buf.code &= ~(1 << 7);	/* Bit7 PS is set to 0    */
2045       buf.len = 0x06;
2046       buf.time_on = val;	/* time lamp has been on  */
2047       if ((status = mode_select (fd, (MP *) & buf)) != SANE_STATUS_GOOD)
2048 	{
2049 	  DBG (DBG_error,
2050 	       "set_lamp_timer: MODE_SELECT failed with status=%d\n", status);
2051 	  return (-1);
2052 	}
2053     }
2054   DBG (DBG_proc, "<< lamp timer\n");
2055   return (buf.time_on);
2056 }
2057 #endif
2058 
2059 static SANE_Status
endorser_control(int fd, int *val, SANE_Bool flag)2060 endorser_control (int fd, int *val, SANE_Bool flag)
2061 {
2062   SANE_Status status;
2063   MP_EndCtrl buf;		/* MPHdr (4bytes) + MPP (8bytes)       */
2064   SANE_Byte mask = 0x7;		/* 7-3:reserved; 2-0: Endorser Control */
2065   size_t bufsize = sizeof (buf);
2066 
2067   DBG (DBG_proc, ">> endorser_control: fd=%d val=%d flag=%d\n", fd, *val,
2068        flag);
2069 
2070   memset (&buf, 0, bufsize);	/* Fill struct with zeros  */
2071 
2072   if (flag)
2073     {				/* GET */
2074       DBG (DBG_info, ">> GET endorser control >> calling mode_sense\n");
2075       status =
2076 	mode_sense (fd, (MP *) & buf, (SANE_Byte) PAGE_CODE_ENDORSER_CONTROL);
2077       if (status != SANE_STATUS_GOOD)
2078 	{
2079 	  DBG (DBG_error,
2080 	       "get_endorser_control: MODE_SELECT failed with status=%d\n",
2081 	       status);
2082 	  return (status);
2083 	}
2084       *val = buf.endorser_control & mask;
2085     }
2086   else
2087     {				/* SET */
2088       DBG (DBG_info, ">> SET endorser control >> calling mode_select\n");
2089       /* Fill in struct then hand off to mode_select */
2090       memset (&buf, 0, sizeof (buf));	/* Fill struct with zeros */
2091       buf.code = PAGE_CODE_ENDORSER_CONTROL;	/* bits5-0: Page Code     */
2092       buf.code &= ~(1 << 7);	/* Bit7 PS is set to 0    */
2093       buf.len = 0x06;
2094 
2095       buf.endorser_control = *val & mask;	/* Power on default is OFF */
2096       status = mode_select (fd, (MP *) & buf);
2097       if (status != SANE_STATUS_GOOD)
2098 	{
2099 	  DBG (DBG_error,
2100 	       "set_endorser_control: MODE_SELECT failed with status=%d\n",
2101 	       status);
2102 	  return (status);
2103 	}
2104     }
2105   DBG (DBG_proc, "<< endorser_control: endorser_control=%#02x\n",
2106        buf.endorser_control);
2107   return (status);
2108 }
2109 
2110 /* When SCAN, READ, or LOAD (in ADF mode) is issued, scanner waits until operator panel start button is pressed */
2111 static SANE_Status
scan_wait_mode(int fd, int val, SANE_Bool flag)2112 scan_wait_mode (int fd, int val, SANE_Bool flag)
2113 {
2114   SANE_Status status;
2115   MP_SWM buf;			/* Scan Wait Mode Page Code */
2116   DBG (DBG_proc, ">> scan_wait_mode\n");
2117 
2118   if (flag)
2119     {				/* GET */
2120       DBG (DBG_info, ">> GET scan_wait_mode >> calling mode_sense\n");
2121       status =
2122 	mode_sense (fd, (MP *) & buf, (SANE_Byte) PAGE_CODE_SCAN_WAIT_MODE);
2123       if (status != SANE_STATUS_GOOD)
2124 	{
2125 	  DBG (DBG_error,
2126 	       "get_scan_wait_mode: MODE_SELECT failed with status=%d\n",
2127 	       status);
2128 	  return (-1);
2129 	}
2130     }
2131   else
2132     {				/* SET */
2133       /* Fill in struct then hand off to mode_select */
2134       memset (&buf, 0, sizeof (buf));	/* Fill struct with zeros */
2135       buf.code = PAGE_CODE_SCAN_WAIT_MODE;	/* bits5-0: Page Code     */
2136       buf.code &= ~(1 << 7);	/* Bit7 PS is set to 0    */
2137       buf.len = 0x06;
2138       buf.swm = 0x00;
2139       if (val == 1)
2140 	buf.swm |= 1;		/* set bit 1   if scan_wait_mode ON  */
2141       else
2142 	buf.swm &= ~1;		/* unset bit 1 if scan_wait_mode OFF */
2143 
2144       DBG (DBG_info, ">> SET scan_wait_mode >> calling mode_sense\n");
2145       if ((status = mode_select (fd, (MP *) & buf)) != SANE_STATUS_GOOD)
2146 	{
2147 	  DBG (DBG_error, "mode_select ERROR %s\n", sane_strstatus (status));
2148 	}
2149     }
2150   DBG (DBG_proc, "<< scan_wait_mode: buf.swm=%#02x\n", buf.swm);
2151   return (status);
2152 }
2153 
2154 /* Selectable when Send Diagnostics command is performed */
2155 static SANE_Int
service_mode(int fd, int val, SANE_Bool flag)2156 service_mode (int fd, int val, SANE_Bool flag)
2157 {
2158   SANE_Status status;
2159   MP_SRV buf;			/* Service Mode Page Code */
2160   DBG (DBG_proc, ">> service_mode\n");
2161 
2162   if (flag)
2163     {				/* GET */
2164       DBG (DBG_info, ">> GET service_mode >> calling mode_sense\n");
2165       status =
2166 	mode_sense (fd, (MP *) & buf,
2167 		    (SANE_Byte) PAGE_CODE_SERVICE_MODE_SELECT);
2168       if (status != SANE_STATUS_GOOD)
2169 	{
2170 	  DBG (DBG_error,
2171 	       "get_service_mode: MODE_SELECT failed with status=%d\n",
2172 	       status);
2173 	  return (-1);
2174 	}
2175     }
2176   else
2177     {				/* SET */
2178       /* Fill in struct then hand off to mode_select */
2179       memset (&buf, 0, sizeof (buf));	/* Fill struct with zeros */
2180       buf.code = PAGE_CODE_SERVICE_MODE_SELECT;	/* bits5-0: Page Code     */
2181       buf.code &= ~(1 << 7);	/* Bit7 PS is set to 0    */
2182       buf.len = 0x06;
2183       /* 0H: Self-Diagnostics Mode, 1H: Optical Adjustment Mode */
2184       buf.service = val & 0x01;
2185       status = mode_select (fd, (MP *) & buf);
2186       if (status != SANE_STATUS_GOOD)
2187 	{
2188 	  DBG (DBG_error,
2189 	       "set_service_mode: MODE_SELECT failed with status=%d\n",
2190 	       status);
2191 	  return (-1);
2192 	}
2193     }
2194   DBG (DBG_proc, "<< service_mode\n");
2195   return (buf.service & 0x01);
2196 }
2197