• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /***************************************************************************
2  * SANE - Scanner Access Now Easy.
3 
4    dc210.c
5 
6    11/11/98
7 
8    This file (C) 1998 Brian J. Murrell
9 
10    This file is part of the SANE package.
11 
12    This program is free software; you can redistribute it and/or
13    modify it under the terms of the GNU General Public License as
14    published by the Free Software Foundation; either version 2 of the
15    License, or (at your option) any later version.
16 
17    This program is distributed in the hope that it will be useful, but
18    WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20    General Public License for more details.
21 
22    You should have received a copy of the GNU General Public License
23    along with this program.  If not, see <https://www.gnu.org/licenses/>.
24 
25    As a special exception, the authors of SANE give permission for
26    additional uses of the libraries contained in this release of SANE.
27 
28    The exception is that, if you link a SANE library with other files
29    to produce an executable, this does not by itself cause the
30    resulting executable to be covered by the GNU General Public
31    License.  Your use of that executable is in no way restricted on
32    account of linking the SANE library code into it.
33 
34    This exception does not, however, invalidate any other reasons why
35    the executable file might be covered by the GNU General Public
36    License.
37 
38    If you submit changes to SANE to the maintainers to be included in
39    a subsequent release, you agree by submitting the changes that
40    those changes may be distributed with this exception intact.
41 
42    If you write modifications of your own for SANE, it is your choice
43    whether to permit this exception to apply to your modifications.
44    If you do not wish that, delete this exception notice.
45 
46  ***************************************************************************
47 
48    This file implements a SANE backend for the Kodak DC-210
49    digital camera.  THIS IS EXTREMELY ALPHA CODE!  USE AT YOUR OWN RISK!!
50 
51    (feedback to:  sane-dc210@interlinx.bc.ca
52 
53    This backend is based somewhat on the dc25 backend included in this
54    package by Peter Fales
55 
56  ***************************************************************************/
57 
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <unistd.h>
61 #include <fcntl.h>
62 #include <termios.h>
63 #include <string.h>
64 
65 #ifndef TRUE
66 #define TRUE	(1==1)
67 #endif
68 
69 #ifndef FALSE
70 #define FALSE	(!TRUE)
71 #endif
72 
73 #ifndef NULL
74 #define NULL	0L
75 #endif
76 
77 typedef struct picture_info
78   {
79     unsigned int low_res;
80     unsigned int size;
81   }
82 PictureInfo;
83 
84 typedef struct DC210_s
85   {
86     int fd;			/* file descriptor to talk to it */
87     char *tty_name;		/* the tty port name it's on */
88     speed_t baud;		/* current tty speed */
89     SANE_Bool scanning;		/* currently scanning an image? */
90     unsigned char model;
91     unsigned char ver_major;
92     unsigned char ver_minor;
93     int pic_taken;
94     int pic_left;
95     struct
96       {
97 	unsigned int low_res:1;
98 	unsigned int low_batt:1;
99       }
100     flags;
101     PictureInfo *Pictures;	/* array of pictures */
102     unsigned int current_picture_number;	/* picture being operated on */
103   }
104 DC210;
105 
106 typedef struct dc210_info_s
107   {
108     unsigned char model;
109     unsigned char ver_major;
110     unsigned char ver_minor;
111     int pic_taken;
112     int pic_left;
113     struct
114       {
115 	unsigned int low_res:1;
116 	unsigned int low_batt:1;
117       }
118     flags;
119   }
120 Dc210Info, *Dc210InfoPtr;
121 
122 static int get_info (DC210 *);
123 
124 #define INIT_PCK	{0x41, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1A}
125 /*                               ^^^^^^^^^^
126  *                               Baud rate: (see pkt_speed structure)
127  *                                 0x96 0x00 -> 9600 baud
128  *                                 0x19 0x20 -> 19200 baud
129  *                                 0x38 0x40 -> 38400 baud
130  *                                 0x57 0x60 -> 57600 baud
131  *                                 0x11 0x52 -> 115200 baud
132  */
133 #define INFO_PCK	{0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1A}
134 #define SHOOT_PCK	{0x7C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1A}
135 #define ERASE_PCK	{0x7B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1A}
136 #define RES_PCK		{0x36, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1A}
137 /*                               ^^^^
138  *                               Resolution: 0x00 = high, 0x01 = low
139  */
140 #define THUMBS_PCK	{0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1A}
141 /*                                     ^^^^
142  *                                     Thumbnail number
143  */
144 #define PICS_PCK	{0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1A}
145 /*                                     ^^^^
146  *                                     Picture number
147  */
148 #define PICS_INFO_PCK	{0x65, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1A}
149 /*                                     ^^^^
150  *                                     Picture number
151  */
152 
153 struct pkt_speed
154   {
155     speed_t baud;
156     unsigned char pkt_code[2];
157   };
158 
159 #if defined (B57600) && defined (B115200)
160 # define SPEEDS			{ {   B9600, { 0x96, 0x00 } }, \
161 				  {  B19200, { 0x19, 0x20 } }, \
162 				  {  B38400, { 0x38, 0x40 } }, \
163 				  {  B57600, { 0x57, 0x60 } }, \
164 				  { B115200, { 0x11, 0x52 } }  }
165 #else
166 # define SPEEDS			{ {   B9600, { 0x96, 0x00 } }, \
167 				  {  B19200, { 0x19, 0x20 } }, \
168 				  {  B38400, { 0x38, 0x40 } }  }
169 #endif
170 
171 #define HIGH_RES		0
172 #define LOW_RES			1
173 
174 /*
175  *    Image parameters
176  */
177 
178 #define LOW_CAMERA_HEADER	256
179 #define HIGH_CAMERA_HEADER	512
180 #define CAMERA_HEADER(r)	( (r) ? LOW_CAMERA_HEADER : HIGH_CAMERA_HEADER )
181 
182 #define LOW_WIDTH			256
183 #define HIGH_WIDTH			512
184 #define WIDTH(r)			( (r) ? LOW_WIDTH : HIGH_WIDTH )
185 
186 #define HEIGHT				243
187 
188 #define LEFT_MARGIN			1
189 
190 #define LOW_RIGHT_MARGIN	5
191 #define HIGH_RIGHT_MARGIN	10
192 #define RIGHT_MARGIN(r)		( (r) ? LOW_RIGHT_MARGIN : HIGH_RIGHT_MARGIN )
193 
194 #define TOP_MARGIN			1
195 
196 #define BOTTOM_MARGIN		1
197 
198 #define BLOCK_SIZE			1024
199 
200 #define LOW_BLOCKS			61
201 #define HIGH_BLOCKS			122
202 #define BLOCKS(r)			( (r) ? LOW_BLOCKS : HIGH_BLOCKS )
203 
204 #define	LOW_IMAGE_SIZE		( LOW_BLOCKS * BLOCK_SIZE )
205 #define HIGH_IMAGE_SIZE		( HIGH_BLOCKS * BLOCK_SIZE )
206 #define IMAGE_SIZE(r)		( (r) ? LOW_IMAGE_SIZE : HIGH_IMAGE_SIZE )
207 #define MAX_IMAGE_SIZE		( HIGH_IMAGE_SIZE )
208 
209 /*
210  *    Comet file
211  */
212 
213 #define COMET_MAGIC			"COMET"
214 #define COMET_HEADER_SIZE	128
215 #define COMET_EXT			"cmt"
216 
217 /*
218  *    Pixmap structure
219  */
220 
221 struct pixmap
222   {
223     int width;
224     int height;
225     int components;
226     unsigned char *planes;
227   };
228 
229 /*
230  *    Rotations
231  */
232 
233 #define ROT_STRAIGHT	0x00
234 #define ROT_LEFT		0x01
235 #define ROT_RIGHT		0x02
236 #define ROT_HEADDOWN	0x03
237 
238 #define ROT_MASK		0x03
239 
240 /*
241  *    File formats
242  */
243 
244 #define SAVE_RAW		0x01
245 #define SAVE_GREYSCALE		0x02
246 #define SAVE_24BITS		0x04
247 #define SAVE_FILES		0x07
248 #define SAVE_FORMATS		0x38
249 #define SAVE_ADJASPECT		0x80
250 
251 /*
252  *    External definitions
253  */
254 
255 extern char *__progname;	/* Defined in /usr/lib/crt0.o */
256 
257 
258 
259 #include <sys/types.h>
260 
261 FILE *sanei_config_open (const char *filename);
262 
263 static int init_dc210 (DC210 *);
264 
265 static void close_dc210 (int);
266 
267 static int read_data (int fd, unsigned char *buf, int sz);
268 
269 static int end_of_data (int fd);
270 
271 static PictureInfo *get_pictures_info (void);
272 
273 static int get_picture_info (PictureInfo * pic, int p);
274 
275 static SANE_Status snap_pic (int);
276 
277 char *sanei_config_read (char *str, int n, FILE * stream);
278