1 /***************************************************************************
2  * SANE - Scanner Access Now Easy.
3 
4    dc240.h
5 
6    03/12/01 - Peter Fales
7 
8    Based on the dc210 driver, (C) 1998 Brian J. Murrell (which is
9 	based on dc25 driver (C) 1998 by Peter Fales)
10 
11    This file (C) 2001 by Peter Fales
12 
13    This file is part of the SANE package.
14 
15    This program is free software; you can redistribute it and/or
16    modify it under the terms of the GNU General Public License as
17    published by the Free Software Foundation; either version 2 of the
18    License, or (at your option) any later version.
19 
20    This program is distributed in the hope that it will be useful, but
21    WITHOUT ANY WARRANTY; without even the implied warranty of
22    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23    General Public License for more details.
24 
25    You should have received a copy of the GNU General Public License
26    along with this program.  If not, see <https://www.gnu.org/licenses/>.
27 
28    As a special exception, the authors of SANE give permission for
29    additional uses of the libraries contained in this release of SANE.
30 
31    The exception is that, if you link a SANE library with other files
32    to produce an executable, this does not by itself cause the
33    resulting executable to be covered by the GNU General Public
34    License.  Your use of that executable is in no way restricted on
35    account of linking the SANE library code into it.
36 
37    This exception does not, however, invalidate any other reasons why
38    the executable file might be covered by the GNU General Public
39    License.
40 
41    If you submit changes to SANE to the maintainers to be included in
42    a subsequent release, you agree by submitting the changes that
43    those changes may be distributed with this exception intact.
44 
45    If you write modifications of your own for SANE, it is your choice
46    whether to permit this exception to apply to your modifications.
47    If you do not wish that, delete this exception notice.
48 
49  ***************************************************************************
50 
51    This file implements a SANE backend for the Kodak DC-240
52    digital camera.  THIS IS EXTREMELY ALPHA CODE!  USE AT YOUR OWN RISK!!
53 
54    (feedback to:  dc240-devel@fales-lorenz.net)
55 
56    This backend is based somewhat on the dc25 backend included in this
57    package by Peter Fales, and the dc210 backend by Brian J. Murrell
58 
59  ***************************************************************************/
60 
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <unistd.h>
64 #include <fcntl.h>
65 #include <termios.h>
66 #include <string.h>
67 
68 #ifndef TRUE
69 #define TRUE	(1==1)
70 #endif
71 
72 #ifndef FALSE
73 #define FALSE	(!TRUE)
74 #endif
75 
76 #ifndef NULL
77 #define NULL	0L
78 #endif
79 
80 typedef struct picture_info
81 {
82   int low_res;
83   int size;
84 }
85 PictureInfo;
86 
87 typedef struct DC240_s
88 {
89   SANE_Int fd;			/* file descriptor to talk to it */
90   char *tty_name;		/* the tty port name it's on */
91   speed_t baud;			/* current tty speed */
92   SANE_Bool scanning;		/* currently scanning an image? */
93   SANE_Byte model;
94   SANE_Byte ver_major;
95   SANE_Byte ver_minor;
96   SANE_Int pic_taken;
97   SANE_Int pic_left;
98   struct
99   {
100     unsigned int low_res:1;
101     unsigned int low_batt:1;
102   }
103   flags;
104   PictureInfo *Pictures;	/* array of pictures */
105   SANE_Int current_picture_number;	/* picture being operated on */
106 }
107 DC240;
108 
109 typedef struct dc240_info_s
110 {
111   SANE_Byte model;
112   SANE_Byte ver_major;
113   SANE_Byte ver_minor;
114   SANE_Int pic_taken;
115   SANE_Int pic_left;
116   struct
117   {
118     SANE_Int low_res:1;
119     SANE_Int low_batt:1;
120   }
121   flags;
122 }
123 Dc240Info, *Dc240InfoPtr;
124 
125 static SANE_Int get_info (DC240 *);
126 
127 #define INIT_PCK	{0x41, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1A}
128 /*                               ^^^^^^^^^^
129  *                               Baud rate: (see pkt_speed structure)
130  *                                 0x96 0x00 -> 9600 baud
131  *                                 0x19 0x20 -> 19200 baud
132  *                                 0x38 0x40 -> 38400 baud
133  *                                 0x57 0x60 -> 57600 baud
134  *                                 0x11 0x52 -> 115200 baud
135  */
136 #define INFO_PCK	{0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1A}
137 #define SHOOT_PCK	{0x7C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1A}
138 #define ERASE_PCK	{0x9D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1A}
139 #define RES_PCK		{0x36, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1A}
140 /*                                   ^^^^
141  *                                   Resolution: 0x00 = low, 0x01 = high
142  */
143 #define THUMBS_PCK	{0x93, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x1A}
144 /*
145  *
146  */
147 #define PICS_PCK	{0x9A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1A}
148 /*
149  *
150  */
151 #define PICS_INFO_PCK	{0x91, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1A}
152 /*
153  *
154  */
155 #define OPEN_CARD_PCK	{0x96, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1A}
156 #define READ_DIR_PCK	{0x99, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1A}
157 /*                                   ^^^^
158  *				     1=Report number of entries only
159  */
160 
161 struct pkt_speed
162 {
163   speed_t baud;
164   SANE_Byte pkt_code[2];
165 };
166 
167 #if defined (B57600) && defined (B115200)
168 # define SPEEDS			{ {   B9600, { 0x96, 0x00 } }, \
169 				  {  B19200, { 0x19, 0x20 } }, \
170 				  {  B38400, { 0x38, 0x40 } }, \
171 				  {  B57600, { 0x57, 0x60 } }, \
172 				  { B115200, { 0x11, 0x52 } }  }
173 #else
174 # define SPEEDS			{ {   B9600, { 0x96, 0x00 } }, \
175 				  {  B19200, { 0x19, 0x20 } }, \
176 				  {  B38400, { 0x38, 0x40 } }  }
177 #endif
178 
179 #define HIGH_RES		0
180 #define LOW_RES			1
181 
182 #define HIGHRES_WIDTH		1280
183 #define HIGHRES_HEIGHT		960
184 
185 #define LOWRES_WIDTH		640
186 #define LOWRES_HEIGHT		480
187 
188 /*
189  *    External definitions
190  */
191 
192 extern char *__progname;	/* Defined in /usr/lib/crt0.o */
193 
194 
195 struct cam_dirent
196 {
197   SANE_Char name[11];
198   SANE_Byte attr;
199   SANE_Byte create_time[2];
200   SANE_Byte creat_date[2];
201   long size;
202 };
203 
204 #ifdef OLD
205 
206 /* This is the layout of the directory in the camera - Unfortunately,
207  * this only works in gcc.
208  */
209 struct dir_buf
210 {
211   SANE_Byte entries_msb PACKED;
212   SANE_Byte entries_lsb PACKED;
213   struct cam_dirent entry[1000] PACKED;
214 };
215 #else
216 
217 /* So, we have to do it the hard way...  */
218 
219 #define CAMDIRENTRYSIZE 20
220 #define DIRENTRIES 1000
221 
222 
223 #define get_name(entry) (SANE_Char*) &dir_buf2[2+CAMDIRENTRYSIZE*(entry)]
224 #define get_attr(entry) dir_buf2[2+11+CAMDIRENTRYSIZE*(entry)]
225 #define get_create_time(entry) \
226    (  dir_buf2[2+12+CAMDIRENTRYSIZE*(entry)] << 8 \
227     + dir_buf2[2+13+CAMDIRENTRYSIZE*(entry)])
228 
229 
230 #endif
231 
232 struct cam_dirlist
233 {
234   SANE_Char name[48];
235   struct cam_dirlist *next;
236 };
237 
238 
239 
240 #include <sys/types.h>
241 
242 FILE *sanei_config_open (const char *filename);
243 
244 static SANE_Int init_dc240 (DC240 *);
245 
246 static void close_dc240 (SANE_Int);
247 
248 static SANE_Int read_data (SANE_Int fd, SANE_Byte * buf, SANE_Int sz);
249 
250 static SANE_Int end_of_data (SANE_Int fd);
251 
252 static PictureInfo *get_pictures_info (void);
253 
254 static SANE_Int get_picture_info (PictureInfo * pic, SANE_Int p);
255 
256 static SANE_Status snap_pic (SANE_Int fd);
257 
258 char *sanei_config_read (char *str, int n, FILE * stream);
259 
260 static SANE_Int read_dir (SANE_String dir);
261 
262 static SANE_Int read_info (SANE_String fname);
263 
264 static SANE_Int dir_insert (struct cam_dirent *entry);
265 
266 static SANE_Int dir_delete (SANE_String name);
267 
268 static SANE_Int send_data (SANE_Byte * buf);
269 
270 static void set_res (SANE_Int lowres);
271