summaryrefslogtreecommitdiff
path: root/camlibs/pccam600/pccam600.c
blob: 2f60e60568e58648754ea99d4d668307ae04a9e5 (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
/****************************************************************/
/* pccam600.c - Gphoto2 library for the Creative PC-CAM600      */
/*                                                              */
/*                                                              */
/* Author: Peter Kajberg <pbk@odense.kollegienet.dk>            */
/*                                                              */
/* 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.                                                */
/*                                                              */
/* Please notice that camera commands was sniffed by use of a   */
/* usbsniffer under windows. This is an experimental driver and */
/* a work in progess(I hope :))                                 */
/*                                                              */
/* 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 "pccam600.h"

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

#define GP_MODULE "pccam600"

#ifdef ENABLE_NLS
#  include <libintl.h>
#  undef _
#  define _(String) dgettext (GETTEXT_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 pccam600_wait_for_status(GPPort *port){
  unsigned char status = 1;
  while(status != 0x00){
    gp_port_set_timeout(port,3000);
    CHECK(gp_port_usb_msg_read(port,0x06,0x00,0x00,(char*)&status,1));
    if (status == 0 || status == 8)
      return GP_OK;
    if (status == 0xb0){
      gp_port_set_timeout(port,200000);
      CHECK(gp_port_usb_msg_read(port,0x06,0x00,0x00,(char*)&status,1));
    }
    if (status == 0x40){
      gp_port_set_timeout(port,400000);
      CHECK(gp_port_usb_msg_read(port,0x06,0x00,0x00,(char*)&status,1));
    }
  }
  return GP_ERROR;
}

/*
 *Deletes a file. The first file has index = 2.
 */
int pccam600_delete_file(GPPort *port, GPContext *context, int index){
  unsigned char response[4];
  index = index + 2;
  gp_port_set_timeout(port,200000);
  CHECK(gp_port_usb_msg_write(port,0x09,index,0x1001,NULL,0x00));
  CHECK(pccam600_wait_for_status(port));
  gp_port_set_timeout(port, 400000);
  CHECK(gp_port_usb_msg_read(port,0x60,0x00,0x03,(char*)response,0x04));
  CHECK(pccam600_wait_for_status(port));
  CHECK(gp_port_usb_msg_read(port,0x60,0x00,0x04,(char*)response,0x04));
  CHECK(pccam600_wait_for_status(port));
  return GP_OK;
}

int pccam600_get_mem_info(GPPort *port, GPContext *context, int *totalmem,
			    int *freemem)
{
  unsigned char response[4];
  gp_port_set_timeout(port, 400000);
  CHECK(gp_port_usb_msg_read(port,0x60,0x00,0x03,(char*)response,0x04));
  *totalmem = response[2]*65536+response[1]*256+response[0];
  CHECK(pccam600_wait_for_status(port));
  CHECK(gp_port_usb_msg_read(port,0x60,0x00,0x04,(char*)response,0x04));
  *freemem = response[2]*65536+response[1]*256+response[0];
  CHECK(pccam600_wait_for_status(port));
  return GP_OK;
}

/*
 *
 */
int pccam600_get_file_list(GPPort *port, GPContext *context){
  unsigned char response[4];
  int nr_of_blocks;
  gp_port_set_timeout(port,500);
  CHECK(gp_port_usb_msg_write(port,0x08,0x00,0x1021,NULL,0x00));
  CHECK(pccam600_wait_for_status(port));
  gp_port_set_timeout(port, 200000);
  CHECK(gp_port_usb_msg_write(port,0x08,0x00,0x1021,NULL,0x00));
  CHECK(pccam600_wait_for_status(port));
  CHECK(gp_port_usb_msg_read(port,0x08,0x00,0x1000,(char*)response,0x04));
  nr_of_blocks = response[2]*256+response[1];
  if (nr_of_blocks == 0){
    gp_log(GP_LOG_DEBUG,"pccam600 library: pccam600_get_file_list",
	   "nr_of_blocks is 0");
    gp_context_error(context,_("pccam600_init: Expected > %d blocks got %d"),
		 0,nr_of_blocks);
    return GP_ERROR;
  }
  return nr_of_blocks / 2;
}

int pccam600_get_file(GPPort *port, GPContext *context, int index){
  unsigned char response[4];
  int nr_of_blocks;
  index = index + 2;
  if (index < 2)  {
    gp_context_error(context,
		 _("pccam600_get_file:got index %d but expected index > %d"),
		 index,2);
    return GP_ERROR;
  }
  gp_port_set_timeout(port,200000);
  CHECK(gp_port_usb_msg_read(port,0x08,index,0x1001,(char*)response,0x04));
  gp_port_set_timeout(port,3000);
  CHECK(gp_port_usb_msg_write(port,0x04,0x00,0x00,NULL,0x00));
  CHECK(pccam600_wait_for_status(port));
  gp_port_set_timeout(port,200000);
  CHECK(gp_port_usb_msg_read(port,0x08,index,0x1002,(char*)response,0x04));
  CHECK(gp_port_usb_msg_read(port,0x08,index,0x1001,(char*)response,0x04));
  nr_of_blocks = response[2]*256+response[1];
  if (nr_of_blocks == 0){
    gp_log(GP_LOG_DEBUG,
	   "pccam600 library: pccam600_get_file","nr_of_msg is 0");
    gp_context_error(context,_("pccam600_init: Expected > %d blocks got %d"),
		 0,nr_of_blocks);
    return GP_ERROR;
  }
  return nr_of_blocks / 2;
}

/*
 *Reads bulk data from the camera in 512bytes chunks.
 */
int pccam600_read_data(GPPort *port, unsigned char *buffer){
  gp_port_set_timeout(port,500);
  CHECK(gp_port_read(port,(char*)buffer,512));
  return GP_OK;
}

/*
 *
 */
int pccam600_close(GPPort *port, GPContext *context){
  int ret;
  gp_port_set_timeout(port,500);
  ret = gp_port_usb_msg_write(port,0x08,0x00,0xf0,NULL,0x00);
  if (ret < 0) {
    gp_context_error(context,
		 _("pccam600_close: return value was %d instead of %d"),
		 ret,0);
    return GP_ERROR;
  }
  CHECK(pccam600_wait_for_status(port));
  return GP_OK;
}

/*
 *Sets up the camera and then read 32 512bytes blocks.
 *Why it is read those 32 blocks I dont know. Just
 *doing what the windows driver does :).
 */
int pccam600_init(GPPort *port, GPContext *context){
  unsigned char response[4];
  unsigned char buffer[512];
  int nr_of_blocks;
  int ret,i;
  gp_port_set_timeout(port,100);
  CHECK(gp_port_usb_msg_write(port,0x0e,0x00,0x01,NULL,0x0));
  CHECK(gp_port_usb_msg_write(port,0x08,0x00,0xff,NULL,0x0));
  CHECK(pccam600_wait_for_status(port));
  gp_port_set_timeout(port,100000);
  CHECK(gp_port_usb_msg_read(port,0x08,0x00,0xff,(char*)response,0x1));
  gp_port_set_timeout(port,500);
  CHECK(gp_port_usb_msg_write(port,0x08,0x00,0x1020,NULL,0x0));
  CHECK(pccam600_wait_for_status(port));
  gp_port_set_timeout(port,200000);
  CHECK(gp_port_usb_msg_read(port,0x08,0x00,0x1000,(char*)response,0x4));
  nr_of_blocks = response[2]*256+response[1];

  if (nr_of_blocks == 0) {
    gp_context_error(context,_("pccam600_init: Expected %d blocks got %d"),64,nr_of_blocks);
    return GP_ERROR;
  }

  nr_of_blocks = 512 / nr_of_blocks;
  gp_log(GP_LOG_DEBUG,"pccam600 library: init","nr_of_blocks %d",nr_of_blocks);
  if (nr_of_blocks == 0) {
    gp_context_error(context,_("pccam600_init: Expected %d blocks got %d"),64,nr_of_blocks);
    return GP_ERROR;
  }
  gp_port_set_timeout(port,500);
  for (i = 0; i<nr_of_blocks; i++){
    ret = gp_port_read(port, (char*)buffer,512);
    if (ret < 0){
      gp_log(GP_LOG_DEBUG,
	     "pccam600 library: init"," gp_port_read returned %d:",
	     ret);
      gp_context_error(context,_("pccam600 init:"
		   " Unexpected error: gp_port_read returned %d instead of %d"),
		   ret,0);
      return GP_ERROR;
    }
  }
  return GP_OK;

}