summaryrefslogtreecommitdiff
path: root/camlibs/pccam300/pccam300.c
blob: 0530d1323a22571c3bc3bb5c1fd42a4412d15129 (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
/****************************************************************/
/* pccam300.c - Gphoto2 library for the Creative PC-CAM 300     */
/*                                                              */
/* Authors: Till Adam <till@adam-lilienthal.de>                 */
/*          Miah Gregory <mace@darksilence.net>                 */
/*                                                              */
/* This library is free software; you can redistribute it       */
/* and/or modify it under the terms of the GNU Library General  */
/* Public License as published by the Free Software Foundation; */
/* either version 2 of the License, or (at your option) any     */
/* later version.                                               */
/*                                                              */
/* This library 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 Library General Public License for     */
/* more details.                                                */
/*                                                              */
/* You should have received a copy of the GNU Library General   */
/* Public License along with this library; if not, write to the */
/* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,*/
/* Boston, MA  02110-1301  USA					*/
/****************************************************************/

#include <config.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#include <_stdint.h>

#include "pccam300.h"

#include <gphoto2/gphoto2.h>
#include <gphoto2/gphoto2-port.h>
#include <gphoto2/gphoto2-port-log.h>

#define GP_MODULE "pccam300"

#ifdef ENABLE_NLS
#  include <libintl.h>
#  undef _
#  define _(String) dgettext (PACKAGE, String)
#  ifdef gettext_noop
#    define N_(String) gettext_noop (String)
#  else
#    define N_(String) (String)
#  endif
#else
#  define _(String) (String)
#  define N_(String) (String)
#endif

/*
 * waits until the status value is 0 or 8. 
 * if status == 0xb0 or 0x40 we will wait some more
 */
static int
pccam300_wait_for_status (GPPort *port)
{
	int ret;
	int retries = 20;
	unsigned char status = 1;

	while (status != 0x00 && retries--) {
		gp_port_set_timeout (port, 3000);
		ret = gp_port_usb_msg_read (port, 0x06, 0x00, 0x00, &status, 1);
		if (status == 0 || status == 8)
			return GP_OK;
		if (status == 0xb0) {
			gp_port_set_timeout (port, 200000);
			ret = gp_port_usb_msg_read (port, 0x06, 0x00, 0x00,
						    &status, 1);
		}
		if (status == 0x40) {
			gp_port_set_timeout (port, 400000);
			ret = gp_port_usb_msg_read (port, 0x06, 0x00, 0x00,
						    &status, 1);
		}
	}
	return GP_ERROR;
}

/*
 * FIXME
 */
int
pccam300_delete_file (GPPort *port, GPContext *context, int index)
{
	return GP_ERROR_NOT_SUPPORTED;
}

int
pccam300_delete_all (GPPort *port, GPContext *context)
{

	CHECK (gp_port_usb_msg_write (port, 0x4, 0x0, 0x0, NULL, 0));
	CHECK (pccam300_wait_for_status (port));
	CHECK (gp_port_usb_msg_write (port, 0x9, 0x0, 0x1, NULL, 0));
	CHECK (pccam300_wait_for_status (port));
	return GP_OK;
}

int
pccam300_get_filecount (GPPort *port, int *filecount)
{
	uint8_t response;

	gp_port_set_timeout (port, 400000);
	CHECK (gp_port_usb_msg_read (port, 0x08, 0x00, 0x00, &response, 0x01));
	*filecount = response;
	return GP_OK;
}

int
pccam300_get_filesize (GPPort *port, unsigned int index,
                       unsigned int *filesize)
{
	uint8_t response[3];
	uint16_t i = index;

	gp_port_set_timeout (port, 400000);
	CHECK (gp_port_usb_msg_read (port, 0x08, i, 0x0001, response, 0x03));
	*filesize = (response[0] & 0xff)
		+ (response[1] & 0xff) * 0x100 + (response[2] & 0xff) * 0x10000;

	return GP_OK;
}

int
pccam300_get_mem_info (GPPort *port, GPContext *context, int *totalmem,
                       int *freemem)
{
	unsigned char response[4];
	int ret;

	gp_port_set_timeout (port, 400000);
	ret = gp_port_usb_msg_read (port, 0x60, 0x00, 0x02, response, 0x04);
	*totalmem = response[2] * 65536 + response[1] * 256 + response[0];
	CHECK (pccam300_wait_for_status (port));
	ret = gp_port_usb_msg_read (port, 0x60, 0x00, 0x03, response, 0x04);
	*freemem = response[2] * 65536 + response[1] * 256 + response[0];
	CHECK (pccam300_wait_for_status (port));
	return GP_OK;
}


int
pccam300_get_file (GPPort *port, GPContext *context, int index,
                   unsigned char **data, unsigned int *size,
                   unsigned int *type)
{
	int data_size, r;
	uint8_t *buf = NULL;

	/* This is somewhat strange, but works, and the win driver does the
	 * same. Apparently requesting the file size twice triggers the
	 * download of the file. */
	CHECK (pccam300_get_filesize (port, index, &data_size));
	CHECK (pccam300_get_filesize (port, index, &data_size));
	
	/* The jpeg header we will download after the data itself is 623
	 * bytes long. The first 0x200 bytes of the data appear to be
	 * garbage, they are overwritten by the header. */
	*size = data_size + 623 - 0x200;
	if (!(buf = malloc (*size)))
		return GP_ERROR_NO_MEMORY;

	/* Read the data into the buffer overlapping the header area by
	 * 0x200 bytes. */
	r = gp_port_read (port, buf + 623 - 0x200, data_size);
	if (r < GP_OK) {
		free (buf);
		return r;
	}

	/* FIXME: if we find a RIFF marker in the data stream, assume that
	 *        this is an avi, for now
	 *        NWG: Sat 18th January 2003.
	 */
	if (buf[0x243] == 'R' && buf[0x244] == 'I' &&
	    buf[0x245] == 'F' && buf[0x246] == 'F')
	{
		/* maybe an avi, but could be a wav too */
		*type = PCCAM300_MIME_AVI;
		memmove(buf, buf + 623 - 0x200, data_size);
		*size = data_size;
	} else {
		/* offset 0x8 in the downloaded data contains the identifier
		 * we need to request the correct header.
		 */
		CHECK(gp_port_usb_msg_read(port, 0x0b, buf[623 - 0x200 + 8],
		                           0x3, buf, 623));
		*type = PCCAM300_MIME_JPEG;
	}
	*data = buf;
	return GP_OK;
}


int
pccam300_init (GPPort *port, GPContext *context)
{
	CHECK (gp_port_usb_msg_write (port, 0xe0, 0x00, 0x1, NULL, 0x00));
	CHECK (pccam300_wait_for_status (port));

	return GP_OK;
}