Debian Codegraph @

camlibs/kodak/ez200/ez200.c

raw
1 /* ez200.c
2 *
3 * Copyright (C) 2004 Bucas Jean-Francois <[email protected]>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301 USA
19 */
20
21 #include <config.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <string.h>
27
28 #include <gphoto2/gphoto2.h>
29 #include <gphoto2/gphoto2-port.h>
30
31 #include "libgphoto2/i18n.h"
32
33 #define PING 0x05
34 #define STATUS 0x06
35 #define PICTURE 0x08
36 #define ERASE 0x09
37 #define PICTURE_HEAD 0x0b
38 #define ACTIVE 0xe0
39
40 #define JPG_HEADER_SIZE 0x24D /* 589 */
41 #define HEADER_SIZE 0x26F
42 #define DATA_HEADER_SIZE 0x200
43
44 typedef unsigned char Info;
45
46 typedef enum {
47 MODEL_MINI
48 } Model;
49
50 #define GP_MODULE "ez200"
51
52 #define WRITE gp_port_usb_msg_write
53 #define READ gp_port_usb_msg_read
54
55 static int
56 ez200_wait_status_ok(GPPort *port) {
57 char c = 0;
58
59 do READ(port, STATUS, 0, 0, &c, 1);
60 while (c != 0); /* Wait */
61 return GP_OK;
62 }
63
64 /* enter photo mode */
65 static int
66 ez200_init (GPPort *port, Model *model, Info *info) {
67 char c = 0;
68
69 GP_DEBUG("Running ez200_init");
70
71 WRITE(port, ACTIVE, 0, 1, NULL, 0); /* index = 1; */
72 ez200_wait_status_ok(port);
73
74 /* get number of pictures */
75 READ(port, PICTURE, 0, 0, &c, 1);
76 memcpy (info, &c, 1);
77 GP_DEBUG("number of pics : %i", c);
78 return GP_OK;
79 }
80
81 /* quit photo mode */
82 static int
83 ez200_exit (GPPort *port) {
84 WRITE(port, ACTIVE, 0, 0, NULL, 0); /* index = 0; */
85 ez200_wait_status_ok(port);
86 return GP_OK;
87 }
88
89 #define ez200_get_num_pics(info) info[0]
90
91 static int
92 ez200_get_picture_size (GPPort *port, int n) {
93 unsigned char c[4];
94 unsigned int size = 0;
95 memset (c,0,sizeof(c));
96
97 GP_DEBUG("Running ez200_get_picture_size");
98
99 READ(port, PICTURE, n, 1, (char *)c, 3);
100 size = (int)c[0] + (int)c[1]*0x100 + (int)c[2]*0x10000;
101
102 GP_DEBUG(" size of picture %i is 0x%x = %i byte(s)", n, size, size);
103 if ( (size >= 0xfffff ) ) { return GP_ERROR; }
104 return size;
105 }
106
107 static int
108 ez200_read_data (GPPort *port, char *data, int size) {
109 int MAX_BULK = 0x1000;
110 int ret;
111
112 /* Read Data by blocks */
113 while(size > 0) {
114 int len = (size>MAX_BULK)?MAX_BULK:size;
115 ret = gp_port_read (port, data, len);
116 if (ret < GP_OK) return ret;
117 data += len;
118 size -= len;
119 }
120 return GP_OK;
121 }
122
123 static int
124 ez200_read_picture_data (GPPort *port, char *data, int size, int n) {
125 char c[4];
126
127 memset(c,0,sizeof(c));
128 /* ask picture n transfert */
129 READ(port, PICTURE, n, 1, c, 3);
130 return ez200_read_data (port, data, size);
131 }
132
133
134 /* after read_picture_data, we can retrieve the header by : */
135 static int
136 ez200_read_picture_header(GPPort *port, char *data) {
137 /* lecture de l'entete :: read header of picture */
138 READ(port, PICTURE_HEAD, 3, 3, data, HEADER_SIZE);
139 return GP_OK;
140 }
141
142 struct _CameraPrivateLibrary {
143 Model model;
144 Info info[2];
145 };
146
147 static const struct {
148 char *name;
149 CameraDriverStatus status;
150 unsigned short idVendor;
151 unsigned short idProduct;
152 } models[] = {
153 {"Kodak EZ200", GP_DRIVER_STATUS_PRODUCTION, 0x040a, 0x0300},
154 {NULL,0,0,0}
155 };
156
157 int
158 camera_id (CameraText *id)
159 {
160 strcpy (id->text, "Kodak EZ200 camera");
161 return GP_OK;
162 }
163
164 int
165 camera_abilities (CameraAbilitiesList *list)
166 {
167 int i;
168 CameraAbilities a;
169
170 for (i = 0; models[i].name; i++) {
171 memset (&a, 0, sizeof(a));
172 strcpy (a.model, models[i].name);
173 a.status = models[i].status;
174 a.port = GP_PORT_USB;
175 a.usb_vendor = models[i].idVendor;
176 a.usb_product= models[i].idProduct;
177 a.operations = GP_OPERATION_NONE;
178 a.folder_operations = GP_FOLDER_OPERATION_DELETE_ALL;
179 a.file_operations = GP_FILE_OPERATION_NONE;
180 gp_abilities_list_append (list, a);
181 }
182 return GP_OK;
183 }
184
185 static int
186 camera_summary (Camera *camera, CameraText *summary, GPContext *context) {
187 sprintf (summary->text, _("Your USB camera is a Kodak EZ200.\n"
188 "Number of PICs = %i\n"
189 ), ez200_get_num_pics(camera->pl->info));
190 return GP_OK;
191 }
192
193 static int
194 camera_about (Camera *camera, CameraText *about, GPContext *context) {
195 strcpy (about->text, _("Kodak EZ200 driver\nBucas Jean-Francois <[email protected]>\n"));
196 return GP_OK;
197 }
198
199 /*
200 * doesnt work, just creates a empty file.
201 static int
202 camera_capture (Camera *camera, CameraCaptureType type, CameraFilePath *path,
203 GPContext *context) {
204 unsigned char c[3];
205 int size;
206 char *data;
207 WRITE(camera->port,0,0x8003,0,NULL,0);
208 WRITE(camera->port,5,0x0000,0,NULL,0);
209
210 return GP_OK;
211 }
212 */
213
214
215 static int
216 delete_all_func (CameraFilesystem *fs, const char *folder, void *data, GPContext *context) {
217 Camera *camera = data;
218
219 WRITE (camera->port, ERASE, 0, 1, NULL, 0);
220 ez200_wait_status_ok(camera->port);
221 return GP_OK;
222 }
223
224 static int
225 file_list_func (CameraFilesystem *fs, const char *folder, CameraList *list,
226 void *data, GPContext *context) {
227 Camera *camera = data;
228
229 return gp_list_populate (list, "ez200_pic%03i.jpg", ez200_get_num_pics (camera->pl->info));
230 }
231
232 static int
233 get_file_func (CameraFilesystem *fs, const char *folder, const char *filename,
234 CameraFileType type, CameraFile *file, void *user_data,
235 GPContext *context)
236 {
237 Camera *camera = user_data;
238 int n, len, ret;
239 char *data, *data_start;
240
241 n = gp_filesystem_number(camera->fs, "/", filename, context);
242 if (n<GP_OK)
243 return n;
244
245 len = ez200_get_picture_size (camera->port, n);
246 GP_DEBUG("len = %i", len);
247 if (len < GP_OK) return len;
248
249 data = (char *)malloc(len + HEADER_SIZE + 1);
250 if (!data) return GP_ERROR_NO_MEMORY;
251
252 data_start = data + (HEADER_SIZE - DATA_HEADER_SIZE);
253 GP_DEBUG("data - data_start : %p %p : %lx",data, data_start, (long) (data_start - data));
254
255 ret = ez200_read_picture_data (camera->port, data_start, len, n);
256 if (ret < GP_OK) return ret;
257 ret = ez200_read_picture_header (camera->port, data);
258 if (ret < GP_OK) return ret;
259 switch (type) {
260 case GP_FILE_TYPE_PREVIEW:
261 case GP_FILE_TYPE_NORMAL:
262 gp_file_set_mime_type (file, GP_MIME_JPEG);
263 gp_file_set_data_and_size (file, data, len + HEADER_SIZE + 1);
264 break;
265 case GP_FILE_TYPE_RAW:
266 gp_file_set_data_and_size (file, data, len);
267 gp_file_set_mime_type (file, GP_MIME_RAW);
268 gp_file_adjust_name_for_mime_type(file);
269 break;
270 default:
271 return (GP_ERROR_NOT_SUPPORTED);
272 }
273 return GP_OK;
274 }
275
276 static int
277 camera_exit (Camera *camera, GPContext *context) {
278 GP_DEBUG ("Kodak EZ200 camera_exit");
279 ez200_exit (camera->port);
280 if (camera->pl) {
281 free (camera->pl);
282 camera->pl = NULL;
283 }
284 return GP_OK;
285 }
286
287 static CameraFilesystemFuncs fsfuncs = {
288 .file_list_func = file_list_func,
289 .get_file_func = get_file_func,
290 .delete_all_func = delete_all_func,
291 };
292
293 int
294 camera_init(Camera *camera, GPContext *context)
295 {
296 GPPortSettings settings;
297 int ret = 0;
298
299 /* First, set up all the function pointers */
300 camera->functions->summary = camera_summary;
301 camera->functions->about = camera_about;
302 camera->functions->exit = camera_exit;
303 /*camera->functions->capture = camera_capture;*/
304
305 GP_DEBUG ("Initializing Kodak EZ200");
306 ret = gp_port_get_settings(camera->port, &settings);
307 if (ret < 0) return ret;
308
309 switch (camera->port->type) {
310 case GP_PORT_USB:
311 settings.usb.config = 0;
312 settings.usb.altsetting = 0;
313 settings.usb.interface = 1;
314 settings.usb.inep = 0x82;
315 settings.usb.outep = 0x03;
316 break;
317 default:
318 return GP_ERROR;
319 }
320
321 ret = gp_port_set_settings(camera->port,settings);
322 if (ret < 0) return ret;
323
324 GP_DEBUG("interface = %i", settings.usb.interface);
325 GP_DEBUG("inep = %x", settings.usb.inep);
326 GP_DEBUG("outep = %x", settings.usb.outep);
327
328 /* Tell the CameraFilesystem where to get lists from */
329 gp_filesystem_set_funcs (camera->fs, &fsfuncs, camera);
330
331 camera->pl = calloc (1, sizeof (CameraPrivateLibrary));
332 if (!camera->pl) return GP_ERROR_NO_MEMORY;
333
334 /* Connect to the camera */
335 ez200_init (camera->port, &camera->pl->model, camera->pl->info);
336 GP_DEBUG("fin_camera_init");
337 return GP_OK;
338 }