The magicolor device apparently does not support WAIT_FOR_BUTTON
[sane-backend-magicolor.git] / sanei / sanei_pv8630.c
blob848558efd1835e6e4b9696a2771a1dd171767e33
1 /* sane - Scanner Access Now Easy.
3 Copyright (C) 2000 Adrian Perez Jorge
4 Copyright (C) 2001 Frank Zago
5 Copyright (C) 2001 Marcio Teixeira
7 This file is part of the SANE package.
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2 of the
12 License, or (at your option) any later version.
14 This program is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22 MA 02111-1307, USA.
24 As a special exception, the authors of SANE give permission for
25 additional uses of the libraries contained in this release of SANE.
27 The exception is that, if you link a SANE library with other files
28 to produce an executable, this does not by itself cause the
29 resulting executable to be covered by the GNU General Public
30 License. Your use of that executable is in no way restricted on
31 account of linking the SANE library code into it.
33 This exception does not, however, invalidate any other reasons why
34 the executable file might be covered by the GNU General Public
35 License.
37 If you submit changes to SANE to the maintainers to be included in
38 a subsequent release, you agree by submitting the changes that
39 those changes may be distributed with this exception intact.
41 If you write modifications of your own for SANE, it is your choice
42 whether to permit this exception to apply to your modifications.
43 If you do not wish that, delete this exception notice.
45 Interface files for the PowerVision 8630 chip, a USB to
46 parallel converter used in many scanners.
50 #include "../include/sane/config.h"
52 #include <stdlib.h>
53 #include <unistd.h>
55 #define BACKEND_NAME sanei_pv8630
56 #include "../include/sane/sane.h"
57 #include "../include/sane/sanei_debug.h"
58 #include "../include/sane/sanei_usb.h"
59 #include "../include/sane/sanei_pv8630.h"
61 #define DBG_error 1
62 #define DBG_info 5
64 void
65 sanei_pv8630_init (void)
67 DBG_INIT();
70 /* Write one control byte */
71 SANE_Status
72 sanei_pv8630_write_byte (int fd, SANEI_PV_Index index, SANE_Byte byte)
74 SANE_Status status;
76 DBG(DBG_info, "sanei_pv8630_write_byte - index=%d, byte=%d\n", index, byte);
77 status =
78 sanei_usb_control_msg (fd, 0x40, PV8630_REQ_WRITEBYTE, byte, index, 0,
79 NULL);
81 if (status != SANE_STATUS_GOOD)
82 DBG (DBG_error, "sanei_pv8630_write_byte error\n");
83 return status;
86 /* Read one control byte */
87 SANE_Status
88 sanei_pv8630_read_byte (int fd, SANEI_PV_Index index, SANE_Byte * byte)
90 SANE_Status status;
92 DBG(DBG_info, "sanei_pv8630_read_byte - index=%d, byte=%p\n", index, byte);
94 status =
95 sanei_usb_control_msg (fd, 0xc0, PV8630_REQ_READBYTE, 0, index, 1, byte);
97 if (status != SANE_STATUS_GOOD)
98 DBG (DBG_error, "sanei_pv8630_read_byte error\n");
99 return status;
102 /* Prepare a bulk read. len is the size of the data going to be
103 * read by pv8630_bulkread(). */
104 SANE_Status
105 sanei_pv8630_prep_bulkread (int fd, int len)
107 SANE_Status status;
109 status =
110 sanei_usb_control_msg (fd, 0x40, PV8630_REQ_EPPBULKREAD, len & 0xffff,
111 len >> 16, 0, NULL);
113 if (status != SANE_STATUS_GOOD)
114 DBG (DBG_error, "sanei_pv8630_prep_bulkread error\n");
115 return status;
118 /* Prepare a bulk write. len is the size of the data going to be
119 * written by pv8630_bulkwrite(). */
120 SANE_Status
121 sanei_pv8630_prep_bulkwrite (int fd, int len)
123 SANE_Status status;
125 status =
126 sanei_usb_control_msg (fd, 0x40, PV8630_REQ_EPPBULKWRITE, len & 0xffff,
127 len >> 16, 0, NULL);
129 if (status != SANE_STATUS_GOOD)
130 DBG (DBG_error, "sanei_pv8630_prep_bulkwrite error\n");
131 return status;
134 /* Flush the buffer. */
135 SANE_Status
136 sanei_pv8630_flush_buffer (int fd)
138 SANE_Status status;
140 status =
141 sanei_usb_control_msg (fd, 0x40, PV8630_REQ_FLUSHBUFFER, 0, 0, 0, NULL);
143 if (status != SANE_STATUS_GOOD)
144 DBG (DBG_error, "sanei_pv8630_flush_buffer error\n");
145 return status;
148 /* Do a bulk write. The length must have previously been sent via
149 * pv8630_prep_bulkwrite(). */
150 SANE_Status
151 sanei_pv8630_bulkwrite (int fd, const void *data, size_t * len)
153 SANE_Status status;
155 status = sanei_usb_write_bulk (fd, (const SANE_Byte *) data, len);
157 if (status != SANE_STATUS_GOOD)
158 DBG (DBG_error, "sanei_pv8630_bulkwrite error\n");
159 return status;
162 /* Do a bulk read. The length must have previously been sent via
163 * pv8630_prep_bulkread(). */
164 SANE_Status
165 sanei_pv8630_bulkread (int fd, void *data, size_t * len)
167 SANE_Status status;
169 status = sanei_usb_read_bulk (fd, data, len);
171 if (status != SANE_STATUS_GOOD)
172 DBG (DBG_error, "sanei_pv8630_bulkread error\n");
173 return status;
176 /* Expects a specific byte in a register */
177 SANE_Status
178 sanei_pv8630_xpect_byte (int fd, SANEI_PV_Index index, SANE_Byte value,
179 SANE_Byte mask)
181 SANE_Status status;
182 SANE_Byte s;
184 status = sanei_pv8630_read_byte (fd, index, &s);
185 if (status != SANE_STATUS_GOOD)
186 return status;
188 if ((s & mask) != value)
190 DBG (DBG_error, "sanei_pv8630_xpect_byte: expected %x, got %x\n", value,
192 return SANE_STATUS_IO_ERROR;
194 return SANE_STATUS_GOOD;
197 /* Wait for the status register to present a given status. A timeout value
198 is given in tenths of a second. */
199 SANE_Status
200 sanei_pv8630_wait_byte (int fd, SANEI_PV_Index index, SANE_Byte value,
201 SANE_Byte mask, int timeout)
203 SANE_Status status;
204 SANE_Byte s;
205 int n;
207 for (n = 0; n < timeout; n++)
210 status = sanei_pv8630_read_byte (fd, index, &s);
211 if (status != SANE_STATUS_GOOD)
212 return status;
214 if ((s & mask) == value)
215 return SANE_STATUS_GOOD;
217 usleep (100000);
220 DBG (DBG_error, "sanei_pv8630_wait_byte: timeout waiting for %x (got %x)\n",
221 value, s);
222 return SANE_STATUS_IO_ERROR;