1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
|
/***************************************************************************
* SANE - Scanner Access Now Easy.
dc210.c
11/11/98
This file (C) 1998 Brian J. Murrell
This file is part of the SANE package.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
As a special exception, the authors of SANE give permission for
additional uses of the libraries contained in this release of SANE.
The exception is that, if you link a SANE library with other files
to produce an executable, this does not by itself cause the
resulting executable to be covered by the GNU General Public
License. Your use of that executable is in no way restricted on
account of linking the SANE library code into it.
This exception does not, however, invalidate any other reasons why
the executable file might be covered by the GNU General Public
License.
If you submit changes to SANE to the maintainers to be included in
a subsequent release, you agree by submitting the changes that
those changes may be distributed with this exception intact.
If you write modifications of your own for SANE, it is your choice
whether to permit this exception to apply to your modifications.
If you do not wish that, delete this exception notice.
***************************************************************************
This file implements a SANE backend for the Kodak DC-210
digital camera. THIS IS EXTREMELY ALPHA CODE! USE AT YOUR OWN RISK!!
(feedback to: sane-dc210@interlinx.bc.ca
This backend is based somewhat on the dc25 backend included in this
package by Peter Fales
***************************************************************************/
#include "../include/sane/config.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <limits.h>
#include "../include/sane/sanei_jpeg.h"
#include <sys/ioctl.h>
#include "../include/sane/sane.h"
#include "../include/sane/sanei.h"
#include "../include/sane/saneopts.h"
#define BACKEND_NAME dc210
#include "../include/sane/sanei_backend.h"
#include "dc210.h"
#ifndef PATH_MAX
# define PATH_MAX 1024
#endif
#define MAGIC (void *)0xab730324
#define DC210_CONFIG_FILE "dc210.conf"
#define THUMBSIZE 20736
#ifdef B115200
# define DEFAULT_BAUD_RATE B115200
#else
# define DEFAULT_BAUD_RATE B38400
#endif
#if defined (__sgi)
# define DEFAULT_TTY "/dev/ttyd1" /* Irix */
#elif defined (__sun)
# define DEFAULT_TTY "/dev/term/a" /* Solaris */
#elif defined (hpux)
# define DEFAULT_TTY "/dev/tty1d0" /* HP-UX */
#elif defined (__osf__)
# define DEFAULT_TTY "/dev/tty00" /* Digital UNIX */
#else
# define DEFAULT_TTY "/dev/ttyS0" /* Linux */
#endif
static SANE_Bool is_open = 0;
static SANE_Bool dc210_opt_thumbnails;
static SANE_Bool dc210_opt_snap;
static SANE_Bool dc210_opt_lowres;
static SANE_Bool dc210_opt_erase;
static SANE_Bool dumpinquiry;
static struct jpeg_decompress_struct cinfo;
static djpeg_dest_ptr dest_mgr = NULL;
static unsigned long cmdrespause = 250000UL; /* pause after sending cmd */
static unsigned long breakpause = 1000000UL; /* pause after sending break */
static int bytes_in_buffer;
static int bytes_read_from_buffer;
static int total_bytes_read;
static DC210 Camera;
static SANE_Range image_range = {
0,
14,
0
};
static SANE_Option_Descriptor sod[] = {
{
SANE_NAME_NUM_OPTIONS,
SANE_TITLE_NUM_OPTIONS,
SANE_DESC_NUM_OPTIONS,
SANE_TYPE_INT,
SANE_UNIT_NONE,
sizeof (SANE_Word),
SANE_CAP_SOFT_DETECT,
SANE_CONSTRAINT_NONE,
{NULL}
}
,
#define D25_OPT_IMAGE_SELECTION 1
{
"",
"Image Selection",
"Selection of the image to load.",
SANE_TYPE_GROUP,
SANE_UNIT_NONE,
0,
0,
SANE_CONSTRAINT_NONE,
{NULL}
}
,
#define DC210_OPT_IMAGE_NUMBER 2
{
"image",
"Image Number",
"Select Image Number to load from camera",
SANE_TYPE_INT,
SANE_UNIT_NONE,
4,
SANE_CAP_SOFT_SELECT | SANE_CAP_SOFT_DETECT,
SANE_CONSTRAINT_RANGE,
{(void *) & image_range}
}
,
#define DC210_OPT_THUMBS 3
{
"thumbs",
"Load Thumbnail",
"Load the image as thumbnail.",
SANE_TYPE_BOOL,
SANE_UNIT_NONE,
sizeof (SANE_Word),
SANE_CAP_SOFT_SELECT | SANE_CAP_SOFT_DETECT,
SANE_CONSTRAINT_NONE,
{NULL}
}
,
#define DC210_OPT_SNAP 4
{
"snap",
"Snap new picture",
"Take new picture and download it",
SANE_TYPE_BOOL,
SANE_UNIT_NONE,
sizeof (SANE_Word),
SANE_CAP_SOFT_SELECT | SANE_CAP_SOFT_DETECT /* | SANE_CAP_ADVANCED */ ,
SANE_CONSTRAINT_NONE,
{NULL}
}
,
#define DC210_OPT_LOWRES 5
{
"lowres",
"Low Resolution",
"Resolution of new pictures",
SANE_TYPE_BOOL,
SANE_UNIT_NONE,
sizeof (SANE_Word),
SANE_CAP_SOFT_SELECT | SANE_CAP_SOFT_DETECT | SANE_CAP_INACTIVE
/* | SANE_CAP_ADVANCED */ ,
SANE_CONSTRAINT_NONE,
{NULL}
}
,
#define DC210_OPT_ERASE 6
{
"erase",
"Erase",
"Erase the picture after downloading",
SANE_TYPE_BOOL,
SANE_UNIT_NONE,
sizeof (SANE_Word),
SANE_CAP_SOFT_SELECT | SANE_CAP_SOFT_DETECT,
SANE_CONSTRAINT_NONE,
{NULL}
}
,
#define DC210_OPT_DEFAULT 7
{
"default-enhancements",
"Defaults",
"Set default values for enhancement controls.",
SANE_TYPE_BUTTON,
SANE_UNIT_NONE,
0,
SANE_CAP_SOFT_SELECT | SANE_CAP_SOFT_DETECT,
SANE_CONSTRAINT_NONE,
{NULL}
}
,
#define DC210_OPT_INIT_DC210 8
{
"camera-init",
"Re-establish Communications",
"Re-establish communications with camera (in case of timeout, etc.)",
SANE_TYPE_BUTTON,
SANE_UNIT_NONE,
0,
SANE_CAP_SOFT_SELECT | SANE_CAP_SOFT_DETECT,
SANE_CONSTRAINT_NONE,
{NULL}
}
};
static SANE_Parameters parms = {
SANE_FRAME_RGB,
0,
0, /* Number of bytes returned per scan line: */
0, /* Number of pixels per scan line. */
0, /* Number of lines for the current scan. */
8, /* Number of bits per sample. */
};
static unsigned char shoot_pck[] = SHOOT_PCK;
static unsigned char init_pck[] = INIT_PCK;
static unsigned char thumb_pck[] = THUMBS_PCK;
static unsigned char pic_pck[] = PICS_PCK;
static unsigned char pic_info_pck[] = PICS_INFO_PCK;
static unsigned char info_pck[] = INFO_PCK;
static unsigned char erase_pck[] = ERASE_PCK;
static unsigned char res_pck[] = RES_PCK;
static struct pkt_speed speeds[] = SPEEDS;
static struct termios tty_orig;
#include <sys/time.h>
#include <unistd.h>
static int
send_pck (int fd, unsigned char *pck)
{
int n;
unsigned char r = 0xf0; /* prime the loop with a "camera busy" */
/* keep trying if camera says it's busy */
while (r == 0xf0)
{
/*
* Not quite sure why we need this, but the program works a whole
* lot better (at least on the DC210) with this short delay.
*/
if (write (fd, (char *) pck, 8) != 8)
{
DBG (2, "send_pck: error: write returned -1\n");
return -1;
}
/* need to wait before we read command result */
usleep (cmdrespause);
if ((n = read (fd, (char *) &r, 1)) != 1)
{
DBG (2, "send_pck: error: read returned -1\n");
return -1;
}
}
return (r == 0xd1) ? 0 : -1;
}
static int
init_dc210 (DC210 * camera)
{
struct termios tty_new;
int speed_index;
for (speed_index = 0; speed_index < NELEMS (speeds); speed_index++)
{
if (speeds[speed_index].baud == camera->baud)
{
init_pck[2] = speeds[speed_index].pkt_code[0];
init_pck[3] = speeds[speed_index].pkt_code[1];
break;
}
}
if (init_pck[2] == 0)
{
DBG (2, "unsupported baud rate.\n");
return -1;
}
/*
Open device file.
*/
if ((camera->fd = open (camera->tty_name, O_RDWR)) == -1)
{
DBG (2, "init_dc210: error: could not open %s for read/write\n",
camera->tty_name);
return -1;
}
/*
Save old device information to restore when we are done.
*/
if (tcgetattr (camera->fd, &tty_orig) == -1)
{
DBG (2, "init_dc210: error: could not get attributes\n");
return -1;
}
memcpy ((char *) &tty_new, (char *) &tty_orig, sizeof (struct termios));
/*
We need the device to be raw. 8 bits even parity on 9600 baud to start.
*/
#ifdef HAVE_CFMAKERAW
cfmakeraw (&tty_new);
#else
/* Modified to set the port REALLY as required. Code inspired by
the gPhoto2 serial port setup */
/* input control settings */
tty_new.c_iflag &= ~(IGNBRK | IGNCR | INLCR | ICRNL | IUCLC |
IXANY | IXON | IXOFF | INPCK | ISTRIP);
tty_new.c_iflag |= (BRKINT | IGNPAR);
/* output control settings */
tty_new.c_oflag &= ~OPOST;
/* hardware control settings */
tty_new.c_cflag = (tty_new.c_cflag & ~CSIZE) | CS8;
tty_new.c_cflag &= ~(PARENB | PARODD | CSTOPB);
# if defined(__sgi)
tty_new.c_cflag &= ~CNEW_RTSCTS;
# else
/* OS/2 doesn't have CRTSCTS - will this work for them? */
# ifdef CRTSCTS
tty_new.c_cflag &= ~CRTSCTS;
# endif
# endif
tty_new.c_cflag |= CLOCAL | CREAD;
#endif
/* line discipline settings */
tty_new.c_lflag &= ~(ICANON | ISIG | ECHO | ECHONL | ECHOE |
ECHOK | IEXTEN);
tty_new.c_cc[VMIN] = 0;
tty_new.c_cc[VTIME] = 5;
cfsetospeed (&tty_new, B9600);
cfsetispeed (&tty_new, B9600);
if (tcsetattr (camera->fd, TCSANOW, &tty_new) == -1)
{
DBG (2, "init_dc210: error: could not set attributes\n");
return -1;
}
/* send a break to get it back to a known state */
/* Used to supply a non-zero argument to tcsendbreak(), TCSBRK,
* and TCSBRKP, but that is system dependent. e.g. on irix a non-zero
* value does a drain instead of a break. A zero value is universally
* used to send a break.
*/
#ifdef HAVE_TCSENDBREAK
tcsendbreak (camera->fd, 0);
# if defined(__sgi)
tcdrain (camera->fd);
# endif
# elif defined(TCSBRKP)
ioctl (camera->fd, TCSBRKP, 0);
# elif defined(TCSBRK)
ioctl (camera->fd, TCSBRK, 0);
#endif
/* and wait for it to recover from the break */
#ifdef HAVE_USLEEP
usleep (breakpause);
#else
sleep (1);
#endif
if (send_pck (camera->fd, init_pck) == -1)
{
/*
* The camera always powers up at 9600, so we try
* that first. However, it may be already set to
* a different speed. Try the entries in the table:
*/
for (speed_index = NELEMS (speeds) - 1; speed_index > 0; speed_index--)
{
int x;
DBG (3, "init_dc210: changing speed to %d\n",
(int) speeds[speed_index].baud);
cfsetospeed (&tty_new, speeds[speed_index].baud);
cfsetispeed (&tty_new, speeds[speed_index].baud);
if (tcsetattr (camera->fd, TCSANOW, &tty_new) == -1)
{
DBG (2, "init_dc210: error: could not set attributes\n");
return -1;
}
for (x = 0; x < 3; x++)
if (send_pck (camera->fd, init_pck) != -1)
break;
}
if (speed_index == 0)
{
tcsetattr (camera->fd, TCSANOW, &tty_orig);
DBG (2, "init_dc210: error: no suitable baud rate\n");
return -1;
}
}
/*
Set speed to requested speed.
*/
cfsetospeed (&tty_new, Camera.baud);
cfsetispeed (&tty_new, Camera.baud);
if (tcsetattr (camera->fd, TCSANOW, &tty_new) == -1)
{
DBG (2, "init_dc210: error: could not set attributes\n");
return -1;
}
return camera->fd;
}
static void
close_dc210 (int fd)
{
/*
* Put the camera back to 9600 baud
*/
if (close (fd) == -1)
{
DBG (4, "close_dc210: error: could not close device\n");
}
}
int
get_info (DC210 * camera)
{
char f[] = "get_info";
unsigned char buf[256];
if (send_pck (camera->fd, info_pck) == -1)
{
DBG (2, "%s: error: send_pck returned -1\n", f);
return -1;
}
DBG (9, "%s: read info packet\n", f);
if (read_data (camera->fd, buf, 256) == -1)
{
DBG (2, "%s: error: read_data returned -1\n", f);
return -1;
}
if (end_of_data (camera->fd) == -1)
{
DBG (2, "%s: error: end_of_data returned -1\n", f);
return -1;
}
camera->model = buf[1];
camera->ver_major = buf[2];
camera->ver_minor = buf[3];
camera->pic_taken = buf[56] << 8 | buf[57];
camera->pic_left = buf[72] << 8 | buf[73];
camera->flags.low_res = buf[22];
camera->flags.low_batt = buf[8];
return 0;
}
static int
read_data (int fd, unsigned char *buf, int sz)
{
unsigned char ccsum;
unsigned char rcsum;
unsigned char c;
int n;
int r = 0;
int i;
/* read the control byte */
if (read (fd, &c, 1) != 1)
{
DBG (2,
"read_data: error: read for packet control byte returned bad status\n");
return -1;
}
if (c != 1)
{
DBG (2, "read_data: error: incorrect packet control byte: %02x\n", c);
return -1;
}
for (n = 0; n < sz && (r = read (fd, (char *) &buf[n], sz - n)) > 0; n += r)
;
if (r <= 0)
{
DBG (2, "read_data: error: read returned -1\n");
return -1;
}
if (n < sz || read (fd, &rcsum, 1) != 1)
{
DBG (2, "read_data: error: buffer underrun or no checksum\n");
return -1;
}
for (i = 0, ccsum = 0; i < n; i++)
ccsum ^= buf[i];
if (ccsum != rcsum)
{
DBG (2, "read_data: error: bad checksum (%02x !=%02x)\n", rcsum, ccsum);
return -1;
}
c = 0xd2;
if (write (fd, (char *) &c, 1) != 1)
{
DBG (2, "read_data: error: write ack\n");
return -1;
}
return 0;
}
static int
end_of_data (int fd)
{
unsigned char c;
do
{ /* loop until the camera isn't busy */
if (read (fd, &c, 1) != 1)
{
DBG (2, "end_of_data: error: read returned -1\n");
return -1;
}
if (c == 0) /* got successful end of data */
return 0; /* return success */
sleep (1); /* not too fast */
}
while (c == 0xf0);
/* Accck! Not busy, but not a good end of data either */
if (c != 0)
{
DBG (2, "end_of_data: error: bad EOD from camera (%02x)\n",
(unsigned) c);
return -1;
}
return 0; /* should never get here but shut gcc -Wall up */
}
static int
erase (int fd)
{
if (send_pck (fd, erase_pck) == -1)
{
DBG (3, "erase: error: send_pck returned -1\n");
return -1;
}
if (end_of_data (fd) == -1)
{
DBG (3, "erase: error: end_of_data returned -1\n");
return -1;
}
return 0;
}
static int
change_res (int fd, unsigned char res)
{
char f[] = "change_res";
DBG (127, "%s called\n", f);
if (res != 0 && res != 1)
{
DBG (3, "%s: error: unsupported resolution\n", f);
return -1;
}
/* cameras resolution semantics are opposite of ours */
res = !res;
DBG (127, "%s: setting res to %d\n", f, res);
res_pck[2] = res;
if (send_pck (fd, res_pck) == -1)
{
DBG (4, "%s: error: send_pck returned -1\n", f);
}
if (end_of_data (fd) == -1)
{
DBG (4, "%s: error: end_of_data returned -1\n", f);
}
return 0;
}
SANE_Status
sane_init (SANE_Int * version_code, SANE_Auth_Callback __sane_unused__ authorize)
{
char f[] = "sane_init";
char dev_name[PATH_MAX], *p;
size_t len;
FILE *fp;
int baud;
DBG_INIT ();
if (version_code)
*version_code = SANE_VERSION_CODE (SANE_CURRENT_MAJOR, V_MINOR, 0);
fp = sanei_config_open (DC210_CONFIG_FILE);
/* defaults */
Camera.baud = DEFAULT_BAUD_RATE;
Camera.tty_name = DEFAULT_TTY;
if (!fp)
{
/* default to /dev/whatever instead of insisting on config file */
DBG (1, "%s: missing config file '%s'\n", f, DC210_CONFIG_FILE);
}
else
{
while (sanei_config_read (dev_name, sizeof (dev_name), fp))
{
dev_name[sizeof (dev_name) - 1] = '\0';
DBG (20, "%s: config- %s\n", f, dev_name);
if (dev_name[0] == '#')
continue; /* ignore line comments */
len = strlen (dev_name);
if (!len)
continue; /* ignore empty lines */
if (strncmp (dev_name, "port=", 5) == 0)
{
p = strchr (dev_name, '/');
if (p)
Camera.tty_name = strdup (p);
DBG (20, "Config file port=%s\n", Camera.tty_name);
}
else if (strncmp (dev_name, "baud=", 5) == 0)
{
baud = atoi (&dev_name[5]);
switch (baud)
{
case 9600:
Camera.baud = B9600;
break;
case 19200:
Camera.baud = B19200;
break;
case 38400:
Camera.baud = B38400;
break;
#ifdef B57600
case 57600:
Camera.baud = B57600;
break;
#endif
#ifdef B115200
case 115200:
Camera.baud = B115200;
break;
#endif
}
DBG (20, "Config file baud=%d\n", Camera.baud);
}
else if (strcmp (dev_name, "dumpinquiry") == 0)
{
dumpinquiry = SANE_TRUE;
}
else if (strncmp (dev_name, "cmdrespause=", 12) == 0)
{
cmdrespause = atoi (&dev_name[12]);
DBG (20, "Config file cmdrespause=%lu\n", cmdrespause);
}
else if (strncmp (dev_name, "breakpause=", 11) == 0)
{
breakpause = atoi (&dev_name[11]);
DBG (20, "Config file breakpause=%lu\n", breakpause);
}
}
fclose (fp);
}
if (init_dc210 (&Camera) == -1)
return SANE_STATUS_INVAL;
if (get_info (&Camera) == -1)
{
DBG (2, "error: could not get info\n");
close_dc210 (Camera.fd);
return SANE_STATUS_INVAL;
}
if (Camera.pic_taken == 0)
{
sod[DC210_OPT_IMAGE_NUMBER].cap |= SANE_CAP_INACTIVE;
image_range.min = 0;
image_range.max = 0;
}
else
{
sod[DC210_OPT_IMAGE_NUMBER].cap &= ~SANE_CAP_INACTIVE;
image_range.min = 1;
image_range.max = Camera.pic_taken;
}
/* load the current images array */
Camera.Pictures = get_pictures_info ();
if (Camera.pic_taken == 0)
{
Camera.current_picture_number = 0;
parms.bytes_per_line = 0;
parms.pixels_per_line = 0;
parms.lines = 0;
}
else
{
Camera.current_picture_number = 1;
if (Camera.Pictures[Camera.current_picture_number - 1].low_res)
{
parms.bytes_per_line = 640 * 3;
parms.pixels_per_line = 640;
parms.lines = 480;
}
else
{
parms.bytes_per_line = 1152 * 3;
parms.pixels_per_line = 1152;
parms.lines = 864;
}
}
if (dumpinquiry)
{
DBG (0, "\nCamera information:\n~~~~~~~~~~~~~~~~~\n\n");
DBG (0, "Model...........: DC%x\n", Camera.model);
DBG (0, "Firmware version: %d.%d\n", Camera.ver_major,
Camera.ver_minor);
DBG (0, "Pictures........: %d/%d\n", Camera.pic_taken,
Camera.pic_taken + Camera.pic_left);
DBG (0, "Resolution......: %s\n",
Camera.flags.low_res ? "low" : "high");
DBG (0, "Battery state...: %s\n",
Camera.flags.low_batt ? "low" : "good");
}
return SANE_STATUS_GOOD;
}
void
sane_exit (void)
{
}
/* Device select/open/close */
static const SANE_Device dev[] = {
{
"0",
"Kodak",
"DC-210",
"still camera"},
};
SANE_Status
sane_get_devices (const SANE_Device *** device_list,
SANE_Bool __sane_unused__ local_only)
{
static const SANE_Device *devlist[] = {
dev + 0, 0
};
DBG (127, "sane_get_devices called\n");
*device_list = devlist;
return SANE_STATUS_GOOD;
}
SANE_Status
sane_open (SANE_String_Const devicename, SANE_Handle * handle)
{
int i;
DBG (127, "sane_open for device %s\n", devicename);
if (!devicename[0])
{
i = 0;
}
else
{
for (i = 0; i < NELEMS (dev); ++i)
{
if (strcmp (devicename, dev[i].name) == 0)
{
break;
}
}
}
if (i >= NELEMS (dev))
{
return SANE_STATUS_INVAL;
}
if (is_open)
{
return SANE_STATUS_DEVICE_BUSY;
}
is_open = 1;
*handle = MAGIC;
DBG (3, "sane_open: pictures taken=%d\n", Camera.pic_taken);
return SANE_STATUS_GOOD;
}
void
sane_close (SANE_Handle handle)
{
DBG (127, "sane_close called\n");
if (handle == MAGIC)
is_open = 0;
DBG (127, "sane_close returning\n");
}
const SANE_Option_Descriptor *
sane_get_option_descriptor (SANE_Handle handle, SANE_Int option)
{
if (handle != MAGIC || !is_open)
return NULL; /* wrong device */
if (option < 0 || option >= NELEMS (sod))
return NULL;
return &sod[option];
}
SANE_Status
sane_control_option (SANE_Handle handle, SANE_Int option,
SANE_Action action, void *value, SANE_Int * info)
{
SANE_Int myinfo = 0;
SANE_Status status;
DBG (127, "control_option(handle=%p,opt=%s,act=%s,val=%p,info=%p)\n",
handle, sod[option].title,
(action ==
SANE_ACTION_SET_VALUE ? "SET" : (action ==
SANE_ACTION_GET_VALUE ? "GET" :
"SETAUTO")), value, (void *)info);
if (handle != MAGIC || !is_open)
return SANE_STATUS_INVAL; /* Unknown handle ... */
if (option < 0 || option >= NELEMS (sod))
return SANE_STATUS_INVAL; /* Unknown option ... */
switch (action)
{
case SANE_ACTION_SET_VALUE:
status = sanei_constrain_value (sod + option, value, &myinfo);
if (status != SANE_STATUS_GOOD)
{
DBG (1, "Constraint error in control_option\n");
return status;
}
switch (option)
{
case DC210_OPT_IMAGE_NUMBER:
Camera.current_picture_number = *(SANE_Word *) value;
myinfo |= SANE_INFO_RELOAD_PARAMS;
/* get the image's resolution */
if (Camera.Pictures[Camera.current_picture_number - 1].low_res)
{
parms.bytes_per_line = 640 * 3;
parms.pixels_per_line = 640;
parms.lines = 480;
}
else
{
parms.bytes_per_line = 1152 * 3;
parms.pixels_per_line = 1152;
parms.lines = 864;
}
break;
case DC210_OPT_THUMBS:
dc210_opt_thumbnails = !!*(SANE_Word *) value;
myinfo |= SANE_INFO_RELOAD_PARAMS;
if (dc210_opt_thumbnails)
{
/*
* DC210 thumbnail are 96x72x8x3
*/
parms.bytes_per_line = 96 * 3;
parms.pixels_per_line = 96;
parms.lines = 72;
}
else
{
if (Camera.Pictures[Camera.current_picture_number - 1].low_res)
{
parms.bytes_per_line = 640 * 3;
parms.pixels_per_line = 640;
parms.lines = 480;
}
else
{
parms.bytes_per_line = 1152 * 3;
parms.pixels_per_line = 1152;
parms.lines = 864;
}
}
break;
case DC210_OPT_SNAP:
dc210_opt_snap = !!*(SANE_Word *) value;
myinfo |= SANE_INFO_RELOAD_PARAMS | SANE_INFO_RELOAD_OPTIONS;
/* if we are snapping a new one */
if (dc210_opt_snap)
{
/* activate the resolution setting */
sod[DC210_OPT_LOWRES].cap &= ~SANE_CAP_INACTIVE;
/* and de-activate the image number selector */
sod[DC210_OPT_IMAGE_NUMBER].cap |= SANE_CAP_INACTIVE;
}
else
{
/* deactivate the resolution setting */
sod[DC210_OPT_LOWRES].cap |= SANE_CAP_INACTIVE;
/* and activate the image number selector */
sod[DC210_OPT_IMAGE_NUMBER].cap &= ~SANE_CAP_INACTIVE;
}
/* set params according to resolution settings */
if (dc210_opt_lowres)
{
parms.bytes_per_line = 640 * 3;
parms.pixels_per_line = 640;
parms.lines = 480;
}
else
{
parms.bytes_per_line = 1152 * 3;
parms.pixels_per_line = 1152;
parms.lines = 864;
}
break;
case DC210_OPT_LOWRES:
dc210_opt_lowres = !!*(SANE_Word *) value;
myinfo |= SANE_INFO_RELOAD_PARAMS;
if (!dc210_opt_thumbnails)
{
/* XXX - change the number of pictures left depending on resolution
perhaps just call get_info again?
*/
if (dc210_opt_lowres)
{
parms.bytes_per_line = 640 * 3;
parms.pixels_per_line = 640;
parms.lines = 480;
}
else
{
parms.bytes_per_line = 1152 * 3;
parms.pixels_per_line = 1152;
parms.lines = 864;
}
}
break;
case DC210_OPT_ERASE:
dc210_opt_erase = !!*(SANE_Word *) value;
break;
case DC210_OPT_DEFAULT:
DBG (1, "Fixme: Set all defaults here!\n");
break;
case DC210_OPT_INIT_DC210:
if ((Camera.fd = init_dc210 (&Camera)) == -1)
{
return SANE_STATUS_INVAL;
}
break;
default:
return SANE_STATUS_INVAL;
}
break;
case SANE_ACTION_GET_VALUE:
switch (option)
{
case 0:
*(SANE_Word *) value = NELEMS (sod);
break;
case DC210_OPT_IMAGE_NUMBER:
*(SANE_Word *) value = Camera.current_picture_number;
break;
case DC210_OPT_THUMBS:
*(SANE_Word *) value = dc210_opt_thumbnails;
break;
case DC210_OPT_SNAP:
*(SANE_Word *) value = dc210_opt_snap;
break;
case DC210_OPT_LOWRES:
*(SANE_Word *) value = dc210_opt_lowres;
break;
case DC210_OPT_ERASE:
*(SANE_Word *) value = dc210_opt_erase;
break;
default:
return SANE_STATUS_INVAL;
}
break;
case SANE_ACTION_SET_AUTO:
switch (option)
{
default:
return SANE_STATUS_UNSUPPORTED; /* We are DUMB */
}
}
if (info)
*info = myinfo;
return SANE_STATUS_GOOD;
}
SANE_Status
sane_get_parameters (SANE_Handle handle, SANE_Parameters * params)
{
int rc = SANE_STATUS_GOOD;
DBG (127, "sane_get_params called\n");
if (handle != MAGIC || !is_open)
rc = SANE_STATUS_INVAL; /* Unknown handle ... */
parms.last_frame = SANE_TRUE; /* Have no idea what this does */
*params = parms;
DBG (127, "sane_get_params return %d\n", rc);
return rc;
}
typedef struct
{
struct jpeg_source_mgr pub;
JOCTET *buffer;
}
my_source_mgr;
typedef my_source_mgr *my_src_ptr;
METHODDEF (void)
sanei_jpeg_init_source (j_decompress_ptr __sane_unused__ cinfo)
{
/* nothing to do */
}
METHODDEF (boolean) sanei_jpeg_fill_input_buffer (j_decompress_ptr cinfo)
{
my_src_ptr src = (my_src_ptr) cinfo->src;
if (read_data (Camera.fd, src->buffer, 1024) == -1)
{
DBG (5, "sane_start: read_data failed\n");
src->buffer[0] = (JOCTET) 0xFF;
src->buffer[1] = (JOCTET) JPEG_EOI;
return FALSE;
}
src->pub.next_input_byte = src->buffer;
src->pub.bytes_in_buffer = 1024;
return TRUE;
}
METHODDEF (void)
sanei_jpeg_skip_input_data (j_decompress_ptr cinfo, long num_bytes)
{
my_src_ptr src = (my_src_ptr) cinfo->src;
if (num_bytes > 0)
{
while (num_bytes > (long) src->pub.bytes_in_buffer)
{
num_bytes -= (long) src->pub.bytes_in_buffer;
(void) sanei_jpeg_fill_input_buffer (cinfo);
}
}
src->pub.next_input_byte += (size_t) num_bytes;
src->pub.bytes_in_buffer -= (size_t) num_bytes;
}
METHODDEF (void)
sanei_jpeg_term_source (j_decompress_ptr __sane_unused__ cinfo)
{
/* no work necessary here */
}
SANE_Status
sane_start (SANE_Handle handle)
{
DBG (127, "sane_start called\n");
if (handle != MAGIC || !is_open ||
(Camera.current_picture_number == 0 && dc210_opt_snap == SANE_FALSE))
return SANE_STATUS_INVAL; /* Unknown handle ... */
if (Camera.scanning)
return SANE_STATUS_EOF;
if (dc210_opt_snap)
{
/*
* Don't allow picture unless there is room in the
* camera.
*/
if (Camera.pic_left == 0)
{
DBG (3, "No room to store new picture\n");
return SANE_STATUS_INVAL;
}
if (snap_pic (Camera.fd) != SANE_STATUS_GOOD)
{
DBG (1, "Failed to snap new picture\n");
return SANE_STATUS_INVAL;
}
}
if (dc210_opt_thumbnails)
{
thumb_pck[3] = (unsigned char) Camera.current_picture_number - 1;
thumb_pck[4] = 1;
if (send_pck (Camera.fd, thumb_pck) == -1)
{
DBG (4, "sane_start: error: send_pck returned -1\n");
return SANE_STATUS_INVAL;
}
parms.bytes_per_line = 96 * 3;
parms.pixels_per_line = 96;
parms.lines = 72;
bytes_in_buffer = 0;
bytes_read_from_buffer = 0;
}
else
{
my_src_ptr src;
struct jpeg_error_mgr jerr;
pic_pck[3] = (unsigned char) Camera.current_picture_number - 1;
if (send_pck (Camera.fd, pic_pck) == -1)
{
DBG (4, "sane_start: error: send_pck returned -1\n");
return SANE_STATUS_INVAL;
}
cinfo.err = jpeg_std_error (&jerr);
jpeg_create_decompress (&cinfo);
cinfo.src = (struct jpeg_source_mgr *) (*cinfo.mem->alloc_small) ((j_common_ptr) & cinfo, JPOOL_PERMANENT, sizeof (my_source_mgr));
src = (my_src_ptr) cinfo.src;
src->buffer = (JOCTET *) (*cinfo.mem->alloc_small) ((j_common_ptr) &
cinfo,
JPOOL_PERMANENT,
1024 *
sizeof (JOCTET));
src->pub.init_source = sanei_jpeg_init_source;
src->pub.fill_input_buffer = sanei_jpeg_fill_input_buffer;
src->pub.skip_input_data = sanei_jpeg_skip_input_data;
src->pub.resync_to_restart = jpeg_resync_to_restart; /* default */
src->pub.term_source = sanei_jpeg_term_source;
src->pub.bytes_in_buffer = 0;
src->pub.next_input_byte = NULL;
(void) jpeg_read_header (&cinfo, TRUE);
dest_mgr = sanei_jpeg_jinit_write_ppm (&cinfo);
(void) jpeg_start_decompress (&cinfo);
}
Camera.scanning = SANE_TRUE; /* don't overlap scan requests */
total_bytes_read = 0;
return SANE_STATUS_GOOD;
}
SANE_Status
sane_read (SANE_Handle __sane_unused__ handle, SANE_Byte * data,
SANE_Int max_length, SANE_Int * length)
{
static char buffer[1024];
if (dc210_opt_thumbnails)
{
if (total_bytes_read == THUMBSIZE)
{
if (dc210_opt_erase)
{
if (erase (Camera.fd) == -1)
{
DBG (1, "Failed to erase memory\n");
return SANE_STATUS_INVAL;
}
Camera.pic_taken--;
Camera.pic_left++;
Camera.current_picture_number = Camera.pic_taken;
image_range.max--;
}
return SANE_STATUS_EOF;
}
*length = 0;
if (!(bytes_in_buffer - bytes_read_from_buffer))
{
if (read_data (Camera.fd, (unsigned char *) buffer, 1024) == -1)
{
DBG (5, "sane_read: read_data failed\n");
return SANE_STATUS_INVAL;
}
bytes_in_buffer = 1024;
bytes_read_from_buffer = 0;
}
while (bytes_read_from_buffer < bytes_in_buffer &&
max_length && total_bytes_read < THUMBSIZE)
{
*data++ = buffer[bytes_read_from_buffer++];
(*length)++;
max_length--;
total_bytes_read++;
}
if (total_bytes_read == THUMBSIZE)
{
if (end_of_data (Camera.fd) == -1)
{
DBG (4, "sane_read: end_of_data error\n");
return SANE_STATUS_INVAL;
}
else
{
return SANE_STATUS_GOOD;
}
}
else
{
return SANE_STATUS_GOOD;
}
}
else
{
int lines = 0;
if (cinfo.output_scanline >= cinfo.output_height)
{
/* clean up comms with the camera */
if (end_of_data (Camera.fd) == -1)
{
DBG (2, "sane_read: error: end_of_data returned -1\n");
return SANE_STATUS_INVAL;
}
if (dc210_opt_erase)
{
DBG (127, "sane_read bp%d, erase image\n", __LINE__);
if (erase (Camera.fd) == -1)
{
DBG (1, "Failed to erase memory\n");
return SANE_STATUS_INVAL;
}
Camera.pic_taken--;
Camera.pic_left++;
Camera.current_picture_number = Camera.pic_taken;
image_range.max--;
}
return SANE_STATUS_EOF;
}
/* XXX - we should read more than 1 line at a time here */
lines = 1;
(void) jpeg_read_scanlines (&cinfo, dest_mgr->buffer, lines);
(*dest_mgr->put_pixel_rows) (&cinfo, dest_mgr, lines, (char *) data);
*length = cinfo.output_width * cinfo.output_components * lines;
return SANE_STATUS_GOOD;
}
}
void
sane_cancel (SANE_Handle __sane_unused__ handle)
{
DBG (127, "sane_cancel() called\n");
if (Camera.scanning)
Camera.scanning = SANE_FALSE; /* done with scan */
else
DBG (127, "sane_cancel() aborted, scanner not scanning\n");
}
SANE_Status
sane_set_io_mode (SANE_Handle __sane_unused__ handle,
SANE_Bool __sane_unused__ non_blocking)
{
return SANE_STATUS_UNSUPPORTED;
}
SANE_Status
sane_get_select_fd (SANE_Handle __sane_unused__ handle, SANE_Int __sane_unused__ * fd)
{
return SANE_STATUS_UNSUPPORTED;
}
static PictureInfo *
get_pictures_info (void)
{
char f[] = "get_pictures_info";
unsigned int p;
PictureInfo *pics;
if ((pics = (PictureInfo *) malloc (Camera.pic_taken *
sizeof (PictureInfo))) == NULL)
{
DBG (4, "%s: error: allocate memory for pictures array\n", f);
return NULL;
}
for (p = 0; p < (unsigned int) Camera.pic_taken; p++)
{
if (get_picture_info (pics + p, p) == -1)
{
free (pics);
return NULL;
}
}
return pics;
}
static int
get_picture_info (PictureInfo * pic, int p)
{
char f[] = "get_picture_info";
static char buffer[256];
DBG (4, "%s: info for pic #%d\n", f, p);
pic_info_pck[3] = (unsigned char) p;
if (send_pck (Camera.fd, pic_info_pck) == -1)
{
DBG (4, "%s: error: send_pck returned -1\n", f);
return -1;
}
if (read_data (Camera.fd, (unsigned char *) buffer, 256) == -1)
{
DBG (2, "%s: error: read_data returned -1\n", f);
return -1;
}
if (end_of_data (Camera.fd) == -1)
{
DBG (2, "%s: error: end_of_data returned -1\n", f);
return -1;
}
if (buffer[3] == 0)
{
pic->low_res = SANE_TRUE;
}
else if (buffer[3] == 1)
{
pic->low_res = SANE_FALSE;
}
else
{
DBG (2, "%s: error: unknown resolution code %u\n", f, buffer[3]);
return -1;
}
pic->size = (buffer[8] & 0xFF) << 24;
pic->size |= (buffer[9] & 0xFF) << 16;
pic->size |= (buffer[10] & 0xFF) << 8;
pic->size |= (buffer[11] & 0xFF);
return 0;
}
static SANE_Status
snap_pic (int fd)
{
char f[] = "snap_pic";
/* make sure camera is set to our settings state */
if (change_res (Camera.fd, dc210_opt_lowres) == -1)
{
DBG (1, "%s: Failed to set resolution\n", f);
return SANE_STATUS_INVAL;
}
/* take the picture */
if (send_pck (fd, shoot_pck) == -1)
{
DBG (4, "%s: error: send_pck returned -1\n", f);
return SANE_STATUS_INVAL;
}
else
{
if (end_of_data (Camera.fd) == -1)
{
DBG (2, "%s: error: end_of_data returned -1\n", f);
return SANE_STATUS_INVAL;
}
}
Camera.pic_taken++;
Camera.pic_left--;
Camera.current_picture_number = Camera.pic_taken;
image_range.max++;
sod[DC210_OPT_IMAGE_NUMBER].cap &= ~SANE_CAP_INACTIVE;
/* add this one to the Pictures array */
if ((Camera.Pictures =
(PictureInfo *) realloc (Camera.Pictures,
Camera.pic_taken * sizeof (PictureInfo))) ==
NULL)
{
DBG (4, "%s: error: allocate memory for pictures array\n", f);
return SANE_STATUS_INVAL;
}
if (get_picture_info (Camera.Pictures + Camera.pic_taken,
Camera.pic_taken) == -1)
{
DBG (1, "%s: Failed to get new picture info\n", f);
/* XXX - I guess we should try to erase the image here */
return SANE_STATUS_INVAL;
}
return SANE_STATUS_GOOD;
}
|