summaryrefslogtreecommitdiff
path: root/camlibs/ax203/ax203.c
blob: 4e0a6ec5d0b7b2372ae97c7e840350e961144e55 (plain)
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
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
/* Appotech ax203 picframe access library
 *
 *   Copyright (c) 2010-2012 Hans de Goede <hdegoede@redhat.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the 
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301  USA
 */
#include "config.h"

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <_stdint.h>
#include <stdlib.h>
#include <time.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifdef HAVE_LIBGD
#include <gd.h>
#endif

#include <gphoto2/gphoto2-result.h>
#include "ax203.h"
#ifdef HAVE_LIBJPEG
#include "jpeg_memsrcdest.h"
#endif

static const struct eeprom_info {
	const char *name;
	uint32_t id;
	int mem_size;
	int has_4k_sectors;
	int pp_64k;
} ax203_eeprom_info[] = {
	{ "AMIC A25L040", 0x37133037,  524288, 1 },
	{ "AMIC A25L080", 0x37143037, 1048576, 1 },
	{ "AMIC A25L40P", 0x1320377f,  524288, 0 },
	{ "AMIC A25L80P", 0x1420377f, 1048576, 0 },
	{ "AMIC A25L16P", 0x1520377f, 2097152, 0 },

	/* Note the ATmel AT26DF041 id:0x0000441f is fsck-ed up. It doesn't
	   support ERASE_64K, (only 4K) and SPI_EEPROM_PP is 0x11 rather then
	   0x02 (0x02 only programs a single byte). */
	/* I cannot find a datasheet for the ATmel AT26DF081 id:0x0000451f */
	{ "ATmel AT26DF161",  0x0000461f, 2097152, 1 },
	{ "ATmel AT26DF081A", 0x0001451f, 1048576, 1 },
	{ "ATmel AT26DF161A", 0x0001461f, 2097152, 1 },
	{ "ATmel AT25DF081",  0x0002451f, 1048576, 1 },
	{ "ATmel AT25DF161",  0x0002461f, 2097152, 1 },

	{ "EON EN25B16", 0x1c15201c, 2097152, 0 },
	{ "EON EN25B32", 0x1c16201c, 4194304, 0 },
	{ "EON EN25F80", 0x1c14311c, 1048576, 1 },
	{ "EON EN25F16", 0x1c15311c, 2097152, 1 },

	{ "ESI ES25P80", 0x0014204a, 1048576, 0 },
	{ "ESI ES25P16", 0x0015204a, 2097152, 0 },

	{ "ESMT F25L008 (top)",    0x8c14208c, 1048576, 1 },
	{ "ESMT F25L008 (bottom)", 0x8c14218c, 1048576, 1 },

	{ "GigaDevice GD25Q40", 0xc81340c8,  524288, 1 },
	{ "GigaDevice GD25Q80", 0xc81440c8, 1048576, 1 },
	{ "GigaDevice GD25Q16", 0xc81540c8, 2097152, 1 },

	{ "MXIC MX25L4005A", 0xc21320c2,  524288, 1 },
	{ "MXIC MX25L8005A", 0xc21420c2, 1048576, 1 },
	{ "MXIC MX25L1605A", 0xc21520c2, 2097152, 1 },

	{ "Nantronics N25S80", 0xd51430d5, 1048576, 1 },                                                                                                                                     

	{ "PMC Pm25LV010", 0x007e9d7f, 524288, 0 },

	{ "Spansion S25FL004A", 0x00120201,  524288, 0 },
	{ "Spansion S25FL008A", 0x00130201, 1048576, 0 },
	{ "Spansion S25FL016A", 0x00140201, 2097152, 0 },

	{ "SST25VF080", 0xbf8e25bf, 1048576, 0, 1 },
	{ "SST25VF016", 0xbf4125bf, 2097152, 0, 1 },

	{ "ST M25P08", 0x7f142020, 1048576, 0 },
	{ "ST M25P16", 0x7f152020, 2097152, 0 },
	{ "ST M25P32", 0x7f162020, 4194304, 0 },
	{ "ST M25P64", 0x7f172020, 8388608, 0 },

	{ "Winbond W25P80", 0x001420ef, 1048576, 0 },
	{ "Winbond W25P16", 0x001520ef, 2097152, 0 },

	{ "Winbond W25X40", 0x001330ef,  524288, 1 },
	{ "Winbond W25X80", 0x001430ef, 1048576, 1 },
	{ "Winbond W25X16", 0x001530ef, 2097152, 1 },
	{ "Winbond W25X32", 0x001630ef, 4194304, 1 },
	{ "Winbond W25X64", 0x001730ef, 8388608, 1 },

	{ "Winbond W25Q80", 0x001440ef, 1048576, 1 },
	{ "Winbond W25Q16", 0x001540ef, 2097152, 1 },
	{ "Winbond W25Q32", 0x001640ef, 4194304, 1 },

	{ }
};

static int
ax203_send_cmd(Camera *camera, int to_dev, char *cmd, int cmd_size,
	char *data, int data_size)
{
	char sense_buffer[32];

	return gp_port_send_scsi_cmd (camera->port, to_dev, cmd, cmd_size,
		sense_buffer, sizeof(sense_buffer), data, data_size);
}

static int
ax203_send_eeprom_cmd(Camera *camera, int to_dev,
	char *eeprom_cmd, int eeprom_cmd_size,
	char *data, int data_size, char extra_arg)
{
	char cmd_buffer[16];
	int i;

	memset (cmd_buffer, 0, sizeof (cmd_buffer));
	if (to_dev)
		cmd_buffer[0] = AX203_TO_DEV;
	else
		cmd_buffer[0] = AX203_FROM_DEV;

	cmd_buffer[5] = AX203_EEPROM_CMD;
	cmd_buffer[6] = eeprom_cmd_size;
	cmd_buffer[7] = (data_size >> 16) & 0xff;
	cmd_buffer[8] = (data_size >> 8) & 0xff;
	cmd_buffer[9] = (data_size) & 0xff;

	for (i = 0; i < eeprom_cmd_size; i++)
		cmd_buffer[10 + i] = eeprom_cmd[i];

	cmd_buffer[15] = extra_arg;

	return ax203_send_cmd (camera, to_dev, cmd_buffer, sizeof(cmd_buffer),
			       data, data_size);
}

static int
ax203_get_version(Camera *camera, char *buf)
{
	int ret;
	char cmd_buffer[16];

	memset (cmd_buffer, 0, sizeof (cmd_buffer));

	cmd_buffer[0] = AX203_FROM_DEV;
	cmd_buffer[5] = AX203_GET_VERSION;
	cmd_buffer[6] = 1;
	cmd_buffer[10] = AX203_GET_VERSION;

	ret = ax203_send_cmd (camera, 0, cmd_buffer, sizeof(cmd_buffer),
			      buf, 64);
	buf[63] = 0; /* ensure 0 termination */

	return ret;
}

/* This would be very nice, if only not all devices would answer that they
   are a 128x128 pixels device */
#if 0
static int
ax203_get_lcd_size(Camera *camera)
{
	char cmd_buffer[16];
	uint8_t data_buffer[4];

	memset (cmd_buffer, 0, sizeof (cmd_buffer));

	cmd_buffer[0] = AX203_FROM_DEV;
	cmd_buffer[5] = AX203_GET_LCD_SIZE;
	cmd_buffer[6] = 0x01;
	cmd_buffer[10] = AX203_GET_LCD_SIZE;

	CHECK (ax203_send_cmd (camera, 0, cmd_buffer, sizeof (cmd_buffer),
			       (char *)data_buffer, sizeof (data_buffer)))

	camera->pl->width  = le16atoh (data_buffer);
	camera->pl->height = le16atoh (data_buffer + 2);

	return GP_OK;
}
#endif

static int
ax203_get_checksum(Camera *camera, int address, int size)
{
	int ret;
	char cmd_buffer[16];
	uint8_t buf[2];

	memset (cmd_buffer, 0, sizeof (cmd_buffer));

	cmd_buffer[0] = AX203_FROM_DEV;
	cmd_buffer[5] = AX203_GET_CHECKSUM;
	cmd_buffer[6] = AX203_GET_CHECKSUM;
	cmd_buffer[7] = (size >> 8) & 0xff;
	cmd_buffer[8] = size & 0xff;
	cmd_buffer[11] = (address >> 16) & 0xff;
	cmd_buffer[12] = (address >> 8) & 0xff;
	cmd_buffer[13] = address & 0xff;

	ret = ax203_send_cmd (camera, 0, cmd_buffer, sizeof(cmd_buffer),
			      (char *)buf, 2);
	if (ret < 0) return ret;

	/*return be16atoh(buf);*/
	return (buf[0] << 8) | buf[1];
}

static int
ax3003_get_frame_id(Camera *camera)
{
	int ret;
	char cmd_buffer[16];
	uint8_t id;

	memset (cmd_buffer, 0, sizeof (cmd_buffer));

	cmd_buffer[0] = AX3003_FRAME_CMD;
	cmd_buffer[1] = AX3003_GET_FRAME_ID;

	ret = ax203_send_cmd (camera, 0, cmd_buffer, sizeof(cmd_buffer),
			      (char *)&id, 1);
	if (ret < 0) return ret;

	return id;
}

static int
ax3003_get_abfs_start(Camera *camera)
{
	int ret;
	char cmd_buffer[16];
	uint8_t buf[2];

	memset (cmd_buffer, 0, sizeof (cmd_buffer));

	cmd_buffer[0] = AX3003_FRAME_CMD;
	cmd_buffer[1] = AX3003_GET_ABFS_START;

	ret = ax203_send_cmd (camera, 0, cmd_buffer, sizeof(cmd_buffer),
			      (char *)buf, 2);
	if (ret < 0) return ret;

	return ((buf[0] << 8) | buf[1]) * 0x100;
	/*return be16atoh(buf) * 0x100;*/
}

int
ax203_set_time_and_date(Camera *camera, struct tm *t)
{
	char cmd_buffer[16];

	memset (cmd_buffer, 0, sizeof (cmd_buffer));
	
	cmd_buffer[0] = AX203_SET_TIME;
	
	cmd_buffer[5] = t->tm_year % 100;

	switch (camera->pl->frame_version) {
	case AX3003_FIRMWARE_3_5_x:
		/* Note this is what the windows software does, with the
		   one AX3003 frame I have this does not do anything */
		cmd_buffer[0] = AX3003_FRAME_CMD;
		cmd_buffer[1] = AX3003_SET_TIME;
		/* fall through */
	case AX203_FIRMWARE_3_3_x:
	case AX203_FIRMWARE_3_4_x:
		cmd_buffer[6] = t->tm_mon + 1;
		cmd_buffer[7] = t->tm_wday;
		break;
	case AX206_FIRMWARE_3_5_x:
		cmd_buffer[6] = 19 + t->tm_year / 100;
		cmd_buffer[7] = t->tm_mon + 1;
		break;
	}
	cmd_buffer[8]  = t->tm_mday;
	cmd_buffer[9]  = t->tm_hour;
	cmd_buffer[10] = t->tm_min;
	cmd_buffer[11] = t->tm_sec;

	return ax203_send_cmd (camera, 0, cmd_buffer, sizeof(cmd_buffer),
			       NULL, 0);
}

static int
ax203_eeprom_device_identification(Camera *camera, char *buf)
{
	char cmd = SPI_EEPROM_RDID;

	return ax203_send_eeprom_cmd (camera, 0, &cmd, 1, buf, 64, 0);
}

static int
ax203_eeprom_release_from_deep_powerdown(Camera *camera)
{
	char cmd = SPI_EEPROM_RDP;

	return ax203_send_eeprom_cmd (camera, 1, &cmd, 1, NULL, 0, 0);
}

static int
ax203_eeprom_read(Camera *camera, int address, char *buf, int buf_size)
{
	char cmd[4];
    
	cmd[0] = SPI_EEPROM_READ;
	cmd[1] = (address >> 16) & 0xff;
	cmd[2] = (address >> 8) & 0xff;
	cmd[3] = (address) & 0xff;
		    
	return ax203_send_eeprom_cmd (camera, 0, cmd, sizeof(cmd), buf,
				      buf_size, 0);
}

static int
ax203_eeprom_program_page(Camera *camera, int address, char *buf, int buf_size,
	char extra_arg)
{
	char cmd[4];

	cmd[0] = SPI_EEPROM_PP;
	cmd[1] = (address >> 16) & 0xff;
	cmd[2] = (address >> 8) & 0xff;
	cmd[3] = (address) & 0xff;
		    
	return ax203_send_eeprom_cmd (camera, 1, cmd, sizeof(cmd), buf,
				      buf_size, extra_arg);
}

static int
ax203_eeprom_write_enable(Camera *camera)
{
	char cmd = SPI_EEPROM_WREN;

	return ax203_send_eeprom_cmd (camera, 1, &cmd, 1, NULL, 0, 0);
}

static int
ax203_eeprom_clear_block_protection(Camera *camera)
{
	char cmd[2];

	cmd[0] = SPI_EEPROM_WRSR;
	cmd[1] = 0;

	return ax203_send_eeprom_cmd (camera, 1, cmd, sizeof(cmd), NULL, 0, 0);
}

static int
ax203_eeprom_erase_4k_sector(Camera *camera, int address)
{
	char cmd[4];

	cmd[0] = SPI_EEPROM_ERASE_4K;
	cmd[1] = (address >> 16) & 0xff;
	cmd[2] = (address >> 8) & 0xff;
	cmd[3] = (address) & 0xff;
		    
	return ax203_send_eeprom_cmd (camera, 1, cmd, sizeof(cmd), NULL, 0, 0);
}

static int
ax203_eeprom_erase_64k_sector(Camera *camera, int address)
{
	char cmd[4];

	cmd[0] = SPI_EEPROM_ERASE_64K;
	cmd[1] = (address >> 16) & 0xff;
	cmd[2] = (address >> 8) & 0xff;
	cmd[3] = (address) & 0xff;
		    
	return ax203_send_eeprom_cmd (camera, 1, cmd, sizeof(cmd), NULL, 0, 0);
}

static int
ax203_eeprom_wait_ready(Camera *camera)
{
	char cmd = SPI_EEPROM_RDSR; /* Read status */
	char buf[64];
	int count = 0;

	switch (camera->pl->frame_version) {
	case AX203_FIRMWARE_3_3_x:
	case AX203_FIRMWARE_3_4_x:
	case AX206_FIRMWARE_3_5_x:
		/* Do as windows does, read the status word 64 times,
		   then check */
		count = 64;
		break;
	case AX3003_FIRMWARE_3_5_x:
		/* On the ax3003 contineously reading the status word
		   does not work. */
		count = 1;
		break;
	}

	while (1) {
		CHECK (ax203_send_eeprom_cmd (camera, 0, &cmd, 1,
					      buf, count, 0))
		/* We only need to check the last read */
		if (!(buf[count - 1] & 0x01)) /* Check write in progress bit */
			break; /* No write in progress, done waiting */
	}
	return GP_OK;
}

static int
ax203_read_sector(Camera *camera, int sector, char *buf)
{
	int ret;
	if (camera->pl->mem_dump) {
		ret = fseek (camera->pl->mem_dump,
			     sector * SPI_EEPROM_SECTOR_SIZE, SEEK_SET);
		if (ret) {
			gp_log (GP_LOG_ERROR, "ax203",
				"seeking in memdump: %s", strerror(errno));
			return GP_ERROR_IO_READ;
		}
		ret = fread (buf, 1, SPI_EEPROM_SECTOR_SIZE,
			     camera->pl->mem_dump);
		if (ret != SPI_EEPROM_SECTOR_SIZE) {
			if (ret < 0)
				gp_log (GP_LOG_ERROR, "ax203",
					"reading memdump: %s",
					strerror(errno));
			else
				gp_log (GP_LOG_ERROR, "ax203",
					"short read reading from memdump");
			return GP_ERROR_IO_READ;
		}
	} else {
		CHECK (ax203_eeprom_read (camera,
					  sector * SPI_EEPROM_SECTOR_SIZE,
					  buf, SPI_EEPROM_SECTOR_SIZE))
	}
	return GP_OK;
}

static int
ax203_write_sector(Camera *camera, int sector, char *buf)
{
	int ret;
	if (camera->pl->mem_dump) {
		ret = fseek (camera->pl->mem_dump,
			     sector * SPI_EEPROM_SECTOR_SIZE, SEEK_SET);
		if (ret) {
			gp_log (GP_LOG_ERROR, "ax203",
				"seeking in memdump: %s", strerror(errno));
			return GP_ERROR_IO_WRITE;
		}
		ret = fwrite (buf, 1, SPI_EEPROM_SECTOR_SIZE,
			      camera->pl->mem_dump);
		if (ret != SPI_EEPROM_SECTOR_SIZE) {
			gp_log (GP_LOG_ERROR, "ax203",
				"writing memdump: %s", strerror(errno));
			return GP_ERROR_IO_WRITE;
		}
	} else {
		int i, base = sector * SPI_EEPROM_SECTOR_SIZE;

		for (i = 0; i < SPI_EEPROM_SECTOR_SIZE; i += 256) {
			CHECK (ax203_eeprom_write_enable (camera))
			CHECK (ax203_eeprom_program_page (camera, base + i,
							  buf + i, 256, 0))
			CHECK (ax203_eeprom_wait_ready (camera))
		}
	}
	return GP_OK;
}

static int
ax203_erase4k_sector(Camera *camera, int sector)
{
	if (camera->pl->mem_dump)
		return GP_OK;

	CHECK (ax203_eeprom_write_enable (camera))
	CHECK (ax203_eeprom_erase_4k_sector (camera,
					     sector * SPI_EEPROM_SECTOR_SIZE))
	CHECK (ax203_eeprom_wait_ready (camera))

	return GP_OK;
}

static int
ax203_erase64k_sector(Camera *camera, int sector)
{
	if (camera->pl->mem_dump)
		return GP_OK;

	CHECK (ax203_eeprom_write_enable (camera))
	CHECK (ax203_eeprom_erase_64k_sector (camera,
					     sector * SPI_EEPROM_SECTOR_SIZE))
	CHECK (ax203_eeprom_wait_ready (camera))

	return GP_OK;
}

static int
ax203_check_sector_present(Camera *camera, int sector)
{
	int ret;

	if ((sector + 1) * SPI_EEPROM_SECTOR_SIZE > camera->pl->mem_size) {
		gp_log (GP_LOG_ERROR, "ax203", "access beyond end of memory");
		return GP_ERROR_CORRUPTED_DATA;
	}

	if (camera->pl->sector_is_present[sector])
		return GP_OK;

	ret = ax203_read_sector(camera, sector, camera->pl->mem +
				sector * SPI_EEPROM_SECTOR_SIZE);
	if (ret == 0)
		camera->pl->sector_is_present[sector] = 1;

	return ret;
}

static int
ax203_read_mem(Camera *camera, int offset,
	void *buf, int len)
{
	int to_copy, sector = offset / SPI_EEPROM_SECTOR_SIZE;

	while (len) {
		CHECK (ax203_check_sector_present (camera, sector))

		to_copy = SPI_EEPROM_SECTOR_SIZE -
			  (offset % SPI_EEPROM_SECTOR_SIZE);
		if (to_copy > len)
			to_copy = len;

		memcpy(buf, camera->pl->mem + offset, to_copy);
		buf += to_copy;
		len -= to_copy;
		offset += to_copy;
		sector++;
	}
	return GP_OK;
}

static int
ax203_write_mem(Camera *camera, int offset,
	void *buf, int len)
{
	int to_copy, sector = offset / SPI_EEPROM_SECTOR_SIZE;

	while (len) {
		CHECK (ax203_check_sector_present (camera, sector))

		to_copy = SPI_EEPROM_SECTOR_SIZE -
			  (offset % SPI_EEPROM_SECTOR_SIZE);
		if (to_copy > len)
			to_copy = len;

		memcpy(camera->pl->mem + offset, buf, to_copy);
		camera->pl->sector_dirty[sector] = 1;

		buf += to_copy;
		len -= to_copy;
		offset += to_copy;
		sector++;
	}
	return GP_OK;
}

/* This function reads the parameter block from the eeprom and:
   1) checks some hopefully constant bytes to make sure it is not reading
      garbage
   2) Gets the lcd size from the paramter block (ax203_get_lcd_size seems
      to result in all frames being detected as being 128x128 pixels).
   3) Determines the compression type for v3.4.x frames
   4) Determines the start of the ABFS
*/
static int ax203_read_parameter_block(Camera *camera)
{
	uint8_t buf[32], expect[32];
	int i, param_offset = 0, resolution_offset = 0;
	int compression_offset = -1, abfs_start_offset = 0, expect_size = 0;
	const int valid_resolutions[][2] = {
		{  96,  64 },
		{ 120, 160 },
		{ 128, 128 },
		{ 132, 132 },
		{ 128, 160 },
		{ 160, 120 },
		{ 160, 128 },
		{ 240, 320 },
		{ 320, 240 },
		{ }
	};
	const uint8_t expect_33x[] = {
		0x13, 0x15, 0, 0, 0x02, 0x01, 0x02, 0x01,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
	const uint8_t expect_34x[] = {
		0x13, 0x15, 0, 0, 0, 0, 0, 0x01,
		0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
	const uint8_t expect_35x[] = {
		0x00, 0x00, 0, 0, 0, 0, 0, 0xd8 };

	switch (camera->pl->frame_version) {
	case AX203_FIRMWARE_3_3_x:
		param_offset = 0x50;
		resolution_offset  = 2;
		abfs_start_offset  = 16;
		memcpy (expect, expect_33x, sizeof(expect_33x));
		expect_size = sizeof(expect_33x);
		/* Byte 4 of the parameter block probably is the compression
		   type like with v3.4.x, but this needs confirmation. */
		camera->pl->compression_version = AX203_COMPRESSION_YUV;
		break;
	case AX203_FIRMWARE_3_4_x:
		param_offset = 0x50;
		resolution_offset  = 2;
		compression_offset = 6;
		abfs_start_offset  = 16;
		memcpy (expect, expect_34x, sizeof(expect_34x));
		expect_size = sizeof(expect_34x);
		break;
	case AX206_FIRMWARE_3_5_x:
		param_offset = 0x20;
		abfs_start_offset  = 2;
		resolution_offset  = 3;
		memcpy (expect, expect_35x, sizeof(expect_35x));
		expect_size = sizeof(expect_35x);
		/* ax206 + 3.5.x firmware has a fixed compression type */
		camera->pl->compression_version = AX206_COMPRESSION_JPEG;
		break;
	case AX3003_FIRMWARE_3_5_x:
		i = ax3003_get_frame_id (camera);
		if (i < 0) return i;
		switch (i) {
		case 0:
		case 1:
			camera->pl->width  = 320;
			camera->pl->height = 240;
			break;
		default:
			gp_log (GP_LOG_ERROR, "ax203",
				"unknown ax3003 frame id: %d", i);
			return GP_ERROR_MODEL_NOT_FOUND;
		}

		i = ax3003_get_abfs_start (camera);
		if (i < 0) return i;
		camera->pl->fs_start = i;

		camera->pl->compression_version = AX3003_COMPRESSION_JPEG;

		goto verify_parameters;
	}

	CHECK (ax203_read_mem (camera, param_offset, buf, sizeof(buf)))

	switch (camera->pl->frame_version) {
	case AX203_FIRMWARE_3_3_x:
		/* 1 byte width / height */
		camera->pl->width  = buf[resolution_offset    ];
		camera->pl->height = buf[resolution_offset + 1];
		expect[resolution_offset    ] = camera->pl->width;
		expect[resolution_offset + 1] = camera->pl->height;
		break;
	case AX203_FIRMWARE_3_4_x:
	case AX206_FIRMWARE_3_5_x:
		/* 2 byte little endian width / height */
		camera->pl->width  = le16atoh (buf + resolution_offset);
		camera->pl->height = le16atoh (buf + resolution_offset + 2);
		htole16a(expect + resolution_offset    , camera->pl->width);
		htole16a(expect + resolution_offset + 2, camera->pl->height);
		break;
	case AX3003_FIRMWARE_3_5_x:
		/* Never reached, here to silence a compiler warning */
		break;
	}

	if (compression_offset != -1) {
		i = buf[compression_offset];
		switch (i) {
		case 2:
			camera->pl->compression_version =
				AX203_COMPRESSION_YUV;
			break;
		case 3:
			camera->pl->compression_version =
				AX203_COMPRESSION_YUV_DELTA;
			break;
		default:
			gp_log (GP_LOG_ERROR, "ax203",
				"unknown compression version: %d", i);
			return GP_ERROR_MODEL_NOT_FOUND;
		}
		expect[compression_offset] = i;
	}

	i = buf[abfs_start_offset];
	camera->pl->fs_start = i * 0x10000;
	expect[abfs_start_offset] = i;

	if (memcmp (buf, expect, expect_size)) {
		gp_log (GP_LOG_ERROR, "ax203",
			"unexpected contents of parameter block");
		return GP_ERROR_MODEL_NOT_FOUND;
	}

verify_parameters:
	for (i = 0; valid_resolutions[i][0]; i++)
		if (valid_resolutions[i][0] == camera->pl->width &&
		    valid_resolutions[i][1] == camera->pl->height)
			break;

	if (!valid_resolutions[i][0]) {
		gp_log (GP_LOG_ERROR, "ax203", "unknown resolution: %dx%d",
			camera->pl->width, camera->pl->height);
		return GP_ERROR_MODEL_NOT_FOUND;
	}

	CHECK (ax203_read_mem (camera, camera->pl->fs_start, buf, 4))
	if (memcmp (buf, AX203_ABFS_MAGIC, 4)) {
		gp_log (GP_LOG_ERROR, "ax203", "ABFS magic not found at: %x",
			camera->pl->fs_start);
		return GP_ERROR_MODEL_NOT_FOUND;
	}

	GP_DEBUG ("lcd size %dx%d, compression ver: %d, fs-start: %x",
		  camera->pl->width, camera->pl->height,
		  camera->pl->compression_version, camera->pl->fs_start);

	return GP_OK;
}

static int ax203_check_file_index(Camera *camera, int idx)
{
	int count;

	if (idx < 0) {
		gp_log (GP_LOG_ERROR, "ax203",
			"file index < 0");
		return GP_ERROR_BAD_PARAMETERS;
	}

	count = ax203_read_filecount (camera);
	if (count < 0) return count;

	if (idx >= count) {
		gp_log (GP_LOG_ERROR, "ax203",
			"file index beyond end of ABFS");
		return GP_ERROR_BAD_PARAMETERS;
	}

	return GP_OK;
}

int
ax203_filesize(Camera *camera)
{
	switch (camera->pl->compression_version) {
	case AX203_COMPRESSION_YUV:
		return camera->pl->width * camera->pl->height;
	case AX203_COMPRESSION_YUV_DELTA:
		return camera->pl->width * camera->pl->height * 3 / 4;
	case AX206_COMPRESSION_JPEG:
	case AX3003_COMPRESSION_JPEG:
		/* Variable size */
		return 0;
	}
	/* Never reached */
	return GP_ERROR_NOT_SUPPORTED;	
}

static int
ax203_max_filecount(Camera *camera)
{
	switch (camera->pl->frame_version) {
	case AX203_FIRMWARE_3_3_x:
	case AX203_FIRMWARE_3_4_x:
		return (AX203_ABFS_SIZE - AX203_ABFS_FILE_OFFSET (0)) / 2;
	case AX206_FIRMWARE_3_5_x:
		return (AX203_ABFS_SIZE - AX206_ABFS_FILE_OFFSET (0)) /
			sizeof(struct ax206_v3_5_x_raw_fileinfo);
	case AX3003_FIRMWARE_3_5_x:
		return (AX203_ABFS_SIZE - AX3003_ABFS_FILE_OFFSET (0)) /
			sizeof(struct ax3003_v3_5_x_raw_fileinfo);
	}
	/* Never reached */
	return GP_ERROR_NOT_SUPPORTED;	
}

static int
ax203_read_v3_3_x_v3_4_x_filecount(Camera *camera)
{
	/* The v3.3.x and v3.4.x firmware ABFS keeps a single byte file count
	   at (camera->pl->fs_start + AX203_ABFS_COUNT_OFFSET) However the
	   frame does not seem to care about this.
	   So always return the maximum number of entries the ABFS can contain
	   (and rely on fileinfo.present to figure out how many files there
	    actually are). */
	return ax203_max_filecount (camera);
}

static int
ax203_write_v3_3_x_v3_4_x_filecount(Camera *camera, int count)
{
	uint8_t c = count;

	/* Despite the frame not caring, we do write the filecount just
	   like windows does. */
	CHECK (ax203_write_mem (camera,
				camera->pl->fs_start + AX203_ABFS_COUNT_OFFSET,
				&c, 1))
	return GP_OK;
}

static int
ax203_read_v3_5_x_filecount(Camera *camera)
{
	/* The v3.5.x firmware ABFS does not contain a separate file count,
	   so always return the maximum number of entries it can contain
	   (and rely on fileinfo.present to figure out how many files there
	    actually are). */
	return ax203_max_filecount (camera);
}

static int
ax203_write_v3_5_x_filecount(Camera *camera, int count)
{
	/* This is a no-op as the v3.5.x firmware ABFS does not contain a
	   separate file count */
	return GP_OK;
}

static
int ax203_read_v3_3_x_v3_4_x_fileinfo(Camera *camera, int idx,
	struct ax203_fileinfo *fileinfo)
{
	uint8_t buf[2];

	CHECK (ax203_read_mem (camera,
			       camera->pl->fs_start +
					AX203_ABFS_FILE_OFFSET (idx),
			       buf, 2))

	/*fileinfo->address = le16atoh (buf) << 8;*/
	fileinfo->address = (buf[0] | (buf[1] << 8)) << 8;
	fileinfo->size    = ax203_filesize (camera);
	fileinfo->present = fileinfo->address? 1 : 0;

	return GP_OK;
}

static
int ax203_write_v3_3_x_v3_4_x_fileinfo(Camera *camera, int idx,
	struct ax203_fileinfo *fileinfo)
{
	uint8_t buf[2];

	if (fileinfo->address & 0xff) {
		gp_log (GP_LOG_ERROR, "ax203", "LSB of address is not 0");
		return GP_ERROR_BAD_PARAMETERS;
	}

	if (!fileinfo->present)
		fileinfo->address = 0;

	/*htole16a(buf, fileinfo->address >> 8);*/
	buf[0] = (fileinfo->address >> 8) & 0xff;
	buf[1] = (fileinfo->address >>16) & 0xff;
	CHECK (ax203_write_mem (camera,
				camera->pl->fs_start +
					AX203_ABFS_FILE_OFFSET (idx),
				buf, 2))

	return GP_OK;
}

static
int ax206_read_v3_5_x_fileinfo(Camera *camera, int idx,
	struct ax203_fileinfo *fileinfo)
{
	struct ax206_v3_5_x_raw_fileinfo raw;

	CHECK (ax203_read_mem (camera,
			       camera->pl->fs_start +
					AX206_ABFS_FILE_OFFSET (idx),
			       &raw, sizeof(raw)))

	fileinfo->present = raw.present == 0x01;
	fileinfo->address = le32toh (raw.address);
	fileinfo->size    = le16toh (raw.size);

	return GP_OK;	
}

static
int ax206_write_v3_5_x_fileinfo(Camera *camera, int idx,
	struct ax203_fileinfo *fileinfo)
{
	struct ax206_v3_5_x_raw_fileinfo raw;

	raw.present = fileinfo->present;
	raw.address = htole32(fileinfo->address);
	raw.size    = htole16(fileinfo->size);

	CHECK (ax203_write_mem (camera,
				camera->pl->fs_start +
					AX206_ABFS_FILE_OFFSET (idx),
				&raw, sizeof(raw)))

	return GP_OK;
}

static
int ax3003_read_v3_5_x_fileinfo(Camera *camera, int idx,
	struct ax203_fileinfo *fileinfo)
{
	struct ax3003_v3_5_x_raw_fileinfo raw;

	CHECK (ax203_read_mem (camera,
			       camera->pl->fs_start +
					AX3003_ABFS_FILE_OFFSET (idx),
			       &raw, sizeof(raw)))

	if (raw.address == 0xffff || raw.size == 0xffff) {
		memset(fileinfo, 0, sizeof(*fileinfo));
		return GP_OK;
	}

	fileinfo->present = raw.address && raw.size;
	fileinfo->address = be16toh (raw.address) * 0x100;
	fileinfo->size    = be16toh (raw.size) * 0x100;

	return GP_OK;
}

static
int ax3003_write_v3_5_x_fileinfo(Camera *camera, int idx,
	struct ax203_fileinfo *fileinfo)
{
	struct ax3003_v3_5_x_raw_fileinfo raw;

	if (fileinfo->address & 0xff) {
		gp_log (GP_LOG_ERROR, "ax203", "LSB of address is not 0");
		return GP_ERROR_BAD_PARAMETERS;
	}

	if (fileinfo->size & 0xff) {
		gp_log (GP_LOG_ERROR, "ax203", "LSB of size is not 0");
		return GP_ERROR_BAD_PARAMETERS;
	}

	if (fileinfo->present) {
		raw.address = htobe16(fileinfo->address / 0x100);
		raw.size    = htobe16(fileinfo->size / 0x100);
	} else {
		raw.address = 0;
		raw.size    = 0;
	}

	CHECK (ax203_write_mem (camera,
				camera->pl->fs_start +
					AX3003_ABFS_FILE_OFFSET (idx),
				&raw, sizeof(raw)))

	return GP_OK;
}

/***************** Begin "public" functions *****************/

int
ax203_read_filecount(Camera *camera)
{
	switch (camera->pl->frame_version) {
	case AX203_FIRMWARE_3_3_x:
	case AX203_FIRMWARE_3_4_x:
		return ax203_read_v3_3_x_v3_4_x_filecount (camera);
	case AX206_FIRMWARE_3_5_x:
	case AX3003_FIRMWARE_3_5_x:
		return ax203_read_v3_5_x_filecount (camera);;
	}
	/* Never reached */
	return GP_ERROR_NOT_SUPPORTED;
}

static int
ax203_update_filecount(Camera *camera)
{
	int i, max, count = 0;

	/* "Calculate" the real file count */
	max = ax203_max_filecount (camera);
	for (i = 0; i < max; i++) {
		if (ax203_file_present (camera, i))
			count = i + 1;
	}

	switch (camera->pl->frame_version) {
	case AX203_FIRMWARE_3_3_x:
	case AX203_FIRMWARE_3_4_x:
		return ax203_write_v3_3_x_v3_4_x_filecount (camera, count);
	case AX206_FIRMWARE_3_5_x:
	case AX3003_FIRMWARE_3_5_x:
		return ax203_write_v3_5_x_filecount (camera, count);
	}
	/* Never reached */
	return GP_ERROR_NOT_SUPPORTED;
}

static
int ax203_read_fileinfo(Camera *camera, int idx,
	struct ax203_fileinfo *fileinfo)
{
	CHECK (ax203_check_file_index (camera, idx))

	switch (camera->pl->frame_version) {
	case AX203_FIRMWARE_3_3_x:
	case AX203_FIRMWARE_3_4_x:
		return ax203_read_v3_3_x_v3_4_x_fileinfo (camera, idx,
							  fileinfo);
	case AX206_FIRMWARE_3_5_x:
		return ax206_read_v3_5_x_fileinfo (camera, idx, fileinfo);
	case AX3003_FIRMWARE_3_5_x:
		return ax3003_read_v3_5_x_fileinfo (camera, idx, fileinfo);
	}
	/* Never reached */
	return GP_ERROR_NOT_SUPPORTED;	
}

static
int ax203_write_fileinfo(Camera *camera, int idx,
	struct ax203_fileinfo *fileinfo)
{
	switch (camera->pl->frame_version) {
	case AX203_FIRMWARE_3_3_x:
	case AX203_FIRMWARE_3_4_x:
		return ax203_write_v3_3_x_v3_4_x_fileinfo (camera, idx,
							   fileinfo);
	case AX206_FIRMWARE_3_5_x:
		return ax206_write_v3_5_x_fileinfo (camera, idx, fileinfo);
	case AX3003_FIRMWARE_3_5_x:
		return ax3003_write_v3_5_x_fileinfo (camera, idx, fileinfo);
	}
	/* Never reached */
	return GP_ERROR_NOT_SUPPORTED;	
}

int
ax203_file_present(Camera *camera, int idx)
{
	struct ax203_fileinfo fileinfo;
	
	CHECK (ax203_read_fileinfo (camera, idx, &fileinfo))

	return fileinfo.present;
}

static int
ax203_decode_image(Camera *camera, char *src, int src_size, int **dest)
{
#ifdef HAVE_LIBGD
	int ret;
	unsigned int x, y, width, height, row_skip = 0;
	unsigned char *components[3];
#ifdef HAVE_LIBJPEG
	struct jpeg_decompress_struct dinfo;
	struct jpeg_error_mgr jderr;
	JSAMPLE row[camera->pl->width * 3];
	JSAMPROW row_pointer[1] = { row };
#endif

	switch (camera->pl->compression_version) {
	case AX203_COMPRESSION_YUV:
		ax203_decode_yuv (src, dest, camera->pl->width,
				  camera->pl->height);
		return GP_OK;
	case AX203_COMPRESSION_YUV_DELTA:
		ax203_decode_yuv_delta (src, dest, camera->pl->width,
					camera->pl->height);
		return GP_OK;
	case AX206_COMPRESSION_JPEG:
		/* Note this uses a modified tinyjpeg which was modified to
		   handle the AX206' custom JPEG format (the AX3003 uses
		   normal JPEG compression). */
		if (!camera->pl->jdec) {
			camera->pl->jdec = tinyjpeg_init ();
			if (!camera->pl->jdec)
				return GP_ERROR_NO_MEMORY;
		}
		
		/* Hack for width / heights which are not a multiple of 16 */
		if (camera->pl->width % 16 || camera->pl->height % 16) {
			width  = (camera->pl->width  + 15) & ~15;
			height = (camera->pl->height + 15) & ~15;
			htobe16a(src,     width);
			htobe16a(src + 2, height);
			row_skip = (width - camera->pl->width) * 3;
		}
		ret = tinyjpeg_parse_header (camera->pl->jdec,
					     (unsigned char *)src, src_size);
		if (ret) {
			gp_log (GP_LOG_ERROR, "ax203",
				"Error parsing header: %s",
				tinyjpeg_get_errorstring (camera->pl->jdec));
			return GP_ERROR_CORRUPTED_DATA;
		}
		if (!row_skip) {
			tinyjpeg_get_size (camera->pl->jdec, &width, &height);
			if ((int)width  != camera->pl->width ||
			    (int)height != camera->pl->height) {
				gp_log (GP_LOG_ERROR, "ax203",
					"Hdr dimensions %ux%u don't match lcd %dx%d",
					width, height,
					camera->pl->width, camera->pl->height);
				return GP_ERROR_CORRUPTED_DATA;
			}
		}
		ret = tinyjpeg_decode (camera->pl->jdec);
		if (ret) {
			gp_log (GP_LOG_ERROR, "ax203",
				"Error decoding JPEG data: %s",
				tinyjpeg_get_errorstring (camera->pl->jdec));
			return GP_ERROR_CORRUPTED_DATA;
		}
		tinyjpeg_get_components (camera->pl->jdec, components);
		for (y = 0; y < camera->pl->height; y++) {
			for (x = 0; x < camera->pl->width; x++) {
				dest[y][x] = gdTrueColor (components[0][0],
							  components[0][1],
							  components[0][2]);
				components[0] += 3;
			}
			components[0] += row_skip;
		}
		return GP_OK;
	case AX3003_COMPRESSION_JPEG:
#ifdef HAVE_LIBJPEG
		dinfo.err = jpeg_std_error (&jderr);
		jpeg_create_decompress (&dinfo);
		jpeg_mem_src (&dinfo, (unsigned char *)src, src_size);
		jpeg_read_header (&dinfo, TRUE);
		jpeg_start_decompress (&dinfo);
		if (dinfo.output_width  != camera->pl->width ||
		    dinfo.output_height != camera->pl->height ||
		    dinfo.output_components != 3 ||
		    dinfo.out_color_space != JCS_RGB) {
			gp_log (GP_LOG_ERROR, "ax203",
				"Wrong JPEG header parameters: %dx%d, "
				"%d components, colorspace: %d",
				dinfo.output_width, dinfo.output_height,
				dinfo.output_components,
				dinfo.out_color_space);
			return GP_ERROR_CORRUPTED_DATA;
		}

		for (y = 0; y < dinfo.output_height; y++) {
			jpeg_read_scanlines (&dinfo, row_pointer, 1);
			for (x = 0; x < dinfo.output_width; x++) {
				dest[y][x] = gdTrueColor (row[x * 3 + 0],
							  row[x * 3 + 1],
							  row[x * 3 + 2]);
			}
		}
		jpeg_finish_decompress (&dinfo);
		jpeg_destroy_decompress (&dinfo);
		return GP_OK;
#else
		gp_log(GP_LOG_ERROR,"ax203", "jpeg decompression not supported - no libjpeg during build");
		return GP_ERROR_NOT_SUPPORTED;
#endif
		break;
	}
#endif
	gp_log(GP_LOG_ERROR,"ax203", "GD decompression not supported - no libGD present during build");
	/* Never reached */
	return GP_ERROR_NOT_SUPPORTED;
}

/* Returns the number of bytes of dest used or a negative error code */
static int
ax203_encode_image(Camera *camera, int **src, char *dest, int dest_size)
{
#ifdef HAVE_LIBGD
	int size = ax203_filesize (camera);
#ifdef HAVE_LIBJPEG
	int x, y;
	struct jpeg_compress_struct cinfo;
	struct jpeg_error_mgr jcerr;
	JOCTET *jpeg_dest = NULL;
	unsigned long jpeg_size = 0;
	JSAMPLE row[camera->pl->width * 3];
	JSAMPROW row_pointer[1] = { row };
#endif

	if (dest_size < size)
		return GP_ERROR_FIXED_LIMIT_EXCEEDED;

	switch (camera->pl->compression_version) {
	case AX203_COMPRESSION_YUV:
		ax203_encode_yuv (src, dest, camera->pl->width,
				  camera->pl->height);
		return size;
	case AX203_COMPRESSION_YUV_DELTA:
		ax203_encode_yuv_delta (src, dest, camera->pl->width,
					camera->pl->height);
		return size;
	case AX206_COMPRESSION_JPEG:
#if defined(HAVE_LIBGD) && defined(HAVE_LIBJPEG)
		return ax206_compress_jpeg (camera, src,
					    (uint8_t *)dest, dest_size,
					    camera->pl->width,
					    camera->pl->height);
#else
		return GP_ERROR_NOT_SUPPORTED;
#endif
	case AX3003_COMPRESSION_JPEG:
#ifdef HAVE_LIBJPEG
		cinfo.err = jpeg_std_error (&jcerr);
		jpeg_create_compress (&cinfo);
		jpeg_mem_dest (&cinfo, &jpeg_dest, &jpeg_size);
		cinfo.image_width = camera->pl->width;
		cinfo.image_height = camera->pl->height;
		cinfo.input_components = 3;
		cinfo.in_color_space = JCS_RGB;
		jpeg_set_defaults (&cinfo);
		jpeg_start_compress (&cinfo, TRUE);
		for (y = 0; y < cinfo.image_height; y++) {
			for (x = 0; x < cinfo.image_width; x++) {
				int p = src[y][x];
				row[x * 3 + 0] = gdTrueColorGetRed(p);
				row[x * 3 + 1] = gdTrueColorGetGreen(p);
				row[x * 3 + 2] = gdTrueColorGetBlue(p);
			}
			jpeg_write_scanlines (&cinfo, row_pointer, 1);
		}
		jpeg_finish_compress (&cinfo);
		jpeg_destroy_compress (&cinfo);

		if (jpeg_size > dest_size) {
			free (jpeg_dest);
			gp_log (GP_LOG_ERROR, "ax203",
				"JPEG is bigger then buffer");
			return GP_ERROR_FIXED_LIMIT_EXCEEDED;
		}
		memcpy (dest, jpeg_dest, jpeg_size);
		free (jpeg_dest);
		/* Round size up to a multiple of 256 because of ax3003
		   abfs size granularity. */
                return (jpeg_size + 0xff) & ~0xff;
#else
		return GP_ERROR_NOT_SUPPORTED;
#endif
	}
	/* Never reached */
#endif
	gp_log(GP_LOG_ERROR,"ax203", "GD decompression not supported - no libGD present during build");
	return GP_ERROR_NOT_SUPPORTED;
}

static int
ax203_defrag_memory(Camera *camera)
{
	char **raw_pictures;
       	struct ax203_fileinfo *fileinfo;
	int i, count, ret = GP_OK;

	count = ax203_read_filecount (camera);
	if (count < 0) return count;

	raw_pictures = calloc (count, sizeof (char *));
	fileinfo     = calloc (count, sizeof (struct ax203_fileinfo));
	if (!raw_pictures || !fileinfo) {
		free (raw_pictures); free (fileinfo);
		gp_log (GP_LOG_ERROR, "ax203", "allocating memory");
		return GP_ERROR_NO_MEMORY;
	}

	/* First read all pictures in raw format */
	for (i = 0; i < count; i++) {
		ret = ax203_read_fileinfo (camera, i, &fileinfo[i]);
		if (ret < 0) goto cleanup;

		if (!fileinfo[i].present)
			continue;

		ret = ax203_read_raw_file(camera, i, &raw_pictures[i]);
		if (ret < 0) goto cleanup;
	}

	/* Delete all pictures from the frame */
	ret = ax203_delete_all (camera);
	if (ret < 0) goto cleanup;
	
	/* An last write them back (in one contineous block) */
	for (i = 0; i < count; i++) {
		if (!fileinfo[i].present)
			continue;

		ret = ax203_write_raw_file (camera, i, raw_pictures[i],
					    fileinfo[i].size);
		if (ret < 0) {
			gp_log (GP_LOG_ERROR, "ax203",
				"AAI error writing back images during "
				"defragmentation some images will be lost!");
			break;
		}
	}
cleanup:
	for (i = 0; i < count; i++)
		free (raw_pictures[i]);
	free (raw_pictures);
	free (fileinfo);

	return ret;
}

int
ax203_read_raw_file(Camera *camera, int idx, char **raw)
{
	struct ax203_fileinfo fileinfo;
	int ret;

	*raw = NULL;

	CHECK (ax203_read_fileinfo (camera, idx, &fileinfo))

	if (!fileinfo.present) {
		gp_log (GP_LOG_ERROR, "ax203",
			"trying to read a deleted file");
		return GP_ERROR_BAD_PARAMETERS;
	}

	/* Allocate 1 extra byte as tinyjpeg's huffman decoding sometimes
	   reads a few bits more then it needs */
	*raw = malloc (fileinfo.size + 1);
	if (!*raw) {
		gp_log (GP_LOG_ERROR, "ax203", "allocating memory");
		return GP_ERROR_NO_MEMORY;
	}

	ret = ax203_read_mem (camera, fileinfo.address, *raw, fileinfo.size);
	if (ret < 0) {
		free (*raw);
		*raw = NULL;
		return ret;
	}

	return fileinfo.size;
}

int
ax203_read_file(Camera *camera, int idx, int **rgb24)
{
	int ret;
	char *src;

	ret = ax203_read_raw_file (camera, idx, &src);
	if (ret < 0) return ret;

	ret = ax203_decode_image (camera, src, ret + 1, rgb24);
	free(src);

	return ret;
}

/* ax203_write_file() helper functions */

/* The picture table in ABFS does not necessarily lists the pictures
   in the order they are stored in memory, but when looking for a hole
   in memory to store a new picture we need a list of used memory blocks
   in memory order. So we build one here. */
static int
ax203_fileinfo_cmp(const void *p1, const void *p2)
{
	const struct ax203_fileinfo *f1 = p1;
	const struct ax203_fileinfo *f2 = p2;
	return f1->address - f2->address;
}

static int
ax203_build_used_mem_table(Camera *camera, struct ax203_fileinfo *table)
{
	int i, count, found = 0;
       	struct ax203_fileinfo fileinfo;

	count = ax203_read_filecount (camera);
	if (count < 0) return count;

	/* The beginning of the memory is used by the CD image and stuff */
	fileinfo.address = 0;
	switch (camera->pl->frame_version) {
	case AX203_FIRMWARE_3_3_x:
	case AX203_FIRMWARE_3_4_x:
		fileinfo.size = camera->pl->fs_start + AX203_PICTURE_OFFSET;
		break;
	case AX206_FIRMWARE_3_5_x:
		fileinfo.size = camera->pl->fs_start + AX206_PICTURE_OFFSET;
		break;
	case AX3003_FIRMWARE_3_5_x:
		fileinfo.size = camera->pl->fs_start + AX3003_PICTURE_OFFSET;
		break;
	}
	fileinfo.present = 1;
	table[found++] = fileinfo;

	for (i = 0; i < count; i++) {
		CHECK (ax203_read_fileinfo (camera, i, &fileinfo))
		if (!fileinfo.present)
			continue;
		table[found++] = fileinfo;
	}
	qsort (table, found, sizeof(struct ax203_fileinfo),
	       ax203_fileinfo_cmp);

	/* Add a last memory used region which starts at the end of memory
	   the size does not matter as this is the last entry. */
	switch (camera->pl->frame_version) {
	case AX203_FIRMWARE_3_3_x:
	case AX203_FIRMWARE_3_4_x:
	case AX206_FIRMWARE_3_5_x:
		fileinfo.address = camera->pl->mem_size;
		break;
	case AX3003_FIRMWARE_3_5_x:
		fileinfo.address = camera->pl->mem_size - AX3003_BL_SIZE;
		break;
	}

	fileinfo.size    = 0;
	fileinfo.present = 1;
	table[found++] = fileinfo;

	return found;
}

/* Find a free slot in the ABFS table to store the fileinfo */
static int
ax203_find_free_abfs_slot(Camera *camera)
{
	int i, max;
	struct ax203_fileinfo fileinfo;

	max = ax203_max_filecount (camera);
	for (i = 0; i < max; i++) {
		CHECK (ax203_read_fileinfo (camera, i, &fileinfo))
		if (!fileinfo.present)
			return i;
	}

	gp_log (GP_LOG_ERROR, "ax203", "no free slot in ABFS ??");
	return GP_ERROR_NO_SPACE;
}

int
ax203_write_raw_file(Camera *camera, int idx, char *buf, int size)
{
	struct ax203_fileinfo fileinfo;
	struct ax203_fileinfo used_mem[AX203_ABFS_SIZE / 2];
	int i, hole_size, used_mem_count, prev_end, free;

retry:
	used_mem_count = ax203_build_used_mem_table (camera, used_mem);
	if (used_mem_count < 0) return used_mem_count;

	/* Try to find a large enough "hole" in the memory */
	for (i = 1, free = 0; i < used_mem_count; i++, free += hole_size) {
		prev_end = used_mem[i - 1].address + used_mem[i - 1].size;
		hole_size = used_mem[i].address - prev_end;
		if (hole_size)
			GP_DEBUG ("found a hole at: %08x, of %d bytes "
				  "(need %d)\n", prev_end, hole_size, size);
		if (hole_size >= size) {
			/* bingo we have a large enough hole */
			fileinfo.address = prev_end;
			fileinfo.size    = size;
			fileinfo.present = 1;
			CHECK (ax203_write_fileinfo (camera, idx,
						     &fileinfo))
			CHECK (ax203_update_filecount (camera))
			CHECK (ax203_write_mem (camera, fileinfo.address,
						buf, size))
			return GP_OK;
		}
	}

	if (free >= size) {
		gp_log (GP_LOG_DEBUG, "ax203",
			"not enough contineous freespace to add file, "
			"defragmenting memory");
		CHECK (ax203_defrag_memory (camera))
		goto retry;
	}

	gp_log (GP_LOG_ERROR, "ax203", "not enough freespace to add file");
	return GP_ERROR_NO_SPACE;
}

int
ax203_write_file(Camera *camera, int **rgb24)
{
	const int buf_size = camera->pl->width * camera->pl->height;
	char buf[buf_size];
	int size, abfs_slot;

	size = ax203_encode_image (camera, rgb24, buf, buf_size);
	if (size < 0) return size;

	abfs_slot = ax203_find_free_abfs_slot (camera);
	if (abfs_slot < 0) return abfs_slot;

	CHECK (ax203_write_raw_file (camera, abfs_slot, buf, size))

	return GP_OK;
}

int
ax203_delete_file(Camera *camera, int idx)
{
	struct ax203_fileinfo fileinfo;

	CHECK (ax203_read_fileinfo (camera, idx, &fileinfo))

	if (!fileinfo.present) {
		gp_log (GP_LOG_ERROR, "ax203",
			"trying to delete an already deleted file");
		return GP_ERROR_BAD_PARAMETERS;
	}

	fileinfo.present = 0;
	CHECK (ax203_write_fileinfo (camera, idx, &fileinfo))
	CHECK (ax203_update_filecount (camera))

	return GP_OK;
}

int
ax203_delete_all(Camera *camera)
{
	char buf[AX203_ABFS_SIZE];
	int size, file0_offset = 0;

	switch (camera->pl->frame_version) {
	case AX203_FIRMWARE_3_3_x:
	case AX203_FIRMWARE_3_4_x:
		file0_offset = AX203_ABFS_FILE_OFFSET (0);
		break;
	case AX206_FIRMWARE_3_5_x:
		file0_offset = AX206_ABFS_FILE_OFFSET (0);
		break;
	case AX3003_FIRMWARE_3_5_x:
		file0_offset = AX3003_ABFS_FILE_OFFSET (0);
		break;
	}

	size = AX203_ABFS_SIZE - file0_offset;
	memset(buf, 0, size);
	CHECK (ax203_write_mem (camera, camera->pl->fs_start + file0_offset,
				buf, size))
	CHECK (ax203_update_filecount (camera))

	return GP_OK;
}

/* bss = block start sector */
static int
ax203_commit_block_4k(Camera *camera, int bss)
{
	int block_sector_size = SPI_EEPROM_BLOCK_SIZE / SPI_EEPROM_SECTOR_SIZE;
	int i;

	for (i = 0; i < block_sector_size; i++) {
		if (!camera->pl->sector_dirty[bss + i])
			continue;

		CHECK (ax203_erase4k_sector (camera, bss + i))
		CHECK (ax203_write_sector (camera, bss + i,
					   camera->pl->mem +
					   (bss + i) *
					   SPI_EEPROM_SECTOR_SIZE))
		camera->pl->sector_dirty[bss + i] = 0;
	}
	return GP_OK;
}

static int
ax203_commit_block_64k(Camera *camera, int bss)
{
	int block_sector_size = SPI_EEPROM_BLOCK_SIZE / SPI_EEPROM_SECTOR_SIZE;
	int i;

	/* Make sure we have read the entire block before erasing it !! */
	for (i = 0; i < block_sector_size; i++)
		CHECK (ax203_check_sector_present (camera, bss + i))

	/* Erase the block */
	CHECK (ax203_erase64k_sector (camera, bss))

	/* And re-program all sectors in the block */
	for (i = 0; i < block_sector_size; i++) {
		CHECK (ax203_write_sector (camera, bss + i,
					   camera->pl->mem +
					   (bss + i) *
					   SPI_EEPROM_SECTOR_SIZE))
		camera->pl->sector_dirty[bss + i] = 0;
	}
	return GP_OK;
}

/* The ax3003 and the ax206 with AAI capable eeproms can program 64k at once,
   this is probably done by special handling inside the firmware. */
static int
ax203_commit_block_64k_at_once(Camera *camera, int bss)
{
	int block_sector_size = SPI_EEPROM_BLOCK_SIZE / SPI_EEPROM_SECTOR_SIZE;
	int i, address = bss * SPI_EEPROM_SECTOR_SIZE;
	int checksum = 0;
	char extra_arg = 0;

	/* The ax206 (and we assume the same applies for ax203, untested!):
	   1) Needs the last byte of the scsi cmd to be 2 to enable 64k pp
	   2) Has an extra checksum function, which we might as well use. */
	if (camera->pl->frame_version != AX3003_FIRMWARE_3_5_x) {
		extra_arg = 2;
		checksum = 1;
	}

	/* Make sure we have read the entire block before erasing it !! */
	for (i = 0; i < block_sector_size; i++)
		CHECK (ax203_check_sector_present (camera, bss + i))

	if (!camera->pl->block_protection_removed) {
		CHECK (ax203_eeprom_write_enable (camera))
		CHECK (ax203_eeprom_clear_block_protection (camera))
		CHECK (ax203_eeprom_wait_ready (camera))
		camera->pl->block_protection_removed = 1;
	}

	/* Erase the block */
	CHECK (ax203_erase64k_sector (camera, bss))

	/* program the block in one large 64k page write */
	CHECK (ax203_eeprom_write_enable (camera))
	CHECK (ax203_eeprom_program_page (camera, address,
					  camera->pl->mem + address,
					  SPI_EEPROM_BLOCK_SIZE, extra_arg))
	CHECK (ax203_eeprom_wait_ready (camera))

	/* and ask the device to verify the write with a checksum */
	if (checksum) {
		checksum = 0;
		for (i = address; i < (address + SPI_EEPROM_BLOCK_SIZE); i++)
			checksum += ((uint8_t *)camera->pl->mem)[i];
		checksum &= 0xffff;

		i = ax203_get_checksum(camera, address, SPI_EEPROM_BLOCK_SIZE);
		if (i < 0)
			return i;
		if (i != checksum) {
			gp_log (GP_LOG_ERROR, "ax203",
				"checksum mismatch after programming "
				"expected %04x, got %04x\n", checksum, i);
			return GP_ERROR_IO;
		}
	}

	for (i = 0; i < block_sector_size; i++)
		camera->pl->sector_dirty[bss + i] = 0;

	return GP_OK;
}

int
ax203_commit(Camera *camera)
{
	int i, j;
	int mem_sector_size = camera->pl->mem_size / SPI_EEPROM_SECTOR_SIZE;
	int block_sector_size = SPI_EEPROM_BLOCK_SIZE / SPI_EEPROM_SECTOR_SIZE;
	int dirty_sectors;

	/* We first check each 64k block for dirty sectors. If the block
	   contains dirty sectors, decide wether to use 4k sector erase
	   commands (if the eeprom supports it), or to erase and reprogram
	   the entire block */
	for (i = 0; i < mem_sector_size; i += block_sector_size) {
		dirty_sectors = 0;
		for (j = 0; j < block_sector_size; j++)
			if (camera->pl->sector_dirty[i + j])
				dirty_sectors++;

		/* If we have no dirty sectors in this block continue */
		if (!dirty_sectors)
			continue;

		if (camera->pl->pp_64k)
			CHECK (ax203_commit_block_64k_at_once (camera, i))
		/* There are 16 4k sectors per 64k block, when we need to
		   program 12 or more sectors, programming the entire block
		   becomes faster */
		else if (dirty_sectors < 12 && camera->pl->has_4k_sectors)
			CHECK (ax203_commit_block_4k (camera, i))
		else
			CHECK (ax203_commit_block_64k (camera, i))
	}
	return GP_OK;
}

static int
ax203_init(Camera *camera)
{
	GP_DEBUG ("ax203_init called");

	camera->pl->mem = malloc(camera->pl->mem_size);
	if (!camera->pl->mem)
		return GP_ERROR_NO_MEMORY;

	CHECK (ax203_read_parameter_block (camera))

	if ((camera->pl->width % 4) || (camera->pl->height % 4)) {
		gp_log (GP_LOG_ERROR, "ax203",
			"lcd width and height must be a multiple of 4");
		return GP_ERROR_IO;
	}

	return GP_OK;
}

static void
ax203_exit(Camera *camera)
{
	if (camera->pl->jdec) {
		tinyjpeg_free (camera->pl->jdec);
		camera->pl->jdec = NULL;
	}
	free (camera->pl->mem);
	camera->pl->mem = NULL;
}

int
ax203_open_device(Camera *camera)
{
	char buf[64];
	uint32_t id;
	int i;

	CHECK (ax203_get_version (camera, buf))
	GP_DEBUG ("Appotech ax203 picframe firmware version: %s", buf);

	/* Not sure if this is necessary, but the windows software does it */
	CHECK (ax203_eeprom_release_from_deep_powerdown (camera))
	CHECK (ax203_eeprom_device_identification (camera, buf))
	/*id = le32atoh((uint8_t *)buf);*/
	id =	 (uint8_t)buf[0]	|
		((uint8_t)buf[1] << 8)	|
		((uint8_t)buf[2] << 16)	|
		((uint8_t)buf[3] << 24);
	for (i = 0; ax203_eeprom_info[i].name; i++) {
		if (ax203_eeprom_info[i].id == id)
			break;
	}
	
	if (!ax203_eeprom_info[i].name) {
		gp_log (GP_LOG_ERROR, "ax203", "unknown eeprom id: %08x", id);
		return GP_ERROR_MODEL_NOT_FOUND;
	}

	camera->pl->mem_size       = ax203_eeprom_info[i].mem_size;
	camera->pl->has_4k_sectors = ax203_eeprom_info[i].has_4k_sectors;
	camera->pl->pp_64k         = ax203_eeprom_info[i].pp_64k;
	if (camera->pl->frame_version == AX3003_FIRMWARE_3_5_x)
		camera->pl->pp_64k = 1;
	GP_DEBUG (
		"%s EEPROM found, capacity: %d, has 4k sectors: %d, pp_64k %d",
		ax203_eeprom_info[i].name, camera->pl->mem_size,
		camera->pl->has_4k_sectors, camera->pl->pp_64k);

	return ax203_init (camera);
}

int
ax203_open_dump(Camera *camera, const char *dump)
{
	camera->pl->mem_dump = fopen(dump, "r+");
	if (!camera->pl->mem_dump) {
		gp_log (GP_LOG_ERROR, "ax203", "opening memdump file: %s: %s",
			dump, strerror(errno));
		return GP_ERROR_IO_INIT;
	}

	if (fseek (camera->pl->mem_dump, 0, SEEK_END)) {
		gp_log (GP_LOG_ERROR, "ax203", "seeking memdump file: %s: %s",
			dump, strerror(errno));
		return GP_ERROR_IO_INIT;
	}
	camera->pl->mem_size = ftell (camera->pl->mem_dump);
	camera->pl->has_4k_sectors = 1;

	return ax203_init (camera);
}

void ax203_close(Camera *camera)
{
	ax203_exit (camera);
	if (camera->pl->mem_dump) {
		fclose (camera->pl->mem_dump);
		camera->pl->mem_dump = NULL;
	}
}

int
ax203_get_mem_size(Camera *camera)
{
	return camera->pl->mem_size;
}

int
ax203_get_free_mem_size(Camera *camera)
{
	struct ax203_fileinfo used_mem[AX203_ABFS_SIZE / 2];
	int i, used_mem_count, prev_end, free = 0;

	used_mem_count = ax203_build_used_mem_table (camera, used_mem);
	if (used_mem_count < 0) return used_mem_count;

	/* Add the size of all holes in memory together */
	for (i = 1; i < used_mem_count; i++) {
		prev_end = used_mem[i - 1].address + used_mem[i - 1].size;
		free += used_mem[i].address - prev_end;
	}

	return free;
}