summaryrefslogtreecommitdiff
path: root/camlibs/digigr8/digigr8.c
blob: d8caf120babbe0b097be098e0a46898a88b81194 (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
/*
 * digigr8.c
 *
 * Part of libgphoto2/camlibs/digigr8
 * Here are the functions which are needed to get data from the camera.  
 *
 * Copyright (c) 2005 Theodore Kilgore <kilgota@auburn.edu>
 * Camera library support under libgphoto2.1.1 for camera(s) 
 * with chipset from Service & Quality Technologies, Taiwan. 
 * Cameras supported by this driver have Product ID 0x905C, 0x9050, or.
 * 0x913D.
 *
 * Licensed under GNU Lesser General Public License, as part of Gphoto
 * camera support project. For a copy of the license, see the file 
 * COPYING in the main source tree of libgphoto2.
 */    

#include <config.h>


#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <math.h>
#ifdef OS2
#include <db.h>
#endif

#include <gphoto2/gphoto2.h>
#include <gphoto2/gphoto2-port.h>
#include "digigr8.h"

#define GP_MODULE "digigr8" 

#define SQWRITE gp_port_usb_msg_write
#define SQREAD  gp_port_usb_msg_read

int 
digi_init (GPPort *port, CameraPrivateLibrary *priv)
{
	char c[0x14];
	int i,j=0;
	unsigned char *catalog = calloc(0x4010,1);
	unsigned char *catalog_tmp;

	if (!catalog) return GP_ERROR_NO_MEMORY;

	SQWRITE (port, 0x0c, 0x14f4, 0x0, NULL, 0);
	SQREAD (port, 0x0c, 0xf5, 0x00, c, 0x14);
	SQWRITE (port, 0x0c, 0x1440, 0x110f, NULL, 0);
	digi_reset (port);
	SQWRITE (port, 0x0c, 0x14f0, 0x0, NULL, 0);

	gp_port_read (port, c, 0x14); 	
	digi_reset(port);
	SQWRITE (port, 0x0c, 0x20, 0x40, NULL, 0);
	gp_port_read(port, (char *)catalog, 0x4000); /* We need 16 bytes for each photo. */
	digi_reset (port);

	/* The first occurence of a zero denotes end of files entries */
	for (i=0; i<0x4000 && catalog[i]; i+=16);
	priv->nb_entries = i>>4;
	catalog_tmp = realloc(catalog, i+16);
	if (!catalog_tmp) return GP_ERROR_NO_MEMORY;
	catalog = catalog_tmp;
	memset (catalog+i, 0, 16);
	if (i) {
		/*
		 * 0x913c cameras allow individual photo deletion. This causes 
		 * the relevant catalog line to start with 0x64. So the related
		 * lines of config data must be removed, and the deleted 
		 * images need to be cast out from the count.
		 */

		for (j=0; j<i; j+=16) {
			if ((!catalog[j]) || (catalog[j] == 0x64)) {
				memmove(catalog+j, catalog+j+16, i-j);
				priv->nb_entries -- ;
			}
		}
		priv->catalog = catalog;
	} else {
		free (catalog);
		priv->catalog = NULL;	/* We just have freed catalog_tmp */
	}

	digi_reset (port);
	priv->last_fetched_entry = -1;

	priv->init_done=1;
	return GP_OK;
}


unsigned int
digi_get_comp_ratio (CameraPrivateLibrary *priv, int entry)
{
	switch (priv->catalog[16*entry]) {
	case 0x61:
	case 0x62:
	case 0x63:
	case 0x76: return 1;
	case 0x41:
	case 0x42:
	case 0x43:
	case 0x52:
	case 0x53:
	case 0x56: 
	case 0x72: return 0;
	default:
		GP_DEBUG ("Your camera has unknown resolution settings.\n");
			return GP_ERROR;
	}
}

unsigned int
digi_get_data_size (CameraPrivateLibrary *priv, int entry)
{
	return ((priv->catalog[16*entry + 6] <<16) 
		| (priv->catalog[16*entry + 5] <<8) 
		| (priv->catalog[16*entry + 4] ) );
}

unsigned int
digi_get_picture_width (CameraPrivateLibrary *priv, int entry)
{
	switch (priv->catalog[16*entry]) {  
	case 0x41:
	case 0x52:
	case 0x61: return 352;
	case 0x42:
	case 0x62:
	case 0x72: return 176;
	case 0x43:
	case 0x53:
	case 0x63: return 320;
	case 0x56:
	case 0x76: return 640;
	default:
		GP_DEBUG ("Your pictures have unknown width.\n");
			return 0;
	}
}

int
digi_rewind (GPPort *port, CameraPrivateLibrary *priv)
{
	static char dummy_buf[0x4000];
	
	
	GP_DEBUG("REWIND cam's data pointer");

	/* One has to read the catalog to rewind the data stream...
	 * I don't know if it's by design. There ought to be something better to do... :-/
	 */

	digi_reset(port);
	SQWRITE (port, 0x0c, 0x20, 0x40, NULL, 0); /* Access config */
	gp_port_read(port, dummy_buf, 0x4000); 
	digi_reset (port);

	priv->last_fetched_entry = -1;
	return GP_OK;
}


int
digi_reset (GPPort *port)
{
	/* Release current register */
	SQWRITE (port, 0x0c, 0xa0, 0x00, NULL, 0);

	return GP_OK;
}


int
digi_read_picture_data (GPPort *port, unsigned char *data, unsigned int size, int n )
{
	unsigned int remainder = size % 0x8000;
	unsigned int offset = 0;
	int ret;

	if (!n) {
		SQWRITE (port, 0x0c, 0x30, 0x00, NULL, 0); 
	}	
	while ((offset + 0x8000 < size)) {
		ret = gp_port_read (port, (char *)data + offset, 0x8000);
		if (ret < GP_OK) return ret;
		offset = offset + 0x8000;
	}
	ret = gp_port_read (port, (char *)data + offset, remainder);
	if (ret < GP_OK) return ret;

	return GP_OK;
} 

int digi_delete_all     (GPPort *port, CameraPrivateLibrary *priv) 
{
	int size;
	int num_pics;
	unsigned char get_size[0x50];
	unsigned char *junk=NULL;

	num_pics = priv->nb_entries;
	GP_DEBUG("number of entries is %i\n", num_pics);
	digi_reset (port);
	digi_reset (port);
	if(!num_pics) {
		GP_DEBUG("Camera is already empty!\n");
		return GP_OK;
	}
	SQWRITE (port, 0x0c, 0x1440, 0x110f, NULL, 0);
	if ((gp_port_read(port, (char *)get_size, 0x50)!=0x50)) {
		GP_DEBUG("Error in reading data\n");
		return GP_ERROR;
	}
	GP_DEBUG("get_size[0x40] = 0x%x\n", get_size[0x40]);
	size = get_size[0x40]|(get_size[0x41]<<8)|(get_size[0x42]<<16)|
						(get_size[0x43]<<24);
	GP_DEBUG("size = 0x%x\n", size);
	if(size <= 0xff) {
		GP_DEBUG("No size to read. This will not work.\n");
		digi_reset(port);
		return GP_OK;
	}
	junk = malloc(size);
	if(! junk) {
		GP_DEBUG("allocation of junk space failed\n");
		return GP_ERROR_NO_MEMORY;
	}
	gp_port_read(port, (char *)junk, size);
	free(junk); 
	digi_reset (port);

	return GP_OK;
}