1141cc406Sopenharmony_ci/***************************************************************************
2141cc406Sopenharmony_ci * SANE - Scanner Access Now Easy.
3141cc406Sopenharmony_ci
4141cc406Sopenharmony_ci   dc240.h
5141cc406Sopenharmony_ci
6141cc406Sopenharmony_ci   03/12/01 - Peter Fales
7141cc406Sopenharmony_ci
8141cc406Sopenharmony_ci   Based on the dc210 driver, (C) 1998 Brian J. Murrell (which is
9141cc406Sopenharmony_ci	based on dc25 driver (C) 1998 by Peter Fales)
10141cc406Sopenharmony_ci
11141cc406Sopenharmony_ci   This file (C) 2001 by Peter Fales
12141cc406Sopenharmony_ci
13141cc406Sopenharmony_ci   This file is part of the SANE package.
14141cc406Sopenharmony_ci
15141cc406Sopenharmony_ci   This program is free software; you can redistribute it and/or
16141cc406Sopenharmony_ci   modify it under the terms of the GNU General Public License as
17141cc406Sopenharmony_ci   published by the Free Software Foundation; either version 2 of the
18141cc406Sopenharmony_ci   License, or (at your option) any later version.
19141cc406Sopenharmony_ci
20141cc406Sopenharmony_ci   This program is distributed in the hope that it will be useful, but
21141cc406Sopenharmony_ci   WITHOUT ANY WARRANTY; without even the implied warranty of
22141cc406Sopenharmony_ci   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23141cc406Sopenharmony_ci   General Public License for more details.
24141cc406Sopenharmony_ci
25141cc406Sopenharmony_ci   You should have received a copy of the GNU General Public License
26141cc406Sopenharmony_ci   along with this program.  If not, see <https://www.gnu.org/licenses/>.
27141cc406Sopenharmony_ci
28141cc406Sopenharmony_ci   As a special exception, the authors of SANE give permission for
29141cc406Sopenharmony_ci   additional uses of the libraries contained in this release of SANE.
30141cc406Sopenharmony_ci
31141cc406Sopenharmony_ci   The exception is that, if you link a SANE library with other files
32141cc406Sopenharmony_ci   to produce an executable, this does not by itself cause the
33141cc406Sopenharmony_ci   resulting executable to be covered by the GNU General Public
34141cc406Sopenharmony_ci   License.  Your use of that executable is in no way restricted on
35141cc406Sopenharmony_ci   account of linking the SANE library code into it.
36141cc406Sopenharmony_ci
37141cc406Sopenharmony_ci   This exception does not, however, invalidate any other reasons why
38141cc406Sopenharmony_ci   the executable file might be covered by the GNU General Public
39141cc406Sopenharmony_ci   License.
40141cc406Sopenharmony_ci
41141cc406Sopenharmony_ci   If you submit changes to SANE to the maintainers to be included in
42141cc406Sopenharmony_ci   a subsequent release, you agree by submitting the changes that
43141cc406Sopenharmony_ci   those changes may be distributed with this exception intact.
44141cc406Sopenharmony_ci
45141cc406Sopenharmony_ci   If you write modifications of your own for SANE, it is your choice
46141cc406Sopenharmony_ci   whether to permit this exception to apply to your modifications.
47141cc406Sopenharmony_ci   If you do not wish that, delete this exception notice.
48141cc406Sopenharmony_ci
49141cc406Sopenharmony_ci ***************************************************************************
50141cc406Sopenharmony_ci
51141cc406Sopenharmony_ci   This file implements a SANE backend for the Kodak DC-240
52141cc406Sopenharmony_ci   digital camera.  THIS IS EXTREMELY ALPHA CODE!  USE AT YOUR OWN RISK!!
53141cc406Sopenharmony_ci
54141cc406Sopenharmony_ci   (feedback to:  dc240-devel@fales-lorenz.net)
55141cc406Sopenharmony_ci
56141cc406Sopenharmony_ci   This backend is based somewhat on the dc25 backend included in this
57141cc406Sopenharmony_ci   package by Peter Fales, and the dc210 backend by Brian J. Murrell
58141cc406Sopenharmony_ci
59141cc406Sopenharmony_ci ***************************************************************************/
60141cc406Sopenharmony_ci
61141cc406Sopenharmony_ci#include <stdio.h>
62141cc406Sopenharmony_ci#include <stdlib.h>
63141cc406Sopenharmony_ci#include <unistd.h>
64141cc406Sopenharmony_ci#include <fcntl.h>
65141cc406Sopenharmony_ci#include <termios.h>
66141cc406Sopenharmony_ci#include <string.h>
67141cc406Sopenharmony_ci
68141cc406Sopenharmony_ci#ifndef TRUE
69141cc406Sopenharmony_ci#define TRUE	(1==1)
70141cc406Sopenharmony_ci#endif
71141cc406Sopenharmony_ci
72141cc406Sopenharmony_ci#ifndef FALSE
73141cc406Sopenharmony_ci#define FALSE	(!TRUE)
74141cc406Sopenharmony_ci#endif
75141cc406Sopenharmony_ci
76141cc406Sopenharmony_ci#ifndef NULL
77141cc406Sopenharmony_ci#define NULL	0L
78141cc406Sopenharmony_ci#endif
79141cc406Sopenharmony_ci
80141cc406Sopenharmony_citypedef struct picture_info
81141cc406Sopenharmony_ci{
82141cc406Sopenharmony_ci  int low_res;
83141cc406Sopenharmony_ci  int size;
84141cc406Sopenharmony_ci}
85141cc406Sopenharmony_ciPictureInfo;
86141cc406Sopenharmony_ci
87141cc406Sopenharmony_citypedef struct DC240_s
88141cc406Sopenharmony_ci{
89141cc406Sopenharmony_ci  SANE_Int fd;			/* file descriptor to talk to it */
90141cc406Sopenharmony_ci  char *tty_name;		/* the tty port name it's on */
91141cc406Sopenharmony_ci  speed_t baud;			/* current tty speed */
92141cc406Sopenharmony_ci  SANE_Bool scanning;		/* currently scanning an image? */
93141cc406Sopenharmony_ci  SANE_Byte model;
94141cc406Sopenharmony_ci  SANE_Byte ver_major;
95141cc406Sopenharmony_ci  SANE_Byte ver_minor;
96141cc406Sopenharmony_ci  SANE_Int pic_taken;
97141cc406Sopenharmony_ci  SANE_Int pic_left;
98141cc406Sopenharmony_ci  struct
99141cc406Sopenharmony_ci  {
100141cc406Sopenharmony_ci    unsigned int low_res:1;
101141cc406Sopenharmony_ci    unsigned int low_batt:1;
102141cc406Sopenharmony_ci  }
103141cc406Sopenharmony_ci  flags;
104141cc406Sopenharmony_ci  PictureInfo *Pictures;	/* array of pictures */
105141cc406Sopenharmony_ci  SANE_Int current_picture_number;	/* picture being operated on */
106141cc406Sopenharmony_ci}
107141cc406Sopenharmony_ciDC240;
108141cc406Sopenharmony_ci
109141cc406Sopenharmony_citypedef struct dc240_info_s
110141cc406Sopenharmony_ci{
111141cc406Sopenharmony_ci  SANE_Byte model;
112141cc406Sopenharmony_ci  SANE_Byte ver_major;
113141cc406Sopenharmony_ci  SANE_Byte ver_minor;
114141cc406Sopenharmony_ci  SANE_Int pic_taken;
115141cc406Sopenharmony_ci  SANE_Int pic_left;
116141cc406Sopenharmony_ci  struct
117141cc406Sopenharmony_ci  {
118141cc406Sopenharmony_ci    SANE_Int low_res:1;
119141cc406Sopenharmony_ci    SANE_Int low_batt:1;
120141cc406Sopenharmony_ci  }
121141cc406Sopenharmony_ci  flags;
122141cc406Sopenharmony_ci}
123141cc406Sopenharmony_ciDc240Info, *Dc240InfoPtr;
124141cc406Sopenharmony_ci
125141cc406Sopenharmony_cistatic SANE_Int get_info (DC240 *);
126141cc406Sopenharmony_ci
127141cc406Sopenharmony_ci#define INIT_PCK	{0x41, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1A}
128141cc406Sopenharmony_ci/*                               ^^^^^^^^^^
129141cc406Sopenharmony_ci *                               Baud rate: (see pkt_speed structure)
130141cc406Sopenharmony_ci *                                 0x96 0x00 -> 9600 baud
131141cc406Sopenharmony_ci *                                 0x19 0x20 -> 19200 baud
132141cc406Sopenharmony_ci *                                 0x38 0x40 -> 38400 baud
133141cc406Sopenharmony_ci *                                 0x57 0x60 -> 57600 baud
134141cc406Sopenharmony_ci *                                 0x11 0x52 -> 115200 baud
135141cc406Sopenharmony_ci */
136141cc406Sopenharmony_ci#define INFO_PCK	{0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1A}
137141cc406Sopenharmony_ci#define SHOOT_PCK	{0x7C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1A}
138141cc406Sopenharmony_ci#define ERASE_PCK	{0x9D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1A}
139141cc406Sopenharmony_ci#define RES_PCK		{0x36, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1A}
140141cc406Sopenharmony_ci/*                                   ^^^^
141141cc406Sopenharmony_ci *                                   Resolution: 0x00 = low, 0x01 = high
142141cc406Sopenharmony_ci */
143141cc406Sopenharmony_ci#define THUMBS_PCK	{0x93, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x1A}
144141cc406Sopenharmony_ci/*
145141cc406Sopenharmony_ci *
146141cc406Sopenharmony_ci */
147141cc406Sopenharmony_ci#define PICS_PCK	{0x9A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1A}
148141cc406Sopenharmony_ci/*
149141cc406Sopenharmony_ci *
150141cc406Sopenharmony_ci */
151141cc406Sopenharmony_ci#define PICS_INFO_PCK	{0x91, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1A}
152141cc406Sopenharmony_ci/*
153141cc406Sopenharmony_ci *
154141cc406Sopenharmony_ci */
155141cc406Sopenharmony_ci#define OPEN_CARD_PCK	{0x96, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1A}
156141cc406Sopenharmony_ci#define READ_DIR_PCK	{0x99, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1A}
157141cc406Sopenharmony_ci/*                                   ^^^^
158141cc406Sopenharmony_ci *				     1=Report number of entries only
159141cc406Sopenharmony_ci */
160141cc406Sopenharmony_ci
161141cc406Sopenharmony_cistruct pkt_speed
162141cc406Sopenharmony_ci{
163141cc406Sopenharmony_ci  speed_t baud;
164141cc406Sopenharmony_ci  SANE_Byte pkt_code[2];
165141cc406Sopenharmony_ci};
166141cc406Sopenharmony_ci
167141cc406Sopenharmony_ci#if defined (B57600) && defined (B115200)
168141cc406Sopenharmony_ci# define SPEEDS			{ {   B9600, { 0x96, 0x00 } }, \
169141cc406Sopenharmony_ci				  {  B19200, { 0x19, 0x20 } }, \
170141cc406Sopenharmony_ci				  {  B38400, { 0x38, 0x40 } }, \
171141cc406Sopenharmony_ci				  {  B57600, { 0x57, 0x60 } }, \
172141cc406Sopenharmony_ci				  { B115200, { 0x11, 0x52 } }  }
173141cc406Sopenharmony_ci#else
174141cc406Sopenharmony_ci# define SPEEDS			{ {   B9600, { 0x96, 0x00 } }, \
175141cc406Sopenharmony_ci				  {  B19200, { 0x19, 0x20 } }, \
176141cc406Sopenharmony_ci				  {  B38400, { 0x38, 0x40 } }  }
177141cc406Sopenharmony_ci#endif
178141cc406Sopenharmony_ci
179141cc406Sopenharmony_ci#define HIGH_RES		0
180141cc406Sopenharmony_ci#define LOW_RES			1
181141cc406Sopenharmony_ci
182141cc406Sopenharmony_ci#define HIGHRES_WIDTH		1280
183141cc406Sopenharmony_ci#define HIGHRES_HEIGHT		960
184141cc406Sopenharmony_ci
185141cc406Sopenharmony_ci#define LOWRES_WIDTH		640
186141cc406Sopenharmony_ci#define LOWRES_HEIGHT		480
187141cc406Sopenharmony_ci
188141cc406Sopenharmony_ci/*
189141cc406Sopenharmony_ci *    External definitions
190141cc406Sopenharmony_ci */
191141cc406Sopenharmony_ci
192141cc406Sopenharmony_ciextern char *__progname;	/* Defined in /usr/lib/crt0.o */
193141cc406Sopenharmony_ci
194141cc406Sopenharmony_ci
195141cc406Sopenharmony_cistruct cam_dirent
196141cc406Sopenharmony_ci{
197141cc406Sopenharmony_ci  SANE_Char name[11];
198141cc406Sopenharmony_ci  SANE_Byte attr;
199141cc406Sopenharmony_ci  SANE_Byte create_time[2];
200141cc406Sopenharmony_ci  SANE_Byte creat_date[2];
201141cc406Sopenharmony_ci  long size;
202141cc406Sopenharmony_ci};
203141cc406Sopenharmony_ci
204141cc406Sopenharmony_ci#ifdef OLD
205141cc406Sopenharmony_ci
206141cc406Sopenharmony_ci/* This is the layout of the directory in the camera - Unfortunately,
207141cc406Sopenharmony_ci * this only works in gcc.
208141cc406Sopenharmony_ci */
209141cc406Sopenharmony_cistruct dir_buf
210141cc406Sopenharmony_ci{
211141cc406Sopenharmony_ci  SANE_Byte entries_msb PACKED;
212141cc406Sopenharmony_ci  SANE_Byte entries_lsb PACKED;
213141cc406Sopenharmony_ci  struct cam_dirent entry[1000] PACKED;
214141cc406Sopenharmony_ci};
215141cc406Sopenharmony_ci#else
216141cc406Sopenharmony_ci
217141cc406Sopenharmony_ci/* So, we have to do it the hard way...  */
218141cc406Sopenharmony_ci
219141cc406Sopenharmony_ci#define CAMDIRENTRYSIZE 20
220141cc406Sopenharmony_ci#define DIRENTRIES 1000
221141cc406Sopenharmony_ci
222141cc406Sopenharmony_ci
223141cc406Sopenharmony_ci#define get_name(entry) (SANE_Char*) &dir_buf2[2+CAMDIRENTRYSIZE*(entry)]
224141cc406Sopenharmony_ci#define get_attr(entry) dir_buf2[2+11+CAMDIRENTRYSIZE*(entry)]
225141cc406Sopenharmony_ci#define get_create_time(entry) \
226141cc406Sopenharmony_ci   (  dir_buf2[2+12+CAMDIRENTRYSIZE*(entry)] << 8 \
227141cc406Sopenharmony_ci    + dir_buf2[2+13+CAMDIRENTRYSIZE*(entry)])
228141cc406Sopenharmony_ci
229141cc406Sopenharmony_ci
230141cc406Sopenharmony_ci#endif
231141cc406Sopenharmony_ci
232141cc406Sopenharmony_cistruct cam_dirlist
233141cc406Sopenharmony_ci{
234141cc406Sopenharmony_ci  SANE_Char name[48];
235141cc406Sopenharmony_ci  struct cam_dirlist *next;
236141cc406Sopenharmony_ci};
237141cc406Sopenharmony_ci
238141cc406Sopenharmony_ci
239141cc406Sopenharmony_ci
240141cc406Sopenharmony_ci#include <sys/types.h>
241141cc406Sopenharmony_ci
242141cc406Sopenharmony_ciFILE *sanei_config_open (const char *filename);
243141cc406Sopenharmony_ci
244141cc406Sopenharmony_cistatic SANE_Int init_dc240 (DC240 *);
245141cc406Sopenharmony_ci
246141cc406Sopenharmony_cistatic void close_dc240 (SANE_Int);
247141cc406Sopenharmony_ci
248141cc406Sopenharmony_cistatic SANE_Int read_data (SANE_Int fd, SANE_Byte * buf, SANE_Int sz);
249141cc406Sopenharmony_ci
250141cc406Sopenharmony_cistatic SANE_Int end_of_data (SANE_Int fd);
251141cc406Sopenharmony_ci
252141cc406Sopenharmony_cistatic PictureInfo *get_pictures_info (void);
253141cc406Sopenharmony_ci
254141cc406Sopenharmony_cistatic SANE_Int get_picture_info (PictureInfo * pic, SANE_Int p);
255141cc406Sopenharmony_ci
256141cc406Sopenharmony_cistatic SANE_Status snap_pic (SANE_Int fd);
257141cc406Sopenharmony_ci
258141cc406Sopenharmony_cichar *sanei_config_read (char *str, int n, FILE * stream);
259141cc406Sopenharmony_ci
260141cc406Sopenharmony_cistatic SANE_Int read_dir (SANE_String dir);
261141cc406Sopenharmony_ci
262141cc406Sopenharmony_cistatic SANE_Int read_info (SANE_String fname);
263141cc406Sopenharmony_ci
264141cc406Sopenharmony_cistatic SANE_Int dir_insert (struct cam_dirent *entry);
265141cc406Sopenharmony_ci
266141cc406Sopenharmony_cistatic SANE_Int dir_delete (SANE_String name);
267141cc406Sopenharmony_ci
268141cc406Sopenharmony_cistatic SANE_Int send_data (SANE_Byte * buf);
269141cc406Sopenharmony_ci
270141cc406Sopenharmony_cistatic void set_res (SANE_Int lowres);
271