1141cc406Sopenharmony_ci/* sane - Scanner Access Now Easy.
2141cc406Sopenharmony_ci   Copyright (C) 2001, 2002 Henning Meier-Geinitz
3141cc406Sopenharmony_ci   Copyright (C) 2003, 2005 Rene Rebe (sanei_read_int,sanei_set_timeout)
4141cc406Sopenharmony_ci   Copyright (C) 2008 m. allan noah (sanei_usb_clear_halt)
5141cc406Sopenharmony_ci   Copyright (C) 2011 Reinhold Kainhofer (sanei_usb_set_endpoint)
6141cc406Sopenharmony_ci   This file is part of the SANE package.
7141cc406Sopenharmony_ci
8141cc406Sopenharmony_ci   SANE is free software; you can redistribute it and/or modify it
9141cc406Sopenharmony_ci   under the terms of the GNU General Public License as published by
10141cc406Sopenharmony_ci   the Free Software Foundation; either version 2 of the License, or
11141cc406Sopenharmony_ci   (at your option) any later version.
12141cc406Sopenharmony_ci
13141cc406Sopenharmony_ci   SANE is distributed in the hope that it will be useful, but WITHOUT
14141cc406Sopenharmony_ci   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15141cc406Sopenharmony_ci   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
16141cc406Sopenharmony_ci   License for more details.
17141cc406Sopenharmony_ci
18141cc406Sopenharmony_ci   You should have received a copy of the GNU General Public License
19141cc406Sopenharmony_ci   along with sane; see the file COPYING.
20141cc406Sopenharmony_ci   If not, see <https://www.gnu.org/licenses/>.
21141cc406Sopenharmony_ci
22141cc406Sopenharmony_ci   As a special exception, the authors of SANE give permission for
23141cc406Sopenharmony_ci   additional uses of the libraries contained in this release of SANE.
24141cc406Sopenharmony_ci
25141cc406Sopenharmony_ci   The exception is that, if you link a SANE library with other files
26141cc406Sopenharmony_ci   to produce an executable, this does not by itself cause the
27141cc406Sopenharmony_ci   resulting executable to be covered by the GNU General Public
28141cc406Sopenharmony_ci   License.  Your use of that executable is in no way restricted on
29141cc406Sopenharmony_ci   account of linking the SANE library code into it.
30141cc406Sopenharmony_ci
31141cc406Sopenharmony_ci   This exception does not, however, invalidate any other reasons why
32141cc406Sopenharmony_ci   the executable file might be covered by the GNU General Public
33141cc406Sopenharmony_ci   License.
34141cc406Sopenharmony_ci
35141cc406Sopenharmony_ci   If you submit changes to SANE to the maintainers to be included in
36141cc406Sopenharmony_ci   a subsequent release, you agree by submitting the changes that
37141cc406Sopenharmony_ci   those changes may be distributed with this exception intact.
38141cc406Sopenharmony_ci
39141cc406Sopenharmony_ci   If you write modifications of your own for SANE, it is your choice
40141cc406Sopenharmony_ci   whether to permit this exception to apply to your modifications.
41141cc406Sopenharmony_ci   If you do not wish that, delete this exception notice.
42141cc406Sopenharmony_ci*/
43141cc406Sopenharmony_ci
44141cc406Sopenharmony_ci/** @file sanei_usb.h
45141cc406Sopenharmony_ci * This file provides a generic USB interface.
46141cc406Sopenharmony_ci *
47141cc406Sopenharmony_ci * Currently, two access methods to USB devices are provided:
48141cc406Sopenharmony_ci * - Access to device
49141cc406Sopenharmony_ci * files as used by the Linux kernel USB scanner driver is supported. FreeBSD
50141cc406Sopenharmony_ci * and OpenBSD with their uscanner drivers also work this way. However,
51141cc406Sopenharmony_ci * detection and control messages aren't supported on these platforms.
52141cc406Sopenharmony_ci * - Access using libusb (where available).
53141cc406Sopenharmony_ci *
54141cc406Sopenharmony_ci * A general remark: Do not mix sanei_usb functions with "normal" file-related
55141cc406Sopenharmony_ci * libc functions like open() or close.  The device numbers used in sanei_usb
56141cc406Sopenharmony_ci * are not file descriptors.
57141cc406Sopenharmony_ci *
58141cc406Sopenharmony_ci * @sa sanei_lm983x.h, sanei_pa4s2.h, sanei_pio.h, sanei_scsi.h, and <a
59141cc406Sopenharmony_ci * href="http://www.sane-project.org/man/sane-usb.5.html">man sane-usb(5)</a>
60141cc406Sopenharmony_ci * for user-oriented documentation
61141cc406Sopenharmony_ci */
62141cc406Sopenharmony_ci
63141cc406Sopenharmony_ci#ifndef sanei_usb_h
64141cc406Sopenharmony_ci#define sanei_usb_h
65141cc406Sopenharmony_ci
66141cc406Sopenharmony_ci#include "../include/sane/config.h"
67141cc406Sopenharmony_ci#include "../include/sane/sane.h"
68141cc406Sopenharmony_ci
69141cc406Sopenharmony_ci#include <stdlib.h> /* for size_t */
70141cc406Sopenharmony_ci
71141cc406Sopenharmony_ci#ifdef __cplusplus
72141cc406Sopenharmony_ciextern "C" {
73141cc406Sopenharmony_ci#endif
74141cc406Sopenharmony_ci
75141cc406Sopenharmony_ci/* USB spec defines */
76141cc406Sopenharmony_ci#ifndef USB_CLASS_PER_INTERFACE
77141cc406Sopenharmony_ci/* Also defined in libusb */
78141cc406Sopenharmony_ci/** @name Device and/or interface class codes */
79141cc406Sopenharmony_ci/* @{ */
80141cc406Sopenharmony_ci#define USB_CLASS_PER_INTERFACE         0x00
81141cc406Sopenharmony_ci#define USB_CLASS_AUDIO                 0x01
82141cc406Sopenharmony_ci#define USB_CLASS_COMM                  0x02
83141cc406Sopenharmony_ci#define USB_CLASS_HID                   0x03
84141cc406Sopenharmony_ci#define USB_CLASS_PRINTER               0x07
85141cc406Sopenharmony_ci#define USB_CLASS_MASS_STORAGE          0x08
86141cc406Sopenharmony_ci#define USB_CLASS_HUB                   0x09
87141cc406Sopenharmony_ci#define USB_CLASS_DATA                  0x0a
88141cc406Sopenharmony_ci#define USB_CLASS_VENDOR_SPEC           0xff
89141cc406Sopenharmony_ci/* @} */
90141cc406Sopenharmony_ci
91141cc406Sopenharmony_ci/** @name USB descriptor types */
92141cc406Sopenharmony_ci/* @{ */
93141cc406Sopenharmony_ci#define USB_DT_DEVICE                   0x01
94141cc406Sopenharmony_ci#define USB_DT_CONFIG                   0x02
95141cc406Sopenharmony_ci#define USB_DT_STRING                   0x03
96141cc406Sopenharmony_ci#define USB_DT_INTERFACE                0x04
97141cc406Sopenharmony_ci#define USB_DT_ENDPOINT                 0x05
98141cc406Sopenharmony_ci#define USB_DT_HID                      0x21
99141cc406Sopenharmony_ci#define USB_DT_REPORT                   0x22
100141cc406Sopenharmony_ci#define USB_DT_PHYSICAL                 0x23
101141cc406Sopenharmony_ci#define USB_DT_HUB                      0x29
102141cc406Sopenharmony_ci/* @} */
103141cc406Sopenharmony_ci
104141cc406Sopenharmony_ci/** @name Descriptor sizes per descriptor type */
105141cc406Sopenharmony_ci/* @{ */
106141cc406Sopenharmony_ci#define USB_DT_DEVICE_SIZE              18
107141cc406Sopenharmony_ci#define USB_DT_CONFIG_SIZE              9
108141cc406Sopenharmony_ci#define USB_DT_INTERFACE_SIZE           9
109141cc406Sopenharmony_ci#define USB_DT_ENDPOINT_SIZE            7
110141cc406Sopenharmony_ci#define USB_DT_ENDPOINT_AUDIO_SIZE      9
111141cc406Sopenharmony_ci#define USB_DT_HUB_NONVAR_SIZE          7
112141cc406Sopenharmony_ci/* @} */
113141cc406Sopenharmony_ci
114141cc406Sopenharmony_ci/** @name Endpoint descriptors */
115141cc406Sopenharmony_ci/* @{ */
116141cc406Sopenharmony_ci#define USB_ENDPOINT_ADDRESS_MASK       0x0f
117141cc406Sopenharmony_ci#define USB_ENDPOINT_DIR_MASK           0x80
118141cc406Sopenharmony_ci#define USB_ENDPOINT_TYPE_MASK          0x03
119141cc406Sopenharmony_ci#define USB_ENDPOINT_TYPE_CONTROL       0
120141cc406Sopenharmony_ci#define USB_ENDPOINT_TYPE_ISOCHRONOUS   1
121141cc406Sopenharmony_ci#define USB_ENDPOINT_TYPE_BULK          2
122141cc406Sopenharmony_ci#define USB_ENDPOINT_TYPE_INTERRUPT     3
123141cc406Sopenharmony_ci/* @} */
124141cc406Sopenharmony_ci
125141cc406Sopenharmony_ci/** @name Standard requests */
126141cc406Sopenharmony_ci/* @{ */
127141cc406Sopenharmony_ci#define USB_REQ_GET_STATUS              0x00
128141cc406Sopenharmony_ci#define USB_REQ_CLEAR_FEATURE           0x01
129141cc406Sopenharmony_ci#define USB_REQ_SET_FEATURE             0x03
130141cc406Sopenharmony_ci#define USB_REQ_SET_ADDRESS             0x05
131141cc406Sopenharmony_ci#define USB_REQ_GET_DESCRIPTOR          0x06
132141cc406Sopenharmony_ci#define USB_REQ_SET_DESCRIPTOR          0x07
133141cc406Sopenharmony_ci#define USB_REQ_GET_CONFIGURATION       0x08
134141cc406Sopenharmony_ci#define USB_REQ_SET_CONFIGURATION       0x09
135141cc406Sopenharmony_ci#define USB_REQ_GET_INTERFACE           0x0A
136141cc406Sopenharmony_ci#define USB_REQ_SET_INTERFACE           0x0B
137141cc406Sopenharmony_ci#define USB_REQ_SYNCH_FRAME             0x0C
138141cc406Sopenharmony_ci/* @} */
139141cc406Sopenharmony_ci
140141cc406Sopenharmony_ci/** @name USB types */
141141cc406Sopenharmony_ci/* @{ */
142141cc406Sopenharmony_ci#define USB_TYPE_STANDARD               (0x00 << 5)
143141cc406Sopenharmony_ci#define USB_TYPE_CLASS                  (0x01 << 5)
144141cc406Sopenharmony_ci#define USB_TYPE_VENDOR                 (0x02 << 5)
145141cc406Sopenharmony_ci#define USB_TYPE_RESERVED               (0x03 << 5)
146141cc406Sopenharmony_ci/* @} */
147141cc406Sopenharmony_ci
148141cc406Sopenharmony_ci/** @name USB recipients */
149141cc406Sopenharmony_ci/* @{ */
150141cc406Sopenharmony_ci#define USB_RECIP_DEVICE                0x00
151141cc406Sopenharmony_ci#define USB_RECIP_INTERFACE             0x01
152141cc406Sopenharmony_ci#define USB_RECIP_ENDPOINT              0x02
153141cc406Sopenharmony_ci#define USB_RECIP_OTHER                 0x03
154141cc406Sopenharmony_ci/* @} */
155141cc406Sopenharmony_ci
156141cc406Sopenharmony_ci#endif /* not USB_CLASS_PER_INTERFACE */
157141cc406Sopenharmony_ci
158141cc406Sopenharmony_ci/* Not defined in libsub */
159141cc406Sopenharmony_ci/** @name USB Masks */
160141cc406Sopenharmony_ci/* @{ */
161141cc406Sopenharmony_ci#define USB_TYPE_MASK                   (0x03 << 5)
162141cc406Sopenharmony_ci#define USB_RECIP_MASK                  0x1f
163141cc406Sopenharmony_ci/* @} */
164141cc406Sopenharmony_ci
165141cc406Sopenharmony_ci/** @name USB directions */
166141cc406Sopenharmony_ci/* @{ */
167141cc406Sopenharmony_ci#define USB_DIR_OUT                     0x00
168141cc406Sopenharmony_ci#define USB_DIR_IN                      0x80
169141cc406Sopenharmony_ci/* @} */
170141cc406Sopenharmony_ci
171141cc406Sopenharmony_ci/** */
172141cc406Sopenharmony_cistruct sanei_usb_dev_descriptor
173141cc406Sopenharmony_ci{
174141cc406Sopenharmony_ci	SANE_Byte    desc_type;
175141cc406Sopenharmony_ci	unsigned int bcd_usb;
176141cc406Sopenharmony_ci	unsigned int bcd_dev;
177141cc406Sopenharmony_ci	SANE_Byte    dev_class;
178141cc406Sopenharmony_ci	SANE_Byte    dev_sub_class;
179141cc406Sopenharmony_ci	SANE_Byte    dev_protocol;
180141cc406Sopenharmony_ci	SANE_Byte    max_packet_size;
181141cc406Sopenharmony_ci};
182141cc406Sopenharmony_ci
183141cc406Sopenharmony_ci/** Initialize sanei_usb for replay testing.
184141cc406Sopenharmony_ci
185141cc406Sopenharmony_ci    Initializes sanei_usb for testing by mocking whole USB stack. This function
186141cc406Sopenharmony_ci    must be called before sanei_usb_init().
187141cc406Sopenharmony_ci
188141cc406Sopenharmony_ci    The sanei_usb subsystem also implements a "development mode". It modifies
189141cc406Sopenharmony_ci    the XML data file with the actual commands of the test run and attempts to
190141cc406Sopenharmony_ci    proceed testing until a mismatching input command is found for which
191141cc406Sopenharmony_ci    input data is required.
192141cc406Sopenharmony_ci
193141cc406Sopenharmony_ci    A <known_commands_end/> node in the data XML file will cause sanei_usb not
194141cc406Sopenharmony_ci    to continue to the subsequent command in the XML file and instead it will
195141cc406Sopenharmony_ci    prepend all output commands before that node before an output command is
196141cc406Sopenharmony_ci    encountered.
197141cc406Sopenharmony_ci
198141cc406Sopenharmony_ci    @param path Path to the XML data file.
199141cc406Sopenharmony_ci    @param development_mode Enables development mode.
200141cc406Sopenharmony_ci */
201141cc406Sopenharmony_ciextern SANE_Status sanei_usb_testing_enable_replay(SANE_String_Const path,
202141cc406Sopenharmony_ci                                                   int development_mode);
203141cc406Sopenharmony_ci
204141cc406Sopenharmony_ci/** Initialize sanei_usb for recording.
205141cc406Sopenharmony_ci *
206141cc406Sopenharmony_ci * Initializes sanei_usb for recording communication with the scanner. This
207141cc406Sopenharmony_ci * function must be called before sanei_usb_init().
208141cc406Sopenharmony_ci *
209141cc406Sopenharmony_ci * @param path Path to the XML data file.
210141cc406Sopenharmony_ci * @param be_name The name of the backend to enable recording for.
211141cc406Sopenharmony_ci */
212141cc406Sopenharmony_ciextern SANE_Status sanei_usb_testing_enable_record(SANE_String_Const path,
213141cc406Sopenharmony_ci                                                   SANE_String_Const be_name);
214141cc406Sopenharmony_ci
215141cc406Sopenharmony_ci/** Returns backend name for testing.
216141cc406Sopenharmony_ci *
217141cc406Sopenharmony_ci * Returns backend name for the file registered in sanei_usb_testing_enable.
218141cc406Sopenharmony_ci * The caller is responsible for freeing it.
219141cc406Sopenharmony_ci */
220141cc406Sopenharmony_ciextern SANE_String sanei_usb_testing_get_backend();
221141cc406Sopenharmony_ci
222141cc406Sopenharmony_ci/** Returns SANE_TRUE if replay testing mode is enabled, i.e. whether we are working with fake
223141cc406Sopenharmony_ci * scan data.
224141cc406Sopenharmony_ci */
225141cc406Sopenharmony_ciextern SANE_Bool sanei_usb_is_replay_mode_enabled();
226141cc406Sopenharmony_ci
227141cc406Sopenharmony_ci/** Clears currently recorded data.
228141cc406Sopenharmony_ci
229141cc406Sopenharmony_ci    This is useful on certain backends to clear the currently recorded data if it relates to
230141cc406Sopenharmony_ci    other devices than the one that the scan will be performed on. On these backends all
231141cc406Sopenharmony_ci    devices that the backend supports are opened multiple times. Recording this interaction
232141cc406Sopenharmony_ci    to XML file makes it impossible to replay it, as the existence of these devices is not mocked
233141cc406Sopenharmony_ci    currently.
234141cc406Sopenharmony_ci
235141cc406Sopenharmony_ci    This function may only be called when no USB devices are open, otherwise the behavior is
236141cc406Sopenharmony_ci    unpredictable.
237141cc406Sopenharmony_ci */
238141cc406Sopenharmony_ciextern void sanei_usb_testing_record_clear();
239141cc406Sopenharmony_ci
240141cc406Sopenharmony_ci/** Records a debug message in the captured USB data if testing mode is enabled. If testing mode
241141cc406Sopenharmony_ci * is not enabled, this function does nothing.
242141cc406Sopenharmony_ci *
243141cc406Sopenharmony_ci * @param msg Message to record
244141cc406Sopenharmony_ci */
245141cc406Sopenharmony_ciextern void sanei_usb_testing_record_message(SANE_String_Const message);
246141cc406Sopenharmony_ci
247141cc406Sopenharmony_ci/** Initialize sanei_usb.
248141cc406Sopenharmony_ci *
249141cc406Sopenharmony_ci * Call this before any other sanei_usb function.
250141cc406Sopenharmony_ci */
251141cc406Sopenharmony_ciextern void sanei_usb_init (void);
252141cc406Sopenharmony_ci
253141cc406Sopenharmony_ci/** End sanei_usb use, freeing resources when needed.
254141cc406Sopenharmony_ci *
255141cc406Sopenharmony_ci * When the use count of sanei_usb reach 0, free resources and end
256141cc406Sopenharmony_ci * sanei_usb use.
257141cc406Sopenharmony_ci */
258141cc406Sopenharmony_ciextern void sanei_usb_exit (void);
259141cc406Sopenharmony_ci
260141cc406Sopenharmony_ci/** Search for USB devices.
261141cc406Sopenharmony_ci *
262141cc406Sopenharmony_ci * Search USB buses for scanner devices.
263141cc406Sopenharmony_ci */
264141cc406Sopenharmony_ciextern void sanei_usb_scan_devices (void);
265141cc406Sopenharmony_ci
266141cc406Sopenharmony_ci/** Get the vendor and product ids by device name.
267141cc406Sopenharmony_ci *
268141cc406Sopenharmony_ci * @param devname
269141cc406Sopenharmony_ci * @param vendor vendor id
270141cc406Sopenharmony_ci * @param product product id
271141cc406Sopenharmony_ci *
272141cc406Sopenharmony_ci * @return
273141cc406Sopenharmony_ci * - SANE_STATUS_GOOD - if the ids could be determined
274141cc406Sopenharmony_ci * - SANE_STATUS_INVAL - if the device is not found
275141cc406Sopenharmony_ci * - SANE_STATUS_UNSUPPORTED - if this method is not supported with the current
276141cc406Sopenharmony_ci *   access method
277141cc406Sopenharmony_ci */
278141cc406Sopenharmony_ciSANE_Status
279141cc406Sopenharmony_cisanei_usb_get_vendor_product_byname (SANE_String_Const devname,
280141cc406Sopenharmony_ci				     SANE_Word * vendor, SANE_Word * product);
281141cc406Sopenharmony_ci
282141cc406Sopenharmony_ci/** Get the vendor and product ids.
283141cc406Sopenharmony_ci *
284141cc406Sopenharmony_ci * Currently, only libusb devices and scanners supported by the Linux USB
285141cc406Sopenharmony_ci * scanner module can be found.  For the latter method, the Linux version
286141cc406Sopenharmony_ci * must be 2.4.8 or higher.
287141cc406Sopenharmony_ci *
288141cc406Sopenharmony_ci * @param dn device number of an already sanei_usb_opened device
289141cc406Sopenharmony_ci * @param vendor vendor id
290141cc406Sopenharmony_ci * @param product product id
291141cc406Sopenharmony_ci *
292141cc406Sopenharmony_ci * @return
293141cc406Sopenharmony_ci * - SANE_STATUS_GOOD - if the ids could be determined
294141cc406Sopenharmony_ci * - SANE_STATUS_UNSUPPORTED - if the OS doesn't support detection of ids
295141cc406Sopenharmony_ci */
296141cc406Sopenharmony_ciextern SANE_Status
297141cc406Sopenharmony_cisanei_usb_get_vendor_product (SANE_Int dn, SANE_Word * vendor,
298141cc406Sopenharmony_ci			      SANE_Word * product);
299141cc406Sopenharmony_ci
300141cc406Sopenharmony_ci/** Find devices that match given vendor and product ids.
301141cc406Sopenharmony_ci *
302141cc406Sopenharmony_ci * For limitations, see function sanei_usb_get_vendor_product().
303141cc406Sopenharmony_ci * The function attach is called for every device which has been found.
304141cc406Sopenharmony_ci *
305141cc406Sopenharmony_ci * @param vendor vendor id
306141cc406Sopenharmony_ci * @param product product id
307141cc406Sopenharmony_ci * @param attach attach function
308141cc406Sopenharmony_ci *
309141cc406Sopenharmony_ci * @return SANE_STATUS_GOOD - on success (even if no scanner was found)
310141cc406Sopenharmony_ci */
311141cc406Sopenharmony_ciextern SANE_Status
312141cc406Sopenharmony_cisanei_usb_find_devices (SANE_Int vendor, SANE_Int product,
313141cc406Sopenharmony_ci			SANE_Status (*attach) (SANE_String_Const devname));
314141cc406Sopenharmony_ci
315141cc406Sopenharmony_ci/** Open a USB device.
316141cc406Sopenharmony_ci *
317141cc406Sopenharmony_ci * The device is opened by its name devname and the device number is
318141cc406Sopenharmony_ci * returned in dn on success.
319141cc406Sopenharmony_ci *
320141cc406Sopenharmony_ci * Device names can be either device file names for direct access over
321141cc406Sopenharmony_ci * kernel drivers (like /dev/usb/scanner) or libusb names. The libusb format
322141cc406Sopenharmony_ci * looks like this: "libusb:bus-id:device-id". Bus-id and device-id are
323141cc406Sopenharmony_ci * platform-dependent. An example could look like this: "libusb:001:002"
324141cc406Sopenharmony_ci * (Linux).
325141cc406Sopenharmony_ci *
326141cc406Sopenharmony_ci * @param devname name of the device to open
327141cc406Sopenharmony_ci * @param dn device number
328141cc406Sopenharmony_ci *
329141cc406Sopenharmony_ci * @return
330141cc406Sopenharmony_ci * - SANE_STATUS_GOOD - on success
331141cc406Sopenharmony_ci * - SANE_STATUS_ACCESS_DENIED - if the file couldn't be accessed due to
332141cc406Sopenharmony_ci *   permissions
333141cc406Sopenharmony_ci * - SANE_STATUS_INVAL - on every other error
334141cc406Sopenharmony_ci */
335141cc406Sopenharmony_ciextern SANE_Status sanei_usb_open (SANE_String_Const devname, SANE_Int * dn);
336141cc406Sopenharmony_ci
337141cc406Sopenharmony_ci/** Set the endpoint for the USB communication
338141cc406Sopenharmony_ci *
339141cc406Sopenharmony_ci * Allows to switch to a different endpoint for the USB communication than
340141cc406Sopenharmony_ci * the default (auto-detected) endpoint. This function can only be called
341141cc406Sopenharmony_ci * after sanei_usb_open.
342141cc406Sopenharmony_ci *
343141cc406Sopenharmony_ci * @param dn device number
344141cc406Sopenharmony_ci * @param ep_type type of endpoint to set (bitwise or of USB_DIR_IN/OUT and
345141cc406Sopenharmony_ci *                USB_ENDPOINT_TYPE_BULK/CONTROL/INTERRUPT/ISOCHRONOUS
346141cc406Sopenharmony_ci * @param ep endpoint to use for the given type
347141cc406Sopenharmony_ci *
348141cc406Sopenharmony_ci */
349141cc406Sopenharmony_ciextern void sanei_usb_set_endpoint (SANE_Int dn, SANE_Int ep_type, SANE_Int ep);
350141cc406Sopenharmony_ci
351141cc406Sopenharmony_ci/** Retrieve the endpoint used for the USB communication
352141cc406Sopenharmony_ci *
353141cc406Sopenharmony_ci * Returns the endpoint used for the USB communication of the given type.
354141cc406Sopenharmony_ci * This function can only be called after sanei_usb_open.
355141cc406Sopenharmony_ci *
356141cc406Sopenharmony_ci * @param dn device number
357141cc406Sopenharmony_ci * @param ep_type type of endpoint to retrieve (bitwise or of USB_DIR_IN/OUT
358141cc406Sopenharmony_ci *                and USB_ENDPOINT_TYPE_BULK/CONTROL/INTERRUPT/ISOCHRONOUS
359141cc406Sopenharmony_ci * @return endpoint used for the given type
360141cc406Sopenharmony_ci *
361141cc406Sopenharmony_ci */
362141cc406Sopenharmony_ciextern SANE_Int sanei_usb_get_endpoint (SANE_Int dn, SANE_Int ep_type);
363141cc406Sopenharmony_ci
364141cc406Sopenharmony_ci/** Close a USB device.
365141cc406Sopenharmony_ci *
366141cc406Sopenharmony_ci * @param dn device number
367141cc406Sopenharmony_ci */
368141cc406Sopenharmony_ciextern void sanei_usb_close (SANE_Int dn);
369141cc406Sopenharmony_ci
370141cc406Sopenharmony_ci/** Set the libusb timeout for bulk and interrupt reads.
371141cc406Sopenharmony_ci *
372141cc406Sopenharmony_ci * @param timeout the new timeout in ms
373141cc406Sopenharmony_ci */
374141cc406Sopenharmony_ciextern void sanei_usb_set_timeout (SANE_Int timeout);
375141cc406Sopenharmony_ci
376141cc406Sopenharmony_ci/** Check if sanei_usb_set_timeout() is available.
377141cc406Sopenharmony_ci */
378141cc406Sopenharmony_ci#define HAVE_SANEI_USB_SET_TIMEOUT
379141cc406Sopenharmony_ci
380141cc406Sopenharmony_ci/** Clear halt condition on bulk endpoints
381141cc406Sopenharmony_ci *
382141cc406Sopenharmony_ci * @param dn device number
383141cc406Sopenharmony_ci */
384141cc406Sopenharmony_ciextern SANE_Status sanei_usb_clear_halt (SANE_Int dn);
385141cc406Sopenharmony_ci
386141cc406Sopenharmony_ci/** Check if sanei_usb_clear_halt() is available.
387141cc406Sopenharmony_ci */
388141cc406Sopenharmony_ci#define HAVE_SANEI_USB_CLEAR_HALT
389141cc406Sopenharmony_ci
390141cc406Sopenharmony_ci/** Reset device
391141cc406Sopenharmony_ci *
392141cc406Sopenharmony_ci * @param dn device number
393141cc406Sopenharmony_ci */
394141cc406Sopenharmony_ciextern SANE_Status sanei_usb_reset (SANE_Int dn);
395141cc406Sopenharmony_ci
396141cc406Sopenharmony_ci/** Initiate a bulk transfer read.
397141cc406Sopenharmony_ci *
398141cc406Sopenharmony_ci * Read up to size bytes from the device to buffer. After the read, size
399141cc406Sopenharmony_ci * contains the number of bytes actually read.
400141cc406Sopenharmony_ci *
401141cc406Sopenharmony_ci * @param dn device number
402141cc406Sopenharmony_ci * @param buffer buffer to store read data in
403141cc406Sopenharmony_ci * @param size size of the data
404141cc406Sopenharmony_ci *
405141cc406Sopenharmony_ci * @return
406141cc406Sopenharmony_ci * - SANE_STATUS_GOOD - on success
407141cc406Sopenharmony_ci * - SANE_STATUS_EOF - if zero bytes have been read
408141cc406Sopenharmony_ci * - SANE_STATUS_IO_ERROR - if an error occurred during the read
409141cc406Sopenharmony_ci * - SANE_STATUS_INVAL - on every other error
410141cc406Sopenharmony_ci *
411141cc406Sopenharmony_ci */
412141cc406Sopenharmony_ciextern SANE_Status
413141cc406Sopenharmony_cisanei_usb_read_bulk (SANE_Int dn, SANE_Byte * buffer, size_t * size);
414141cc406Sopenharmony_ci
415141cc406Sopenharmony_ci/** Initiate a bulk transfer write.
416141cc406Sopenharmony_ci *
417141cc406Sopenharmony_ci * Write up to size bytes from buffer to the device. After the write size
418141cc406Sopenharmony_ci * contains the number of bytes actually written.
419141cc406Sopenharmony_ci *
420141cc406Sopenharmony_ci * @param dn device number
421141cc406Sopenharmony_ci * @param buffer buffer to write to device
422141cc406Sopenharmony_ci * @param size size of the data
423141cc406Sopenharmony_ci *
424141cc406Sopenharmony_ci * @return
425141cc406Sopenharmony_ci * - SANE_STATUS_GOOD - on success
426141cc406Sopenharmony_ci * - SANE_STATUS_IO_ERROR - if an error occurred during the write
427141cc406Sopenharmony_ci * - SANE_STATUS_INVAL - on every other error
428141cc406Sopenharmony_ci */
429141cc406Sopenharmony_ciextern SANE_Status
430141cc406Sopenharmony_cisanei_usb_write_bulk (SANE_Int dn, const SANE_Byte * buffer, size_t * size);
431141cc406Sopenharmony_ci
432141cc406Sopenharmony_ci/** Send/receive a control message to/from a USB device.
433141cc406Sopenharmony_ci *
434141cc406Sopenharmony_ci * This function is only supported for libusb devices and kernel access with
435141cc406Sopenharmony_ci * Linux 2.4.13 and newer.
436141cc406Sopenharmony_ci * For a detailed explanation of the parameters, have a look at the USB
437141cc406Sopenharmony_ci * specification at the <a href="http://www.usb.org/developers/docs/">
438141cc406Sopenharmony_ci * www.usb.org developers information page</a>.
439141cc406Sopenharmony_ci *
440141cc406Sopenharmony_ci * @param dn device number
441141cc406Sopenharmony_ci * @param rtype specifies the characteristics of the request (e.g. data
442141cc406Sopenharmony_ci *    direction)
443141cc406Sopenharmony_ci * @param req actual request
444141cc406Sopenharmony_ci * @param value parameter specific to the request
445141cc406Sopenharmony_ci * @param index parameter specific to the request (often used to select
446141cc406Sopenharmony_ci *     endpoint)
447141cc406Sopenharmony_ci * @param len length of data to send/receive
448141cc406Sopenharmony_ci * @param data buffer to send/receive data
449141cc406Sopenharmony_ci *
450141cc406Sopenharmony_ci * @return
451141cc406Sopenharmony_ci * - SANE_STATUS_GOOD - on success
452141cc406Sopenharmony_ci * - SANE_STATUS_IO_ERROR - on error
453141cc406Sopenharmony_ci * - SANE_STATUS_UNSUPPORTED - if the feature is not supported by the OS or
454141cc406Sopenharmony_ci *   SANE.
455141cc406Sopenharmony_ci */
456141cc406Sopenharmony_ciextern SANE_Status
457141cc406Sopenharmony_cisanei_usb_control_msg (SANE_Int dn, SANE_Int rtype, SANE_Int req,
458141cc406Sopenharmony_ci		       SANE_Int value, SANE_Int index, SANE_Int len,
459141cc406Sopenharmony_ci		       SANE_Byte * data);
460141cc406Sopenharmony_ci
461141cc406Sopenharmony_ci/** Initiate a interrupt transfer read.
462141cc406Sopenharmony_ci *
463141cc406Sopenharmony_ci * Read up to size bytes from the interrupt endpoint from the device to
464141cc406Sopenharmony_ci * buffer. After the read, size contains the number of bytes actually read.
465141cc406Sopenharmony_ci *
466141cc406Sopenharmony_ci * @param dn device number
467141cc406Sopenharmony_ci * @param buffer buffer to store read data in
468141cc406Sopenharmony_ci * @param size size of the data
469141cc406Sopenharmony_ci *
470141cc406Sopenharmony_ci * @return
471141cc406Sopenharmony_ci * - SANE_STATUS_GOOD - on success
472141cc406Sopenharmony_ci * - SANE_STATUS_EOF - if zero bytes have been read
473141cc406Sopenharmony_ci * - SANE_STATUS_IO_ERROR - if an error occurred during the read
474141cc406Sopenharmony_ci * - SANE_STATUS_INVAL - on every other error
475141cc406Sopenharmony_ci *
476141cc406Sopenharmony_ci */
477141cc406Sopenharmony_ci
478141cc406Sopenharmony_ciextern SANE_Status
479141cc406Sopenharmony_cisanei_usb_read_int (SANE_Int dn, SANE_Byte * buffer, size_t * size);
480141cc406Sopenharmony_ci
481141cc406Sopenharmony_ci/** Expand device name patterns into a list of devices.
482141cc406Sopenharmony_ci *
483141cc406Sopenharmony_ci * Apart from a normal device name (such as /dev/usb/scanner0 or
484141cc406Sopenharmony_ci * libusb:002:003), this function currently supports USB device
485141cc406Sopenharmony_ci * specifications of the form:
486141cc406Sopenharmony_ci *
487141cc406Sopenharmony_ci *	usb VENDOR PRODUCT
488141cc406Sopenharmony_ci *
489141cc406Sopenharmony_ci * VENDOR and PRODUCT are non-negative integer numbers in decimal or
490141cc406Sopenharmony_ci * hexadecimal format. A similar function for SCSI devices can be found
491141cc406Sopenharmony_ci * in include/sane/config.h.
492141cc406Sopenharmony_ci *
493141cc406Sopenharmony_ci * @param name device name pattern
494141cc406Sopenharmony_ci * @param attach attach function
495141cc406Sopenharmony_ci *
496141cc406Sopenharmony_ci */
497141cc406Sopenharmony_ciextern void
498141cc406Sopenharmony_cisanei_usb_attach_matching_devices (const char *name,
499141cc406Sopenharmony_ci				   SANE_Status (*attach) (const char *dev));
500141cc406Sopenharmony_ci
501141cc406Sopenharmony_ci/** Initiate set configuration.
502141cc406Sopenharmony_ci *
503141cc406Sopenharmony_ci * Change set configuration
504141cc406Sopenharmony_ci *
505141cc406Sopenharmony_ci * @param dn device number
506141cc406Sopenharmony_ci * @param configuration, configuration nummber
507141cc406Sopenharmony_ci *
508141cc406Sopenharmony_ci * @return
509141cc406Sopenharmony_ci * - SANE_STATUS_GOOD - on success
510141cc406Sopenharmony_ci * - SANE_STATUS_EOF - if zero bytes have been read
511141cc406Sopenharmony_ci * - SANE_STATUS_IO_ERROR - if an error occurred during the read
512141cc406Sopenharmony_ci * - SANE_STATUS_INVAL - on every other error
513141cc406Sopenharmony_ci *
514141cc406Sopenharmony_ci */
515141cc406Sopenharmony_ci
516141cc406Sopenharmony_ciextern SANE_Status
517141cc406Sopenharmony_cisanei_usb_set_configuration (SANE_Int dn, SANE_Int configuration);
518141cc406Sopenharmony_ci
519141cc406Sopenharmony_ci/** Initiate claim interface.
520141cc406Sopenharmony_ci *
521141cc406Sopenharmony_ci * Change claim interface
522141cc406Sopenharmony_ci *
523141cc406Sopenharmony_ci * @param dn device number
524141cc406Sopenharmony_ci * @param interface_number interface number
525141cc406Sopenharmony_ci *
526141cc406Sopenharmony_ci * @return
527141cc406Sopenharmony_ci * - SANE_STATUS_GOOD - on success
528141cc406Sopenharmony_ci * - SANE_STATUS_EOF - if zero bytes have been read
529141cc406Sopenharmony_ci * - SANE_STATUS_IO_ERROR - if an error occurred during the read
530141cc406Sopenharmony_ci * - SANE_STATUS_INVAL - on every other error
531141cc406Sopenharmony_ci *
532141cc406Sopenharmony_ci */
533141cc406Sopenharmony_ci
534141cc406Sopenharmony_ciextern SANE_Status
535141cc406Sopenharmony_cisanei_usb_claim_interface (SANE_Int dn, SANE_Int interface_number);
536141cc406Sopenharmony_ci
537141cc406Sopenharmony_ci/** Initiate release interface.
538141cc406Sopenharmony_ci *
539141cc406Sopenharmony_ci * Change release interface
540141cc406Sopenharmony_ci *
541141cc406Sopenharmony_ci * @param dn device number
542141cc406Sopenharmony_ci * @param interface_number interface number
543141cc406Sopenharmony_ci *
544141cc406Sopenharmony_ci * @return
545141cc406Sopenharmony_ci * - SANE_STATUS_GOOD - on success
546141cc406Sopenharmony_ci * - SANE_STATUS_EOF - if zero bytes have been read
547141cc406Sopenharmony_ci * - SANE_STATUS_IO_ERROR - if an error occurred during the read
548141cc406Sopenharmony_ci * - SANE_STATUS_INVAL - on every other error
549141cc406Sopenharmony_ci *
550141cc406Sopenharmony_ci */
551141cc406Sopenharmony_ci
552141cc406Sopenharmony_ciextern SANE_Status
553141cc406Sopenharmony_cisanei_usb_release_interface (SANE_Int dn, SANE_Int interface_number);
554141cc406Sopenharmony_ci
555141cc406Sopenharmony_ci/** Initiate a set altinterface.
556141cc406Sopenharmony_ci *
557141cc406Sopenharmony_ci * Change set alternate
558141cc406Sopenharmony_ci *
559141cc406Sopenharmony_ci * @param dn device number
560141cc406Sopenharmony_ci * @param alternate, alternate nummber
561141cc406Sopenharmony_ci *
562141cc406Sopenharmony_ci * @return
563141cc406Sopenharmony_ci * - SANE_STATUS_GOOD - on success
564141cc406Sopenharmony_ci * - SANE_STATUS_EOF - if zero bytes have been read
565141cc406Sopenharmony_ci * - SANE_STATUS_IO_ERROR - if an error occurred during the read
566141cc406Sopenharmony_ci * - SANE_STATUS_INVAL - on every other error
567141cc406Sopenharmony_ci *
568141cc406Sopenharmony_ci */
569141cc406Sopenharmony_ci
570141cc406Sopenharmony_ciextern SANE_Status
571141cc406Sopenharmony_cisanei_usb_set_altinterface (SANE_Int dn, SANE_Int alternate);
572141cc406Sopenharmony_ci
573141cc406Sopenharmony_ci/** Get some information from the device descriptor
574141cc406Sopenharmony_ci *
575141cc406Sopenharmony_ci * Sometimes it's useful to know something about revisions and
576141cc406Sopenharmony_ci * other stuff reported by the USB system
577141cc406Sopenharmony_ci *
578141cc406Sopenharmony_ci * @param dn device number
579141cc406Sopenharmony_ci * @param desc where to put the information to
580141cc406Sopenharmony_ci *
581141cc406Sopenharmony_ci * @return
582141cc406Sopenharmony_ci * - SANE_STATUS_GOOD - on success
583141cc406Sopenharmony_ci * - SANE_STATUS_UNSUPPORTED - if the feature is not supported by the OS or
584141cc406Sopenharmony_ci *   SANE.
585141cc406Sopenharmony_ci * - SANE_STATUS_INVAL - on every other error
586141cc406Sopenharmony_ci *
587141cc406Sopenharmony_ci */
588141cc406Sopenharmony_ci
589141cc406Sopenharmony_ciextern SANE_Status
590141cc406Sopenharmony_cisanei_usb_get_descriptor( SANE_Int dn, struct sanei_usb_dev_descriptor *desc );
591141cc406Sopenharmony_ci
592141cc406Sopenharmony_ci#ifdef __cplusplus
593141cc406Sopenharmony_ci} // extern "C"
594141cc406Sopenharmony_ci#endif
595141cc406Sopenharmony_ci
596141cc406Sopenharmony_ci/*------------------------------------------------------*/
597141cc406Sopenharmony_ci#endif /* sanei_usb_h */
598